import test from "node:test"; import assert from "node:assert/strict"; import { dispatchTeamTickets, eligibleMembers, teamTickets } from "../src/lib/team-workspace.ts"; import { customerOwnsTicket, customerVisibleTickets, customerTicketAction, customerLiveTicketActions } from "../src/lib/customer-workspace.ts"; import { scopedCustomers, scopedTeamTickets, viewerParty } from "../src/lib/workspace-scope.ts"; import { memberCoveredIndexes } from "../src/lib/organization-demo.ts"; function fixture() { const team = { id: "team-a", ticketTypes: ["开户"], scope: { regions: ["r"], enterprises: [] }, members: [{ id: "a", name: "甲", ticketTypes: ["开户"], account: "在用", enterprises: [0] }, { id: "b", name: "乙", ticketTypes: ["开户"], account: "在用", enterprises: [0] }, { id: "c", name: "丙", ticketTypes: ["开户"], account: "停用", enterprises: [0] }, { id: "d", name: "丁", ticketTypes: ["故障"], account: "在用", enterprises: [0] }] }; const data = { regions: [{ id: "r", parent: null }], enterprises: [{ no: "customer-a", region: "r", teamIds: ["team-a"] }, { no: "customer-b", region: "outside", teamIds: ["team-a"] }] }; const ticket = (id, extra = {}) => ({ id, teamId: "team-a", type: "开户", enterpriseId: "customer-a", assigneeId: "", assigneeName: "", status: "待处理", handlingRecords: [], ...extra }); return { team, data, ticket }; } test("自动平均逐张重算负荷,固定顺序破同分,并保留历史和输入", () => { const { team, data, ticket } = fixture(); const records = [ticket("active-a", { assigneeId: "a", assigneeName: "甲", status: "处理中" }), ticket("1"), ticket("2"), ticket("3")]; const before = structuredClone(records); const result = dispatchTeamTickets(records, data, team, "孙队", "2026-10-27"); assert.equal(result.assigned, 3); assert.deepEqual(result.records.slice(1).map((item) => item.assigneeId), ["b", "a", "b"]); assert.equal(result.records[1].handlingRecords[0].actor, "孙队"); assert.ok(result.records.slice(1).every((item) => item.status === "待处理")); assert.deepEqual(records, before); }); test("团队队列保留同团队各类型,类型或范围不匹配时留队", () => { const { team, data, ticket } = fixture(); const records = [ticket("fault", { type: "故障" }), ticket("other-team", { teamId: "other" }), ticket("done", { status: "已完成" }), ticket("assigned", { assigneeId: "b", assigneeName: "乙" }), ticket("out-of-scope", { enterpriseId: "customer-b" }), ticket("unknown", { enterpriseId: "missing" })]; const result = dispatchTeamTickets(records, data, team, "孙队", "2026-10-27"); assert.equal(result.assigned, 0); assert.equal(result.skipped, 3); assert.equal(teamTickets(records, team).length, 5); assert.deepEqual(result.records, records); assert.deepEqual(eligibleMembers(data, team, ticket("normal")).map((item) => item.id), ["a", "b"]); }); test("手工分派只修改指定的未分派工单,不能分给不具备资格的成员", () => { const { team, data, ticket } = fixture(); const records = [ticket("1"), ticket("2")]; const rejected = dispatchTeamTickets(records, data, team, "主账号", "2026-10-27", { ticketId: "1", memberId: "c" }); assert.equal(rejected.assigned, 0); const result = dispatchTeamTickets(records, data, team, "主账号", "2026-10-27", { ticketId: "1", memberId: "b" }); assert.equal(result.records[0].assigneeId, "b"); assert.equal(result.records[1].assigneeId, ""); assert.equal(result.assigned, 1); }); test("成员三种负责范围均受团队范围和客户挂接限制", () => { const data = { regions: [{ id: "province", parent: null, name: "省", pathName: "全网/省" }, { id: "city", parent: "province", name: "市", pathName: "全网/省/市" }, { id: "outside", parent: null, name: "外", pathName: "全网/外" }], enterprises: [ { region: "city", teamIds: ["team-a"] }, { region: "city", teamIds: ["team-b"] }, { region: "outside", teamIds: ["team-a"] }, ], }; const team = { id: "team-a", scope: { regions: ["province"], enterprises: [] } }; assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "follow_team", enterprises: [] }), [0]); assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "region", scopeRegions: ["province"], enterprises: [] }), [0]); assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "customer", enterprises: [0, 1, 2] }), [0]); assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "region", scopeRegions: ["outside"], enterprises: [] }), []); }); test("树外挂载点按 region_path 前缀落入团队负责区域", () => { const data = { regions: [{ id: "province", parent: null, name: "上海", pathName: "全网/上海" }], enterprises: [ { region: "district-x", regionPath: "全网 / 上海 / 浦东", teamIds: ["team-a"] }, { region: "district-y", regionPath: "全网 / 江苏 / 南京", teamIds: ["team-a"] }, { region: "district-z", regionPath: "全网 / 上海 / 静安", teamIds: ["team-b"] }, ], }; const team = { id: "team-a", scope: { regions: ["province"], enterprises: [] } }; assert.deepEqual(memberCoveredIndexes(data, team, { scopeMode: "follow_team", enterprises: [] }), [0]); }); test("客户主账号只看本公司;分账号的新旧提交记录均只归本人", () => { const identity = { id: "zhang", role: "sub", customerNo: "customer-a", login: "a-1" }; const accounts = [{ login: "a-1" }, { login: "a-2" }]; const records = [ { id: "new-own", enterpriseId: "customer-a", submitterId: "zhang" }, { id: "new-other", enterpriseId: "customer-a", submitterId: "li" }, { id: "old-own", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 1" }, { id: "old-other", enterpriseId: "customer-a", submitter: "下属分账号", subAccountId: "分账号 2" }, { id: "master", enterpriseId: "customer-a", submitter: "客户主账号" }, { id: "outside", enterpriseId: "customer-b", submitterId: "zhang" }, ]; assert.deepEqual(customerVisibleTickets(records, identity, accounts).map((item) => item.id), ["new-own", "old-own"]); const master = { id: "master-a", role: "enterprise", customerNo: "customer-a" }; assert.equal(customerVisibleTickets(records, master, accounts).length, 5); assert.equal(customerOwnsTicket(records[0], master, accounts), false); assert.equal(customerOwnsTicket(records[4], master, accounts), true); assert.deepEqual(customerVisibleTickets(records, { ...identity, role: "operator" }, accounts), []); }); test("只读对账方按绑定与运营商取客户", () => { const customers = [{ no: "1", name: "同名", partner: "甲", operator: "上海市 / 联通", teams: [] }, { no: "2", name: "同名", partner: "乙", operator: "广东省 / 联通", teams: [] }, { no: "3", name: "唯一", partner: "甲", operator: "上海市 / 电信", teams: [] }]; const organization = { partners: [{ id: "p", name: "甲" }], operators: [{ id: "o", name: "联通", region: "sh" }], regions: [{ id: "sh", name: "上海市" }] }; const own = scopedCustomers(customers, { role: "partner", partnerId: "p" }, organization); assert.deepEqual(own.map((item) => item.no), ["1", "3"]); assert.deepEqual(scopedCustomers(customers, { role: "operator", operatorId: "o" }, organization).map((item) => item.no), ["1"]); assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).type, "运营商"); assert.equal(viewerParty({ role: "operator", operatorId: "o" }, organization).name, "上海市 / 联通"); }); test("运维主账号看本团队,成员只看本人,缺失归属时不放大", () => { const { data, team, ticket } = fixture(); const organization = { ...data, maintenanceTeams: [team] }; const records = [ticket("own", { assigneeId: "a" }), ticket("other-person", { assigneeId: "b" }), ticket("fault", { assigneeId: "a", type: "故障" }), ticket("other-team", { teamId: "other", assigneeId: "a" })]; assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: team.id, memberId: "a" }, organization).map((item) => item.id), ["own", "fault"]); assert.deepEqual(scopedTeamTickets(records, { role: "teamLead", maintenanceTeamId: team.id }, organization).map((item) => item.id), ["own", "other-person", "fault"]); assert.deepEqual(scopedTeamTickets(records, { role: "ops", maintenanceTeamId: "missing" }, organization), []); }); test("共用工单详情保留客户本人撤回及完成确认,拒绝跨人和错误状态", () => { const identity = { id: "zhang", name: "张敏", role: "sub", customerNo: "c", login: "c-1" }; const ticket = { id: "test", type: "故障", enterpriseId: "c", submitterId: "zhang", status: "处理中", closeReason: null, handlingRecords: [] }; const withdrawn = customerTicketAction(ticket, identity, [], "withdraw", "问题已解决", "2026-10-27"); assert.equal(withdrawn.status, "已关闭"); assert.equal(withdrawn.closeReason, "问题已解决"); assert.equal(ticket.status, "处理中"); const confirmed = customerTicketAction({ ...ticket, status: "已完成" }, identity, [], "confirm", "", "2026-10-27"); assert.equal(confirmed.status, "已办结"); assert.equal(confirmed.handlingRecords[0].actor, "张敏"); assert.throws(() => customerTicketAction(ticket, identity, [], "confirm", "", "2026-10-27")); assert.throws(() => customerTicketAction(ticket, { ...identity, id: "li" }, [], "withdraw", "申请撤回", "2026-10-27")); assert.throws(() => customerTicketAction(ticket, identity, [], "withdraw", " ", "2026-10-27")); assert.throws(() => customerTicketAction({ ...ticket, type: "开户" }, identity, [], "withdraw", "申请撤回", "2026-10-27")); }); /* ── 真实接口口径:客户侧「我的工单」这一行该给哪些操作(缺陷 P-21,判据=finalize.rs)── */ const liveRow = (extra = {}) => ({ status: "processing", ticket_type: "fault", submitter_type: "customer", ...extra }); test("客户自报的非主线单标完成后给「确认完成,关闭工单」,三档在办单给「撤回」", () => { assert.deepEqual(customerLiveTicketActions(liveRow({ status: "completed" })), { confirmClose: true, withdraw: false, needsDetail: false, blockedReason: null }); for (const status of ["unassigned", "pending", "processing"]) assert.deepEqual(customerLiveTicketActions(liveRow({ status })), { confirmClose: false, withdraw: true, needsDetail: false, blockedReason: null }); }); test("主线四类走竣工回执闭环,⛔ 在客户侧确认关闭", () => { for (const type of ["open", "service_open", "expiry_stop", "service_stop"]) { const gate = customerLiveTicketActions(liveRow({ status: "completed", ticket_type: type })); assert.equal(gate.confirmClose, false, type); assert.equal(gate.withdraw, false, type); assert.match(gate.blockedReason ?? "", /主线/); } // 主线单在办期间仍可撤回(闸门只看 submitter_type+状态,工单§5.1.9) assert.equal(customerLiveTicketActions(liveRow({ status: "processing", ticket_type: "open" })).withdraw, true); }); test("运维代建/系统生成/运营商报文的单⛔ 出现确认完成与撤回(标记完成即结束)", () => { for (const submitter of ["ops", "system", "unicom"]) { for (const status of ["completed", "processing", "settled", "terminated"]) { const gate = customerLiveTicketActions(liveRow({ status, submitter_type: submitter })); assert.equal(gate.confirmClose, false, `${submitter}/${status}`); assert.equal(gate.withdraw, false, `${submitter}/${status}`); assert.equal(gate.needsDetail, false, `${submitter}/${status}`); assert.ok(gate.blockedReason, `${submitter}/${status} 要给可见原因`); } } }); test("提交方未回传时两把⛔ 给,只报 needsDetail 让界面按需取一次详情", () => { // `read.rs` 的 LIST_COLS 不下发 submitter_type ⇒ 列表行到这里就是 undefined assert.deepEqual(customerLiveTicketActions({ status: "completed", ticket_type: "fault" }), { confirmClose: false, withdraw: false, needsDetail: true, blockedReason: null }); assert.deepEqual(customerLiveTicketActions(liveRow({ submitter_type: null })), { confirmClose: false, withdraw: false, needsDetail: true, blockedReason: null }); }); test("已办结/已终止的单没有客户操作,只给可见原因", () => { for (const status of ["settled", "terminated"]) { const gate = customerLiveTicketActions(liveRow({ status })); assert.equal(gate.confirmClose, false); assert.equal(gate.withdraw, false); assert.equal(gate.needsDetail, false); assert.ok(gate.blockedReason); } });