import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import vm from "node:vm"; import { demoIdentities, findDemoIdentity } from "../src/lib/demo-identities.ts"; import { getDemoNavigation, getDemoLandingPath, getActiveDemoNavigation } from "../src/access/demo-navigation.ts"; const prototype = readFileSync(new URL("../prototype/app.js", import.meta.url), "utf8"); const navSource = prototype.slice(prototype.indexOf("const NAV_ADMIN ="), prototype.indexOf("function firstRoute(")); const context = vm.createContext({}); vm.runInContext(navSource, context); function prototypeMenu(identity) { context.identity = identity; const source = vm.runInContext("navFor(identity)", context); const rows = []; for (const item of source) { if (item.length === 2) rows.push({ title: item[1], section: "" }); else for (const child of item[2]) rows.push({ title: child[1], section: item[1] }); } // 工单组改造:2026-11-17「工单与开停」→「工单与任务」,拆成五个入口;「定时任务」摘为独立菜单。 // 原型仍是旧结构(工单管理+定时任务同组),这里做一次等价映射后再逐项比对。 const workOrders = rows.findIndex((item) => item.title === "工单管理" && item.section === "工单与开停"); if (workOrders >= 0) { rows.splice(workOrders, 1, { title: "工单总览", section: "工单与任务" }, { title: "我的待办", section: "工单与任务" }, { title: "全部工单", section: "工单与任务" }, { title: "待分派", section: "工单与任务" }, { title: "交付与回执", section: "工单与任务" }, { title: "工单配置", section: "工单与任务" }, ); const daily = rows.findIndex((item) => item.title === "定时任务"); if (daily >= 0) rows.splice(daily, 1); const audit = rows.findIndex((item) => item.title === "审计日志"); rows.splice(audit, 0, { title: "定时任务", section: "" }); } // 2026-11-15 业主裁定:页面更名「套餐配置」(原「套餐目录」),原型仍留旧名。 for (const row of rows) if (row.title === "套餐目录") row.title = "套餐配置"; // 2026-11-16 定案:口径回退——实体名恢复「订阅」(与原型/后端 `subscription` 一致), // ⛔ 不再做「订阅→订单」的折算;菜单名一并回到原型原名「全部订阅」,⛔ 无需任何映射。 // 第 8 册 §3.4 已取代旧原型的三身份菜单;第 10 册读口未上线前不渲染变更记录。 // 2026-11-17 工单组按角色分口径(原菜单只有一条「工单管理」):运维团队主先清队列, // 运维成员只关心「我的待办」,故这两档直接给期望菜单,不再从原型推导。 if (identity.role === "teamLead") return [ { title: "待分派", section: "" }, { title: "我的待办", section: "" }, { title: "工单总览", section: "" }, { title: "全部工单", section: "" }, { title: "交付与回执", section: "" }, { title: "团队客户", section: "团队管理" }, { title: "成员与负责范围", section: "团队管理" }, { title: "分派方式", section: "团队管理" }, { title: "本方对账", section: "对账与分成" }, ]; if (identity.role === "ops" || identity.role === "worker") return [ { title: "我的待办", section: "" }, { title: "全部工单", section: "" }, ]; if (identity.role === "partner") return [ { title: "本区域对账", section: "对账与分成" }, { title: "客户清单", section: "客户与合同" }, { title: "合同清单", section: "客户与合同" }, ]; if (identity.role === "operator") return [ { title: "本方对账清单", section: "本方对账" }, { title: "账单明细", section: "本方对账" }, { title: "客户清单", section: "客户与合同" }, { title: "合同清单", section: "客户与合同" }, ]; return rows; } test("13 个身份的菜单名称、顺序和分组符合原型及第 8 册现行契约", () => { for (const identity of demoIdentities) { const actual = getDemoNavigation(identity); assert.deepEqual(actual.map(({ title, section }) => ({ title, section })), prototypeMenu(identity), identity.name); assert.equal(new Set(actual.map((item) => item.path)).size, actual.length, identity.name); assert.equal(new Set(actual.map((item) => item.id)).size, actual.length, identity.name); } }); test("主子账号差异及角色首屏遵循原型,不落到总运营概览", () => { const menu = (name) => getDemoNavigation(findDemoIdentity(name)); assert.equal(menu("林管").length, 25); assert.equal(menu("周运营").length, 25); assert.equal(menu("许芸").some((item) => item.section === "组织与区域"), false); assert.equal(menu("张敏").some((item) => item.id === "accounts"), false); assert.deepEqual(menu("孙队"), menu("吴队")); for (const name of ["周宁", "赵工"]) assert.deepEqual(menu(name).map((item) => item.title), ["我的待办", "全部工单"]); for (const identity of demoIdentities) assert.equal(getDemoLandingPath(identity), getDemoNavigation(identity)[0].path); assert.equal(getDemoLandingPath(findDemoIdentity("张敏")), "/workspace/plan"); // 运维成员落地「我的待办」,团队主落地「待分派」:⛔ 都回到全量台账自己捞。 assert.equal(getDemoLandingPath(findDemoIdentity("周宁")), "/work-orders/mine"); assert.equal(getDemoLandingPath(findDemoIdentity("赵工")), "/work-orders/mine"); assert.equal(getDemoLandingPath(findDemoIdentity("吴队")), "/work-orders/queue"); assert.equal(getDemoLandingPath(findDemoIdentity("孙队")), "/work-orders/queue"); }); test("第 8 册详情路由可高亮正确菜单,旧运营侧路由不越权高亮", () => { const partner = getDemoNavigation(findDemoIdentity("邹合")); assert.equal(getActiveDemoNavigation(partner, "/bos/pv/bill-detail/bill-1").id, "region"); assert.equal(getActiveDemoNavigation(partner, "/bos/pv/customer/KL-2026-0001").id, "enterprises"); assert.equal(getActiveDemoNavigation(partner, "/settlement-batches/pt-0/2026-10"), undefined); assert.equal(getActiveDemoNavigation(partner, "/customers-other"), undefined); const admin = getDemoNavigation(findDemoIdentity("林管")); assert.equal(getActiveDemoNavigation(admin, "/organization/maintenance-teams/mt-0/accounts").id, "maintenanceTeams"); assert.equal(getActiveDemoNavigation(admin, "/").id, "home"); }); test("工单组五个入口各自高亮,详情落回全部工单,「定时任务」不在组内", () => { const admin = getDemoNavigation(findDemoIdentity("林管")); assert.equal(getActiveDemoNavigation(admin, "/work-orders/overview").id, "workOrderOverview"); assert.equal(getActiveDemoNavigation(admin, "/work-orders/queue").id, "workOrderQueue"); assert.equal(getActiveDemoNavigation(admin, "/work-orders/delivery").id, "workOrderDelivery"); assert.equal(getActiveDemoNavigation(getDemoNavigation(findDemoIdentity("周宁")), "/work-orders/mine").id, "workOrderMine"); assert.equal(getActiveDemoNavigation(admin, "/work-orders").id, "workOrders"); // 详情是 `/work-orders/:id`:共享前缀下必须靠最长前缀规则落到「全部工单」。 assert.equal(getActiveDemoNavigation(admin, "/work-orders/1024").id, "workOrders"); const group = admin.filter((item) => item.section === "工单与任务").map((item) => item.title); assert.deepEqual(group, ["工单总览", "我的待办", "全部工单", "待分派", "交付与回执", "工单配置"]); const daily = admin.find((item) => item.id === "dailyTasks"); assert.equal(daily.section, ""); // 独立菜单排在工单组之后、审计日志之前。 assert.ok(admin.indexOf(daily) > admin.findIndex((item) => item.id === "workOrderSettings")); assert.ok(admin.indexOf(daily) < admin.findIndex((item) => item.id === "auditLogs")); }); test("每个菜单有现成路由或专属工作台页面,避免死链接", () => { const appSource = readFileSync(new URL("../src/app/App.tsx", import.meta.url), "utf8"); const routePaths = Array.from(appSource.matchAll(/ match[1]); for (const identity of demoIdentities) for (const item of getDemoNavigation(identity)) { if (item.workspace) { assert.ok(item.path.startsWith("/workspace/")); assert.ok(routePaths.includes("/workspace/:page")); } else assert.ok(routePaths.includes(item.path), `${identity.name}: ${item.path}`); } });