energy-admin-adjust.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * S-cfc-492 energy.admin.adjust 三类流(正常/异常/权限)
  3. * ================================================================
  4. * SC-P-89 手动发放留痕 / SC-P-92 来源比例校验(energy.admin.adjust 能力键)
  5. *
  6. * 载体端点:
  7. * POST /api/energy/grant 手动发放(无 taskId + remark/workOrderNo → sourceType=manual 留痕)
  8. * POST /api/energy/logs 查看流水(验证 sourceType=manual 留痕)
  9. * POST /api/energy/configure 全局来源比例配置(admin 写端点)
  10. *
  11. * 探针实证契约(本地 127.0.0.1:9082 + 测试库 192.168.16.251/zxyj,_probe_energy_adjust.spec.js):
  12. * grant 正常:{memberId,amount>0,dimensionCode,remark}(无 taskId)→ code=200,
  13. * data.sourceType="manual", data.sourceId=null, data.description=remark;
  14. * logs 返回 records 含 sourceType="manual"
  15. * grant 异常:amount=-10/0 → code=400「能量数量必须为正数」;
  16. * dimensionCode=nonexistent → code=400「维度代码无效:nonexistent」;
  17. * 缺 amount → code=500「memberId、taskId和amount不能为空」(Result.error 默认 500)
  18. * grant 权限:跨家庭 memberId → code=403「无权给其他家庭成员发放能量」(isMemberInOtherFamily);
  19. * 匿名 → HTTP 401「请先登录」(JwtInterceptor)
  20. * configure 权限:body.role≠admin → code=403「仅管理员可配置全局能量来源比例」;
  21. * 匿名 → HTTP 401
  22. * configure 异常:ratios 合计≠100 → code=400「来源比例之和必须为100%,当前为X%」;
  23. * 缺 sourceType/sourceId → code=500「sourceType和sourceId不能为空」;
  24. * 缺 ratios → code=500「ratios不能为空」
  25. * configure 正常:见下方承袭 blocker 标注
  26. *
  27. * ⚠️ 承袭·真实缺陷未修(比例校验正常流):
  28. * 测试库 energy_source_config.ratio 列类型 decimal(5,4)(容 0~9.9999),但 service 传整数 100,
  29. * 触发 DataIntegrityViolationException → 400「数据不完整或格式错误」。
  30. * 已实测确认(_probe_energy_adjust2):valid config ratio=100 → 400;ratio=1 → 400「合计必须为100%」。
  31. * 现有 5 行(product/source_id=1~5)ratio=1.0000,非 100,说明生产数据也不按百分比整数存储。
  32. * service 期望「合计=100(百分比)」与 DB 列 decimal(5,4) 矛盾 → 合法 configure 写入必失败。
  33. * 按纪律不为缺陷造绿:本 spec 不断言 configure 合法写入=200,登记为承袭 blocker。
  34. *
  35. * 运行:CFC_API_BASE=http://127.0.0.1:9082 npx playwright test api/energy-admin-adjust.spec.js \
  36. * -c playwright.config.api.js --project=desktop
  37. * 安全约定:不打印 token / 手机号。
  38. */
  39. const { test, expect } = require('@playwright/test');
  40. const { registerRole, apiPost, pickMemberByUserId, uniqueSuffix } = require('../helpers/api');
  41. test.describe('S-cfc-492 energy.admin.adjust 三类流', () => {
  42. let parent, selfMemberId, otherMemberId, admin;
  43. test.beforeAll(async ({ request }) => {
  44. // 家长 + 自身成员
  45. parent = await registerRole(request, 'parent', 'QA-EA' + uniqueSuffix());
  46. const mem = await apiPost(request, '/api/family/member/list', {}, parent.token);
  47. expect(mem.code, 'member/list: ' + mem.message).toBe(200);
  48. selfMemberId = pickMemberByUserId(mem.data, parent.userId).id;
  49. // 跨家庭对照
  50. const other = await registerRole(request, 'parent', 'QA-EA-O' + uniqueSuffix());
  51. const omem = await apiPost(request, '/api/family/member/list', {}, other.token);
  52. otherMemberId = pickMemberByUserId(omem.data, other.userId).id;
  53. // admin 角色(比例配置)
  54. admin = await registerRole(request, 'admin', 'QA-EA-ADM' + uniqueSuffix());
  55. });
  56. // ==================== 正常流(SC-P-89 手动发放留痕) ====================
  57. test('[P-89 正常] 手动发放(无 taskId + remark)→ manual 留痕可查', async ({ request }) => {
  58. const remark = 'QA手动发放-三流测试-' + uniqueSuffix();
  59. const r = await apiPost(request, '/api/energy/grant', {
  60. memberId: selfMemberId, amount: 10, dimensionCode: 'body', remark,
  61. }, parent.token);
  62. expect(r.code, 'grant manual: ' + r.message).toBe(200);
  63. // EnergyLog.sourceType=manual / sourceId=null(区分任务奖励式 sourceType=task)
  64. expect(r.data.sourceType, 'sourceType=manual').toBe('manual');
  65. expect(r.data.sourceId).toBeNull();
  66. expect(r.data.description, 'description=remark').toBe(remark);
  67. // logs 可查:sourceType=manual 且 description 含备注
  68. const logs = await apiPost(request, '/api/energy/logs',
  69. { memberId: selfMemberId, page: 1, size: 20 }, parent.token);
  70. expect(logs.code, 'logs: ' + logs.message).toBe(200);
  71. const records = logs.data.records || [];
  72. const found = records.find((x) => x.sourceType === 'manual' && x.description === remark);
  73. expect(found, 'logs 应含本次 manual 流水').toBeTruthy();
  74. });
  75. // ==================== 异常流(SC-P-89 / SC-P-92) ====================
  76. test('[P-89-E 异常] 负额/零额 → 400「能量数量必须为正数」', async ({ request }) => {
  77. const neg = await apiPost(request, '/api/energy/grant',
  78. { memberId: selfMemberId, amount: -10, dimensionCode: 'body', remark: 'x' }, parent.token);
  79. expect(neg.code, 'negative → ' + neg.message).toBe(400);
  80. expect(neg.message).toBe('能量数量必须为正数');
  81. const zero = await apiPost(request, '/api/energy/grant',
  82. { memberId: selfMemberId, amount: 0, dimensionCode: 'body', remark: 'x' }, parent.token);
  83. expect(zero.code, 'zero → ' + zero.message).toBe(400);
  84. expect(zero.message).toBe('能量数量必须为正数');
  85. });
  86. test('[P-89-E 异常] 非法维度 → 400「维度代码无效」;缺 amount → 500 参数校验', async ({ request }) => {
  87. const badDim = await apiPost(request, '/api/energy/grant',
  88. { memberId: selfMemberId, amount: 5, dimensionCode: 'nonexistent', remark: 'x' }, parent.token);
  89. expect(badDim.code, 'bad dim → ' + badDim.message).toBe(400);
  90. expect(badDim.message).toContain('维度代码无效');
  91. // 缺 amount 且无 taskId + remark 非空 → 参数校验(Result.error 默认 500)
  92. const miss = await apiPost(request, '/api/energy/grant',
  93. { memberId: selfMemberId, dimensionCode: 'body', remark: 'x' }, parent.token);
  94. expect(miss.code, 'missing amount → ' + miss.message).toBe(500);
  95. expect(miss.message).toBe('memberId、taskId和amount不能为空');
  96. });
  97. test('[P-92-E 异常] 来源比例合计≠100 → 400「来源比例之和必须为100%」', async ({ request }) => {
  98. const r = await apiPost(request, '/api/energy/configure', {
  99. role: 'admin', sourceType: 'task', sourceId: 1, sourceName: 'QA-TEST',
  100. ratios: [{ dimensionCode: 'body', ratio: 60 }, { dimensionCode: 'mind', ratio: 60 }],
  101. }, admin.token);
  102. expect(r.code, 'ratio sum=120 → ' + r.message).toBe(400);
  103. expect(r.message).toContain('来源比例之和必须为100%');
  104. expect(r.message).toContain('120%');
  105. });
  106. test('[P-92-E 异常] 缺 sourceType/sourceId → 500;缺 ratios → 500', async ({ request }) => {
  107. // Result.error 默认 500(非 badRequest);探针实测一致
  108. const missType = await apiPost(request, '/api/energy/configure', {
  109. role: 'admin', sourceId: 1, ratios: [{ dimensionCode: 'body', ratio: 100 }],
  110. }, admin.token);
  111. expect(missType.code, 'miss sourceType → ' + missType.message).toBe(500);
  112. expect(missType.message).toBe('sourceType和sourceId不能为空');
  113. const missRatios = await apiPost(request, '/api/energy/configure', {
  114. role: 'admin', sourceType: 'task', sourceId: 1,
  115. }, admin.token);
  116. expect(missRatios.code, 'miss ratios → ' + missRatios.message).toBe(500);
  117. expect(missRatios.message).toBe('ratios不能为空');
  118. });
  119. // ==================== 权限流(SC-P-89-P 跨家庭 / SC-P-92-P 非 admin) ====================
  120. test('[P-89-P 权限] 跨家庭 memberId → 403「无权给其他家庭成员发放能量」', async ({ request }) => {
  121. const r = await apiPost(request, '/api/energy/grant',
  122. { memberId: otherMemberId, amount: 5, dimensionCode: 'body', remark: 'x' }, parent.token);
  123. expect(r.code, 'cross-family grant → ' + r.message).toBe(403);
  124. expect(r.message).toBe('无权给其他家庭成员发放能量');
  125. });
  126. test('[P-92-P 权限] body.role≠admin → 403「仅管理员可配置全局能量来源比例」', async ({ request }) => {
  127. const r = await apiPost(request, '/api/energy/configure', {
  128. role: 'parent', sourceType: 'task', sourceId: 1,
  129. ratios: [{ dimensionCode: 'body', ratio: 100 }],
  130. }, parent.token);
  131. expect(r.code, 'non-admin configure → ' + r.message).toBe(403);
  132. expect(r.message).toBe('仅管理员可配置全局能量来源比例');
  133. });
  134. test('[P-89/P-92-P 权限] 匿名访问 grant/configure → HTTP 401「请先登录」', async ({ request }) => {
  135. const g = await request.post('/api/energy/grant', {
  136. data: { memberId: selfMemberId, amount: 5, dimensionCode: 'body', remark: 'x' },
  137. });
  138. expect(g.status(), 'anon grant → HTTP ' + g.status()).toBe(401);
  139. const gBody = await g.json().catch(() => ({}));
  140. expect(gBody.message).toBe('请先登录');
  141. const c = await request.post('/api/energy/configure', {
  142. data: { role: 'admin', sourceType: 'task', sourceId: 1,
  143. ratios: [{ dimensionCode: 'body', ratio: 100 }] },
  144. });
  145. expect(c.status(), 'anon configure → HTTP ' + c.status()).toBe(401);
  146. const cBody = await c.json().catch(() => ({}));
  147. expect(cBody.message).toBe('请先登录');
  148. });
  149. // ==================== 承袭 blocker(比例校验正常流) ====================
  150. test.fixme('[P-92 正常·承袭·真实缺陷未修] 合法比例 configure 应=200(ratio 列 decimal(5,4) 拦截)', async ({ request }) => {
  151. // 探针实测:admin + sourceName 齐全 + ratio=100 → 400「数据不完整或格式错误」
  152. // 根因:energy_source_config.ratio 列 decimal(5,4)(max 9.9999),service 传 100 溢出
  153. // → DataIntegrityViolationException → GlobalExceptionHandler 400
  154. // 现有生产/测试数据 ratio=1.0000(基点非百分比),与「合计=100」service 契约矛盾。
  155. // 不造绿:标记 fixme,待后端统一 service「基点 ↔ 百分比」口径 + schema 迁移后转正式断言。
  156. const r = await apiPost(request, '/api/energy/configure', {
  157. role: 'admin', sourceType: 'task', sourceId: 1, sourceName: 'QA',
  158. ratios: [{ dimensionCode: 'body', ratio: 100 }],
  159. }, admin.token);
  160. expect(r.code, 'valid configure → ' + r.message).toBe(200);
  161. });
  162. });