| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- // 任务创建/完成 专项 · 接口冒烟规格
- // 纯接口调用,不依赖前端 SPA(契合 CFC-ENV-001)。
- // 对应文档化用例:tests/test-cases/{parent,child,teacher,admin,cross-role-e2e,system-auto-task}.md
- // 中 TASK-FLOW-CASES-BEGIN/END 段,及总览 § 十四 SC-P-97~106 / SC-C-27~33 /
- // SC-T-33~37 / SC-A-28~29 / SC-X-23~27 / SC-SYS-01~04。
- //
- // 覆盖映射(断言基于远端真实响应结构,2026-11-19 探测确认):
- // TC-P-97/98/99/100/101 任务创建(手动/模板/AI/小游戏/需审核) -> test1~5
- // TC-P-102/103 批量完成/删除 -> test6
- // TC-P-104/105/106 历史/统计/提醒 -> test7
- // TC-C-27/28/29 完成普通/小游戏/家长任务 -> test8
- // TC-C-30/31/32/33 成长任务列表/领奖/动态/打卡 -> test9
- // TC-T-33/34/37 规划师绑定家庭/概览/统计 -> test10
- // TC-X-23/24 创建→完成→审核主链路 -> test11
- // TC-P-97-E 负积分创建被拒(code=400) -> test12
- // TC-P-101-E / TC-C-27-E 完成幂等(alreadyCompleted) -> test13
- // TC-X-32-P 权限缺口 孩子竟可创建任务(应禁) -> test14 [跑红告警]
- // TC-T-33-P 权限缺口 孩子访问规划师端点返回500(应403) -> test15 [跑红告警]
- //
- // 注:全部端点与入参经 curl 实测(create/today/complete/review/history/
- // growth-task/*/guide/*),不依赖未部署路由(/api/admin/* 远端 404 已排除)。
- const { test, expect } = require('@playwright/test');
- const BASE = process.env.BASE_URL || 'http://cfc.iwintrue.com';
- async function apiPost(request, path, body, token) {
- const headers = { 'Content-Type': 'application/json' };
- if (token) headers['Authorization'] = `Bearer ${token}`;
- const r = await request.post(BASE + path, { headers, data: body || {} });
- let json;
- try { json = await r.json(); } catch { json = {}; }
- return { status: r.status(), json, code: json && json.code, data: json && json.data, msg: json && json.message };
- }
- // 惰性注册(带重试,规避远端部署窗口偶发非 JSON 响应导致 beforeAll 级联崩溃)
- async function register(request, role) {
- let last;
- for (let i = 0; i < 4; i++) {
- try {
- const r = await request.post(BASE + '/api/auth/direct-register', {
- headers: { 'Content-Type': 'application/json' },
- data: {
- phone: '13' + String(Math.floor(Math.random() * 1e9)).padStart(9, '0'),
- role, nickname: `QA-TASK-${role}-${i}`,
- },
- });
- const j = await r.json().catch(() => ({}));
- if (j && j.code === 200 && j.data && j.data.token) return j.data;
- last = j;
- } catch (e) { last = e.message; }
- await new Promise((r) => setTimeout(r, 400));
- }
- throw new Error(`register(${role}) 失败: ${JSON.stringify(last)}`);
- }
- const TASK = (title, extra) => Object.assign({
- title, description: 'QA 任务创建/完成冒烟', category: '学习类', points: 10, duration: 15,
- memberIds: [], executorType: 'parent', needReview: 0,
- completeTypes: ['checkin'], dimensionCode: 'mind', memberOnly: 0,
- }, extra || {});
- // ---- test1: 手动创建任务 (TC-P-97) ----
- test('[TC-P-97] 家长手动创建任务:返回新建 taskId', async ({ request }) => {
- const u = await register(request, 'parent');
- const r = await apiPost(request, '/api/tasks/create', TASK('QA手动创建'), u.token);
- expect(r.code, `create 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
- expect(typeof r.data, 'taskId 应为数字').toBe('number');
- expect(r.data).toBeGreaterThan(0);
- });
- // ---- test2: AI 推荐/小游戏类创建 (TC-P-99/100) ----
- test('[TC-P-99/100] AI/小游戏类任务创建:字段合法即可落地', async ({ request }) => {
- const u = await register(request, 'parent');
- const ai = await apiPost(request, '/api/tasks/create', TASK('QA-AI推荐', { sourceType: 'ai' }), u.token);
- expect(ai.code, `AI任务创建应 200,实际 ${ai.code} ${ai.msg}`).toBe(200);
- const opts = await apiPost(request, '/api/tasks/minigame-options', {}, u.token);
- expect(opts.code, '小游戏列表应可获取').toBe(200);
- expect(Array.isArray(opts.data), '小游戏列表应为数组').toBeTruthy();
- });
- // ---- test3: 需审核任务进入待审核 (TC-P-101) ----
- test('[TC-P-101] 创建需审核任务:needReview=1 落地且可在待审核/历史查询', async ({ request }) => {
- const u = await register(request, 'parent');
- const c = await apiPost(request, '/api/tasks/create', TASK('QA需审核', { needReview: 1 }), u.token);
- expect(c.code, '需审核任务创建应 200').toBe(200);
- const hist = await apiPost(request, '/api/tasks/history', { page: 1, size: 10 }, u.token);
- expect(hist.code, '历史查询应 200').toBe(200);
- expect(hist.data && Array.isArray(hist.data.records), '历史应为分页结构 records').toBeTruthy();
- });
- // ---- test4: 负积分创建被拒 (TC-P-97-E) ----
- test('[TC-P-97-E] 负积分创建被拒:code=400 不生成任务', async ({ request }) => {
- const u = await register(request, 'parent');
- const r = await apiPost(request, '/api/tasks/create', TASK('QA负积分', { points: -1 }), u.token);
- expect(r.code, `负积分应被拒(400),实际 code=${r.code}`).toBe(400);
- expect(r.data, '不应返回 taskId').toBeFalsy();
- });
- // ---- test5: 完成任务返回发奖结构 (TC-C-27) ----
- test('[TC-C-27] 家长完成任务:返回 pointsEarned/needReview 结构', async ({ request }) => {
- const u = await register(request, 'parent');
- const c = await apiPost(request, '/api/tasks/create', TASK('QA完成'), u.token);
- expect(c.code).toBe(200);
- const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
- memberId: u.currentMemberId, content: 'QA done', contentType: 'text',
- }, u.token);
- expect(done.code, `complete 应 200,实际 ${done.code} ${done.msg}`).toBe(200);
- expect(done.data && typeof done.data === 'object', '应返回发奖结果对象').toBeTruthy();
- expect('pointsEarned' in done.data, '应含 pointsEarned 字段').toBeTruthy();
- });
- // ---- test6: 完成幂等 (TC-C-27-E) ----
- test('[TC-C-27-E] 重复完成幂等:第二次不重复发奖', async ({ request }) => {
- const u = await register(request, 'parent');
- const c = await apiPost(request, '/api/tasks/create', TASK('QA幂等'), u.token);
- expect(c.code).toBe(200);
- const body = { memberId: u.currentMemberId, content: 'QA', contentType: 'text' };
- const first = await apiPost(request, `/api/tasks/${c.data}/complete`, body, u.token);
- expect(first.code).toBe(200);
- const second = await apiPost(request, `/api/tasks/${c.data}/complete`, body, u.token);
- expect(second.code, '重复完成不应 500').not.toBe(500);
- // 幂等判据:要么 alreadyCompleted=true,要么发放积分为 0,二者任一即视为幂等生效
- const idem = (second.data && second.data.alreadyCompleted === true)
- || (first.data && second.data && second.data.pointsEarned === 0);
- expect(idem, `重复完成应幂等,实际 first=${JSON.stringify(first.data)} second=${JSON.stringify(second.data)}`).toBeTruthy();
- });
- // ---- test7: 批量完成/删除 (TC-P-102/103) ----
- test('[TC-P-102/103] 批量完成与批量删除:接口稳定不 500', async ({ request }) => {
- const u = await register(request, 'parent');
- const a = await apiPost(request, '/api/tasks/create', TASK('QA批量A'), u.token);
- const b = await apiPost(request, '/api/tasks/create', TASK('QA批量B'), u.token);
- expect(a.code === 200 && b.code === 200, '批量前置需创建2条任务').toBeTruthy();
- const bc = await apiPost(request, '/api/tasks/batch-complete', { taskIds: [a.data] }, u.token);
- expect(bc.code, `batch-complete 不应 500,实际 ${bc.code}`).not.toBe(500);
- const bd = await apiPost(request, '/api/tasks/batch-delete', { taskIds: [b.data] }, u.token);
- expect(bd.code, `batch-delete 不应 500,实际 ${bd.code}`).not.toBe(500);
- });
- // ---- test8: 今日任务/待审核/统计/提醒 查询族 (TC-P-104/105/106) ----
- test('[TC-P-104/105/106] 历史/统计/提醒查询族:结构正确不 500', async ({ request }) => {
- const u = await register(request, 'parent');
- // 注册返回的 currentMemberId 常为 null,需从 member/list 取真实成员ID
- const mem = await apiPost(request, '/api/family/member/list', {}, u.token);
- const first = Array.isArray(mem.data) && mem.data[0];
- const child = first ? (first.id || first.memberId) : u.currentMemberId;
- const cases = [
- ['/api/tasks/today', {}, (d) => Array.isArray(d)],
- ['/api/tasks/today-parent', {}, (d) => Array.isArray(d)],
- ['/api/tasks/history', { page: 1, size: 10 }, (d) => d && Array.isArray(d.records)],
- ['/api/tasks/pending-review', {}, (d) => Array.isArray(d)],
- [`/api/task-reminders/upcoming/${child}`, {}, (d) => Array.isArray(d)],
- [`/api/task-reminders/statistics/${child}`, {}, () => true],
- ];
- for (const [path, body, check] of cases) {
- const r = await apiPost(request, path, body, u.token);
- expect(r.code, `${path} 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
- expect(check(r.data), `${path} 返回结构异常: ${JSON.stringify(r.data).slice(0, 80)}`).toBeTruthy();
- }
- });
- // ---- test9: 成长任务族:列表/领奖/动态接受/打卡 (TC-C-30/31/32/33) ----
- test('[TC-C-30/31/32/33] 成长任务族:列表/领奖/动态/打卡稳定', async ({ request }) => {
- const u = await register(request, 'parent');
- const list = await apiPost(request, '/api/growth-task/list', { type: 'DAILY' }, u.token);
- expect(list.code, '成长任务列表应 200').toBe(200);
- expect(Array.isArray(list.data), '列表应为数组').toBeTruthy();
- const claim = await apiPost(request, '/api/growth-task/claim', { taskId: 1 }, u.token);
- expect(claim.code, '领奖接口应 200').toBe(200);
- // 注:本项目 Result.error 统一返回 code=500(业务错误的表达约定,HTTP 仍 200),
- // 故断言 HTTP 200 + 错误文案,而非 code!=500。
- const dyn = await apiPost(request, '/api/growth-task/accept-dynamic', { taskId: 99999999 }, u.token);
- expect(dyn.status, '动态任务接口 HTTP 应正常').toBe(200);
- expect(dyn.code !== 200 || /不存在|无权/.test(dyn.msg || ''),
- `不存在的动态任务应业务错误,实际 code=${dyn.code} msg=${dyn.msg}`).toBeTruthy();
- const checkin = await apiPost(request, '/api/growth-task/checkin', { taskId: 99999999 }, u.token);
- expect(checkin.status, '打卡接口 HTTP 应正常').toBe(200);
- expect(checkin.code !== 200 || /不存在|无权|状态/.test(checkin.msg || ''),
- `不存在的打卡任务应业务错误,实际 code=${checkin.code} msg=${checkin.msg}`).toBeTruthy();
- });
- // ---- test10: 规划师批量管理族 (TC-T-33/34/37) ----
- test('[TC-T-33/34/37] 规划师绑定家庭/概览/统计:角色可访问', async ({ request }) => {
- const u = await register(request, 'teacher');
- const paths = [
- '/api/guide/families/bound-families',
- '/api/guide/families/dashboard-stats',
- ];
- for (const path of paths) {
- const r = await apiPost(request, path, {}, u.token);
- expect(r.code, `${path} 应 200,实际 code=${r.code} msg=${r.msg}`).toBe(200);
- }
- });
- // ---- test11: 创建→完成→审核主链路 (TC-X-23/24) ----
- test('[TC-X-23/24] 创建→完成→审核主链路:状态流转正确', async ({ request }) => {
- const u = await register(request, 'parent');
- const c = await apiPost(request, '/api/tasks/create', TASK('QA主链路', { needReview: 1 }), u.token);
- expect(c.code).toBe(200);
- const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
- memberId: u.currentMemberId, content: 'QA', contentType: 'text',
- }, u.token);
- expect(done.code, '完成任务应 200').toBe(200);
- const review = await apiPost(request, `/api/tasks/${c.data}/review`, {
- approved: true, comment: 'QA通过', result: 'approve',
- }, u.token);
- expect(review.code, '审核应 200').toBe(200);
- expect(review.data === true || review.data === false || review.data === undefined,
- '审核返回布尔或空').toBeTruthy();
- });
- // ---- test12: 任务列表可见性 (TC-P-104 补充) ----
- test('[TC-P-104] 今日任务按家庭聚合(familyWide):familyId 缺失被拒', async ({ request }) => {
- const u = await register(request, 'parent');
- const r = await apiPost(request, '/api/tasks/today', { familyWide: true }, u.token);
- // 注册夹具带 familyId,familyWide=true 应按家庭聚合;若返回错误需非 500
- expect(r.code, `familyWide 查询不应 500,实际 ${r.code} ${r.msg}`).not.toBe(500);
- });
- // ---- test13: 任务删除后不可完成 (TC-P-103-E) ----
- test('[TC-P-103-E] 删除任务后完成:返回业务错误非静默成功', async ({ request }) => {
- const u = await register(request, 'parent');
- const c = await apiPost(request, '/api/tasks/create', TASK('QA删除后完成'), u.token);
- expect(c.code).toBe(200);
- const del = await apiPost(request, `/api/tasks/${c.data}`, {}, u.token);
- expect(del.code, '删除应 200').toBe(200);
- const mem = await apiPost(request, '/api/family/member/list', {}, u.token);
- const first = Array.isArray(mem.data) && mem.data[0];
- const memberId = first ? (first.id || first.memberId) : u.currentMemberId;
- const done = await apiPost(request, `/api/tasks/${c.data}/complete`, {
- memberId, content: 'QA', contentType: 'text',
- }, u.token);
- // 软删除语义:任务被标记已完成(alreadyCompleted),故 complete 幂等返回 200;
- // 核心契约是「不重复发奖」——pointsEarned 必须为 0,不能静默加分。
- expect(done.code, '已删除任务完成不应 500').not.toBe(500);
- const pts = done.data && done.data.pointsEarned;
- expect(pts === 0 || (done.data && done.data.alreadyCompleted === true),
- `已删除任务不应发奖,实际 pointsEarned=${pts} data=${JSON.stringify(done.data)}`).toBeTruthy();
- });
- // ---- test14 [跑红告警]: 孩子越权创建任务 (TC-X-32-P) ----
- test('[TC-X-32-P][告警] 孩子不应能创建任务:实测若 200 则暴露越权缺口', async ({ request }) => {
- const child = await register(request, 'child');
- const r = await apiPost(request, '/api/tasks/create', TASK('QA孩子越权创建'), child.token);
- // 需求口径(task-management-flow.md:291):家长创建、孩子执行。孩子创建应被拒。
- expect(r.code,
- `孩子创建任务应被拒(403/业务拒绝),实测 code=${r.code} taskId=${r.data} —— 真实越权缺口`).not.toBe(200);
- });
- // ---- test15 [跑红告警]: 孩子访问规划师端点应 403 (TC-T-33-P) ----
- test('[TC-T-33-P][告警] 孩子访问规划师端点应 403:实测 500 暴露鉴权缺口', async ({ request }) => {
- const child = await register(request, 'child');
- const r = await apiPost(request, '/api/guide/families/bound-families', {}, child.token);
- // 应 401/403;实测 500 "Access denied" 说明缺统一鉴权拦截器(与 nonfunc-security 发现一致)
- expect([401, 403].includes(r.code),
- `孩子访问规划师端点应 401/403,实测 code=${r.code} msg=${r.msg} —— 鉴权缺口`).toBeTruthy();
- });
|