journey-01-register-login.spec.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * 关键旅程 E2E 测试 01: 注册→登录→首页浏览
  3. * Journey: 新用户注册 → 获取Token → 浏览首页
  4. */
  5. import { test, expect } from '@playwright/test';
  6. import { apiGet, apiPost, apiDelete, initApiClient, disposeApiClient, setAuthToken } from '../../../helpers/api-client';
  7. const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:5173';
  8. test.describe('关键旅程 01: 注册登录', () => {
  9. let phone: string;
  10. let authToken: string;
  11. test.beforeAll(async () => {
  12. await initApiClient();
  13. phone = `138${String(Date.now()).slice(-8)}`;
  14. });
  15. test.afterAll(async () => {
  16. await disposeApiClient();
  17. });
  18. test('C01-1: 发送验证码', async () => {
  19. const res = await apiPost('/api/auth/send-code', { phone });
  20. expect(res.code).toBe(0);
  21. expect(res.data?.code).toBeDefined();
  22. });
  23. test('C01-2: 验证码登录/注册', async () => {
  24. const res = await apiPost('/api/auth/login', {
  25. phone,
  26. code: '123456', // 测试验证码
  27. });
  28. expect(res.code).toBe(0);
  29. expect(res.data?.token).toBeDefined();
  30. authToken = res.data?.token;
  31. setAuthToken(authToken);
  32. });
  33. test('C01-3: 获取用户信息', async () => {
  34. const res = await apiGet('/api/auth/user-info');
  35. expect(res.code).toBe(0);
  36. expect(res.data?.phone).toBe(phone);
  37. });
  38. test('C01-4: 新用户默认免费会员(memberLevel=0)', async () => {
  39. const res = await apiGet('/api/auth/user-info');
  40. expect(res.data?.memberLevel).toBe(0);
  41. });
  42. test('C01-5: 首页可正常访问', async ({ page }) => {
  43. await page.goto(`${FRONTEND_URL}/#/pages/index/index`, { waitUntil: 'networkidle', timeout: 30000 });
  44. await page.waitForTimeout(3000);
  45. await expect(page.locator('#app')).toBeVisible();
  46. });
  47. test('C01-6: 清理测试用户', async () => {
  48. // 如果后端支持删除账户则调用,否则跳过
  49. try {
  50. await apiDelete('/api/auth/account');
  51. } catch {
  52. // 跳过清理
  53. }
  54. });
  55. });