// ============================================================================ // [D-06 · SC-H-06 · 能力键 butler.ai.collaborate · TC-H-06 / TC-H-06-E / TC-H-06-P] // 三流(功能 / 异常 / 权限)e2e 契约用例 —— AI 管家协同闭环 // ---------------------------------------------------------------------------- // 接口(docs/tdd/orchestration/discovery-2026-11-30-1008.md §3;D8 ButlerController L376/L391/L403): // POST /api/butler/collaborate/close 闭环标记:结论/处置/时长完整保存 + 工单置 CLOSED + 审计 // POST /api/butler/collaborate/feedback 提炼可复用结论 + 反哺:脱敏 + 幂等去重 → 入待审队列(PENDING_REVIEW),非直接入库 // POST /api/butler/collaborate/notify 反馈原家长 + 关闭升级链路:仅「审核通过」的反哺可通知 // // 闭环完整链路(sc-key-map SC-H-06 / TC-H-06): // 闭环标记 → 知识反哺(脱敏+去重) → 入待审 → 运营审核(APPROVED 入库 / REJECTED 退回) → 可检索 → 反馈家长 → 全程审计 // // 契约来源(已逐条核对后端实现,只读,不改生产源码): // cfc-backend/.../controller/butler/ButlerController.java L376/L391/L403 // cfc-backend/.../service/ButlerAiCollaborationService.java // closeOrder: workorderId/userId 空→400;工单不存在→404「工单不存在」;非本家庭→403「仅本家庭工单可操作」; // 已 CLOSED→409「工单已闭环」;保存 resolution/reviewConclusion/durationMinutes + status=CLOSED + closedAt; // 审计 ACTION_CLOSE_ORDER(actor_role=butler);成功 code=200 message「工单已闭环」 // submitFeedback: workorderId/userId 空→400;脱敏后空→400「反哺内容不能为空或不合规」;工单不存在→404; // 非本家庭→403;同工单+同内容指纹重复→409「该结论已反哺,请勿重复提交」; // status=PENDING_REVIEW + dedupeKey(WO-{id}-{hash}) + notified=0;继承工单 conclusion/disposition/duration; // 审计 ACTION_SUBMIT_FEEDBACK;code=200 message「反哺已提交待审队列」 // sanitizeFeedback: 手机号→1**********;身份证→******************;邮箱→***@***(脱敏,不以 403 拒绝隐私内容) // notifyParent: workorderId/feedbackId 空→400;反哺不存在或工单不匹配→404; // !isApprovedForKnowledge→409「反哺未审核通过,不得反馈家长」(未审不入库/不可反馈); // 通过→ set notified=1;DTO{workorderId,feedbackId,familyId,conclusion,disposition,durationMinutes, // sourceWorkorderId,notifiedAt};审计 ACTION_NOTIFY_PARENT(actor_role=operator);code=200「已反馈原家长」 // 审核(运营侧,非本任务 3 端点,用于驱动闭环至「审核通过」): // POST /api/admin/knowledge-base/unified/feedback/pending 运营待审队列;非运营→403「仅运营角色可审核反哺」;匿名→401 // POST /api/admin/knowledge-base/unified/feedback/audit {feedbackId,decision:APPROVED|REJECTED,remark} // 仅 admin/operator/knowledge_admin;非运营→403;PENDING_REVIEW→APPROVED+reviewerId+reviewedAt; // 已审核→409;写入审计 ACTION_AUDIT_FEEDBACK(actor_role=operator) // 家庭隔离 familyMatches(user.familyId == order.familyId) → 跨家庭 403 // Result: success code=200;error code 在 body、HTTP 仍 200;JwtInterceptor 匿名→401; // /api/admin/** 匿名→401、非 admin/operator→403 // // 鉴权 / 真实契约(实测,见本任务探针 _probe_*.spec.js gitignore): // ① 协作端点「完全不带 token」:close/feedback → 403「仅本家庭工单可操作」(匿名 userId=-1,familyId=null→不匹配); // 非本家庭→403;非运营审核→403。(协作端点不强制 401;admin 端点匿名→401。) // ② 管家自审核 → 403「仅运营角色可审核反哺」;审核权归运营(TC-H-06-P)。 // ③ 跨家庭读/写工单/反哺 → 403;SC-H-06 本组实现家庭隔离(对比 G-304-1 只涉本组 A,本组不承袭)。 // // 隐私脱敏 + 403(任务口径核实,真实契约): // ✦「隐私脱敏」:反哺含家长隐私(手机号/身份证/邮箱)→ sanitizeFeedback 脱敏后入待审/入库; // 原文不入库、不下发;不以 403 拒绝隐私内容(真实为 code=200 + 脱敏)。TC-H-06-E「拦截并提示脱敏,不允许带隐私入库」 // 在本实现 = 「脱敏后才入待审」;仅「脱敏后为空」→ 400。 // ✦「403」:越界/越权 —— 管家读/写他人家庭工单/反哺→403;管家自行审核→403;家长直接写入 AI 知识库(非运营审核)→403; // 审核权归运营。(TC-H-06-P) // 两者合并断言:隐私内容不以明文入库/下发 + 越权 403。 // // 「未审不入库」(TC-H-06-E):PENDING_REVIEW 反哺不得反馈家长(notify→409)、不得计入已批准知识; // 仅 APPROVED 且 content 非空(isApprovedForKnowledge)才可 notify / 可检索。 // 本任务 3 端点无「未审入库」路径;notify 是未审→已审的硬闸门。 // // 「审核通过可检索」(TC-H-06 步骤 4/5 / SC-H-06):审核通过 → status=APPROVED + reviewerId + reviewedAt, // 从待审队列移除,isApprovedForKnowledge=true(ButlerAiCollaborationService.verifyKnowledgeHit),notify 放行。 // ⚠ 承袭差距(真实契约核实,非放水):本实现「审核通过」= butler_ai_feedback.status=APPROVED + isApprovedForKnowledge; // 但**未观测到把反哺写入 AI 知识库正文的独立 insert**,且 ButlerAiCollaborationService.verifyKnowledgeHit / // isApprovedForKnowledge / pendingFeedbacks 的「可检索」判定**未暴露 HTTP 查询端点**(仅运营 /feedback/pending 可见状态)。 // 故本用例断言真实可检索代理(APPROVED + 移出待审 + notify 放行 + 脱敏内容保留),并标注承袭差距,不伪造 kb-hit 绿。 // 「AI 再问同类问题能命中新知识」需 kb 检索端点,本组 3 端点不覆盖(见 SC-H-06 登记 / discovery O-03)。 // // 「审计可读」(SC-H-06 可审计 / TC-H-06):close/feedback/audit/notify 全程写 butler_ai_collaboration_audit_log; // ButlerAiCollaborationService.auditLog(workorderId[,familyId]) 只读查询存在,但**未暴露 HTTP 端点**。 // 本用例:① 断言 HTTP 可读审计代理(已闭环字段、待审/已审状态、reviewerId、reviewedAt、notified、来源追溯 sourceWorkorderId); // ② 直接查 butler_ai_collaboration_audit_log 校验 CLOSE_ORDER/SUBMIT_FEEDBACK/AUDIT_FEEDBACK/NOTIFY_PARENT 落库(需 mysql CLI)。 // 并标注「审计表无 HTTP 读端点」承袭差距,不伪造。 // // 工单构造:后端**无 HTTP 创建 butler_escalation_orders 的端点**(ButlerEscalationOrderMapper 无 insert 路径; // /api/butler/escalation/* 只 claim/list/detail/...已存在工单)。本用例用 mysql CLI 直接 seed 一条 PROCESSING 工单 // (与 tests/e2e/reset-db.sh / flow-l1-tests.js 的 mysql 直连约定一致),不改生产源码、不改台账。 // mysql CLI 不可用时本用例明确失败(不造绿)。DB 连接用 MYSQL_HOST/MYSQL_USER/MYSQL_PASS/MYSQL_DB(默认对齐本地 cfc-backend)。 // // 运行: // CFC_API_BASE=http://127.0.0.1:9082 MYSQL_HOST=192.168.16.251 MYSQL_USER=zxyj MYSQL_PASS=zxyj@123 MYSQL_DB=zxyj \ // npx playwright test api/butler-collaborate-contract.spec.js -c tests/playwright.config.api.js --project=desktop // 安全约定:不打印 token / 手机号;测试数据全部自建 direct-register + seed,不改动存量业务数据。 // ============================================================================ const { test, expect } = require('@playwright/test'); const { execFileSync } = require('child_process'); const { apiPost } = require('../helpers/api'); const API_TIMEOUT_MS = 120000; // --------------------------------------------------------------------------- // helpers(禁可选链 ?.;不用 new Date(string);中文内容经参数传入 SQL,不拼接裸值) // --------------------------------------------------------------------------- /** 注册账号(direct-register),返回 token/userId/familyId */ async function register(request, role, tag) { const phone = '13' + String(Math.floor(Math.random() * 900000000) + 100000000); let lastErr; for (let i = 0; i < 3; i++) { try { const r = await request.post('/api/auth/direct-register', { data: { phone, role, nickname: 'QA-' + tag }, timeout: 60000, }); const body = await r.json().catch(() => ({})); if (body.code === 200 && body.data && body.data.token) { return { token: body.data.token, userId: body.data.userId, familyId: body.data.familyId, role: role }; } lastErr = body; } catch (e) { lastErr = e && e.message ? e.message : String(e); } } throw new Error('register(' + role + ') 失败: ' + JSON.stringify(lastErr)); } // ---------- mysql CLI(与 reset-db.sh / flow-l1-tests.js 约定一致;环境变量对齐 cfc-backend 数据源) ---------- const MYSQL_CLI = process.env.MYSQL_CLI || 'mysql'; const MYSQL_HOST = process.env.MYSQL_HOST || '192.168.16.251'; const MYSQL_PORT = process.env.MYSQL_PORT || '3306'; const MYSQL_USER = process.env.MYSQL_USER || 'zxyj'; const MYSQL_PASS = process.env.MYSQL_PASS != null ? process.env.MYSQL_PASS : 'zxyj@123'; const MYSQL_DB = process.env.MYSQL_DB || 'zxyj'; /** 转义 SQL 字符串字面量(单引号翻倍 + 去 NUL/反斜杠处理),仅用于本测试构造的受控值 */ function sqlStr(s) { return "'" + String(s == null ? '' : s).replace(/\\/g, '\\\\').replace(/'/g, "''").replace(/\0/g, '') + "'"; } /** 执行 SQL(经 stdin 传入,返回原始输出;mysql CLI 不可用即抛错,不造绿) */ function mysqlExec(sql) { const args = ['-h', MYSQL_HOST, '-P', MYSQL_PORT, '-u', MYSQL_USER]; if (MYSQL_PASS !== '') args.push('-p' + MYSQL_PASS); // -N --skip-column-names -B --batch:tab 分隔、无表头/无边框,便于机器解析(真实 mysql CLI 支持) args.push('-N', '-B', '--default-character-set=utf8mb4', MYSQL_DB); return execFileSync(MYSQL_CLI, args, { input: sql + ';\n', encoding: 'utf8', timeout: 30000 }); } /** * seed 一条 butler_escalation_orders(status=PROCESSING),返回 workorderId。 * 后端无 HTTP 创建路径;不改生产源码。 */ function seedWorkorder(familyId, butlerUserId, parentDemand, aiConclusion) { mysqlExec( 'INSERT INTO butler_escalation_orders ' + '(family_id, butler_user_id, source_type, risk_level, status, parent_demand, ai_conclusion, escalation_level, created_at, updated_at) ' + 'VALUES (' + Number(familyId) + ', ' + Number(butlerUserId) + ', ' + sqlStr('AI_ESCALATION') + ', ' + sqlStr('MEDIUM') + ', ' + sqlStr('PROCESSING') + ', ' + sqlStr(parentDemand || 'QA 家长诉求') + ', ' + sqlStr(aiConclusion || 'QA AI 已给结论') + ', 1, NOW(), NOW())', ); // 回退/主路径:mysql CLI 每次独立连接,LAST_INSERT_ID() 跨进程不可靠 → 按家庭取最新一条(workers=1,本测试自建数据) const out = mysqlExec('SELECT id FROM butler_escalation_orders WHERE family_id=' + Number(familyId) + " AND status='PROCESSING' ORDER BY id DESC LIMIT 1").trim(); const lines = out.split('\n').map(function (l) { return l.trim(); }).filter(function (l) { return l !== ''; }); let id = null; for (const line of lines) { if (/^\d+$/.test(line)) id = Number(line); } if (id == null) throw new Error('seedWorkorder 取不到工单 id, raw=' + JSON.stringify(out)); return id; } /** 查审计表某工单的 action 集合(审计可读;无 HTTP 读端点时的真实落库校验) */ function auditActionsFor(workorderId) { const out = mysqlExec('SELECT action FROM butler_ai_collaboration_audit_log WHERE workorder_id=' + Number(workorderId)).trim(); // -N -B:无表头无边框;仅保留非空、已知 ACTION_* 形态行 return out.split('\n').map(function (l) { return l.trim(); }).filter(function (l) { return l !== '' && l !== 'action' && !/^[-+|]/.test(l); }); } const ACTION_CLOSE_ORDER = 'CLOSE_ORDER'; const ACTION_SUBMIT_FEEDBACK = 'SUBMIT_FEEDBACK'; const ACTION_AUDIT_FEEDBACK = 'AUDIT_FEEDBACK'; const ACTION_NOTIFY_PARENT = 'NOTIFY_PARENT'; // --------------------------------------------------------------------------- // SC-H-06 · 与 AI 管家协同闭环(三流) // --------------------------------------------------------------------------- test.describe.serial('SC-H-06 · /api/butler/collaborate/{close,feedback,notify}(三流)', () => { test.setTimeout(180000); test('[功能 全链路] 闭环留痕→反哺待审→未审不入库→审核通过可检索→反馈家长→审计可读', async ({ request }) => { const butler = await register(request, 'parent', 'sc-h06-fn-butler'); // 管家操作者(本家庭) const operator = await register(request, 'admin', 'sc-h06-fn-op'); // 运营(审核权) const workorderId = seedWorkorder(butler.familyId, butler.userId, 'QA 家长诉求', 'QA AI 已给结论'); // ---- 1. 闭环标记 close:结论/处置/时长完整保存 + 工单 CLOSED ---- const close = await apiPost( request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: '睡眠调整建议有效', disposition: '电话随访', durationMinutes: 25 }, butler.token, ); expect(close.status, 'close 不500').toBe(200); expect(close.code, 'close code=200, 实际=' + close.code + ' msg=' + close.message).toBe(200); expect(close.message).toBe('工单已闭环'); // 闭环留痕可读(audit/readable):经 /api/butler/escalation/detail 读回已保存字段 const detail = await apiPost(request, '/api/butler/escalation/detail', { workorderId: workorderId }, butler.token); expect(detail.code, '读工单 code=200').toBe(200); expect(detail.data.status, '工单置为已闭环 CLOSED').toBe('CLOSED'); expect(detail.data.resolution, '闭环结论完整保存').toBe('睡眠调整建议有效'); expect(detail.data.reviewConclusion, '处置方式完整保存').toBe('电话随访'); expect(detail.data.durationMinutes, '处置时长完整保存').toBe(25); expect(detail.data.closedAt != null, 'closedAt 留痕').toBe(true); // 已闭环再 close → 409 const reclose = await apiPost( request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'x', disposition: 'y', durationMinutes: 1 }, butler.token, ); expect(reclose.code, '已闭环再 close → 409').toBe(409); // ---- 2. 反哺提交 feedback:进入待审队列,未直接入库 ---- const feedback = await apiPost( request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: '可复用结论:规律作息+睡前限屏,睡眠质量提升。' }, butler.token, ); expect(feedback.status).toBe(200); expect(feedback.code, 'feedback code=200, 实际=' + feedback.code + ' msg=' + feedback.message).toBe(200); expect(feedback.message).toBe('反哺已提交待审队列'); const fb = feedback.data || {}; expect(typeof fb.id === 'number', '反哺返回 id').toBe(true); const feedbackId = fb.id; expect(fb.workorderId).toBe(workorderId); expect(fb.familyId, '反哺绑定本家庭').toBe(butler.familyId); expect(fb.status, '入待审队列 PENDING_REVIEW,未直接入库').toBe('PENDING_REVIEW'); expect(fb.notified, 'notified=0 未反馈').toBe(0); expect(typeof fb.dedupeKey === 'string' && fb.dedupeKey.length > 0, '幂等去重键').toBe(true); expect(fb.conclusion, '继承闭环结论').toBe('睡眠调整建议有效'); expect(fb.disposition, '继承处置').toBe('电话随访'); expect(fb.durationMinutes, '继承时长').toBe(25); expect(fb.reviewerId == null, '未审 reviewerId 为空').toBe(true); expect(fb.reviewedAt == null, '未审 reviewedAt 为空').toBe(true); // ---- 3. 未审不入库:PENDING_REVIEW 不可 notify,且不可直接写入已审 ---- const notifyBefore = await apiPost( request, '/api/butler/collaborate/notify', { workorderId: workorderId, feedbackId: feedbackId }, butler.token, ); expect(notifyBefore.status).toBe(200); expect(notifyBefore.code, '未审核不得反馈家长 → 409, 实际=' + notifyBefore.code).toBe(409); expect(notifyBefore.message).toBe('反哺未审核通过,不得反馈家长'); // 待审队列可见(运营) const pending = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/pending', {}, operator.token); expect(pending.code, '运营读待审队列 code=200').toBe(200); const pendingList = pending.data || []; expect(Array.isArray(pendingList)).toBe(true); expect(pendingList.some(function (x) { return x.id === feedbackId && x.status === 'PENDING_REVIEW'; }), '待审队列含本条 PENDING_REVIEW').toBe(true); // ---- 4. 运营审核通过 → APPROVED + reviewer + reviewedAt;移出待审;可检索代理 ---- const audit = await apiPost( request, '/api/admin/knowledge-base/unified/feedback/audit', { feedbackId: feedbackId, decision: 'APPROVED', remark: 'ok' }, operator.token, ); expect(audit.status).toBe(200); expect(audit.code, '审核通过 code=200, 实际=' + audit.code + ' msg=' + audit.message).toBe(200); const approved = audit.data || {}; expect(approved.status, '审核通过 APPROVED').toBe('APPROVED'); expect(approved.reviewerId, '审核人留痕').toBe(operator.userId); expect(approved.reviewRemark).toBe('ok'); expect(approved.reviewedAt != null, '审核时间留痕').toBe(true); expect(approved.content != null && approved.content.trim().length > 0, '已审核通过内容非空(可检索前提)').toBe(true); const pendingAfter = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/pending', {}, operator.token); const pendingAfterList = pendingAfter.data || []; expect(pendingAfterList.some(function (x) { return x.id === feedbackId; }), '审核通过移出待审队列').toBe(false); // 重复审核 → 409(已审核不可重复) const reAudit = await apiPost( request, '/api/admin/knowledge-base/unified/feedback/audit', { feedbackId: feedbackId, decision: 'REJECTED', remark: 'x' }, operator.token, ); expect(reAudit.code, '已审核不可重复 → 409').toBe(409); // ---- 5. notify:仅审核通过可反馈原家长;DTO 来源追溯;notified=1 ---- const notify = await apiPost( request, '/api/butler/collaborate/notify', { workorderId: workorderId, feedbackId: feedbackId }, butler.token, ); expect(notify.status).toBe(200); expect(notify.code, '审核通过可反馈 code=200, 实际=' + notify.code + ' msg=' + notify.message).toBe(200); expect(notify.message).toBe('已反馈原家长'); const dto = notify.data || {}; expect(dto.workorderId).toBe(workorderId); expect(dto.feedbackId).toBe(feedbackId); expect(dto.familyId).toBe(butler.familyId); expect(dto.conclusion, '反馈仅脱敏结论').toBe('睡眠调整建议有效'); expect(dto.disposition).toBe('电话随访'); expect(dto.durationMinutes).toBe(25); expect(dto.sourceWorkorderId, '来源追溯 sourceWorkorderId').toBe(workorderId); expect(dto.notifiedAt != null, 'notifiedAt').toBe(true); // notified=1 可读(审计/状态) const pendingFinal = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/pending', {}, operator.token); expect((pendingFinal.data || []).some(function (x) { return x.id === feedbackId; }), '不在待审').toBe(false); // ---- 6. 审计可读:HTTP 代理 + butler_ai_collaboration_audit_log 落库 ---- // HTTP 可读代理(本组 3 端点 + 运营端点无直接 audit 读 HTTP;见文件头登记) expect(detail.data.closedAt != null).toBe(true); // 直接校验审计表(auditLog 服务未暴露 HTTP;真实落库 + 可读) const actions = auditActionsFor(workorderId); expect(actions.indexOf(ACTION_CLOSE_ORDER) >= 0, '审计含 CLOSE_ORDER').toBe(true); expect(actions.indexOf(ACTION_SUBMIT_FEEDBACK) >= 0, '审计含 SUBMIT_FEEDBACK').toBe(true); expect(actions.indexOf(ACTION_AUDIT_FEEDBACK) >= 0, '审计含 AUDIT_FEEDBACK').toBe(true); expect(actions.indexOf(ACTION_NOTIFY_PARENT) >= 0, '审计含 NOTIFY_PARENT').toBe(true); }); // ------------------------------------------------------------------------- // 异常流 TC-H-06-E // ------------------------------------------------------------------------- test('[异常·隐私脱敏] 反哺含家长隐私(手机号/身份证/邮箱)→ 脱敏后才入待审,原文不入库不下发;脱敏后空→400', async ({ request }) => { const butler = await register(request, 'parent', 'sc-h06-privacy'); const workorderId = seedWorkorder(butler.familyId, butler.userId, 'QA', 'QA'); await apiPost( request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, butler.token, ); const phone = '13800001111'; const idCard = '110101199003070011'; const email = 'parent.secret@example.com'; const raw = '可复用结论:联系家长 张三 ' + phone + ',邮箱 ' + email + ',身份证 ' + idCard + ',建议规律作息。'; const fb = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: raw }, butler.token); expect(fb.code, '含隐私反哺仍 code=200(脱敏,非以明文入库)').toBe(200); const stored = (fb.data && fb.data.content) || ''; // 原始隐私不下发/不入库 expect(stored.indexOf(phone), '手机号原文不出现').toBe(-1); expect(stored.indexOf(idCard), '身份证原文不出现').toBe(-1); expect(stored.indexOf(email), '邮箱原文不出现').toBe(-1); // 脱敏标记 expect(stored, '手机号脱敏').toContain('1**********'); expect(stored, '邮箱脱敏').toContain('***@***'); expect(stored, '身份证脱敏').toContain('******************'); // 非隐私内容保留 expect(stored, '可复用结论保留').toContain('规律作息'); expect(fb.data.status).toBe('PENDING_REVIEW'); // 脱敏后为空 → 400 const empty = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: ' ' }, butler.token); expect(empty.code, '空内容 → 400').toBe(400); expect(empty.message).toBe('反哺内容不能为空或不合规'); }); test('[异常·未审不入库] PENDING_REVIEW 不可 notify(409);重复反哺幂等去重(409);已审核不可重复(409)', async ({ request }) => { const butler = await register(request, 'parent', 'sc-h06-unaudited'); const operator = await register(request, 'admin', 'sc-h06-unaudited-op'); const workorderId = seedWorkorder(butler.familyId, butler.userId, 'QA', 'QA'); await apiPost(request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, butler.token); const content = '可复用结论XYZ-去重测试'; const fb = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: content }, butler.token); expect(fb.code).toBe(200); const feedbackId = fb.data.id; // 未审核不入库/不可反馈 const notifyPre = await apiPost(request, '/api/butler/collaborate/notify', { workorderId: workorderId, feedbackId: feedbackId }, butler.token); expect(notifyPre.code, 'PENDING_REVIEW 不可 notify → 409').toBe(409); // 同一结论重复反哺 → 幂等去重 409 const dup = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: content }, butler.token); expect(dup.code, '重复反哺 → 409').toBe(409); expect(dup.message).toBe('该结论已反哺,请勿重复提交'); // 运营待审队列仅 1 条(去重) const pending = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/pending', {}, operator.token); const matching = (pending.data || []).filter(function (x) { return x.workorderId === workorderId && x.content === content; }); expect(matching.length, '同一结论仅 1 条待审').toBe(1); }); test('[异常·隐私脱敏 403 越权] 非本家庭工单不可 close/feedback(403);跨家庭不可读反哺(403);家长直接写入知识库(403)', async ({ request }) => { const owner = await register(request, 'parent', 'sc-h06-iso-owner'); const intruder = await register(request, 'parent', 'sc-h06-iso-intruder'); const workorderId = seedWorkorder(owner.familyId, owner.userId, 'QA', 'QA'); // 先本家庭闭环 + 反哺,造出反哺条目 await apiPost(request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, owner.token); const fb = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: '可复用结论' }, owner.token); expect(fb.code).toBe(200); // 跨家庭 close → 403 const crossClose = await apiPost(request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'x', disposition: 'y', durationMinutes: 1 }, intruder.token); expect(crossClose.status).toBe(200); expect(crossClose.code, '跨家庭 close → 403').toBe(403); expect(crossClose.message).toBe('仅本家庭工单可操作'); // 跨家庭 feedback → 403 const crossFb = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: '越权内容' }, intruder.token); expect(crossFb.code, '跨家庭 feedback → 403').toBe(403); expect(crossFb.message).toBe('仅本家庭工单可操作'); // 家长直接写入 AI 知识库(非运营审核)→ 403 const directKb = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/audit', { feedbackId: fb.data.id, decision: 'APPROVED' }, owner.token); expect(directKb.status).toBe(200); expect(directKb.code, '家长直接写入知识库 → 403').toBe(403); expect(directKb.message).toBe('仅运营角色可审核反哺'); }); test('[异常·不存在] 工单不存在 close/feedback→404;notify 反哺不存在/不匹配→404;参数缺失→400', async ({ request }) => { const u = await register(request, 'parent', 'sc-h06-404'); const close404 = await apiPost(request, '/api/butler/collaborate/close', { workorderId: 99999999, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, u.token); expect(close404.code, 'close 工单不存在→404').toBe(404); expect(close404.message).toBe('工单不存在'); const fb404 = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: 99999999, content: 'x' }, u.token); expect(fb404.code, 'feedback 工单不存在→404').toBe(404); const notify404 = await apiPost(request, '/api/butler/collaborate/notify', { workorderId: 99999999, feedbackId: 99999999 }, u.token); expect(notify404.code, 'notify 反哺不存在→404').toBe(404); expect(notify404.message).toBe('反哺条目不存在或与工单不匹配'); // 参数缺失 → 400 const missClose = await apiPost(request, '/api/butler/collaborate/close', { conclusion: 'c', disposition: 'd', durationMinutes: 1 }, u.token); expect(missClose.code, 'close workorderId 缺失→400').toBe(400); expect(missClose.message).toBe('workorderId和userId不能为空'); const missNotify = await apiPost(request, '/api/butler/collaborate/notify', { workorderId: 1 }, u.token); expect(missNotify.code, 'notify feedbackId 缺失→400').toBe(400); expect(missNotify.message).toBe('workorderId和feedbackId不能为空'); }); // ------------------------------------------------------------------------- // 权限流 TC-H-06-P // ------------------------------------------------------------------------- test('[权限·审核权归运营] 管家自审核→403;非运营待审队列→403;运营可审核(admin/operator/knowledge_admin)', async ({ request }) => { const butler = await register(request, 'parent', 'sc-h06-p-self'); const operator = await register(request, 'admin', 'sc-h06-p-op'); const workorderId = seedWorkorder(butler.familyId, butler.userId, 'QA', 'QA'); await apiPost(request, '/api/butler/collaborate/close', { workorderId: workorderId, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, butler.token); const fb = await apiPost(request, '/api/butler/collaborate/feedback', { workorderId: workorderId, content: '可复用结论' }, butler.token); expect(fb.code).toBe(200); const feedbackId = fb.data.id; // 管家自行审核通过 → 403 const selfAudit = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/audit', { feedbackId: feedbackId, decision: 'APPROVED' }, butler.token); expect(selfAudit.code, '管家仅有提交权、审核权归运营 → 403').toBe(403); expect(selfAudit.message).toBe('仅运营角色可审核反哺'); // 非运营读待审队列 → 403 const pendingNo = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/pending', {}, butler.token); expect(pendingNo.code, '非运营读待审→403').toBe(403); // 管家自审失败后反哺仍 PENDING_REVIEW,未审不入库 const notifyPre = await apiPost(request, '/api/butler/collaborate/notify', { workorderId: workorderId, feedbackId: feedbackId }, butler.token); expect(notifyPre.code, '管家自审未生效,仍未审 → notify 409').toBe(409); // 运营可审核 const opAudit = await apiPost(request, '/api/admin/knowledge-base/unified/feedback/audit', { feedbackId: feedbackId, decision: 'APPROVED' }, operator.token); expect(opAudit.code, '运营审核 code=200').toBe(200); expect((opAudit.data || {}).status).toBe('APPROVED'); }); test('[权限·鉴权] 协作端点匿名/无 token → 不放行(403);admin 审核匿名→401', async ({ request }) => { const u = await register(request, 'parent', 'sc-h06-anon'); const workorderId = seedWorkorder(u.familyId, u.userId, 'QA', 'QA'); // close 匿名:匿名 userId=-1/familyId=null → 非本家庭 → 403(协作端点不强制 401) const anonClose = await request.post('/api/butler/collaborate/close', { data: { workorderId: workorderId, conclusion: 'c', disposition: 'd', durationMinutes: 1 }, timeout: 30000, }); expect(anonClose.status(), '匿名 close 不 500').toBe(200); const anonCloseBody = await anonClose.json().catch(() => ({})); expect(anonCloseBody.code, '匿名 close → 403').toBe(403); // feedback 匿名 → 403 const anonFb = await request.post('/api/butler/collaborate/feedback', { data: { workorderId: workorderId, content: 'x' }, timeout: 30000 }); const anonFbBody = await anonFb.json().catch(() => ({})); expect(anonFbBody.code, '匿名 feedback → 403').toBe(403); // admin 审核匿名 → 401 const anonAdmin = await request.post('/api/admin/knowledge-base/unified/feedback/pending', { timeout: 30000 }); expect(anonAdmin.status(), '/api/admin 匿名→401').toBe(401); const anonAdminBody = await anonAdmin.json().catch(() => ({})); expect(anonAdminBody.code).toBe(401); expect(anonAdminBody.message).toBe('请先登录'); }); }); // 安全约定:本文件不打印 token / 手机号。