/** * SC-P-13/P-14/C-04 心愿域三类流(wish.parent.closedLoop / wish.parent.reject / wish.child.create) * ================================================================ * 正常流: * - C-04:孩子创建 draft → 家长定价 active → 进入待审批; * - P-13:parent 直接创建 active → child 申请兑换 → parent 批准(扣 CF)→ 标记购买 → 孩子确认收货 → fulfilled; * - P-14:parent 驳回 draft → rejected,孩子可重新提。 * 异常流: * - C-04-E:余额不足/超长/注入创建被拒; * - P-13-E:余额不足 → 拦截;并发双发 → 仅一次成功;已兑换再申请 → 拒绝; * - P-14-E:已兑换/active 状态不可驳回;cancel 失效。 * 权限流(TC-P-07-P): * - 他人家庭跨家庭操作均 500 拒绝; * - 非归属成员申请兑换 → 500 拒绝; * - 越权查他人心愿 → 403。 * 运行: * 远端:npx playwright test api/wish-flow-sc-p13-p14-c04.spec.js -c playwright.config.api.js --project=desktop * 本地:CFC_BASE_URL=http://127.0.0.1:18082 npx playwright test api/wish-flow-sc-p13-p14-c04.spec.js -c playwright.config.api.local.js --project=desktop */ const { test, expect } = require('@playwright/test'); const { registerRole, apiPost, uniqueSuffix, pickMemberByUserId } = require('../helpers/api'); // 辅助:家长给孩子创建需审核为0的任务并完成,以发放 CF(心愿兑换走 CF 账本) async function grantCfViaTask(request, parent, childId) { const created = await apiPost(request, '/api/tasks/create', { title: 'CF预置任务' + uniqueSuffix(), points: 5, memberId: childId, frequency: 'none', needReview: 0, }, parent.token); expect(created.code, '预置任务创建: ' + created.message).toBe(200); const taskId = created.data; const completed = await apiPost(request, `/api/tasks/${taskId}/complete`, { memberId: childId }, parent.token); expect(completed.code, '预置任务完成: ' + completed.message).toBe(200); expect(completed.data.pointsEarned, '应获得正积分').toBeGreaterThan(0); } test.describe('SC-P-13/P-14/C-04 心愿域三类流(TC-P-07)', () => { let parent; let childA; let outsider; test.beforeAll(async ({ request }) => { parent = await registerRole(request, 'parent', 'QA-WISH'); const memList = await apiPost(request, '/api/family/member/list', {}, parent.token); expect(memList.code).toBe(200); const self = pickMemberByUserId(memList.data, parent.userId); expect(self).toBeTruthy(); const addA = await apiPost(request, '/api/family/member/add', { relativeMemberId: self.id, generationLevel: 'child', nickname: 'A-Wish' + uniqueSuffix(), gender: 'female', birthday: '2016-03-01', }, parent.token); expect(addA.code, 'addA: ' + addA.message).toBe(200); childA = addA.data.id; outsider = await registerRole(request, 'parent', 'QA-WISH-X'); }); // ===== SC-C-04 提心愿(正常流 TC-C-04-N)===== test('[C-04 正常] 孩子创建 draft 心愿 → 家长定价 → active 待审批', async ({ request }) => { const title = '孩子提的心愿' + uniqueSuffix(); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA }, parent.token); expect(create.code, 'create: ' + create.message).toBe(200); const wishId = create.data; expect(wishId).toBeTruthy(); // 家长定价(draft → active) const priced = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 100 }, parent.token); expect(priced.code, 'set-price: ' + priced.message).toBe(200); expect(priced.data).toBe(true); // 待审批列表可见 const pend = await apiPost(request, '/api/wishes/pending', {}, parent.token); expect(pend.code, pend.message).toBe(200); const found = (pend.data || []).find((w) => String(w.id) === String(wishId)); expect(found).toBeTruthy(); expect(found.status).toBe('active'); expect(found.pointsRequired).toBe(100); }); // ===== SC-P-13 心愿闭环(正常流 TC-P-07-N)===== test('[P-13 正常] 家长创建 active 心愿 → 孩子申请兑换 → 家长批准扣 CF → fulfilled', async ({ request }) => { const title = 'P13心愿乐高' + uniqueSuffix(); const price = 50; // 预置 CF await grantCfViaTask(request, parent, childA); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA, pointsRequired: price, category: '玩具', }, parent.token); expect(create.code, 'create: ' + create.message).toBe(200); const wishId = create.data; expect(wishId).toBeTruthy(); // 申请兑换(active → pending_exchange) const req = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token); expect(req.code, 'requestExchange: ' + req.message).toBe(200); expect(req.data).toBe(true); // 家长批准(pending_exchange → approved,扣 CF) const approve = await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, { memberId: childA, approved: true, }, parent.token); expect(approve.code, 'approve: ' + approve.message).toBe(200); expect(approve.data).toBe(true); // 家长标记购买 → 孩子确认收货 const fulfill = await apiPost(request, `/api/wishes/${wishId}/fulfill`, {}, parent.token); expect(fulfill.code, 'fulfill: ' + fulfill.message).toBe(200); const confirm = await apiPost(request, `/api/wishes/${wishId}/confirm`, {}, parent.token); expect(confirm.code, 'confirm: ' + confirm.message).toBe(200); // 心愿最终状态 fulfilled const detail = await apiPost(request, `/api/wishes/${wishId}`, {}, parent.token); expect(detail.code, detail.message).toBe(200); expect(detail.data.status).toBe('fulfilled'); }); // ===== SC-P-14 心愿驳回/失效(正常流 TC-P-14-N)===== test('[P-14 正常] 驳回 draft → rejected;取消 active → cancelled;孩子可重新提', async ({ request }) => { // 1) 驳回 draft const create1 = await apiPost(request, '/api/wishes/create', { title: '驳回心愿' + uniqueSuffix(), memberId: childA, }, parent.token); expect(create1.code).toBe(200); const w1 = create1.data; const reject = await apiPost(request, `/api/wishes/${w1}/reject`, { reason: '价格不合理' }, parent.token); expect(reject.code).toBe(200); expect(reject.data).toBe(true); const d1 = await apiPost(request, `/api/wishes/${w1}`, {}, parent.token); expect(d1.data.status).toBe('rejected'); // 2) 取消 active const create2 = await apiPost(request, '/api/wishes/create', { title: '取消心愿' + uniqueSuffix(), memberId: childA, pointsRequired: 20, }, parent.token); expect(create2.code).toBe(200); const w2 = create2.data; const cancel = await apiPost(request, `/api/wishes/${w2}/cancel`, { reason: '孩子不想要了' }, parent.token); expect(cancel.code).toBe(200); expect(cancel.data).toBe(true); const d2 = await apiPost(request, `/api/wishes/${w2}`, {}, parent.token); expect(d2.data.status).toBe('cancelled'); // 3) 孩子可重新提(同名可用新名称) const reCreate = await apiPost(request, '/api/wishes/create', { title: '重新提的心愿' + uniqueSuffix(), memberId: childA, }, parent.token); expect(reCreate.code).toBe(200); expect(reCreate.data).toBeTruthy(); }); // ===== 异常流 ===== test('[P-13-E] 余额不足申请兑换 → 业务拒绝(非 200)', async ({ request }) => { const title = '高价心愿' + uniqueSuffix(); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA, pointsRequired: 99999, }, parent.token); expect(create.code).toBe(200); const wishId = create.data; const req = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token); // 业务错误:code ≠ 200(远端 Result.error → 500) expect(req.code).not.toBe(200); }); test('[P-13-E] 并发双发同一心愿兑换申请 → 仅一次成功(幂等)', async ({ request }) => { const title = '并发心愿' + uniqueSuffix(); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA, pointsRequired: 5, }, parent.token); expect(create.code).toBe(200); const wishId = create.data; const [a, b] = await Promise.all([ apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token), apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token), ]); const success = [a, b].filter((r) => r.code === 200 && r.data === true); expect(success.length, '并发兑换请求应有 1 个成功').toBe(1); }); test('[P-13-E] 已兑换心愿再次申请 → 拒绝', async ({ request }) => { const title = '再兑心愿' + uniqueSuffix(); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA, pointsRequired: 5, }, parent.token); expect(create.code).toBe(200); const wishId = create.data; await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token); await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, { memberId: childA, approved: true, }, parent.token); const again = await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token); expect(again.code).not.toBe(200); }); test('[P-14-E] 已兑换心愿不可驳回', async ({ request }) => { const title = '驳回已兑' + uniqueSuffix(); const create = await apiPost(request, '/api/wishes/create', { title, memberId: childA, pointsRequired: 5, }, parent.token); expect(create.code).toBe(200); const wishId = create.data; await apiPost(request, `/api/wishes/${wishId}/exchange`, { memberId: childA }, parent.token); await apiPost(request, `/api/wishes/${wishId}/approve-exchange`, { memberId: childA, approved: true, }, parent.token); const rejected = await apiPost(request, `/api/wishes/${wishId}/reject`, { reason: '误驳回' }, parent.token); expect(rejected.code).not.toBe(200); }); // ===== 权限流(TC-P-07-P)===== test('[P-13-P] 他人家庭家长为孩子创建心愿 → 拒绝', async ({ request }) => { const r = await apiPost(request, '/api/wishes/create', { title: 'X越权' + uniqueSuffix(), memberId: childA, pointsRequired: 10, }, outsider.token); expect(r.code).not.toBe(200); }); test('[P-13-P] 他人家庭家长审批我的心愿 → 拒绝', async ({ request }) => { const create = await apiPost(request, '/api/wishes/create', { title: '我心愿' + uniqueSuffix(), memberId: childA, pointsRequired: 10, }, parent.token); expect(create.code).toBe(200); const wishId = create.data; const setPrice = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 10 }, parent.token); expect(setPrice.code).toBe(200); // 他人定价 const xPrice = await apiPost(request, `/api/wishes/${wishId}/set-price`, { pointsRequired: 10 }, outsider.token); expect(xPrice.code).not.toBe(200); }); test('[P-13-P] 越权家长代他人家庭孩子申请兑换 → 拒绝', async ({ request }) => { // outsider 自己的心愿 const memX = await apiPost(request, '/api/family/member/list', {}, outsider.token); const selfX = pickMemberByUserId(memX.data, outsider.userId); const addY = await apiPost(request, '/api/family/member/add', { relativeMemberId: selfX.id, generationLevel: 'child', nickname: 'Y-Out' + uniqueSuffix(), gender: 'male', }, outsider.token); expect(addY.code).toBe(200); const yId = addY.data.id; const create = await apiPost(request, '/api/wishes/create', { title: 'Y-Own' + uniqueSuffix(), memberId: yId, pointsRequired: 2, }, outsider.token); expect(create.code).toBe(200); const yWishId = create.data; // parent 越权申请 const ex = await apiPost(request, `/api/wishes/${yWishId}/exchange`, { memberId: yId }, parent.token); expect(ex.code).not.toBe(200); }); test('[P-13-P] 越权查看他人家庭心愿 → 403', async ({ request }) => { const memX = await apiPost(request, '/api/family/member/list', {}, outsider.token); const selfX = pickMemberByUserId(memX.data, outsider.userId); const addY = await apiPost(request, '/api/family/member/add', { relativeMemberId: selfX.id, generationLevel: 'child', nickname: 'Y-View' + uniqueSuffix(), gender: 'male', }, outsider.token); expect(addY.code).toBe(200); const yId = addY.data.id; const create = await apiPost(request, '/api/wishes/create', { title: 'Y-Wish' + uniqueSuffix(), memberId: yId, pointsRequired: 2, }, outsider.token); expect(create.code).toBe(200); const yWishId = create.data; // parent 试图查询 const list = await apiPost(request, '/api/wishes/list', { memberId: yId }, parent.token); expect(list.code).not.toBe(200); // 直接详情查看 const detail = await apiPost(request, `/api/wishes/${yWishId}`, {}, parent.token); // 业务层可能返回 500 或 403;契约:非归属数据访问被拒 expect(detail.code).not.toBe(200); }); });