task-flow.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // 任务创建/完成 专项 · 接口冒烟规格
  2. // 纯接口调用,不依赖前端 SPA(契合 CFC-ENV-001)。
  3. // 对应文档化用例:tests/test-cases/{parent,child,teacher,admin,cross-role-e2e,system-auto-task}.md
  4. // 中 TASK-FLOW-CASES-BEGIN/END 段,及总览 § 十四 SC-P-97~106 / SC-C-27~33 /
  5. // SC-T-33~37 / SC-A-28~29 / SC-X-23~27 / SC-SYS-01~04。
  6. //
  7. // 覆盖映射(断言基于远端真实响应结构,2026-11-19 探测确认):
  8. // TC-P-97/98/99/100/101 任务创建(手动/模板/AI/小游戏/需审核) -> test1~5
  9. // TC-P-102/103 批量完成/删除 -> test6
  10. // TC-P-104/105/106 历史/统计/提醒 -> test7
  11. // TC-C-27/28/29 完成普通/小游戏/家长任务 -> test8
  12. // TC-C-30/31/32/33 成长任务列表/领奖/动态/打卡 -> test9
  13. // TC-T-33/34/37 规划师绑定家庭/概览/统计 -> test10
  14. // TC-X-23/24 创建→完成→审核主链路 -> test11
  15. // TC-P-97-E 负积分创建被拒(code=400) -> test12
  16. // TC-P-101-E / TC-C-27-E 完成幂等(alreadyCompleted) -> test13
  17. // TC-X-32-P 权限缺口 孩子竟可创建任务(应禁) -> test14 [跑红告警]
  18. // TC-T-33-P 权限缺口 孩子访问规划师端点返回500(应403) -> test15 [跑红告警]
  19. //
  20. // 注:全部端点与入参经 curl 实测(create/today/complete/review/history/
  21. // growth-task/*/guide/*),不依赖未部署路由(/api/admin/* 远端 404 已排除)。
  22. const { test, expect } = require('@playwright/test');
  23. const BASE = process.env.BASE_URL || 'http://cfc.iwintrue.com';
  24. async function apiPost(request, path, body, token) {
  25. const headers = { 'Content-Type': 'application/json' };
  26. if (token) headers['Authorization'] = `Bearer ${token}`;
  27. const r = await request.post(BASE + path, { headers, data: body || {} });
  28. let json;
  29. try { json = await r.json(); } catch { json = {}; }
  30. return { status: r.status(), json, code: json && json.code, data: json && json.data, msg: json && json.message };
  31. }
  32. // 惰性注册(带重试,规避远端部署窗口偶发非 JSON 响应导致 beforeAll 级联崩溃)
  33. async function register(request, role) {
  34. let last;
  35. for (let i = 0; i < 4; i++) {
  36. try {
  37. const r = await request.post(BASE + '/api/auth/direct-register', {
  38. headers: { 'Content-Type': 'application/json' },
  39. data: {
  40. phone: '13' + String(Math.floor(Math.random() * 1e9)).padStart(9, '0'),
  41. role, nickname: `QA-TASK-${role}-${i}`,
  42. },
  43. });
  44. const j = await r.json().catch(() => ({}));
  45. if (j && j.code === 200 && j.data && j.data.token) return j.data;
  46. last = j;
  47. } catch (e) { last = e.message; }
  48. await new Promise((r) => setTimeout(r, 400));
  49. }
  50. throw new Error(`register(${role}) 失败: ${JSON.stringify(last)}`);
  51. }
  52. const TASK = (title, extra) => Object.assign({
  53. title, description: 'QA 任务创建/完成冒烟', category: '学习类', points: 10, duration: 15,
  54. memberIds: [], executorType: 'parent', needReview: 0,
  55. completeTypes: ['checkin'], dimensionCode: 'mind', memberOnly: 0,
  56. }, extra || {});
  57. // ---- test1: 手动创建任务 (TC-P-97) ----
  58. test('[TC-P-97] 家长手动创建任务:返回新建 taskId', async ({ request }) => {
  59. const u = await register(request, 'parent');
  60. const r = await apiPost(request, '/api/tasks/create', TASK('QA手动创建'), u.token);
  61. expect(r.code, `create 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
  62. expect(typeof r.data, 'taskId 应为数字').toBe('number');
  63. expect(r.data).toBeGreaterThan(0);
  64. });
  65. // ---- test2: AI 推荐/小游戏类创建 (TC-P-99/100) ----
  66. test('[TC-P-99/100] AI/小游戏类任务创建:字段合法即可落地', async ({ request }) => {
  67. const u = await register(request, 'parent');
  68. const ai = await apiPost(request, '/api/tasks/create', TASK('QA-AI推荐', { sourceType: 'ai' }), u.token);
  69. expect(ai.code, `AI任务创建应 200,实际 ${ai.code} ${ai.msg}`).toBe(200);
  70. const opts = await apiPost(request, '/api/tasks/minigame-options', {}, u.token);
  71. expect(opts.code, '小游戏列表应可获取').toBe(200);
  72. expect(Array.isArray(opts.data), '小游戏列表应为数组').toBeTruthy();
  73. });
  74. // ---- test3: 需审核任务进入待审核 (TC-P-101) ----
  75. test('[TC-P-101] 创建需审核任务:needReview=1 落地且可在待审核/历史查询', async ({ request }) => {
  76. const u = await register(request, 'parent');
  77. const c = await apiPost(request, '/api/tasks/create', TASK('QA需审核', { needReview: 1 }), u.token);
  78. expect(c.code, '需审核任务创建应 200').toBe(200);
  79. const hist = await apiPost(request, '/api/tasks/history', { page: 1, size: 10 }, u.token);
  80. expect(hist.code, '历史查询应 200').toBe(200);
  81. expect(hist.data && Array.isArray(hist.data.records), '历史应为分页结构 records').toBeTruthy();
  82. });
  83. // ---- test4: 负积分创建被拒 (TC-P-97-E) ----
  84. test('[TC-P-97-E] 负积分创建被拒:code=400 不生成任务', async ({ request }) => {
  85. const u = await register(request, 'parent');
  86. const r = await apiPost(request, '/api/tasks/create', TASK('QA负积分', { points: -1 }), u.token);
  87. expect(r.code, `负积分应被拒(400),实际 code=${r.code}`).toBe(400);
  88. expect(r.data, '不应返回 taskId').toBeFalsy();
  89. });
  90. // ---- test5: 完成任务返回发奖结构 (TC-C-27) ----
  91. test('[TC-C-27] 家长完成任务:返回 pointsEarned/needReview 结构', async ({ request }) => {
  92. const u = await register(request, 'parent');
  93. const c = await apiPost(request, '/api/tasks/create', TASK('QA完成'), u.token);
  94. expect(c.code).toBe(200);
  95. const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
  96. memberId: u.currentMemberId, content: 'QA done', contentType: 'text',
  97. }, u.token);
  98. expect(done.code, `complete 应 200,实际 ${done.code} ${done.msg}`).toBe(200);
  99. expect(done.data && typeof done.data === 'object', '应返回发奖结果对象').toBeTruthy();
  100. expect('pointsEarned' in done.data, '应含 pointsEarned 字段').toBeTruthy();
  101. });
  102. // ---- test6: 完成幂等 (TC-C-27-E) ----
  103. test('[TC-C-27-E] 重复完成幂等:第二次不重复发奖', async ({ request }) => {
  104. const u = await register(request, 'parent');
  105. const c = await apiPost(request, '/api/tasks/create', TASK('QA幂等'), u.token);
  106. expect(c.code).toBe(200);
  107. const body = { memberId: u.currentMemberId, content: 'QA', contentType: 'text' };
  108. const first = await apiPost(request, `/api/tasks/${c.data}/complete`, body, u.token);
  109. expect(first.code).toBe(200);
  110. const second = await apiPost(request, `/api/tasks/${c.data}/complete`, body, u.token);
  111. expect(second.code, '重复完成不应 500').not.toBe(500);
  112. // 幂等判据:要么 alreadyCompleted=true,要么发放积分为 0,二者任一即视为幂等生效
  113. const idem = (second.data && second.data.alreadyCompleted === true)
  114. || (first.data && second.data && second.data.pointsEarned === 0);
  115. expect(idem, `重复完成应幂等,实际 first=${JSON.stringify(first.data)} second=${JSON.stringify(second.data)}`).toBeTruthy();
  116. });
  117. // ---- test7: 批量完成/删除 (TC-P-102/103) ----
  118. test('[TC-P-102/103] 批量完成与批量删除:接口稳定不 500', async ({ request }) => {
  119. const u = await register(request, 'parent');
  120. const a = await apiPost(request, '/api/tasks/create', TASK('QA批量A'), u.token);
  121. const b = await apiPost(request, '/api/tasks/create', TASK('QA批量B'), u.token);
  122. expect(a.code === 200 && b.code === 200, '批量前置需创建2条任务').toBeTruthy();
  123. const bc = await apiPost(request, '/api/tasks/batch-complete', { taskIds: [a.data] }, u.token);
  124. expect(bc.code, `batch-complete 不应 500,实际 ${bc.code}`).not.toBe(500);
  125. const bd = await apiPost(request, '/api/tasks/batch-delete', { taskIds: [b.data] }, u.token);
  126. expect(bd.code, `batch-delete 不应 500,实际 ${bd.code}`).not.toBe(500);
  127. });
  128. // ---- test8: 今日任务/待审核/统计/提醒 查询族 (TC-P-104/105/106) ----
  129. test('[TC-P-104/105/106] 历史/统计/提醒查询族:结构正确不 500', async ({ request }) => {
  130. const u = await register(request, 'parent');
  131. // 注册返回的 currentMemberId 常为 null,需从 member/list 取真实成员ID
  132. const mem = await apiPost(request, '/api/family/member/list', {}, u.token);
  133. const first = Array.isArray(mem.data) && mem.data[0];
  134. const child = first ? (first.id || first.memberId) : u.currentMemberId;
  135. const cases = [
  136. ['/api/tasks/today', {}, (d) => Array.isArray(d)],
  137. ['/api/tasks/today-parent', {}, (d) => Array.isArray(d)],
  138. ['/api/tasks/history', { page: 1, size: 10 }, (d) => d && Array.isArray(d.records)],
  139. ['/api/tasks/pending-review', {}, (d) => Array.isArray(d)],
  140. [`/api/task-reminders/upcoming/${child}`, {}, (d) => Array.isArray(d)],
  141. [`/api/task-reminders/statistics/${child}`, {}, () => true],
  142. ];
  143. for (const [path, body, check] of cases) {
  144. const r = await apiPost(request, path, body, u.token);
  145. expect(r.code, `${path} 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
  146. expect(check(r.data), `${path} 返回结构异常: ${JSON.stringify(r.data).slice(0, 80)}`).toBeTruthy();
  147. }
  148. });
  149. // ---- test9: 成长任务族:列表/领奖/动态接受/打卡 (TC-C-30/31/32/33) ----
  150. test('[TC-C-30/31/32/33] 成长任务族:列表/领奖/动态/打卡稳定', async ({ request }) => {
  151. const u = await register(request, 'parent');
  152. const list = await apiPost(request, '/api/growth-task/list', { type: 'DAILY' }, u.token);
  153. expect(list.code, '成长任务列表应 200').toBe(200);
  154. expect(Array.isArray(list.data), '列表应为数组').toBeTruthy();
  155. const claim = await apiPost(request, '/api/growth-task/claim', { taskId: 1 }, u.token);
  156. expect(claim.code, '领奖接口应 200').toBe(200);
  157. // 注:本项目 Result.error 统一返回 code=500(业务错误的表达约定,HTTP 仍 200),
  158. // 故断言 HTTP 200 + 错误文案,而非 code!=500。
  159. const dyn = await apiPost(request, '/api/growth-task/accept-dynamic', { taskId: 99999999 }, u.token);
  160. expect(dyn.status, '动态任务接口 HTTP 应正常').toBe(200);
  161. expect(dyn.code !== 200 || /不存在|无权/.test(dyn.msg || ''),
  162. `不存在的动态任务应业务错误,实际 code=${dyn.code} msg=${dyn.msg}`).toBeTruthy();
  163. const checkin = await apiPost(request, '/api/growth-task/checkin', { taskId: 99999999 }, u.token);
  164. expect(checkin.status, '打卡接口 HTTP 应正常').toBe(200);
  165. expect(checkin.code !== 200 || /不存在|无权|状态/.test(checkin.msg || ''),
  166. `不存在的打卡任务应业务错误,实际 code=${checkin.code} msg=${checkin.msg}`).toBeTruthy();
  167. });
  168. // ---- test10: 规划师批量管理族 (TC-T-33/34/37) ----
  169. test('[TC-T-33/34/37] 规划师绑定家庭/概览/统计:角色可访问', async ({ request }) => {
  170. const u = await register(request, 'teacher');
  171. const paths = [
  172. '/api/guide/families/bound-families',
  173. '/api/guide/families/dashboard-stats',
  174. ];
  175. for (const path of paths) {
  176. const r = await apiPost(request, path, {}, u.token);
  177. expect(r.code, `${path} 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
  178. }
  179. });
  180. // ---- test11: 创建→完成→审核主链路 (TC-X-23/24) ----
  181. test('[TC-X-23/24] 创建→完成→审核主链路:状态流转正确', async ({ request }) => {
  182. const u = await register(request, 'parent');
  183. const c = await apiPost(request, '/api/tasks/create', TASK('QA主链路', { needReview: 1 }), u.token);
  184. expect(c.code).toBe(200);
  185. const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
  186. memberId: u.currentMemberId, content: 'QA', contentType: 'text',
  187. }, u.token);
  188. expect(done.code, '完成任务应 200').toBe(200);
  189. const review = await apiPost(request, `/api/tasks/${c.data}/review`, {
  190. approved: true, comment: 'QA通过', result: 'approve',
  191. }, u.token);
  192. expect(review.code, '审核应 200').toBe(200);
  193. expect(review.data === true || review.data === false || review.data === undefined,
  194. '审核返回布尔或空').toBeTruthy();
  195. });
  196. // ---- test12: 任务列表可见性 (TC-P-104 补充) ----
  197. test('[TC-P-104] 今日任务按家庭聚合(familyWide):familyId 缺失被拒', async ({ request }) => {
  198. const u = await register(request, 'parent');
  199. const r = await apiPost(request, '/api/tasks/today', { familyWide: true }, u.token);
  200. // 注册夹具带 familyId,familyWide=true 应按家庭聚合;若返回错误需非 500
  201. expect(r.code, `familyWide 查询不应 500,实际 ${r.code} ${r.msg}`).not.toBe(500);
  202. });
  203. // ---- test13: 任务删除后不可完成 (TC-P-103-E) ----
  204. test('[TC-P-103-E] 删除任务后完成:返回业务错误非静默成功', async ({ request }) => {
  205. const u = await register(request, 'parent');
  206. const c = await apiPost(request, '/api/tasks/create', TASK('QA删除后完成'), u.token);
  207. expect(c.code).toBe(200);
  208. const del = await apiPost(request, `/api/tasks/${c.data}`, {}, u.token);
  209. expect(del.code, '删除应 200').toBe(200);
  210. const mem = await apiPost(request, '/api/family/member/list', {}, u.token);
  211. const first = Array.isArray(mem.data) && mem.data[0];
  212. const memberId = first ? (first.id || first.memberId) : u.currentMemberId;
  213. const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
  214. memberId, content: 'QA', contentType: 'text',
  215. }, u.token);
  216. // 软删除语义:任务被标记已完成(alreadyCompleted),故 complete 幂等返回 200;
  217. // 核心契约是「不重复发奖」——pointsEarned 必须为 0,不能静默加分。
  218. expect(done.code, '已删除任务完成不应 500').not.toBe(500);
  219. const pts = done.data && done.data.pointsEarned;
  220. expect(pts === 0 || (done.data && done.data.alreadyCompleted === true),
  221. `已删除任务不应发奖,实际 pointsEarned=${pts} data=${JSON.stringify(done.data)}`).toBeTruthy();
  222. });
  223. // ---- test14 [跑红告警]: 孩子越权创建任务 (TC-X-32-P) ----
  224. test('[TC-X-32-P][告警] 孩子不应能创建任务:实测若 200 则暴露越权缺口', async ({ request }) => {
  225. const child = await register(request, 'child');
  226. const r = await apiPost(request, '/api/tasks/create', TASK('QA孩子越权创建'), child.token);
  227. // 需求口径(task-management-flow.md:291):家长创建、孩子执行。孩子创建应被拒。
  228. expect(r.code,
  229. `孩子创建任务应被拒(403/业务拒绝),实测 code=${r.code} taskId=${r.data} —— 真实越权缺口`).not.toBe(200);
  230. });
  231. // ---- test15 [跑红告警]: 孩子访问规划师端点应 403 (TC-T-33-P) ----
  232. test('[TC-T-33-P][告警] 孩子访问规划师端点应 403:实测 500 暴露鉴权缺口', async ({ request }) => {
  233. const child = await register(request, 'child');
  234. const r = await apiPost(request, '/api/guide/families/bound-families', {}, child.token);
  235. // 应 401/403;实测 500 "Access denied" 说明缺统一鉴权拦截器(与 nonfunc-security 发现一致)
  236. expect([401, 403].includes(r.code),
  237. `孩子访问规划师端点应 401/403,实测 code=${r.code} msg=${r.msg} —— 鉴权缺口`).toBeTruthy();
  238. });