journey-05-publish.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * 关键旅程 E2E 测试 05: 发布→首页可见
  3. * Journey: 创建内容 → 发布内容 → 验证在首页可见
  4. */
  5. import { test, expect } from '@playwright/test';
  6. import { apiGet, apiPost, apiPut, initApiClient, disposeApiClient, setAuthToken } from '../../../helpers/api-client';
  7. const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:5173';
  8. test.describe('关键旅程 05: 发布内容', () => {
  9. let phone: string;
  10. let authToken: string;
  11. let testBookId: number;
  12. test.beforeAll(async () => {
  13. await initApiClient();
  14. phone = `138${String(Date.now()).slice(-8)}`;
  15. await apiPost('/api/auth/send-code', { phone });
  16. const loginRes = await apiPost('/api/auth/login', { phone, code: '123456' });
  17. authToken = loginRes.data?.token;
  18. setAuthToken(authToken);
  19. });
  20. test.afterAll(async () => {
  21. await disposeApiClient();
  22. });
  23. test('C05-1: 创建测试书籍用于发布', async () => {
  24. const res = await apiPost('/api/book-generator/langgraph/books', {
  25. title: `发布测试书籍_${Date.now()}`,
  26. topic: '测试主题',
  27. wordCount: 10000,
  28. });
  29. expect(res.code).toBe(0);
  30. testBookId = res.data?.book?.id;
  31. expect(testBookId).toBeDefined();
  32. });
  33. test('C05-2: 获取发布状态', async () => {
  34. if (!testBookId) return test.skip();
  35. const res = await apiGet(`/api/book-generator/langgraph/books/${testBookId}`);
  36. // 刚创建的书籍应该是 draft 状态
  37. expect(res.code).toBe(0);
  38. });
  39. test('C05-3: 发布书籍', async () => {
  40. if (!testBookId) return test.skip();
  41. const res = await apiPut(`/api/book-generator/langgraph/books/${testBookId}/publish`);
  42. expect([0, 400, 409]).toContain(res.code); // 0=成功,其他=业务错误
  43. });
  44. test('C05-4: 发布页可访问', async ({ page }) => {
  45. await page.goto(`${FRONTEND_URL}/#/pages/publish/index`, { waitUntil: 'networkidle', timeout: 30000 });
  46. await page.waitForTimeout(3000);
  47. await expect(page.locator('#app')).toBeVisible();
  48. });
  49. test('C05-5: 首页可访问且显示内容', async ({ page }) => {
  50. await page.goto(`${FRONTEND_URL}/#/pages/index/index`, { waitUntil: 'networkidle', timeout: 30000 });
  51. await page.waitForTimeout(3000);
  52. await expect(page.locator('#app')).toBeVisible();
  53. });
  54. test('C05-6: 获取书籍列表验证已发布', async () => {
  55. const res = await apiGet('/api/book-generator/langgraph/books');
  56. expect(res.code).toBe(0);
  57. // 列表中应该包含我们创建的书籍
  58. const books = res.data?.books || [];
  59. const found = books.some((b: any) => b.id === testBookId);
  60. expect(found).toBe(true);
  61. });
  62. test('C05-7: 播放列表页可访问', async ({ page }) => {
  63. await page.goto(`${FRONTEND_URL}/#/pages/playlists/index`, { waitUntil: 'networkidle', timeout: 30000 });
  64. await page.waitForTimeout(3000);
  65. await expect(page.locator('#app')).toBeVisible();
  66. });
  67. });