|
|
@@ -0,0 +1,290 @@
|
|
|
+/**
|
|
|
+ * 家庭任务打卡 / 补打卡链路 三类流(编排目标 SC-T-9「补打卡 / 家庭任务打卡链路」)
|
|
|
+ * ================================================================
|
|
|
+ * 编号说明(不臆造映射):本文件按任务目标段的主题「补打卡 / 家庭任务打卡链路」+ B-275-1 落地;
|
|
|
+ * 仓库 sc-key-map 中规范编号对应 SC-C-05(child.task.checkin,任务打卡)与 SC-C-02
|
|
|
+ * (child.task.complete,任务完成)。sc-key-map 的 SC-T-09 字面为「测评预约确认(状态守卫)」,
|
|
|
+ * 与目标段主题不一致,已在汇报中如实说明,未改任何台账。
|
|
|
+ *
|
|
|
+ * 【探针先行】以下断言全部来自忽略型探针 tests/e2e/api/_probe_task_chain_v{1..5}.spec.js
|
|
|
+ * (gitignore,不入库)对 http://cfc.iwintrue.com 的实测,不臆造:
|
|
|
+ * - 切换视角 POST /api/family/member/switch 入参是 {memberId},响应 data 不含 token
|
|
|
+ * (孩子为不可登录成员,正确调用模型 = 家长 token + 显式 memberId,同 task-c14/task-expiry 既有 spec)。
|
|
|
+ * - 完成 POST /api/tasks/{id}/complete(登录门 /api/tasks/ 已登记 LOGIN_REQUIRED_PATHS):
|
|
|
+ * 正常 → 200 {pointsEarned>0, overdue:false, needReview:false, newBalance};
|
|
|
+ * 重复完成 → 200 {pointsEarned:0, alreadyCompleted:true, newBalance 不变},积分流水仅 1 条正记录;
|
|
|
+ * achieved=0 → 200 {pointsEarned:0},history status=completed / achieved=0,无积分流水;
|
|
|
+ * 跨天 overdue 补打卡 achieved=1 → 200 {pointsEarned:0, overdue:true},history completed/achieved=1;
|
|
|
+ * requireInput=1 且无内容 → code=400「该任务需填写内容后才能完成」;
|
|
|
+ * 带内容 → 200 {needReview:true, pointsEarned:0}(待确认,不立即发分);
|
|
|
+ * 任务不存在 → code=500「任务不存在」;无 token → HTTP 401 / code 401「请先登录」。
|
|
|
+ * - 打卡 POST /api/growth-task/checkin(统一任务系统同一 service):
|
|
|
+ * 正常 → 200 发奖;缺 taskId → code=500「taskId不能为空」;非法 token → 401。
|
|
|
+ * - 补打卡构造:POST /api/tasks/create 拒绝过去 deadline(「任务截止时间不能早于当前时间」),
|
|
|
+ * 故 overdue 任务经管理端 POST /api/admin/families/{familyId}/tasks/create 构造(同 task-expiry-crossday.spec.js)。
|
|
|
+ *
|
|
|
+ * 【命中长期 blocker(只标承袭,不造绿、不写放水断言)】
|
|
|
+ * - B-275-1(任务完成链路 500,历史 P0):T24-A 幂等热修后,本轮探针在正常/重复/补打卡路径
|
|
|
+ * 均未再复现 500;不为历史缺陷写绿断言,本文件只断言实测契约。
|
|
|
+ * - B-275-3(跨家庭/跨成员越权未拒,P1 长期):跨家庭完成实测 200(非 403)→ 用 test.fixme 保留
|
|
|
+ * 严格期望 403,不写放水断言。
|
|
|
+ * - G-304-1(family ownership,长期 open):跨家庭伪装 memberId 实测 200 且真实发奖 →
|
|
|
+ * test.fixme 严格期望 403,不造绿。
|
|
|
+ * - 鉴权口径缺口(本轮探针新发现,未登记新编号):/api/growth-task/checkin 不在
|
|
|
+ * JwtInterceptor.LOGIN_REQUIRED_PATHS,匿名(无 token)携带 memberId+taskId 可完成全新任务并发奖
|
|
|
+ * (实测 200 {pointsEarned:8})→ test.fixme 严格期望 401;按要求不改 src/main,只在汇报中登记。
|
|
|
+ *
|
|
|
+ * 运行:
|
|
|
+ * 远端:npx playwright test tests/e2e/api/family-task-checkin-backfill.spec.js \
|
|
|
+ * -c tests/playwright.config.api.js --project=desktop
|
|
|
+ * 本地:CFC_API_BASE=http://127.0.0.1:9082 npx playwright test ...(同上)
|
|
|
+ */
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
+const { registerRole, apiPost, uniqueSuffix, pickMemberByUserId } = require('../helpers/api');
|
|
|
+
|
|
|
+/** 分页响应统一取 records(兼容 list) */
|
|
|
+function recordsOf(page) {
|
|
|
+ if (!page) return [];
|
|
|
+ if (Array.isArray(page)) return page;
|
|
|
+ return page.records || page.list || [];
|
|
|
+}
|
|
|
+
|
|
|
+function positiveRows(rows, taskId) {
|
|
|
+ return (rows || []).filter((r) => String(r.taskId) === String(taskId) && (r.amount || 0) > 0);
|
|
|
+}
|
|
|
+
|
|
|
+async function addChild(request, parent, tag) {
|
|
|
+ const mem = await apiPost(request, '/api/family/member/list', {}, parent.token);
|
|
|
+ expect(mem.code, 'member/list: ' + mem.message).toBe(200);
|
|
|
+ const self = pickMemberByUserId(mem.data, parent.userId);
|
|
|
+ expect(self, '家长本人成员记录').toBeTruthy();
|
|
|
+ const add = await apiPost(request, '/api/family/member/add', {
|
|
|
+ relativeMemberId: self.id,
|
|
|
+ generationLevel: 'child',
|
|
|
+ nickname: tag + uniqueSuffix(),
|
|
|
+ gender: 'female',
|
|
|
+ birthday: '2015-05-01',
|
|
|
+ }, parent.token);
|
|
|
+ expect(add.code, 'member/add child: ' + add.message).toBe(200);
|
|
|
+ return add.data.id;
|
|
|
+}
|
|
|
+
|
|
|
+async function createTask(request, token, memberId, extra) {
|
|
|
+ const body = {
|
|
|
+ title: '任务链' + uniqueSuffix(),
|
|
|
+ points: 10,
|
|
|
+ memberId,
|
|
|
+ frequency: 'daily',
|
|
|
+ needReview: 0,
|
|
|
+ };
|
|
|
+ if (extra) Object.keys(extra).forEach((k) => { body[k] = extra[k]; });
|
|
|
+ const r = await apiPost(request, '/api/tasks/create', body, token);
|
|
|
+ expect(r.code, 'tasks/create: ' + r.message).toBe(200);
|
|
|
+ expect(r.data, 'create 应返回 taskId').toBeTruthy();
|
|
|
+ return r.data;
|
|
|
+}
|
|
|
+
|
|
|
+async function historyTask(request, token, memberId, taskId) {
|
|
|
+ const r = await apiPost(request, '/api/tasks/history', { memberId, page: 1, size: 100 }, token);
|
|
|
+ expect(r.code, 'tasks/history: ' + r.message).toBe(200);
|
|
|
+ return recordsOf(r.data).find((x) => String(x.id) === String(taskId)) || null;
|
|
|
+}
|
|
|
+
|
|
|
+async function pointsRows(request, token, memberId, taskId) {
|
|
|
+ const r = await apiPost(request, '/api/points/logs', { memberId, page: 1, size: 100 }, token);
|
|
|
+ expect(r.code, 'points/logs: ' + r.message).toBe(200);
|
|
|
+ return recordsOf(r.data).filter((x) => String(x.taskId) === String(taskId));
|
|
|
+}
|
|
|
+
|
|
|
+test.describe('家庭任务打卡 / 补打卡链路 三类流', () => {
|
|
|
+ test.describe.configure({ mode: 'serial' });
|
|
|
+
|
|
|
+ let p1;
|
|
|
+ let p2;
|
|
|
+ let child1;
|
|
|
+ let child2;
|
|
|
+
|
|
|
+ test.beforeAll(async ({ request }) => {
|
|
|
+ p1 = await registerRole(request, 'parent', 'QA-TASKCHK-P1');
|
|
|
+ p2 = await registerRole(request, 'parent', 'QA-TASKCHK-P2');
|
|
|
+ child1 = await addChild(request, p1, 'C1-TaskChk');
|
|
|
+ child2 = await addChild(request, p2, 'C2-TaskChk');
|
|
|
+ });
|
|
|
+
|
|
|
+ // ================= 正常流 =================
|
|
|
+
|
|
|
+ test('[N1 正常·完成] 家长创建任务 → 以孩子 memberId 完成 → 200 发奖且非逾期', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 10 });
|
|
|
+ const done = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 }, p1.token);
|
|
|
+ expect(done.code, '完成任务: ' + done.message).toBe(200);
|
|
|
+ expect(done.data.pointsEarned, '正常完成应发奖').toBeGreaterThan(0);
|
|
|
+ expect(done.data.overdue, '当日任务非逾期').toBe(false);
|
|
|
+ expect(done.data.needReview, 'needReview=0 直接完成').toBe(false);
|
|
|
+ expect(positiveRows(await pointsRows(request, p1.token, child1, taskId), taskId).length, '正积分流水 1 条').toBe(1);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[N2 正常·填写式] requireInput 带内容提交 → 待确认(needReview)且不立即发分', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5, requireInput: 1 });
|
|
|
+ const done = await apiPost(request, `/api/tasks/${taskId}/complete`, {
|
|
|
+ memberId: child1, content: '完成内容' + uniqueSuffix(), contentType: 'text',
|
|
|
+ }, p1.token);
|
|
|
+ expect(done.code, '提交填写式任务: ' + done.message).toBe(200);
|
|
|
+ expect(done.data.needReview, '填写式需确认').toBe(true);
|
|
|
+ expect(done.data.pointsEarned, '待确认前不发分').toBe(0);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[N3 正常·补打卡] 跨天 overdue 任务 achieved=1 补打卡 → 完成但零奖励', async ({ request }) => {
|
|
|
+ const admin = await registerRole(request, 'admin', 'QA-TASKCHK-A');
|
|
|
+ const adminChild = await addChild(request, admin, 'AC-TaskChk');
|
|
|
+ const created = await apiPost(request, `/api/admin/families/${admin.familyId}/tasks/create`, {
|
|
|
+ title: '补打卡任务' + uniqueSuffix(),
|
|
|
+ description: 'overdue backfill',
|
|
|
+ category: '其他',
|
|
|
+ points: 10,
|
|
|
+ taskType: 'once',
|
|
|
+ frequency: 'none',
|
|
|
+ repeatType: 'none',
|
|
|
+ needReview: 0,
|
|
|
+ executorType: 'child',
|
|
|
+ executorId: adminChild,
|
|
|
+ memberId: adminChild,
|
|
|
+ familyMemberId: adminChild,
|
|
|
+ deadline: '2000-01-01 00:00:00',
|
|
|
+ latestEnd: '2000-01-01 00:00:00',
|
|
|
+ isTemplate: 0,
|
|
|
+ status: 'pending',
|
|
|
+ }, admin.token);
|
|
|
+ expect(created.code, '管理端构造 overdue: ' + created.message).toBe(200);
|
|
|
+ const taskId = created.data;
|
|
|
+
|
|
|
+ const backfill = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: adminChild, achieved: 1 }, admin.token);
|
|
|
+ expect(backfill.code, '补打卡: ' + backfill.message).toBe(200);
|
|
|
+ expect(backfill.data.overdue, '补打卡命中逾期').toBe(true);
|
|
|
+ expect(backfill.data.pointsEarned, '逾期补打卡零奖励').toBe(0);
|
|
|
+
|
|
|
+ const hit = await historyTask(request, admin.token, adminChild, taskId);
|
|
|
+ expect(hit, '补打卡后历史可查').toBeTruthy();
|
|
|
+ expect(hit.status, '补打卡后任务完成').toBe('completed');
|
|
|
+ expect(hit.achieved, 'achieved=1 落库').toBe(1);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[N4 正常·checkin 端点] /api/growth-task/checkin 正常打卡 → 200 发奖', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 6 });
|
|
|
+ const r = await apiPost(request, '/api/growth-task/checkin', {
|
|
|
+ memberId: child1, taskId, content: '打卡内容' + uniqueSuffix(), contentType: 'text',
|
|
|
+ }, p1.token);
|
|
|
+ expect(r.code, 'checkin: ' + r.message).toBe(200);
|
|
|
+ expect(r.data.pointsEarned, 'checkin 正常发奖').toBeGreaterThan(0);
|
|
|
+ });
|
|
|
+
|
|
|
+ // ================= 异常流 =================
|
|
|
+
|
|
|
+ test('[E1 异常·重复] 重复完成 → alreadyCompleted 幂等、0 分、余额不变、仅 1 条正流水', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 8 });
|
|
|
+ const first = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 }, p1.token);
|
|
|
+ expect(first.code, '首次完成: ' + first.message).toBe(200);
|
|
|
+ const balanceAfterFirst = first.data.newBalance;
|
|
|
+
|
|
|
+ const second = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 }, p1.token);
|
|
|
+ expect(second.code, '重复完成幂等: ' + second.message).toBe(200);
|
|
|
+ expect(second.data.alreadyCompleted, '重复完成标记').toBe(true);
|
|
|
+ expect(second.data.pointsEarned, '重复完成不重复发奖').toBe(0);
|
|
|
+ expect(second.data.newBalance, '重复完成余额不变').toBe(balanceAfterFirst);
|
|
|
+
|
|
|
+ expect(positiveRows(await pointsRows(request, p1.token, child1, taskId), taskId).length, '正积分流水仅 1 条').toBe(1);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E2 异常·未达成] achieved=0 → 任务完成但零奖励、achieved=0 落库、无积分流水', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 9 });
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1, achieved: 0 }, p1.token);
|
|
|
+ expect(r.code, '未达成提交: ' + r.message).toBe(200);
|
|
|
+ expect(r.data.pointsEarned, '未达成零奖励').toBe(0);
|
|
|
+
|
|
|
+ const hit = await historyTask(request, p1.token, child1, taskId);
|
|
|
+ expect(hit, '未达成任务历史可查').toBeTruthy();
|
|
|
+ expect(hit.status, '未达成也算完成').toBe('completed');
|
|
|
+ expect(hit.achieved, 'achieved=0 落库').toBe(0);
|
|
|
+ expect((await pointsRows(request, p1.token, child1, taskId)).length, '未达成无积分流水').toBe(0);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E3 异常·填写式缺内容] requireInput 无内容 → code=400 需填写内容', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5, requireInput: 1 });
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 }, p1.token);
|
|
|
+ expect(r.code, '缺内容应被拒').toBe(400);
|
|
|
+ expect(String(r.message)).toContain('该任务需填写内容后才能完成');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E4 异常·任务不存在] 完成不存在任务 → code=500 任务不存在(不 200 假成功)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/tasks/999999999/complete', { memberId: child1 }, p1.token);
|
|
|
+ expect(r.code, '不存在任务应报错').toBe(500);
|
|
|
+ expect(String(r.message)).toContain('任务不存在');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E5 异常·未登录] 完成接口无 token → HTTP 401 / code=401', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 });
|
|
|
+ expect(r.status, 'HTTP 状态 401').toBe(401);
|
|
|
+ expect(r.code, '业务码 401').toBe(401);
|
|
|
+ expect(String(r.message)).toContain('请先登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E6 异常·checkin 缺 taskId] → code=500 taskId不能为空', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/growth-task/checkin', { memberId: child1 }, p1.token);
|
|
|
+ expect(r.code, '缺 taskId 应被拒').toBe(500);
|
|
|
+ expect(String(r.message)).toContain('taskId不能为空');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[E7 异常·checkin 非法 token] → 401', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ const r = await apiPost(request, '/api/growth-task/checkin', { memberId: child1, taskId }, 'invalid.token.value');
|
|
|
+ expect(r.status, 'HTTP 状态 401').toBe(401);
|
|
|
+ expect(r.code, '业务码 401').toBe(401);
|
|
|
+ });
|
|
|
+
|
|
|
+ // ================= 权限流 =================
|
|
|
+
|
|
|
+ test('[P1 权限·跨家庭无副作用] 另一家庭 token + 其孩子 memberId 尝试完成 → 不得改变任务状态', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ // 实测:服务端按幂等成功返回 200 alreadyCompleted(未拒绝),但不得产生任何写入/发奖。
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child2 }, p2.token);
|
|
|
+ expect(r.code, '真实契约:幂等包装(拒绝能力见 P2/P3 fixme)').toBe(200);
|
|
|
+ expect(r.data.pointsEarned, '跨家庭尝试不得发奖').toBe(0);
|
|
|
+
|
|
|
+ const today = await apiPost(request, '/api/tasks/today', { memberId: child1 }, p1.token);
|
|
|
+ expect(today.code).toBe(200);
|
|
|
+ const hit = (today.data || []).find((t) => String(t.id) === String(taskId));
|
|
|
+ expect(hit, '任务仍在 P1 孩子今日列表').toBeTruthy();
|
|
|
+ expect(hit.status, '任务状态仍 pending').toBe('pending');
|
|
|
+
|
|
|
+ const hist = await historyTask(request, p1.token, child1, taskId);
|
|
|
+ expect(hist && hist.status, '历史状态仍 pending').toBe('pending');
|
|
|
+ });
|
|
|
+
|
|
|
+ test.fixme('[P2 权限·跨家庭应拒] 另一家庭 token + 其孩子 memberId 完成任务应 403(B-275-3/G-304-1 承袭·真实缺陷未修)', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child2 }, p2.token);
|
|
|
+ // 实测真实契约:200 alreadyCompleted:true(未做家庭归属校验);期望:403。
|
|
|
+ expect(r.code, '跨家庭完成任务应 403(当前实测 200,B-275-3/G-304-1 承袭)').toBe(403);
|
|
|
+ });
|
|
|
+
|
|
|
+ test.fixme('[P3 权限·伪装 memberId 应拒] 另一家庭 token + 受害孩子 memberId 完成任务应 403(G-304-1 承袭·真实缺陷未修)', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ const r = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: child1 }, p2.token);
|
|
|
+ // 实测真实契约:200 且真实发奖(pointsEarned>0,任务被他人完成);期望:403。
|
|
|
+ expect(r.code, '跨家庭伪装 memberId 应 403(当前实测 200 且发奖,G-304-1 承袭)').toBe(403);
|
|
|
+ });
|
|
|
+
|
|
|
+ test.fixme('[P4 权限·匿名 checkin 应拒] 无 token 完成全新任务应 401(鉴权口径缺口·未登记新编号)', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 7 });
|
|
|
+ const r = await apiPost(request, '/api/growth-task/checkin', { memberId: child1, taskId, content: 'anon' });
|
|
|
+ // 实测真实契约:200 且发奖(/api/growth-task/checkin 未入 LOGIN_REQUIRED_PATHS);期望:401。
|
|
|
+ expect(r.code, '匿名 checkin 应 401(当前实测 200 且发奖)').toBe(401);
|
|
|
+ });
|
|
|
+
|
|
|
+ test.fixme('[P5 权限·checkin 跨家庭应拒] 另一家庭 token + 受害孩子 memberId 打卡应 403(B-275-3/G-304-1 承袭·真实缺陷未修)', async ({ request }) => {
|
|
|
+ const taskId = await createTask(request, p1.token, child1, { points: 5 });
|
|
|
+ const r = await apiPost(request, '/api/growth-task/checkin', { memberId: child1, taskId, content: 'x' }, p2.token);
|
|
|
+ // 实测真实契约:200 且真实发奖;期望:403。
|
|
|
+ expect(r.code, '跨家庭 checkin 应 403(当前实测 200 且发奖)').toBe(403);
|
|
|
+ });
|
|
|
+});
|