test-tts-via-queue.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * 测试:通过 TtsQueue 路径发 TTS 请求,验证日志是否入库
  3. * 模拟真实的 book generation → TtsQueue → TTS provider 流程
  4. */
  5. import { prisma } from '../src/models';
  6. import { generateAudio } from '../src/modules/tts/tts.service';
  7. async function main() {
  8. const beforeCount = await prisma.aiCallLog.count();
  9. console.log(`基线 AiCallLog: ${beforeCount}`);
  10. // 直接调用 generateAudio(这是 processTtsTask 内部调用的函数)
  11. console.log('\n发送 3 次 generateAudio(文本="测试音频1234567890")...');
  12. for (let i = 0; i < 3; i++) {
  13. try {
  14. await generateAudio(
  15. '1', // userId
  16. `测试音频${i + 1},这是用于验证TTS日志记录完整性的测试文本。`.repeat(3), // text ~150字
  17. 'default', // voiceId
  18. { speed: 1, pitch: 0, volume: 50 },
  19. undefined, // onComplete
  20. {
  21. bookId: '999',
  22. chapterId: 999,
  23. chapterTitle: `测试章节${i + 1}`,
  24. }
  25. );
  26. console.log(` #${i + 1}: ✅ generateAudio 返回成功`);
  27. } catch (err: any) {
  28. console.log(` #${i + 1}: ❌ ${err.message?.substring(0, 80)}`);
  29. }
  30. }
  31. // 等待异步日志落库
  32. await new Promise(r => setTimeout(r, 3000));
  33. const afterCount = await prisma.aiCallLog.count();
  34. const newRecords = afterCount - beforeCount;
  35. console.log(`\n=== 结果 ===`);
  36. console.log(`新增 AiCallLog: ${newRecords} 条`);
  37. // 按类型拆分
  38. const byType = await prisma.aiCallLog.groupBy({
  39. by: ['callType', 'provider', 'success'],
  40. where: { id: { gt: beforeCount } },
  41. _count: { id: true },
  42. });
  43. let total = 0;
  44. for (const t of byType) {
  45. const s = t.success ? '✅' : '❌';
  46. console.log(` ${s} ${t.callType.padEnd(25)} ${(t.provider || '-').padEnd(12)} ${t._count.id} 次`);
  47. total += t._count.id;
  48. }
  49. console.log(` ---`);
  50. console.log(` 实际总记录: ${total} (新增: ${afterCount - beforeCount})`);
  51. if (total === 0) {
  52. console.log('\n❌ 问题:generateAudio 路径完全没有日志记录!');
  53. console.log(' 可能原因:');
  54. console.log(' 1. withAiLog 在 provider.synthesize 中没有被调用');
  55. console.log(' 2. 或者 processAudioGeneration 中 provider 全部不可用时直接抛异常');
  56. console.log(' 3. 或者 logAiCall 的 fire-and-forget 写入失败');
  57. } else {
  58. console.log(`\n✅ generateAudio 路径有 ${total} 条日志记录`);
  59. }
  60. await prisma.$disconnect();
  61. }
  62. main().catch(e => { console.error(e); process.exit(1); });