Files
ChannelTests-TS/test/workflows/factories/TestContextFactory.ts

99 lines
2.6 KiB
TypeScript

import { ITestContext, IChannelTestContext, IPublisherTestContext, ISubscriberTestContext } from '../interfaces/ITestContext';
export interface TestContextConfig {
applicationCredentials: {
id: string;
secret: string;
};
uri: {
pcast: string;
ingest: string;
channel: string;
};
streamKind: string;
}
export class TestContextFactory {
private static createBaseContext(config: TestContextConfig): ITestContext {
return {
applicationCredentials: config.applicationCredentials,
uri: config.uri,
streamKind: config.streamKind,
cleanup: [],
addCleanupTask: function(task: () => Promise<void>): void {
this.cleanup.push(task);
},
executeCleanup: async function(): Promise<void> {
for (const task of this.cleanup) {
try {
await task();
} catch (error) {
console.error('Cleanup task failed:', error);
}
}
}
};
}
static createChannelContext(config: TestContextConfig): IChannelTestContext {
const baseContext = this.createBaseContext(config);
try {
// Try to import the real PCastApi if available
const PCastApi = require('@techniker-me/pcast-api');
const pcastApi = new PCastApi(config.uri.pcast, config.applicationCredentials);
return {
...baseContext,
pcastApi,
channel: null as any, // Will be set later
};
} catch (error) {
console.warn('Real PCastApi not available, using mock API:', error);
// Return mock context for testing
return {
...baseContext,
pcastApi: {
createChannel: async () => ({ channelId: 'mock-channel' }),
deleteChannel: async () => ({ status: 'ok' })
} as any,
channel: null as any,
};
}
}
static createPublisherContext(config: TestContextConfig): IPublisherTestContext {
const channelContext = this.createChannelContext(config);
try {
// Try to import the real RtmpPush if available
const RtmpPush = require('@techniker-me/rtmp-push');
return {
...channelContext,
publishSource: new RtmpPush(),
};
} catch (error) {
console.warn('Real RtmpPush not available, using mock:', error);
return {
...channelContext,
publishSource: {} as any,
};
}
}
static createSubscriberContext(config: TestContextConfig): ISubscriberTestContext {
const channelContext = this.createChannelContext(config);
return {
...channelContext,
publishDestination: {
token: '',
capabilities: [],
},
};
}
}