Add mock data and unit tests for Channels and Members APIs
This commit is contained in:
87
test/unit/apis/Stream.test.ts
Normal file
87
test/unit/apis/Stream.test.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import {describe, expect, it, beforeEach, mock} from 'bun:test';
|
||||
import {Streams} from '../../../src/apis/Stream';
|
||||
import type {PCastHttpRequests} from '../../../src/apis/PCastRequests';
|
||||
import {HttpMethod} from '../../../src/net/http/HttpMethod';
|
||||
|
||||
describe('Streams', () => {
|
||||
let mockHttpRequests: PCastHttpRequests;
|
||||
let streams: Streams;
|
||||
|
||||
beforeEach(() => {
|
||||
mockHttpRequests = {
|
||||
request: mock(() => Promise.resolve({status: 'ok'}))
|
||||
} as unknown as PCastHttpRequests;
|
||||
|
||||
streams = new Streams(mockHttpRequests);
|
||||
});
|
||||
|
||||
describe('when publishing a URI', () => {
|
||||
it('should send PUT request with media URI and token', async () => {
|
||||
const mediaUri = 'https://example.com/video.mp4';
|
||||
const token = 'auth-token-123';
|
||||
|
||||
await streams.publishUri(mediaUri, token);
|
||||
|
||||
expect(mockHttpRequests.request).toHaveBeenCalledWith(
|
||||
HttpMethod.PUT,
|
||||
'/stream/publish/uri/mp4',
|
||||
expect.objectContaining({
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
uri: mediaUri,
|
||||
options: []
|
||||
})
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should extract media type from URI extension', async () => {
|
||||
await streams.publishUri('https://example.com/live/stream.m3u8', 'token');
|
||||
|
||||
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, '/stream/publish/uri/m3u8', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should handle various media types', async () => {
|
||||
const testCases = [
|
||||
{uri: 'https://example.com/video.webm', expectedType: 'webm'},
|
||||
{uri: 'https://example.com/video.mkv', expectedType: 'mkv'},
|
||||
{uri: 'https://example.com/video.ts', expectedType: 'ts'},
|
||||
{uri: 'https://example.com/video.flv', expectedType: 'flv'}
|
||||
];
|
||||
|
||||
for (const {uri, expectedType} of testCases) {
|
||||
(mockHttpRequests.request as any).mockClear();
|
||||
await streams.publishUri(uri, 'token');
|
||||
|
||||
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, `/stream/publish/uri/${expectedType}`, expect.any(Object));
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw error when URI has no file extension', async () => {
|
||||
const invalidUri = 'https://example.com/stream';
|
||||
|
||||
await expect(streams.publishUri(invalidUri, 'token')).rejects.toThrow('Invalid media URI');
|
||||
});
|
||||
|
||||
it('should handle URIs with query parameters', async () => {
|
||||
await streams.publishUri('https://example.com/video.mp4?token=abc&quality=hd', 'token');
|
||||
|
||||
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, '/stream/publish/uri/mp4', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should handle URIs with fragments', async () => {
|
||||
await streams.publishUri('https://example.com/video.webm#start=10', 'token');
|
||||
|
||||
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, '/stream/publish/uri/webm', expect.any(Object));
|
||||
});
|
||||
|
||||
it('should return the API response', async () => {
|
||||
const mockResponse = {status: 'ok', streamId: 'stream-123'};
|
||||
(mockHttpRequests.request as any).mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await streams.publishUri('https://example.com/video.mp4', 'token');
|
||||
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user