demo-navigation.test.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { readFileSync } from "node:fs";
  4. import vm from "node:vm";
  5. import { demoIdentities, findDemoIdentity } from "../src/lib/demo-identities.ts";
  6. import { getDemoNavigation, getDemoLandingPath, getActiveDemoNavigation } from "../src/access/demo-navigation.ts";
  7. const prototype = readFileSync(new URL("../prototype/app.js", import.meta.url), "utf8");
  8. const navSource = prototype.slice(prototype.indexOf("const NAV_ADMIN ="), prototype.indexOf("function firstRoute("));
  9. const context = vm.createContext({});
  10. vm.runInContext(navSource, context);
  11. function prototypeMenu(identity) {
  12. context.identity = identity;
  13. const source = vm.runInContext("navFor(identity)", context);
  14. const rows = [];
  15. for (const item of source) {
  16. if (item.length === 2) rows.push({ title: item[1], section: "" });
  17. else for (const child of item[2]) rows.push({ title: child[1], section: item[1] });
  18. }
  19. // 工单配置来自新版原型,其余菜单仍按旧版基线核对。
  20. const workOrders = rows.findIndex((item) => item.title === "工单管理" && item.section === "工单与开停");
  21. if (workOrders >= 0) rows.splice(workOrders + 1, 0, { title: "工单配置", section: "工单与开停" });
  22. // 2026-11-15 业主裁定:页面更名「套餐配置」(原「套餐目录」),原型仍留旧名。
  23. for (const row of rows) if (row.title === "套餐目录") row.title = "套餐配置";
  24. // 2026-11-16 定案:口径回退——实体名恢复「订阅」(与原型/后端 `subscription` 一致),
  25. // ⛔ 不再做「订阅→订单」的折算;台账页由「全部订阅」更名「订阅台账」(需求 5.2 只读 + 名称回归台账口径)。
  26. for (const row of rows) if (row.title === "全部订阅") row.title = "订阅台账";
  27. // 第 8 册 §3.4 已取代旧原型的三身份菜单;第 10 册读口未上线前不渲染变更记录。
  28. if (identity.role === "teamLead") {
  29. const recon = rows.findIndex((item) => item.title === "对账清单");
  30. rows.splice(recon, 2, { title: "本方对账", section: "对账与分成" });
  31. }
  32. if (identity.role === "partner") return [
  33. { title: "本区域对账", section: "对账与分成" },
  34. { title: "客户清单", section: "客户与合同" },
  35. { title: "合同清单", section: "客户与合同" },
  36. ];
  37. if (identity.role === "operator") return [
  38. { title: "本方对账清单", section: "本方对账" },
  39. { title: "账单明细", section: "本方对账" },
  40. { title: "客户清单", section: "客户与合同" },
  41. { title: "合同清单", section: "客户与合同" },
  42. ];
  43. return rows;
  44. }
  45. test("13 个身份的菜单名称、顺序和分组符合原型及第 8 册现行契约", () => {
  46. for (const identity of demoIdentities) {
  47. const actual = getDemoNavigation(identity);
  48. assert.deepEqual(actual.map(({ title, section }) => ({ title, section })), prototypeMenu(identity), identity.name);
  49. assert.equal(new Set(actual.map((item) => item.path)).size, actual.length, identity.name);
  50. assert.equal(new Set(actual.map((item) => item.id)).size, actual.length, identity.name);
  51. }
  52. });
  53. test("主子账号差异及角色首屏遵循原型,不落到总运营概览", () => {
  54. const menu = (name) => getDemoNavigation(findDemoIdentity(name));
  55. assert.equal(menu("林管").length, 21);
  56. assert.equal(menu("周运营").length, 21);
  57. assert.equal(menu("许芸").some((item) => item.section === "组织与区域"), false);
  58. assert.equal(menu("张敏").some((item) => item.id === "accounts"), false);
  59. assert.deepEqual(menu("孙队"), menu("吴队"));
  60. for (const name of ["周宁", "赵工"]) assert.deepEqual(menu(name).map((item) => item.title), ["工单管理"]);
  61. for (const identity of demoIdentities) assert.equal(getDemoLandingPath(identity), getDemoNavigation(identity)[0].path);
  62. assert.equal(getDemoLandingPath(findDemoIdentity("张敏")), "/workspace/plan");
  63. assert.equal(getDemoLandingPath(findDemoIdentity("周宁")), "/work-orders");
  64. });
  65. test("第 8 册详情路由可高亮正确菜单,旧运营侧路由不越权高亮", () => {
  66. const partner = getDemoNavigation(findDemoIdentity("邹合"));
  67. assert.equal(getActiveDemoNavigation(partner, "/bos/pv/bill-detail/bill-1").id, "region");
  68. assert.equal(getActiveDemoNavigation(partner, "/bos/pv/customer/KL-2026-0001").id, "enterprises");
  69. assert.equal(getActiveDemoNavigation(partner, "/settlement-batches/pt-0/2026-10"), undefined);
  70. assert.equal(getActiveDemoNavigation(partner, "/customers-other"), undefined);
  71. const admin = getDemoNavigation(findDemoIdentity("林管"));
  72. assert.equal(getActiveDemoNavigation(admin, "/organization/maintenance-teams/mt-0/accounts").id, "maintenanceTeams");
  73. assert.equal(getActiveDemoNavigation(admin, "/").id, "home");
  74. });
  75. test("每个菜单有现成路由或专属工作台页面,避免死链接", () => {
  76. const appSource = readFileSync(new URL("../src/app/App.tsx", import.meta.url), "utf8");
  77. const routePaths = Array.from(appSource.matchAll(/<Route\s+path="([^"]+)"/g), (match) => match[1]);
  78. for (const identity of demoIdentities) for (const item of getDemoNavigation(identity)) {
  79. if (item.workspace) {
  80. assert.ok(item.path.startsWith("/workspace/"));
  81. assert.ok(routePaths.includes("/workspace/:page"));
  82. } else assert.ok(routePaths.includes(item.path), `${identity.name}: ${item.path}`);
  83. }
  84. });