/** * S-cfc-492 energy.admin.adjust 三类流(正常/异常/权限) * ================================================================ * SC-P-89 手动发放留痕 / SC-P-92 来源比例校验(energy.admin.adjust 能力键) * * 载体端点: * POST /api/energy/grant 手动发放(无 taskId + remark/workOrderNo → sourceType=manual 留痕) * POST /api/energy/logs 查看流水(验证 sourceType=manual 留痕) * POST /api/energy/configure 全局来源比例配置(admin 写端点) * * 探针实证契约(本地 127.0.0.1:9082 + 测试库 192.168.16.251/zxyj,_probe_energy_adjust.spec.js): * grant 正常:{memberId,amount>0,dimensionCode,remark}(无 taskId)→ code=200, * data.sourceType="manual", data.sourceId=null, data.description=remark; * logs 返回 records 含 sourceType="manual" * grant 异常:amount=-10/0 → code=400「能量数量必须为正数」; * dimensionCode=nonexistent → code=400「维度代码无效:nonexistent」; * 缺 amount → code=500「memberId、taskId和amount不能为空」(Result.error 默认 500) * grant 权限:跨家庭 memberId → code=403「无权给其他家庭成员发放能量」(isMemberInOtherFamily); * 匿名 → HTTP 401「请先登录」(JwtInterceptor) * configure 权限:body.role≠admin → code=403「仅管理员可配置全局能量来源比例」; * 匿名 → HTTP 401 * configure 异常:ratios 合计≠100 → code=400「来源比例之和必须为100%,当前为X%」; * 缺 sourceType/sourceId → code=500「sourceType和sourceId不能为空」; * 缺 ratios → code=500「ratios不能为空」 * configure 正常:见下方承袭 blocker 标注 * * ⚠️ 承袭·真实缺陷未修(比例校验正常流): * 测试库 energy_source_config.ratio 列类型 decimal(5,4)(容 0~9.9999),但 service 传整数 100, * 触发 DataIntegrityViolationException → 400「数据不完整或格式错误」。 * 已实测确认(_probe_energy_adjust2):valid config ratio=100 → 400;ratio=1 → 400「合计必须为100%」。 * 现有 5 行(product/source_id=1~5)ratio=1.0000,非 100,说明生产数据也不按百分比整数存储。 * service 期望「合计=100(百分比)」与 DB 列 decimal(5,4) 矛盾 → 合法 configure 写入必失败。 * 按纪律不为缺陷造绿:本 spec 不断言 configure 合法写入=200,登记为承袭 blocker。 * * 运行:CFC_API_BASE=http://127.0.0.1:9082 npx playwright test api/energy-admin-adjust.spec.js \ * -c playwright.config.api.js --project=desktop * 安全约定:不打印 token / 手机号。 */ const { test, expect } = require('@playwright/test'); const { registerRole, apiPost, pickMemberByUserId, uniqueSuffix } = require('../helpers/api'); test.describe('S-cfc-492 energy.admin.adjust 三类流', () => { let parent, selfMemberId, otherMemberId, admin; test.beforeAll(async ({ request }) => { // 家长 + 自身成员 parent = await registerRole(request, 'parent', 'QA-EA' + uniqueSuffix()); const mem = await apiPost(request, '/api/family/member/list', {}, parent.token); expect(mem.code, 'member/list: ' + mem.message).toBe(200); selfMemberId = pickMemberByUserId(mem.data, parent.userId).id; // 跨家庭对照 const other = await registerRole(request, 'parent', 'QA-EA-O' + uniqueSuffix()); const omem = await apiPost(request, '/api/family/member/list', {}, other.token); otherMemberId = pickMemberByUserId(omem.data, other.userId).id; // admin 角色(比例配置) admin = await registerRole(request, 'admin', 'QA-EA-ADM' + uniqueSuffix()); }); // ==================== 正常流(SC-P-89 手动发放留痕) ==================== test('[P-89 正常] 手动发放(无 taskId + remark)→ manual 留痕可查', async ({ request }) => { const remark = 'QA手动发放-三流测试-' + uniqueSuffix(); const r = await apiPost(request, '/api/energy/grant', { memberId: selfMemberId, amount: 10, dimensionCode: 'body', remark, }, parent.token); expect(r.code, 'grant manual: ' + r.message).toBe(200); // EnergyLog.sourceType=manual / sourceId=null(区分任务奖励式 sourceType=task) expect(r.data.sourceType, 'sourceType=manual').toBe('manual'); expect(r.data.sourceId).toBeNull(); expect(r.data.description, 'description=remark').toBe(remark); // logs 可查:sourceType=manual 且 description 含备注 const logs = await apiPost(request, '/api/energy/logs', { memberId: selfMemberId, page: 1, size: 20 }, parent.token); expect(logs.code, 'logs: ' + logs.message).toBe(200); const records = logs.data.records || []; const found = records.find((x) => x.sourceType === 'manual' && x.description === remark); expect(found, 'logs 应含本次 manual 流水').toBeTruthy(); }); // ==================== 异常流(SC-P-89 / SC-P-92) ==================== test('[P-89-E 异常] 负额/零额 → 400「能量数量必须为正数」', async ({ request }) => { const neg = await apiPost(request, '/api/energy/grant', { memberId: selfMemberId, amount: -10, dimensionCode: 'body', remark: 'x' }, parent.token); expect(neg.code, 'negative → ' + neg.message).toBe(400); expect(neg.message).toBe('能量数量必须为正数'); const zero = await apiPost(request, '/api/energy/grant', { memberId: selfMemberId, amount: 0, dimensionCode: 'body', remark: 'x' }, parent.token); expect(zero.code, 'zero → ' + zero.message).toBe(400); expect(zero.message).toBe('能量数量必须为正数'); }); test('[P-89-E 异常] 非法维度 → 400「维度代码无效」;缺 amount → 500 参数校验', async ({ request }) => { const badDim = await apiPost(request, '/api/energy/grant', { memberId: selfMemberId, amount: 5, dimensionCode: 'nonexistent', remark: 'x' }, parent.token); expect(badDim.code, 'bad dim → ' + badDim.message).toBe(400); expect(badDim.message).toContain('维度代码无效'); // 缺 amount 且无 taskId + remark 非空 → 参数校验(Result.error 默认 500) const miss = await apiPost(request, '/api/energy/grant', { memberId: selfMemberId, dimensionCode: 'body', remark: 'x' }, parent.token); expect(miss.code, 'missing amount → ' + miss.message).toBe(500); expect(miss.message).toBe('memberId、taskId和amount不能为空'); }); test('[P-92-E 异常] 来源比例合计≠100 → 400「来源比例之和必须为100%」', async ({ request }) => { const r = await apiPost(request, '/api/energy/configure', { role: 'admin', sourceType: 'task', sourceId: 1, sourceName: 'QA-TEST', ratios: [{ dimensionCode: 'body', ratio: 60 }, { dimensionCode: 'mind', ratio: 60 }], }, admin.token); expect(r.code, 'ratio sum=120 → ' + r.message).toBe(400); expect(r.message).toContain('来源比例之和必须为100%'); expect(r.message).toContain('120%'); }); test('[P-92-E 异常] 缺 sourceType/sourceId → 500;缺 ratios → 500', async ({ request }) => { // Result.error 默认 500(非 badRequest);探针实测一致 const missType = await apiPost(request, '/api/energy/configure', { role: 'admin', sourceId: 1, ratios: [{ dimensionCode: 'body', ratio: 100 }], }, admin.token); expect(missType.code, 'miss sourceType → ' + missType.message).toBe(500); expect(missType.message).toBe('sourceType和sourceId不能为空'); const missRatios = await apiPost(request, '/api/energy/configure', { role: 'admin', sourceType: 'task', sourceId: 1, }, admin.token); expect(missRatios.code, 'miss ratios → ' + missRatios.message).toBe(500); expect(missRatios.message).toBe('ratios不能为空'); }); // ==================== 权限流(SC-P-89-P 跨家庭 / SC-P-92-P 非 admin) ==================== test('[P-89-P 权限] 跨家庭 memberId → 403「无权给其他家庭成员发放能量」', async ({ request }) => { const r = await apiPost(request, '/api/energy/grant', { memberId: otherMemberId, amount: 5, dimensionCode: 'body', remark: 'x' }, parent.token); expect(r.code, 'cross-family grant → ' + r.message).toBe(403); expect(r.message).toBe('无权给其他家庭成员发放能量'); }); test('[P-92-P 权限] body.role≠admin → 403「仅管理员可配置全局能量来源比例」', async ({ request }) => { const r = await apiPost(request, '/api/energy/configure', { role: 'parent', sourceType: 'task', sourceId: 1, ratios: [{ dimensionCode: 'body', ratio: 100 }], }, parent.token); expect(r.code, 'non-admin configure → ' + r.message).toBe(403); expect(r.message).toBe('仅管理员可配置全局能量来源比例'); }); test('[P-89/P-92-P 权限] 匿名访问 grant/configure → HTTP 401「请先登录」', async ({ request }) => { const g = await request.post('/api/energy/grant', { data: { memberId: selfMemberId, amount: 5, dimensionCode: 'body', remark: 'x' }, }); expect(g.status(), 'anon grant → HTTP ' + g.status()).toBe(401); const gBody = await g.json().catch(() => ({})); expect(gBody.message).toBe('请先登录'); const c = await request.post('/api/energy/configure', { data: { role: 'admin', sourceType: 'task', sourceId: 1, ratios: [{ dimensionCode: 'body', ratio: 100 }] }, }); expect(c.status(), 'anon configure → HTTP ' + c.status()).toBe(401); const cBody = await c.json().catch(() => ({})); expect(cBody.message).toBe('请先登录'); }); // ==================== 承袭 blocker(比例校验正常流) ==================== test.fixme('[P-92 正常·承袭·真实缺陷未修] 合法比例 configure 应=200(ratio 列 decimal(5,4) 拦截)', async ({ request }) => { // 探针实测:admin + sourceName 齐全 + ratio=100 → 400「数据不完整或格式错误」 // 根因:energy_source_config.ratio 列 decimal(5,4)(max 9.9999),service 传 100 溢出 // → DataIntegrityViolationException → GlobalExceptionHandler 400 // 现有生产/测试数据 ratio=1.0000(基点非百分比),与「合计=100」service 契约矛盾。 // 不造绿:标记 fixme,待后端统一 service「基点 ↔ 百分比」口径 + schema 迁移后转正式断言。 const r = await apiPost(request, '/api/energy/configure', { role: 'admin', sourceType: 'task', sourceId: 1, sourceName: 'QA', ratios: [{ dimensionCode: 'body', ratio: 100 }], }, admin.token); expect(r.code, 'valid configure → ' + r.message).toBe(200); }); });