book-generator-flow.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * 书籍生成流程集成测试
  3. * 测试预估→检测类型→创建→进度完整流程
  4. */
  5. import { describe, it, expect, beforeAll } from 'vitest';
  6. import { apiPost, apiGet, apiPut, setAuthToken, initHttpClient, createTestUser } from '../../integration/http-client';
  7. describe('书籍生成流程集成测试', () => {
  8. let authToken: string;
  9. let userId: string;
  10. beforeAll(async () => {
  11. // 创建测试用户
  12. try {
  13. const user = await createTestUser();
  14. authToken = user.token;
  15. userId = user.userId;
  16. setAuthToken(authToken);
  17. } catch (e) {
  18. console.log('创建测试用户失败,跳过部分测试');
  19. }
  20. });
  21. describe('Step 1: 预估 API', () => {
  22. it('预估字数和时长', async () => {
  23. if (!authToken) return;
  24. const res = await apiPost('/api/book-generator/langgraph/estimate', {
  25. topic: '人工智能发展史',
  26. wordCount: 50000
  27. });
  28. expect(res.code).toBe(0);
  29. expect(res.data?.estimatedCost).toBeGreaterThan(0);
  30. expect(res.data?.estimatedDuration).toBeGreaterThan(0);
  31. });
  32. it('无效字数返回错误', async () => {
  33. if (!authToken) return;
  34. const res = await apiPost('/api/book-generator/langgraph/estimate', {
  35. topic: '测试',
  36. wordCount: -100
  37. });
  38. expect(res.code).not.toBe(0);
  39. });
  40. });
  41. describe('Step 2: 检测书籍类型', () => {
  42. it('检测书籍类型成功', async () => {
  43. if (!authToken) return;
  44. const res = await apiPost('/api/book-generator/langgraph/detect-book-type', {
  45. topic: '人工智能简史'
  46. });
  47. expect(res.code).toBe(0);
  48. expect(['技术', '科普', '学术', '其他']).toContain(res.data?.bookType);
  49. });
  50. it('空主题返回错误', async () => {
  51. if (!authToken) return;
  52. const res = await apiPost('/api/book-generator/langgraph/detect-book-type', {
  53. topic: ''
  54. });
  55. expect(res.code).not.toBe(0);
  56. });
  57. });
  58. describe('Step 3: 创建书籍', () => {
  59. it('创建书籍成功', async () => {
  60. if (!authToken) return;
  61. const res = await apiPost('/api/book-generator/langgraph/create', {
  62. title: `测试书籍_${Date.now()}`,
  63. topic: '人工智能发展史',
  64. wordCount: 50000,
  65. strategy: 'deep-plan-parallel'
  66. });
  67. expect(res.code).toBe(0);
  68. expect(res.data?.bookId).toBeDefined();
  69. });
  70. it('配额不足时返回错误', async () => {
  71. if (!authToken) return;
  72. // 尝试超额创建
  73. const res = await apiPost('/api/book-generator/langgraph/create', {
  74. title: '超配额测试',
  75. topic: '测试',
  76. wordCount: 1000000 // 远超配额
  77. });
  78. // 应该返回配额不足错误
  79. expect(res.code).toBeGreaterThan(0);
  80. });
  81. });
  82. describe('Step 4: 查询书籍列表', () => {
  83. it('获取用户的书籍列表', async () => {
  84. if (!authToken) return;
  85. const res = await apiGet('/api/book-generator/langgraph/books');
  86. expect(res.code).toBe(0);
  87. expect(Array.isArray(res.data?.books)).toBe(true);
  88. });
  89. it('获取书籍详情', async () => {
  90. if (!authToken) return;
  91. // 先创建一本书
  92. const createRes = await apiPost('/api/book-generator/langgraph/create', {
  93. title: `详情测试书籍_${Date.now()}`,
  94. topic: '测试',
  95. wordCount: 30000
  96. });
  97. const bookId = createRes.data?.bookId;
  98. if (!bookId) return;
  99. const res = await apiGet(`/api/book-generator/langgraph/books/${bookId}`);
  100. expect(res.code).toBe(0);
  101. expect(res.data?.id).toBe(bookId);
  102. });
  103. });
  104. describe('Step 5: 章节生成进度', () => {
  105. it('查询章节生成状态', async () => {
  106. if (!authToken) return;
  107. // 先创建一本书
  108. const createRes = await apiPost('/api/book-generator/langgraph/create', {
  109. title: `章节测试书籍_${Date.now()}`,
  110. topic: '测试',
  111. wordCount: 20000
  112. });
  113. const bookId = createRes.data?.bookId;
  114. if (!bookId) return;
  115. const res = await apiGet(`/api/book-generator/langgraph/books/${bookId}/chapters`);
  116. expect(res.code).toBe(0);
  117. });
  118. });
  119. });