Add mock data and unit tests for Channels and Members APIs

This commit is contained in:
2025-12-07 04:41:41 -05:00
parent 4673547b95
commit a88fa40ba2
12 changed files with 1377 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import {describe, expect, it} from 'bun:test';
import {ReportKind, ReportKindMapping, type ReportKindType} from '../../../../src/apis/Reporting/ReportKind';
describe('ReportKind', () => {
describe('when accessing enum values', () => {
it('should have Publishing as 0', () => {
expect(ReportKind.Publishing).toBe(0);
});
it('should have Viewing as 1', () => {
expect(ReportKind.Viewing).toBe(1);
});
it('should have Ingest as 2', () => {
expect(ReportKind.Ingest).toBe(2);
});
});
});
describe('ReportKindMapping', () => {
describe('when converting from type string to enum', () => {
it('should convert "Publishing" to ReportKind.Publishing', () => {
const result = ReportKindMapping.convertReportKindTypeToReportKind('Publishing');
expect(result).toBe(ReportKind.Publishing);
});
it('should convert "Viewing" to ReportKind.Viewing', () => {
const result = ReportKindMapping.convertReportKindTypeToReportKind('Viewing');
expect(result).toBe(ReportKind.Viewing);
});
it('should convert "IngestReport" to ReportKind.Ingest', () => {
const result = ReportKindMapping.convertReportKindTypeToReportKind('IngestReport');
expect(result).toBe(ReportKind.Ingest);
});
});
describe('when converting from enum to type string', () => {
it('should convert ReportKind.Publishing to "Publishing"', () => {
const result = ReportKindMapping.convertReportKindToReportKindType(ReportKind.Publishing);
expect(result).toBe('Publishing');
});
it('should convert ReportKind.Viewing to "Viewing"', () => {
const result = ReportKindMapping.convertReportKindToReportKindType(ReportKind.Viewing);
expect(result).toBe('Viewing');
});
it('should convert ReportKind.Ingest to "IngestReport"', () => {
const result = ReportKindMapping.convertReportKindToReportKindType(ReportKind.Ingest);
expect(result).toBe('IngestReport');
});
});
describe('when performing roundtrip conversions', () => {
const reportKindTypes: ReportKindType[] = ['Publishing', 'Viewing', 'IngestReport'];
it.each(reportKindTypes)('should preserve %s through type->enum->type conversion', kindType => {
const enumValue = ReportKindMapping.convertReportKindTypeToReportKind(kindType);
const backToType = ReportKindMapping.convertReportKindToReportKindType(enumValue);
expect(backToType).toBe(kindType);
});
const reportKinds = [ReportKind.Publishing, ReportKind.Viewing, ReportKind.Ingest];
it.each(reportKinds)('should preserve enum %d through enum->type->enum conversion', kindEnum => {
const typeValue = ReportKindMapping.convertReportKindToReportKindType(kindEnum);
const backToEnum = ReportKindMapping.convertReportKindTypeToReportKind(typeValue);
expect(backToEnum).toBe(kindEnum);
});
});
});

View File

@@ -0,0 +1,194 @@
import {describe, expect, it, beforeEach, mock} from 'bun:test';
import {Reporting, type PublishingReportOptions, type ViewingReportOptions, type IngestBufferUnderrunOptions} from '../../../../src/apis/Reporting/Reporting';
import {ReportKind} from '../../../../src/apis/Reporting/ReportKind';
import {ViewingReportKind} from '../../../../src/apis/Reporting/ViewingReportKind';
import type {PCastHttpRequests} from '../../../../src/apis/PCastRequests';
import {HttpMethod} from '../../../../src/net/http/HttpMethod';
describe('Reporting', () => {
let mockHttpRequests: PCastHttpRequests;
let reporting: Reporting;
beforeEach(() => {
mockHttpRequests = {
request: mock(() => Promise.resolve('report-id-123'))
} as unknown as PCastHttpRequests;
reporting = new Reporting(mockHttpRequests);
});
describe('when generating a publishing report', () => {
const publishingOptions: PublishingReportOptions = {
start: '2024-01-01T00:00:00Z',
end: '2024-01-02T00:00:00Z',
applicationIds: ['app-123'],
channelIds: ['channel-456']
};
it('should send PUT request to publishing endpoint', async () => {
await reporting.generateReport(ReportKind.Publishing, publishingOptions);
expect(mockHttpRequests.request).toHaveBeenCalledWith(
HttpMethod.PUT,
'/reporting/publishing',
expect.objectContaining({
body: expect.any(String)
})
);
});
it('should include all options in the request body', async () => {
await reporting.generateReport(ReportKind.Publishing, publishingOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.publishingReport.start).toBe('2024-01-01T00:00:00Z');
expect(body.publishingReport.end).toBe('2024-01-02T00:00:00Z');
expect(body.publishingReport.applicationIds).toEqual(['app-123']);
expect(body.publishingReport.channelIds).toEqual(['channel-456']);
});
it('should return the report ID', async () => {
const result = await reporting.generateReport(ReportKind.Publishing, publishingOptions);
expect(result).toBe('report-id-123');
});
});
describe('when generating a viewing report', () => {
const viewingOptions: ViewingReportOptions = {
kind: ViewingReportKind.RealTime,
start: '2024-01-01T00:00:00Z',
end: '2024-01-02T00:00:00Z',
sessionIds: ['session-123']
};
it('should send PUT request to viewing endpoint', async () => {
await reporting.generateReport(ReportKind.Viewing, viewingOptions);
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, '/reporting/viewing', expect.any(Object));
});
it('should convert ViewingReportKind enum to type string', async () => {
await reporting.generateReport(ReportKind.Viewing, viewingOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.viewingReport.kind).toBe('RealTime');
});
it('should handle HLS viewing report kind', async () => {
const hlsOptions: ViewingReportOptions = {
...viewingOptions,
kind: ViewingReportKind.HLS
};
await reporting.generateReport(ReportKind.Viewing, hlsOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.viewingReport.kind).toBe('HLS');
});
it('should handle DASH viewing report kind', async () => {
const dashOptions: ViewingReportOptions = {
...viewingOptions,
kind: ViewingReportKind.DASH
};
await reporting.generateReport(ReportKind.Viewing, dashOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.viewingReport.kind).toBe('DASH');
});
it('should default tags and originTags to empty arrays when not provided', async () => {
await reporting.generateReport(ReportKind.Viewing, viewingOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.viewingReport.tags).toEqual([]);
expect(body.viewingReport.originTags).toEqual([]);
});
it('should preserve provided tags and originTags', async () => {
const optionsWithTags: ViewingReportOptions = {
...viewingOptions,
tags: ['tag1', 'tag2'],
originTags: ['origin1']
};
await reporting.generateReport(ReportKind.Viewing, optionsWithTags);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.viewingReport.tags).toEqual(['tag1', 'tag2']);
expect(body.viewingReport.originTags).toEqual(['origin1']);
});
});
describe('when generating an ingest buffer underrun report', () => {
const ingestOptions: IngestBufferUnderrunOptions = {
kind: 'BufferUnderrun',
start: '2024-01-01T00:00:00Z',
end: '2024-01-02T00:00:00Z',
ingestIds: ['ingest-123']
};
it('should send PUT request to ingest endpoint', async () => {
await reporting.generateReport(ReportKind.Ingest, ingestOptions);
expect(mockHttpRequests.request).toHaveBeenCalledWith(HttpMethod.PUT, '/reporting/ingest', expect.any(Object));
});
it('should include BufferUnderrun kind in request body', async () => {
await reporting.generateReport(ReportKind.Ingest, ingestOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.ingestReport.kind).toBe('BufferUnderrun');
});
it('should include ingestIds in request body', async () => {
await reporting.generateReport(ReportKind.Ingest, ingestOptions);
const [, , options] = (mockHttpRequests.request as any).mock.calls[0];
const body = JSON.parse(options.body);
expect(body.ingestReport.ingestIds).toEqual(['ingest-123']);
});
});
describe('when calling requestPublishingReport directly', () => {
it('should throw error when start date is missing', async () => {
const invalidOptions = {end: '2024-01-02T00:00:00Z'} as PublishingReportOptions;
await expect(reporting.requestPublishingReport(invalidOptions)).rejects.toThrow('requires a start and end Date');
});
it('should throw error when end date is missing', async () => {
const invalidOptions = {start: '2024-01-01T00:00:00Z'} as PublishingReportOptions;
await expect(reporting.requestPublishingReport(invalidOptions)).rejects.toThrow('requires a start and end Date');
});
it('should succeed with valid start and end dates', async () => {
const validOptions: PublishingReportOptions = {
start: '2024-01-01T00:00:00Z',
end: '2024-01-02T00:00:00Z'
};
const result = await reporting.requestPublishingReport(validOptions);
expect(result).toBe('report-id-123');
});
});
});

View File

@@ -0,0 +1,72 @@
import {describe, expect, it} from 'bun:test';
import {ViewingReportKind, ViewingReportKindMapping, type ViewingReportKindType} from '../../../../src/apis/Reporting/ViewingReportKind';
describe('ViewingReportKind', () => {
describe('when accessing enum values', () => {
it('should have RealTime as 0', () => {
expect(ViewingReportKind.RealTime).toBe(0);
});
it('should have HLS as 1', () => {
expect(ViewingReportKind.HLS).toBe(1);
});
it('should have DASH as 2', () => {
expect(ViewingReportKind.DASH).toBe(2);
});
});
});
describe('ViewingReportKindMapping', () => {
describe('when converting from type string to enum', () => {
it('should convert "RealTime" to ViewingReportKind.RealTime', () => {
const result = ViewingReportKindMapping.convertViewingReportKindTypeToViewingReportKind('RealTime');
expect(result).toBe(ViewingReportKind.RealTime);
});
it('should convert "HLS" to ViewingReportKind.HLS', () => {
const result = ViewingReportKindMapping.convertViewingReportKindTypeToViewingReportKind('HLS');
expect(result).toBe(ViewingReportKind.HLS);
});
it('should convert "DASH" to ViewingReportKind.DASH', () => {
const result = ViewingReportKindMapping.convertViewingReportKindTypeToViewingReportKind('DASH');
expect(result).toBe(ViewingReportKind.DASH);
});
});
describe('when converting from enum to type string', () => {
it('should convert ViewingReportKind.RealTime to "RealTime"', () => {
const result = ViewingReportKindMapping.convertViewingReportKindToViewingReportKindType(ViewingReportKind.RealTime);
expect(result).toBe('RealTime');
});
it('should convert ViewingReportKind.HLS to "HLS"', () => {
const result = ViewingReportKindMapping.convertViewingReportKindToViewingReportKindType(ViewingReportKind.HLS);
expect(result).toBe('HLS');
});
it('should convert ViewingReportKind.DASH to "DASH"', () => {
const result = ViewingReportKindMapping.convertViewingReportKindToViewingReportKindType(ViewingReportKind.DASH);
expect(result).toBe('DASH');
});
});
describe('when performing roundtrip conversions', () => {
const viewingReportKindTypes: ViewingReportKindType[] = ['RealTime', 'HLS', 'DASH'];
it.each(viewingReportKindTypes)('should preserve %s through type->enum->type conversion', kindType => {
const enumValue = ViewingReportKindMapping.convertViewingReportKindTypeToViewingReportKind(kindType);
const backToType = ViewingReportKindMapping.convertViewingReportKindToViewingReportKindType(enumValue);
expect(backToType).toBe(kindType);
});
const viewingReportKinds = [ViewingReportKind.RealTime, ViewingReportKind.HLS, ViewingReportKind.DASH];
it.each(viewingReportKinds)('should preserve enum %d through enum->type->enum conversion', kindEnum => {
const typeValue = ViewingReportKindMapping.convertViewingReportKindToViewingReportKindType(kindEnum);
const backToEnum = ViewingReportKindMapping.convertViewingReportKindTypeToViewingReportKind(typeValue);
expect(backToEnum).toBe(kindEnum);
});
});
});