test-p0-p1-features.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * P0 和 P1 功能综合测试脚本
  3. * 测试音频下载、批量操作、搜索、编辑、播放模式等功能
  4. */
  5. const http = require('http');
  6. const BASE_URL = 'http://localhost:3000';
  7. // 测试结果统计
  8. const results = {
  9. passed: 0,
  10. failed: 0,
  11. tests: []
  12. };
  13. // 测试函数
  14. async function test(name, fn) {
  15. try {
  16. await fn();
  17. results.passed++;
  18. results.tests.push({ name, status: '✅ PASSED' });
  19. console.log(`✅ ${name}`);
  20. } catch (error) {
  21. results.failed++;
  22. results.tests.push({ name, status: '❌ FAILED', error: error.message });
  23. console.log(`❌ ${name}: ${error.message}`);
  24. }
  25. }
  26. // HTTP 请求辅助函数
  27. function request(method, path, body = null) {
  28. return new Promise((resolve, reject) => {
  29. const url = new URL(path, BASE_URL);
  30. const options = {
  31. hostname: url.hostname,
  32. port: url.port,
  33. path: url.pathname,
  34. method: method,
  35. headers: {
  36. 'Content-Type': 'application/json',
  37. },
  38. };
  39. const req = http.request(options, (res) => {
  40. let data = '';
  41. res.on('data', (chunk) => { data += chunk; });
  42. res.on('end', () => {
  43. try {
  44. const json = JSON.parse(data);
  45. if (res.statusCode >= 200 && res.statusCode < 300) {
  46. resolve(json);
  47. } else {
  48. reject(new Error(`HTTP ${res.statusCode}: ${json.message || data}`));
  49. }
  50. } catch (e) {
  51. reject(new Error(`解析失败: ${data}`));
  52. }
  53. });
  54. });
  55. req.on('error', reject);
  56. if (body) {
  57. req.write(JSON.stringify(body));
  58. }
  59. req.end();
  60. });
  61. }
  62. // 主测试函数
  63. async function runTests() {
  64. console.log('\n🚀 开始 P0 和 P1 功能测试\n');
  65. console.log('=' .repeat(60));
  66. // ========== P0-1: 音频下载功能 ==========
  67. console.log('\n📦 P0-1: 音频下载功能测试');
  68. console.log('-'.repeat(60));
  69. await test('P0-1-1: 获取音频下载信息', async () => {
  70. // 先获取一个音频记录
  71. const history = await request('GET', '/api/history?page=1&pageSize=1');
  72. if (history.list && history.list.length > 0) {
  73. const audioId = history.list[0].id;
  74. const downloadInfo = await request('GET', `/api/tts/download/${audioId}`);
  75. if (!downloadInfo.data || !downloadInfo.data.downloadUrl) {
  76. throw new Error('下载信息不完整');
  77. }
  78. }
  79. });
  80. await test('P0-1-2: 批量获取下载链接', async () => {
  81. const history = await request('GET', '/api/history?page=1&pageSize=3');
  82. if (history.list && history.list.length > 0) {
  83. const audioIds = history.list.map(a => String(a.id));
  84. const batchResult = await request('POST', '/api/tts/download/batch', { audioIds });
  85. if (!batchResult.data || !batchResult.data.audios) {
  86. throw new Error('批量下载数据不完整');
  87. }
  88. }
  89. });
  90. // ========== P0-2: 批量操作增强 ==========
  91. console.log('\n📦 P0-2: 批量操作增强测试');
  92. console.log('-'.repeat(60));
  93. await test('P0-2-1: 获取书籍章节列表', async () => {
  94. const books = await request('GET', '/api/book-generator/langgraph/books');
  95. if (books.books && books.books.length > 0) {
  96. const bookId = books.books[0].id;
  97. const bookDetail = await request('GET', `/api/book-generator/langgraph/books/${bookId}`);
  98. if (!bookDetail.book || !bookDetail.book.chapters) {
  99. throw new Error('书籍详情不完整');
  100. }
  101. }
  102. });
  103. // ========== P1-1: 章节内容编辑 ==========
  104. console.log('\n📦 P1-1: 章节内容编辑测试');
  105. console.log('-'.repeat(60));
  106. await test('P1-1-1: 章节内容保存接口存在', async () => {
  107. // 验证 API 路由是否存在(即使没有权限也应该返回错误而不是 404)
  108. try {
  109. await request('POST', '/api/book-generator/langgraph/books/1/chapters/1/content', {
  110. content: '测试内容'
  111. });
  112. } catch (error) {
  113. // 如果返回 404 说明路由不存在
  114. if (error.message.includes('404')) {
  115. throw new Error('章节内容保存 API 路由不存在');
  116. }
  117. // 其他错误(如权限、参数错误)说明路由存在
  118. }
  119. });
  120. // ========== P1-2: 搜索功能 ==========
  121. console.log('\n📦 P1-2: 搜索功能测试');
  122. console.log('-'.repeat(60));
  123. await test('P1-2-1: 历史记录搜索功能', async () => {
  124. const result = await request('GET', '/api/history?page=1&pageSize=10&keyword=测试');
  125. // 搜索应该正常返回(即使结果为空)
  126. if (!result.data || result.data.list === undefined) {
  127. throw new Error('搜索结果格式不正确');
  128. }
  129. });
  130. await test('P1-2-2: 清空搜索返回全部', async () => {
  131. const result = await request('GET', '/api/history?page=1&pageSize=5');
  132. if (!result.data || !result.data.list || !Array.isArray(result.data.list)) {
  133. throw new Error('历史记录列表格式不正确');
  134. }
  135. });
  136. // ========== P1-3: 播放模式 ==========
  137. console.log('\n📦 P1-3: 播放模式测试');
  138. console.log('-'.repeat(60));
  139. await test('P1-3-1: 音频 Store 包含播放模式', async () => {
  140. // 这个需要在前端测试,后端只需验证音频相关接口正常
  141. const voices = await request('GET', '/api/tts/voices');
  142. if (!voices.data || !voices.data.voices || !Array.isArray(voices.data.voices)) {
  143. throw new Error('音色列表格式不正确');
  144. }
  145. });
  146. // ========== 综合测试 ==========
  147. console.log('\n📦 综合功能测试');
  148. console.log('-'.repeat(60));
  149. await test('综合-1: 书籍公开状态字段', async () => {
  150. const books = await request('GET', '/api/book-generator/langgraph/books');
  151. if (books.books && books.books.length > 0) {
  152. const book = books.books[0];
  153. if (book.isPublished === undefined) {
  154. throw new Error('书籍缺少 isPublished 字段');
  155. }
  156. }
  157. });
  158. await test('综合-2: 音频生成状态检查', async () => {
  159. const history = await request('GET', '/api/history?page=1&pageSize=1');
  160. if (history.list && history.list.length > 0) {
  161. const audio = history.list[0];
  162. if (!audio.status) {
  163. throw new Error('音频记录缺少状态字段');
  164. }
  165. }
  166. });
  167. // ========== 输出测试结果 ==========
  168. console.log('\n' + '='.repeat(60));
  169. console.log('📊 测试结果统计');
  170. console.log('='.repeat(60));
  171. console.log(`✅ 通过: ${results.passed}`);
  172. console.log(`❌ 失败: ${results.failed}`);
  173. console.log(`📈 通过率: ${((results.passed / (results.passed + results.failed)) * 100).toFixed(1)}%`);
  174. console.log('='.repeat(60));
  175. if (results.failed > 0) {
  176. console.log('\n❌ 失败的测试:');
  177. results.tests
  178. .filter(t => t.status === '❌ FAILED')
  179. .forEach(t => {
  180. console.log(` - ${t.name}: ${t.error}`);
  181. });
  182. }
  183. console.log('\n✨ 测试完成!\n');
  184. // 如果有失败的测试,退出码为 1
  185. process.exit(results.failed > 0 ? 1 : 0);
  186. }
  187. // 运行测试
  188. runTests().catch(error => {
  189. console.error('测试执行失败:', error);
  190. process.exit(1);
  191. });