| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- // ============================================================================
- // [F-1111-01 · SC-H-01/04 · 能力键 butler.escalation.handle · TC-H-01/04]
- // 三流(功能 / 异常 / 权限)e2e 契约用例 —— 真人管家 AI 升级工单治理
- // 缺口来源:docs/tdd/orchestration/discovery-2026-11-30-1111.md F-1111-01
- // (7 端点 e2e 无 spec;F-1111-03 登记 workorder fixme 载体错位,实际载体 = /api/butler/escalation/*)
- // ----------------------------------------------------------------------------
- // 接口(API_REFERENCE.md §4.64.6 L3674–3680;ButlerController L206–279):
- // POST /api/butler/escalation/list 工作台:按风险等级 HIGH→MEDIUM→LOW 排序(仅 PENDING/PROCESSING/ESCALATED)
- // POST /api/butler/escalation/detail 工单详情(仅本家庭,越界 403)
- // POST /api/butler/escalation/claim 乐观锁认领(CAS on status=PENDING;他人「已被认领」409)
- // POST /api/butler/escalation/log-contact 联系动作留痕(CALL/MESSAGE/IN_APP)
- // POST /api/butler/escalation/close 人工处置结论并闭环(PENDING/PROCESSING→CLOSED 留痕)
- // POST /api/butler/escalation/transfer 转交规划师(teacher_id + target_role=teacher 留痕)
- // POST /api/butler/escalation/withdraw 撤回工单(→WITHDRAWN,终态不可重复操作)
- //
- // 契约来源(已逐条核对后端实现,只读,不改生产源码):
- // cfc-backend/.../service/ButlerEscalationService.java(S-cfc-251/278/290 落地):
- // list: butlerUserId 空→400;按 butler_user_id + status∈(PENDING,PROCESSING,ESCALATED) 过滤,
- // ORDER BY FIELD(risk_level,'HIGH','MEDIUM','LOW') ASC, created_at DESC
- // detail: workorderId 空→400「workorderId不能为空」;不存在→404「工单不存在」;
- // !canHandle(同家庭)→403「仅本家庭工单可查看」
- // claim: 参数空→400「workorderId和butlerUserId不能为空」;不存在→404「工单不存在」;
- // 非 PENDING:本人→500「该工单已被你认领」(Result.error(String)=500 真实契约)、
- // 他人→409「该工单已被认领」;CAS PENDING→PROCESSING + butler_user_id + claimed_at,
- // 并发仅 1 人成功(rows=0→409「该工单已被认领」)
- // log-contact: contactType 空→400;非 CALL/MESSAGE/IN_APP→400「contactType必须为 CALL / MESSAGE / IN_APP」;
- // !canHandle→403「仅本家庭工单可操作」(不存在的工单同样 fail-closed 403);
- // 成功 200「联系动作已记录」+ contact_type/contact_content 留痕
- // close: resolution 空→400「workorderId和resolution不能为空」;!canHandle→403;
- // CLOSED→409「工单已闭环」;WITHDRAWN→409「工单已撤回,不可闭环」;
- // 成功 200「工单已闭环」+ status=CLOSED + resolution + closed_at
- // transfer: 参数空→400「workorderId和teacherId不能为空」;!canHandle→403;
- // 成功 200「已转交规划师」+ teacher_id + target_role='teacher'
- // withdraw: 参数空→400「workorderId不能为空」;!canHandle→403「仅本家庭工单可操作」;
- // CLOSED/WITHDRAWN→409「工单已闭环或已撤回,不可重复操作」;成功 200「工单已撤回」
- // 家庭隔离 canHandle:order.family_id == user.family_id;越界/匿名(userId=-1)→false→403
- // Result<T>:业务码在 body,HTTP 恒 200;/api/butler/** 未列匿名 401 强制族 → 匿名降级 userId=-1
- // 走业务 403(实测探针 _probe_escalation.spec.js:anon detail → http=200 code=403)
- //
- // 真实契约探针(_probe_escalation.spec.js,gitignore,已跑通本地 127.0.0.1:9082 + 测试库 192.168.16.251/zxyj):
- // ① 排序实测 HIGH/MEDIUM/LOW 位置 0/1/2;② 本人重复认领 → code=500「该工单已被你认领」(非 409,
- // Result.error(String) 口径);他人(已认领后)→ 409「该工单已被认领」;
- // ③ 不存在工单 detail/claim→404,log-contact/close/transfer/withdraw→403(fail-closed,写操作先过 canHandle);
- // ④ 匿名 detail → http=200 code=403「仅本家庭工单可查看」(butler 族非 401 强制);
- // ⑤ 观察(不造绿、不判红):claim 无 canHandle 校验 —— PENDING 工单跨家庭 claim 会成功并改写
- // butler_user_id(文档契约仅承诺「已被认领」409 与 detail 越界 403,PENDING 跨家庭 claim 未承诺 403,
- // 故本 spec 只按真实契约断言 409/403 场景,不把该观察当绿灯断言,留台账裁定是否入 G-304-1 口径)。
- //
- // 工单构造:后端**无 HTTP 创建 butler_escalation_orders 的端点**(与 butler-collaborate-contract.spec.js 一致),
- // 本 spec 用 mysql CLI 直接 seed PENDING 工单(marker 前缀 QA- 便于清理),不改生产源码、不改台账。
- // mysql CLI 不可用时本 spec 明确失败(不造绿)。
- //
- // 运行:
- // 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-escalation-contract.spec.js -c playwright.config.api.js --project=desktop
- // 安全约定:不打印 token / 手机号;测试数据全部自建(direct-register + mysql seed),结束时清理自建工单。
- // ============================================================================
- const { test, expect } = require('@playwright/test');
- const { execFileSync } = require('child_process');
- const { apiPost } = require('../helpers/api');
- // ---------- register(direct-register,与既有契约 spec 同约定) ----------
- 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: body.data.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 约定一致) ----------
- 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';
- function sqlStr(s) {
- return "'" + String(s == null ? '' : s).replace(/\\/g, '\\\\').replace(/'/g, "''").replace(/\0/g, '') + "'";
- }
- function mysqlExec(sql) {
- const args = ['-h', MYSQL_HOST, '-P', MYSQL_PORT, '-u', MYSQL_USER];
- if (MYSQL_PASS !== '') args.push('-p' + MYSQL_PASS);
- // -N -B:无表头无边框。实测该客户端 --default-character-set 不生效(连接恒 latin1,中文双编码),
- // 故每会话显式 SET NAMES utf8mb4(mysql CLI 独立连接,前置于 SQL)。
- args.push('-N', '-B', MYSQL_DB);
- return execFileSync(MYSQL_CLI, args, { input: 'SET NAMES utf8mb4;\n' + sql + ';\n', encoding: 'utf8', timeout: 30000 });
- }
- /** seed 一条 PENDING 工单(marker 唯一化),返回 workorderId;mysql CLI 不可用即抛错,不造绿 */
- function seedPendingOrder(familyId, butlerUserId, riskLevel, marker) {
- mysqlExec(
- 'INSERT INTO butler_escalation_orders ' +
- '(family_id, butler_user_id, source_type, risk_level, status, parent_demand, ai_conclusion, created_at, updated_at) ' +
- 'VALUES (' + Number(familyId) + ', ' + Number(butlerUserId) + ', ' + sqlStr('AI_ESCALATION') + ', ' +
- sqlStr(riskLevel) + ', ' + sqlStr('PENDING') + ', ' + sqlStr(marker + '-家长诉求') + ', ' +
- sqlStr(marker + '-AI结论') + ', NOW(), NOW())',
- );
- const out = mysqlExec('SELECT id FROM butler_escalation_orders WHERE parent_demand=' + sqlStr(marker + '-家长诉求') + ' 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('seedPendingOrder 取不到工单 id(mysql CLI 不可用或 seed 失败), raw=' + JSON.stringify(out));
- return id;
- }
- function cleanupOrders(ids) {
- const list = ids.filter(function (x) { return x != null; }).map(String).join(',');
- if (list !== '') mysqlExec('DELETE FROM butler_escalation_orders WHERE id IN (' + list + ')');
- }
- const uniq = () => 'QA' + Date.now() + String(Math.floor(Math.random() * 100000));
- // ============================================================================
- // F-1111-01 · /api/butler/escalation/*(三流)
- // ============================================================================
- test.describe.serial('F-1111-01 · /api/butler/escalation/{list,detail,claim,log-contact,close,transfer,withdraw}(三流)', () => {
- test.setTimeout(180000);
- // -------------------------------------------------------------------------
- // 正常流 TC-H-01(工作台排序 / 乐观锁认领 / 联系留痕 / 闭环流转 / 转交)
- // -------------------------------------------------------------------------
- test('[功能] 工作台按风险等级 HIGH→MEDIUM→LOW 排序,且携带家长诉求/AI结论上下文', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-ws-A');
- const B = await register(request, 'parent', 'esc-ws-B');
- const m = uniq();
- const ids = [
- seedPendingOrder(A.familyId, A.userId, 'LOW', m + '-low'),
- seedPendingOrder(A.familyId, A.userId, 'MEDIUM', m + '-med'),
- seedPendingOrder(A.familyId, A.userId, 'HIGH', m + '-high'),
- ];
- try {
- const res = await apiPost(request, '/api/butler/escalation/list', {}, A.token);
- expect(res.status, 'list 不500').toBe(200);
- expect(res.code, 'list code=200, 实际=' + res.code + ' msg=' + res.message).toBe(200);
- const orders = res.data || [];
- expect(Array.isArray(orders), 'list 返回数组').toBe(true);
- const pos = ids.map(function (id) { return orders.findIndex(function (o) { return o.id === id; }); });
- expect(pos.every(function (p) { return p >= 0; }), '本管家(PENDING)工单全部在工作台内, 位置=' + pos.join(',')).toBe(true);
- expect(pos[2] < pos[1] && pos[1] < pos[0], '风险排序 HIGH 先展示(位置:high<med<low), 实际=' + pos.join(',')).toBe(true);
- // 上下文完整携带(不丢上下文)
- const high = orders.find(function (o) { return o.id === ids[2]; });
- expect(String(high.parentDemand || '').indexOf(m + '-high') === 0, '家长诉求上下文非空').toBe(true);
- expect(String(high.aiConclusion || '').indexOf(m + '-high') === 0, 'AI 已给结论上下文非空').toBe(true);
- expect(high.riskLevel).toBe('HIGH');
- expect(high.status).toBe('PENDING');
- // 工作台身份隔离:B 的 list 不含 A 名下的工单(butler_user_id=B 过滤)
- const resB = await apiPost(request, '/api/butler/escalation/list', {}, B.token);
- expect(resB.code, 'B list code=200').toBe(200);
- const bIds = (resB.data || []).map(function (o) { return o.id; });
- expect(bIds.some(function (id) { return ids.indexOf(id) >= 0; }), 'B 工作台不得出现 A 名下的工单').toBe(false);
- } finally {
- cleanupOrders(ids);
- }
- });
- test('[功能] 乐观锁认领→联系留痕→闭环:PENDING→PROCESSING→CLOSED 全程留痕', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-flow-A');
- const m = uniq();
- const ordHigh = seedPendingOrder(A.familyId, A.userId, 'HIGH', m + '-high');
- const ordMed = seedPendingOrder(A.familyId, A.userId, 'MEDIUM', m + '-med');
- try {
- // ---- 1. 认领(乐观锁)----
- const claim = await apiPost(request, '/api/butler/escalation/claim', { workorderId: ordHigh }, A.token);
- expect(claim.code, '认领 code=200, 实际=' + claim.code + ' msg=' + claim.message).toBe(200);
- expect(claim.message).toBe('认领成功');
- let d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordHigh }, A.token);
- expect(d.code, '认领后详情 code=200').toBe(200);
- expect(d.data.status, 'PENDING→PROCESSING').toBe('PROCESSING');
- expect(d.data.butlerUserId, 'butler_user_id 记为认领人').toBe(A.userId);
- expect(d.data.claimedAt != null, '认领时间 claimed_at 留痕').toBe(true);
- // ---- 2. 联系动作留痕(电话/消息/站内)----
- const lc = await apiPost(request, '/api/butler/escalation/log-contact', {
- workorderId: ordMed, contactType: 'CALL', content: '电话回访确认家长诉求',
- }, A.token);
- expect(lc.code, 'log-contact code=200, 实际=' + lc.code + ' msg=' + lc.message).toBe(200);
- expect(lc.message).toBe('联系动作已记录');
- d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordMed }, A.token);
- expect(d.code, '留痕后详情 code=200').toBe(200);
- expect(d.data.contactType, '联系类型留痕').toBe('CALL');
- expect(d.data.contactContent, '联系内容留痕').toBe('电话回访确认家长诉求');
- // ---- 3. 人工处置结论并闭环(PENDING/PROCESSING→CLOSED)----
- const close = await apiPost(request, '/api/butler/escalation/close', {
- workorderId: ordHigh, resolution: '已电话安抚并调整作息建议',
- }, A.token);
- expect(close.code, 'close code=200, 实际=' + close.code + ' msg=' + close.message).toBe(200);
- expect(close.message).toBe('工单已闭环');
- d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordHigh }, A.token);
- expect(d.code, '闭环后详情 code=200').toBe(200);
- expect(d.data.status, '→CLOSED').toBe('CLOSED');
- expect(d.data.resolution, '人工结论留痕').toBe('已电话安抚并调整作息建议');
- expect(d.data.closedAt != null, '闭环时间 closed_at 留痕').toBe(true);
- } finally {
- cleanupOrders([ordHigh, ordMed]);
- }
- });
- test('[功能] 转交规划师:teacher_id + target_role=teacher 留痕;撤回:终态不可重复操作', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-tr-A');
- const T = await register(request, 'teacher', 'esc-tr-T');
- const m = uniq();
- const ordT = seedPendingOrder(A.familyId, A.userId, 'LOW', m + '-tr');
- const ordW = seedPendingOrder(A.familyId, A.userId, 'MEDIUM', m + '-wd');
- try {
- // 转交规划师
- const tf = await apiPost(request, '/api/butler/escalation/transfer', { workorderId: ordT, teacherId: T.userId }, A.token);
- expect(tf.code, 'transfer code=200, 实际=' + tf.code + ' msg=' + tf.message).toBe(200);
- expect(tf.message).toBe('已转交规划师');
- let d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordT }, A.token);
- expect(d.code, '转交后详情 code=200').toBe(200);
- expect(d.data.teacherId, '转交对象留痕').toBe(T.userId);
- expect(d.data.targetRole, 'target_role=teacher').toBe('teacher');
- // 撤回(SC-H-04 已撤回状态)
- const wd = await apiPost(request, '/api/butler/escalation/withdraw', { workorderId: ordW }, A.token);
- expect(wd.code, 'withdraw code=200, 实际=' + wd.code + ' msg=' + wd.message).toBe(200);
- expect(wd.message).toBe('工单已撤回');
- d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordW }, A.token);
- expect(d.code, '撤回后详情 code=200').toBe(200);
- expect(d.data.status, '→WITHDRAWN').toBe('WITHDRAWN');
- } finally {
- cleanupOrders([ordT, ordW]);
- }
- });
- // -------------------------------------------------------------------------
- // 异常流 TC-H-01-E(参数 / 状态冲突 / 不存在)
- // -------------------------------------------------------------------------
- test('[异常] 参数缺失→400;工单不存在:detail/claim→404,写操作 fail-closed→403', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-400-A');
- // 参数缺失 → 400(逐端点)
- const miss = [
- ['detail', {}, 'workorderId不能为空'],
- ['claim', {}, 'workorderId和butlerUserId不能为空'],
- ['log-contact', { workorderId: 1 }, 'workorderId和contactType不能为空'],
- ['close', { workorderId: 1 }, 'workorderId和resolution不能为空'],
- ['transfer', { workorderId: 1 }, 'workorderId和teacherId不能为空'],
- ['withdraw', {}, 'workorderId不能为空'],
- ];
- for (const [ep, body, msg] of miss) {
- const r = await apiPost(request, '/api/butler/escalation/' + ep, body, A.token);
- expect(r.code, ep + ' 参数缺失→400, 实际=' + r.code + ' msg=' + r.message).toBe(400);
- expect(r.message, ep + ' 缺失提示').toBe(msg);
- }
- // 不存在工单:detail/claim 先查后判 → 404
- const d404 = await apiPost(request, '/api/butler/escalation/detail', { workorderId: 99999999 }, A.token);
- expect(d404.code, 'detail 不存在→404, 实际=' + d404.code).toBe(404);
- expect(d404.message).toBe('工单不存在');
- const c404 = await apiPost(request, '/api/butler/escalation/claim', { workorderId: 99999999 }, A.token);
- expect(c404.code, 'claim 不存在→404, 实际=' + c404.code).toBe(404);
- expect(c404.message).toBe('工单不存在');
- // 写操作先过 canHandle(工单不存在→越界口径)→ fail-closed 403(真实契约,实测探针)
- const failClosed = ['log-contact', 'close', 'transfer', 'withdraw'];
- for (const ep of failClosed) {
- const body = ep === 'log-contact' ? { workorderId: 99999999, contactType: 'CALL' }
- : ep === 'close' ? { workorderId: 99999999, resolution: 'x' }
- : ep === 'transfer' ? { workorderId: 99999999, teacherId: 1 }
- : { workorderId: 99999999 };
- const r = await apiPost(request, '/api/butler/escalation/' + ep, body, A.token);
- expect(r.code, ep + ' 不存在→403 fail-closed, 实际=' + r.code + ' msg=' + r.message).toBe(403);
- expect(r.message).toBe('仅本家庭工单可操作');
- }
- // 非法联系类型 → 400
- const A2 = await register(request, 'parent', 'esc-400-A2');
- const m = uniq();
- const ord = seedPendingOrder(A2.familyId, A2.userId, 'LOW', m + '-sms');
- try {
- const bad = await apiPost(request, '/api/butler/escalation/log-contact', { workorderId: ord, contactType: 'SMS', content: 'x' }, A2.token);
- expect(bad.code, '非法 contactType→400, 实际=' + bad.code).toBe(400);
- expect(bad.message).toBe('contactType必须为 CALL / MESSAGE / IN_APP');
- } finally {
- cleanupOrders([ord]);
- }
- });
- test('[异常] 乐观锁冲突:已被认领 409(他人)/ 已被你认领 500(本人,真实契约);终态 409 不重复操作', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-conf-A');
- const B = await register(request, 'parent', 'esc-conf-B');
- const m = uniq();
- const ordC = seedPendingOrder(A.familyId, A.userId, 'HIGH', m + '-c');
- const ordX = seedPendingOrder(A.familyId, A.userId, 'MEDIUM', m + '-x');
- const ordY = seedPendingOrder(A.familyId, A.userId, 'LOW', m + '-y');
- try {
- // A 先认领 → B 再认领 → 409「该工单已被认领」(TC-H-01-E 他人提示)
- const first = await apiPost(request, '/api/butler/escalation/claim', { workorderId: ordC }, A.token);
- expect(first.code, '首次认领 200').toBe(200);
- const second = await apiPost(request, '/api/butler/escalation/claim', { workorderId: ordC }, B.token);
- expect(second.code, '他人再认领→409, 实际=' + second.code + ' msg=' + second.message).toBe(409);
- expect(second.message).toBe('该工单已被认领');
- // 本人重复认领 → 500「该工单已被你认领」(Result.error(String)=500 真实契约,探针实测;非 409 口径)
- const selfAgain = await apiPost(request, '/api/butler/escalation/claim', { workorderId: ordC }, A.token);
- expect(selfAgain.code, '本人重复认领→500(真实契约), 实际=' + selfAgain.code).toBe(500);
- expect(selfAgain.message).toBe('该工单已被你认领');
- // 认领归属未被跨家庭改写(CAS 失败 rows=0,butler_user_id 保持 A)
- const d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ordC }, A.token);
- expect(d.data.butlerUserId, '认领归属不被跨家庭改写').toBe(A.userId);
- // 闭环终态:重复 close→409;close 空 conclusion→400
- await apiPost(request, '/api/butler/escalation/close', { workorderId: ordX, resolution: '处置结论' }, A.token);
- const reclose = await apiPost(request, '/api/butler/escalation/close', { workorderId: ordX, resolution: '再闭环' }, A.token);
- expect(reclose.code, '已闭环再 close→409, 实际=' + reclose.code).toBe(409);
- expect(reclose.message).toBe('工单已闭环');
- // 撤回终态:重复 withdraw→409;撤回后 close→409
- await apiPost(request, '/api/butler/escalation/withdraw', { workorderId: ordY }, A.token);
- const rewd = await apiPost(request, '/api/butler/escalation/withdraw', { workorderId: ordY }, A.token);
- expect(rewd.code, '已撤回再 withdraw→409, 实际=' + rewd.code).toBe(409);
- expect(rewd.message).toBe('工单已闭环或已撤回,不可重复操作');
- const closeWd = await apiPost(request, '/api/butler/escalation/close', { workorderId: ordY, resolution: 'x' }, A.token);
- expect(closeWd.code, '已撤回不可 close→409, 实际=' + closeWd.code).toBe(409);
- expect(closeWd.message).toBe('工单已撤回,不可闭环');
- } finally {
- cleanupOrders([ordC, ordX, ordY]);
- }
- });
- // -------------------------------------------------------------------------
- // 权限流 TC-H-01-P / TC-H-04-P(家庭隔离 / 匿名降级)
- // -------------------------------------------------------------------------
- test('[权限] 跨家庭越界:detail 403「仅本家庭工单可查看」,写操作 403「仅本家庭工单可操作」', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-x-A');
- const B = await register(request, 'parent', 'esc-x-B');
- const m = uniq();
- const ord = seedPendingOrder(A.familyId, A.userId, 'HIGH', m + '-x');
- try {
- expect(A.familyId != null && B.familyId != null && A.familyId !== B.familyId, '两家庭隔离前提').toBe(true);
- const d = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ord }, B.token);
- expect(d.status, '越界 detail 不500').toBe(200);
- expect(d.code, '跨家庭 detail→403, 实际=' + d.code + ' msg=' + d.message).toBe(403);
- expect(d.message).toBe('仅本家庭工单可查看');
- const cases = [
- ['log-contact', { workorderId: ord, contactType: 'CALL', content: 'x' }],
- ['close', { workorderId: ord, resolution: 'x' }],
- ['transfer', { workorderId: ord, teacherId: B.userId }],
- ['withdraw', { workorderId: ord }],
- ];
- for (const [ep, body] of cases) {
- const r = await apiPost(request, '/api/butler/escalation/' + ep, body, B.token);
- expect(r.code, '跨家庭 ' + ep + '→403, 实际=' + r.code + ' msg=' + r.message).toBe(403);
- expect(r.message).toBe('仅本家庭工单可操作');
- }
- // 工单未被越界改写(状态/butler 归属不变)
- const dAfter = await apiPost(request, '/api/butler/escalation/detail', { workorderId: ord }, A.token);
- expect(dAfter.code, 'A 仍可查看').toBe(200);
- expect(dAfter.data.status, '越界操作未改状态').toBe('PENDING');
- } finally {
- cleanupOrders([ord]);
- }
- });
- test('[权限] 匿名降级:无 token → http=200 code=403(butler 族非 401 强制,真实契约)', async ({ request }) => {
- const A = await register(request, 'parent', 'esc-anon-A');
- const m = uniq();
- const ord = seedPendingOrder(A.familyId, A.userId, 'MEDIUM', m + '-a');
- try {
- const anon = await request.post('/api/butler/escalation/detail', { data: { workorderId: ord }, timeout: 30000 });
- expect(anon.status(), '匿名 detail http=200(降级匿名 userId=-1,非 401), 实际=' + anon.status()).toBe(200);
- const body = await anon.json().catch(() => ({}));
- expect(body.code, '匿名 detail→403, 实际=' + body.code + ' msg=' + body.message).toBe(403);
- expect(body.message).toBe('仅本家庭工单可查看');
- const anonW = await request.post('/api/butler/escalation/withdraw', { data: { workorderId: ord }, timeout: 30000 });
- const bodyW = await anonW.json().catch(() => ({}));
- expect(bodyW.code, '匿名 withdraw→403, 实际=' + bodyW.code).toBe(403);
- expect(bodyW.message).toBe('仅本家庭工单可操作');
- } finally {
- cleanupOrders([ord]);
- }
- });
- });
- // 安全约定:本文件不打印 token / 手机号;测试数据结束时清理(cleanupOrders)。
|