tts.mock.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. export function createTTSMock(options: TTSMockOptions = {}) {
  2. const { shouldFail = false, delayMs = 0, audioUrl = 'https://mock-cdn.example.com/audio/test.mp3', duration = 120 } = options;
  3. return {
  4. name: 'mock-tts' as const,
  5. vendor: 'mock' as const,
  6. mode: 'mock' as const,
  7. maxTextLength: 5000,
  8. concurrency: 3,
  9. synthesize: vi.fn().mockImplementation(async (text: string, voice: string, params: any, outputPath: string) => {
  10. if (delayMs > 0) {
  11. await new Promise((resolve) => setTimeout(resolve, delayMs));
  12. }
  13. if (shouldFail) {
  14. throw new Error('Mock TTS failure');
  15. }
  16. return {
  17. audioUrl,
  18. duration,
  19. size: 2048000,
  20. textLength: text.length,
  21. };
  22. }),
  23. getVoices: vi.fn().mockReturnValue([
  24. { id: 'zh_female_qingxin', name: '清心女声', language: 'zh-CN' },
  25. { id: 'zh_male_qingxin', name: '清心男声', language: 'zh-CN' },
  26. ]),
  27. };
  28. }
  29. export interface TTSMockOptions {
  30. shouldFail?: boolean;
  31. delayMs?: number;
  32. audioUrl?: string;
  33. duration?: number;
  34. }