|
|
@@ -0,0 +1,201 @@
|
|
|
+/**
|
|
|
+ * S-cfc-507 food.rule.pairingMatrix 三类流 e2e 契约用例
|
|
|
+ * ================================================================
|
|
|
+ * 接口(FoodRuleManagementController L68-L98,权限在 Controller 层守卫):
|
|
|
+ * POST /api/admin/food-rule/pairing/list 列表(可选 body.filter.foodId 按 A/B 两侧搜)
|
|
|
+ * POST /api/admin/food-rule/pairing/save 新增/更新一条配伍规则
|
|
|
+ * POST /api/admin/food-rule/pairing/delete 删除一条规则(body.id)
|
|
|
+ *
|
|
|
+ * 探针取证(_probe_pairing_matrix.spec.js,gitignore 不入库):
|
|
|
+ * LIST_EMPTY 200 data=[] (InMemoryService:每次 JVM 启动初始为空数组;
|
|
|
+ * 远程多轮 Probe 累加后 count≠0,断言改为"返回数组即可")
|
|
|
+ * SAVE_VALID 200 data={id:auto-increment,...} msg="success"
|
|
|
+ * LIST_AFTER_SAVE 200 count↑1 回显刚保存的项
|
|
|
+ * DELETE_OK 200 data=true;再 list count↓1
|
|
|
+ * BAD_RELATION → code=200 "success" (Service 层未做白名单校验——B-507-1)
|
|
|
+ * BAD_SOURCE → code=200 "success" (同上——B-507-1)
|
|
|
+ * OUT_OF_RANGE_EFFECT → code=200 effectScore=999 (clamp 仅在 FoodPairingMatrixService 内部生效、
|
|
|
+ * Controller 直接透传 Service 原值——B-507-2)
|
|
|
+ * MISSING_FOOD_IDS → code=200 "success" (无必填校验——B-507-3)
|
|
|
+ * ANON_LIST → http=401 code=401 "请先登录"
|
|
|
+ * PARENT_SAVE → code=403 "无权限"
|
|
|
+ *
|
|
|
+ * ── 真实缺陷·承袭登记(不为缺陷造绿)──
|
|
|
+ * [B-507-1] relationType/source 未在 Controller/Service 入口做白名单校验,任意字符串均落库。
|
|
|
+ * 本用例断言「服务端当前接受非法值」的真实契约(非期望契约),判红登记不造绿。
|
|
|
+ * [B-507-2] effectScore ∈ (-∞,+∞),Controller 未调 clampEffectScore(),
|
|
|
+ * 超出 [-50,+50] 的值原样写入——违反 FoodPairingMatrixService.EFFECT_SCORE_MIN/MAX 契约。
|
|
|
+ * [B-507-3] foodAId/foodBId 缺省时 Controller 仍存一条 null-A/null-B 规则,
|
|
|
+ * 后续 resolvePairingEffect(null,null,...) 短路返回 0.0,属无效数据污染。
|
|
|
+ *
|
|
|
+ * 【硬边界】仅写测试文件;禁 `?.`;探针文件保持 gitignore 不入库。
|
|
|
+ * 运行:cd tests && npx playwright test api/s-cfc-507-pairing-matrix.spec.js \
|
|
|
+ * -c playwright.config.api.js --project=desktop
|
|
|
+ */
|
|
|
+const { test, expect } = require('@playwright/test');
|
|
|
+const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
|
|
|
+
|
|
|
+// 最小合法配伍规则载荷(evidenceRef 带唯一后缀避免与存量条目混淆)
|
|
|
+function makeValidPairing(ref) {
|
|
|
+ return {
|
|
|
+ foodAId: 1,
|
|
|
+ foodBId: 2,
|
|
|
+ relationType: 'synergy',
|
|
|
+ intensity: 3,
|
|
|
+ effectScore: 10.5,
|
|
|
+ source: 'modern_nutrition',
|
|
|
+ evidenceRef: ref || ('QA-M507-' + uniqueSuffix()),
|
|
|
+ enabled: 1,
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+test.describe('S-cfc-507 food.rule.pairingMatrix 三类流', () => {
|
|
|
+ let admin, parent;
|
|
|
+
|
|
|
+ test.beforeAll(async ({ request }) => {
|
|
|
+ admin = await registerRole(request, 'admin', 'QA-M507-A' + uniqueSuffix());
|
|
|
+ parent = await registerRole(request, 'parent', 'QA-M507-P' + uniqueSuffix());
|
|
|
+ });
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+ // 正常流(TC-S507-N)
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+
|
|
|
+ test('[S507-N 正常] list 返回 HTTP 200 + 数组类型', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/list', {}, admin.token);
|
|
|
+ expect(r.code, 'list 正常应 200, 实际=' + r.code).toBe(200);
|
|
|
+ expect(Array.isArray(r.data), 'data 应为数组, 实际 type=' + typeof r.data).toBe(true);
|
|
|
+ // 远程 InMemory 状态跨 Probe 累积,count≥0 均可接受
|
|
|
+ expect(r.data.length >= 0, 'data.length 应为非负整数').toBe(true);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-N 正常] save(valid) 返回新规则对象且含自增 id', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save', makeValidPairing('save-id-test'), admin.token);
|
|
|
+ expect(r.code, 'save 正常应 200, 实际=' + r.code).toBe(200);
|
|
|
+ expect(typeof r.data, 'data 应为 object').toBe('object');
|
|
|
+ expect(r.data.id, 'id 应被自动分配').not.toBeUndefined();
|
|
|
+ expect(r.data.relationType, 'relationType 应回显调用方传入值').toBe('synergy');
|
|
|
+ expect(r.data.source, 'source 应回显调用方传入值').toBe('modern_nutrition');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-N 正常] save 后 list 条目数递增,并按有效 foodId 过滤可命中', async ({ request }) => {
|
|
|
+ const before = await apiPost(request, '/api/admin/food-rule/pairing/list', {}, admin.token);
|
|
|
+ expect(before.code).toBe(200);
|
|
|
+ const saved = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ makeValidPairing('filter-count-test'), admin.token);
|
|
|
+ expect(saved.code).toBe(200);
|
|
|
+ const after = await apiPost(request, '/api/admin/food-rule/pairing/list', {}, admin.token);
|
|
|
+ expect(after.code).toBe(200);
|
|
|
+ expect(after.data.length, 'save 后 list 条目数应比 save 前多 1').toBe(before.data.length + 1);
|
|
|
+ // 按 foodAId 过滤(匹配到刚存的条目)
|
|
|
+ const fMatch = await apiPost(request, '/api/admin/food-rule/pairing/list',
|
|
|
+ { foodId: 1 }, admin.token);
|
|
|
+ expect(fMatch.code).toBe(200);
|
|
|
+ expect(fMatch.data.length > 0, 'foodId=1 应至少命中刚保存的条目').toBe(true);
|
|
|
+ // 清理:删掉刚才那条
|
|
|
+ await apiPost(request, '/api/admin/food-rule/pairing/delete', { id: saved.data.id }, admin.token);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-N 正常] delete(id) 后该条目从 list 消失', async ({ request }) => {
|
|
|
+ const s = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ makeValidPairing('del-test'), admin.token);
|
|
|
+ expect(s.code).toBe(200);
|
|
|
+ const id = s.data.id;
|
|
|
+ const d = await apiPost(request, '/api/admin/food-rule/pairing/delete', { id: id }, admin.token);
|
|
|
+ expect(d.code, 'delete 正常应 200, 实际=' + d.code).toBe(200);
|
|
|
+ expect(d.data, 'delete data 应 true').toBe(true);
|
|
|
+ const after = await apiPost(request, '/api/admin/food-rule/pairing/list', {}, admin.token);
|
|
|
+ const found = after.data.find(function(item) { return item && item.id === id; });
|
|
|
+ expect(found, 'deleted id 不应再出现在 list 中').toBeUndefined();
|
|
|
+ });
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+ // 异常流(TC-S507-E)
|
|
|
+ // 注:下列断言针对服务端当前真实行为(Probe 实测),而非理想契约;
|
|
|
+ // B-507-{1,2,3} 缺陷未修时断言将如实判红并标注「承袭·真实缺陷未修」。
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+
|
|
|
+ test('[S507-E 异常] 非法 relationType 服务端当前接受(B-507-1 缺陷)→ 真实契约断言', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ Object.assign(makeValidPairing('bad-rel'), { relationType: 'evil' }), admin.token);
|
|
|
+ console.log(
|
|
|
+ '[DEFECT-PROBE] relationType=evil → code=' + r.code + ' msg=' + r.message
|
|
|
+ + ';预期 400「非法配伍关系类型」,实返 200 —— B-507-1 缺陷(Service 层 whitelist 未校验)。'
|
|
|
+ );
|
|
|
+ expect(r.code,
|
|
|
+ '【承袭·真实缺陷未修|B-507-1】relationType 白名单未校验,服务端应 400 拒绝非法值。'
|
|
|
+ + 'FoodPairingMatrixService.supportedRelationTypes() 未被 Controller 调用。'
|
|
|
+ ).not.toBe(200);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-E 异常] 非法 source 服务端当前接受(B-507-1 缺陷)→ 真实契约断言', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ Object.assign(makeValidPairing('bad-src'), { source: 'foo' }), admin.token);
|
|
|
+ console.log('[DEFECT-PROBE] source=foo → code=' + r.code + ' msg=' + r.message);
|
|
|
+ expect(r.code,
|
|
|
+ '【承袭·真实缺陷未修|B-507-1】source 白名单未校验,服务端应 400 拒绝非法值。'
|
|
|
+ + 'FoodPairingMatrixService.SOURCES 未被 Controller 调用。'
|
|
|
+ ).not.toBe(200);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-E 异常] effectScore=999 超出 [-50,+50] 时服务端未做 clamp(B-507-2 缺陷)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ Object.assign(makeValidPairing('bad-score'), { effectScore: 999 }), admin.token);
|
|
|
+ console.log('[DEFECT-PROBE] effectScore=999 → code=' + r.code + ' data.effectScore='
|
|
|
+ + (r.data && r.data.effectScore));
|
|
|
+ expect(r.data && r.data.effectScore,
|
|
|
+ '【承袭·真实缺陷未修|B-507-2】effectScore 应被 clamp 到 50,实返 999。'
|
|
|
+ + 'Controller 未调用 FoodPairingMatrixService.clampEffectScore()。'
|
|
|
+ ).not.toBe(999);
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-E 异常] 缺 foodAId/foodBId 时 Controller 应拒绝(B-507-3 缺陷)→ 真实契约断言', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save',
|
|
|
+ { relationType: 'synergy' }, admin.token);
|
|
|
+ console.log('[DEFECT-PROBE] missing food IDs → code=' + r.code + ' msg=' + r.message);
|
|
|
+ expect(r.code,
|
|
|
+ '【承袭·真实缺陷未修|B-507-3】缺少 foodAId/foodBId 应 400 拒绝,实返 200 静默接受。'
|
|
|
+ + 'Controller 未校验必填字段。'
|
|
|
+ ).not.toBe(200);
|
|
|
+ });
|
|
|
+
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+ // 权限流(TC-S507-P)
|
|
|
+ // -------------------------------------------------------------------------
|
|
|
+
|
|
|
+ test('[S507-P 权限] 匿名 list → 401「请先登录」(TC-AU-U23 已在 auth-guard-admin-food-rule 覆盖)', async ({ request }) => {
|
|
|
+ const r = await request.post('/api/admin/food-rule/pairing/list', {
|
|
|
+ data: {}, timeout: 30000,
|
|
|
+ });
|
|
|
+ const body = await r.json().catch(function() { return {}; });
|
|
|
+ expect(r.status(), 'anon list → 401, 实际 http=' + r.status()).toBe(401);
|
|
|
+ expect(body.code, 'anon list body.code=401').toBe(401);
|
|
|
+ expect(String(body.message || ''), 'anon message 含「登录」').toContain('登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-P 权限] 匿名 save → 401「请先登录」(TC-AU-U24)', async ({ request }) => {
|
|
|
+ const r = await request.post('/api/admin/food-rule/pairing/save', {
|
|
|
+ data: makeValidPairing('anon-save'), timeout: 30000,
|
|
|
+ });
|
|
|
+ const body = await r.json().catch(function() { return {}; });
|
|
|
+ expect(r.status(), 'anon save → 401, 实际 http=' + r.status()).toBe(401);
|
|
|
+ expect(body.code, 'anon save body.code=401').toBe(401);
|
|
|
+ expect(String(body.message || ''), 'anon message 含「登录」').toContain('登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-P 权限] 匿名 delete → 401「请先登录」(TC-AU-U25)', async ({ request }) => {
|
|
|
+ const r = await request.post('/api/admin/food-rule/pairing/delete', {
|
|
|
+ data: { id: 1 }, timeout: 30000,
|
|
|
+ });
|
|
|
+ const body = await r.json().catch(function() { return {}; });
|
|
|
+ expect(r.status(), 'anon delete → 401, 实际 http=' + r.status()).toBe(401);
|
|
|
+ expect(body.code, 'anon delete body.code=401').toBe(401);
|
|
|
+ expect(String(body.message || ''), 'anon message 含「登录」').toContain('登录');
|
|
|
+ });
|
|
|
+
|
|
|
+ test('[S507-P 权限] parent token save → 403「无权限」(role != admin 被 Controller 守卫拦截)', async ({ request }) => {
|
|
|
+ const r = await apiPost(request, '/api/admin/food-rule/pairing/save', makeValidPairing('perm-parent'), parent.token);
|
|
|
+ expect(r.code, 'parent 角色 save → 403, 实际=' + r.code).toBe(403);
|
|
|
+ expect(r.message, 'message 应提示无权限').toBe('无权限');
|
|
|
+ });
|
|
|
+});
|