test-full-audio-generation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function testFullAudioGeneration() {
  4. const bookId = 5; // Git入门(最小,章节少)
  5. console.log('\n🎵 开始完整音频生成测试\n');
  6. console.log('=' .repeat(60));
  7. // 步骤1:记录测试前的状态
  8. console.log('\n📊 步骤1:记录测试前状态\n');
  9. const beforeBooks = await prisma.book.count();
  10. const beforeAudioRecords = await prisma.audioRecord.count({
  11. where: { userId: 1 }
  12. });
  13. const beforeSubsectionsWithAudio = await prisma.bookChapter.count({
  14. where: {
  15. bookId,
  16. level: 3,
  17. audioUrl: { not: '' }
  18. }
  19. });
  20. console.log(`书籍总数: ${beforeBooks}`);
  21. console.log(`AudioRecord记录: ${beforeAudioRecords}`);
  22. console.log(`已有音频的小节: ${beforeSubsectionsWithAudio}\n`);
  23. // 步骤2:调用批量生成音频API
  24. console.log('📤 步骤2:调用批量生成音频API\n');
  25. try {
  26. const response = await fetch(`http://localhost:3000/api/book-generator/langgraph/books/${bookId}/audio`, {
  27. method: 'POST',
  28. headers: { 'Content-Type': 'application/json' },
  29. body: JSON.stringify({ voiceId: 'cherry' }),
  30. });
  31. const data = await response.json();
  32. console.log('API响应:');
  33. console.log(` 状态码: ${response.status}`);
  34. console.log(` code: ${data.code}`);
  35. console.log(` message: ${data.message}`);
  36. console.log(` totalSubsections: ${data.data?.totalSubsections}\n`);
  37. if (data.code !== 0) {
  38. console.log('❌ API调用失败,测试终止');
  39. await prisma.$disconnect();
  40. return;
  41. }
  42. console.log('✅ API调用成功!\n');
  43. } catch (error) {
  44. console.log('❌ API调用异常:', error.message);
  45. await prisma.$disconnect();
  46. return;
  47. }
  48. // 步骤3:等待30秒让音频生成
  49. console.log('⏳ 步骤3:等待音频生成(30秒)...\n');
  50. await new Promise(resolve => setTimeout(resolve, 30000));
  51. // 步骤4:检查生成结果
  52. console.log('📊 步骤4:检查生成结果\n');
  53. const afterBooks = await prisma.book.count();
  54. const afterAudioRecords = await prisma.audioRecord.count({
  55. where: { userId: 1 }
  56. });
  57. const afterSubsectionsWithAudio = await prisma.bookChapter.count({
  58. where: {
  59. bookId,
  60. level: 3,
  61. audioUrl: { not: '' }
  62. }
  63. });
  64. console.log('对比结果:');
  65. console.log(` 书籍总数: ${beforeBooks} → ${afterBooks} ${afterBooks === beforeBooks ? '✅' : '❌ 增加了!'}`);
  66. console.log(` AudioRecord: ${beforeAudioRecords} → ${afterAudioRecords} ${afterAudioRecords > beforeAudioRecords ? '✅ 增加了' : '⚠️ 未增加'}`);
  67. console.log(` 有音频的小节: ${beforeSubsectionsWithAudio} → ${afterSubsectionsWithAudio} ${afterSubsectionsWithAudio > beforeSubsectionsWithAudio ? '✅ 增加了' : '⚠️ 未增加'}\n`);
  68. // 步骤5:验证音频质量
  69. console.log('📝 步骤5:验证音频质量\n');
  70. const completedAudios = await prisma.audioRecord.findMany({
  71. where: {
  72. userId: 1,
  73. status: 'completed'
  74. },
  75. orderBy: { createdAt: 'desc' },
  76. take: 3,
  77. select: {
  78. id: true,
  79. title: true,
  80. status: true,
  81. audioUrl: true,
  82. audioDuration: true,
  83. wordCount: true,
  84. createdAt: true,
  85. }
  86. });
  87. if (completedAudios.length > 0) {
  88. console.log(`最近3个完成的音频:\n`);
  89. completedAudios.forEach((audio, index) => {
  90. console.log(`${index + 1}. ${audio.title}`);
  91. console.log(` 状态: ${audio.status}`);
  92. console.log(` 字数: ${audio.wordCount}字`);
  93. console.log(` 时长: ${audio.audioDuration}秒`);
  94. console.log(` URL: ${audio.audioUrl || '无'}`);
  95. console.log(` 时间: ${audio.createdAt}\n`);
  96. });
  97. } else {
  98. console.log('⚠️ 还没有完成的音频,可能需要更长时间\n');
  99. }
  100. // 步骤6:检查是否创建了错误的书籍
  101. console.log('🔍 步骤6:检查是否创建了错误的"我的音频"书籍\n');
  102. const myAudioBooks = await prisma.book.count({
  103. where: { title: '我的音频' }
  104. });
  105. if (myAudioBooks === 0) {
  106. console.log('✅ 没有创建错误的"我的音频"书籍!\n');
  107. } else {
  108. console.log(`❌ 创建了${myAudioBooks}本"我的音频"书籍!\n`);
  109. }
  110. // 最终总结
  111. console.log('=' .repeat(60));
  112. console.log('\n📋 测试总结:\n');
  113. const allPassed =
  114. afterBooks === beforeBooks && // 书籍数量没增加
  115. afterSubsectionsWithAudio > beforeSubsectionsWithAudio && // 音频小节增加了
  116. myAudioBooks === 0; // 没有错误的书籍
  117. if (allPassed) {
  118. console.log('✅ 所有测试通过!音频生成功能正常!\n');
  119. } else {
  120. console.log('❌ 部分测试未通过,请检查上面的详细结果\n');
  121. }
  122. await prisma.$disconnect();
  123. }
  124. testFullAudioGeneration().catch(console.error);