detail-navigation.test.mjs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { customerTicketBack, detailBack, detailLinkState, formBack } from "../src/lib/detail-navigation.ts";
  4. test("列表到详情再返回保留筛选条件与角色菜单名称", () => {
  5. assert.deepEqual(detailBack({ from: "/contracts?q=CL&status=生效中" }, "/contracts", "返回合同清单"), { to: "/contracts?q=CL&status=生效中", state: null, label: "返回合同清单" });
  6. assert.equal(detailBack({ from: "/workspace/progress?status=已完成" }, "/work-orders", "返回工单列表").label, "返回工单进度");
  7. assert.equal(detailBack({ from: "/workspace/plan" }, "/orders", "返回订单管理").label, "返回我的套餐");
  8. // 页码在 URL 上:返回时连页带筛选一起送回(⛔ 退回第一页)
  9. assert.equal(detailBack({ from: "/customers?query=跨境&page=3" }, "/customers", "返回客户管理").to, "/customers?query=跨境&page=3");
  10. });
  11. test("合同、客户、订单详情逐级返回保留前一级历史", () => {
  12. const list = { from: "/contracts?q=CL" };
  13. const contract = detailLinkState("/contracts/HT-CL", list);
  14. const customer = detailLinkState("/customers/KL-1", contract);
  15. const back = detailBack(customer, "/orders", "返回订单管理");
  16. assert.equal(back.to, "/customers/KL-1");
  17. const previous = detailBack(back.state, "/customers", "返回客户清单");
  18. assert.equal(previous.to, "/contracts/HT-CL");
  19. assert.deepEqual(previous.state, list);
  20. });
  21. test("返回路径只接受已知业务列表与详情,拒绝外部或任意路径", () => {
  22. for (const from of ["https://example.com", "//example.com", "/\\example.com", "/login", "/workspace/other", "/contracts-evil?q=x", "/contracts/%2e%2e", 123, null]) {
  23. assert.equal(detailBack({ from }, "/contracts", "返回合同管理").to, "/contracts");
  24. }
  25. });
  26. test("跨财务详情逐级返回原入口,并保留查询、锚点和页面状态", () => {
  27. const correction = detailLinkState("/correction-notes", { correctionView: { detailId: "C-9", page: 2 } });
  28. const bill = detailLinkState("/settlement-bills/B-1?month=2026-10#details", { ...correction, billTab: "reconciliation" });
  29. const batchBack = detailBack(bill, "/settlement-batches", "返回对账清单");
  30. assert.deepEqual(batchBack, {
  31. to: "/settlement-bills/B-1?month=2026-10#details",
  32. state: { ...correction, billTab: "reconciliation" },
  33. label: "返回账单明细详情",
  34. });
  35. assert.deepEqual(detailBack(batchBack.state, "/settlement-bills", "返回账单明细"), {
  36. to: "/correction-notes", state: { correctionView: { detailId: "C-9", page: 2 } }, label: "返回出账更正记录",
  37. });
  38. assert.equal(detailBack({ from: "/settlement-bills", listSearch: "?month=2026-10" }, "/settlement-bills", "返回账单明细").to, "/settlement-bills?month=2026-10");
  39. });
  40. test("团队账号、只读台账和移动端支持返回各自的入口", () => {
  41. for (const [from, label] of [
  42. ["/organization/maintenance-teams?region=R-1", "返回运维团队"],
  43. ["/organization/operations-teams/T-1/accounts", "返回运营团队账号"],
  44. ["/bos/pv/recon", "返回对账清单"],
  45. ["/bos/ov/bill-detail?batch=B-2", "返回账单明细"],
  46. ["/m/work-orders/T-1", "返回移动端工单详情"],
  47. ["/workspace/submit", "返回提交工单"],
  48. ]) {
  49. assert.deepEqual(detailBack(detailLinkState(from, null), "/work-orders", "返回工单列表"), { to: from, state: null, label });
  50. }
  51. });
  52. test("工单组四个新列表入口可返回并保留筛选,且不被当成详情", () => {
  53. const cases = [
  54. ["/work-orders/overview", "返回工单总览"],
  55. ["/work-orders/mine?type=fault", "返回我的待办"],
  56. ["/work-orders/queue?q=LG&page=2", "返回待分派队列"],
  57. ["/work-orders/delivery?tab=rejected", "返回交付与回执"],
  58. ["/work-orders?status=processing&page=3", "返回工单列表"],
  59. ];
  60. for (const [from, label] of cases) {
  61. const back = detailBack({ from }, "/work-orders", "返回工单列表");
  62. assert.equal(back.to, from, from);
  63. assert.equal(back.label, label, from);
  64. }
  65. // 四个保留段不是详情:从总览进详情再「返回」应回总览,⛔ 反被当成「回工单详情」。
  66. for (const path of ["/work-orders/overview", "/work-orders/mine", "/work-orders/queue", "/work-orders/delivery"]) {
  67. const state = detailLinkState(path, null);
  68. assert.deepEqual(detailBack(state, "/work-orders", "返回工单列表").to, path, path);
  69. }
  70. // 真正的详情仍被识别:`/work-orders/1024` 返回后落在详情,而不是列表。
  71. const state = detailLinkState("/work-orders/1024", detailLinkState("/work-orders?page=2", null));
  72. assert.equal(detailBack(state, "/work-orders", "返回工单列表").to, "/work-orders/1024");
  73. });
  74. /**
  75. * 客户侧工单详情:客户的工单列表只有 `/workspace/progress` 一张。
  76. * 来路是运营台账(`/work-orders` 及四个子列表)时⛔ 送客户去那张表——列集与筛选器都不同,
  77. * 且旧实现正是这样让「返回工单列表」回到了另一张列表。
  78. */
  79. test("客户工单详情:来路是客户工单进度 ⇒ 原样带回筛选与页码", () => {
  80. const state = detailLinkState("/workspace/progress?q=办公&status=processing&page=2", null);
  81. assert.deepEqual(customerTicketBack(state), { to: "/workspace/progress?q=办公&status=processing&page=2", state: null, label: "返回工单进度" });
  82. });
  83. test("客户工单详情:来路是运营台账口径 ⇒ 改回客户自己的工单列表", () => {
  84. for (const from of ["/work-orders", "/work-orders?status=processing&page=3", "/work-orders/overview", "/work-orders/mine?type=fault", "/work-orders/queue", "/work-orders/delivery?tab=rejected"]) {
  85. assert.deepEqual(customerTicketBack(detailLinkState(from, null)), { to: "/workspace/progress", state: null, label: "返回工单进度" }, from);
  86. }
  87. });
  88. test("客户工单详情:无来路或来路是套餐详情 ⇒ 不被改成工单列表", () => {
  89. assert.deepEqual(customerTicketBack(null), { to: "/workspace/progress", state: null, label: "返回工单进度" });
  90. assert.deepEqual(customerTicketBack(detailLinkState("/orders/KL-1", null)), { to: "/orders/KL-1", state: null, label: "返回订阅详情" });
  91. });
  92. const editor = (state) => formBack({ state, detail: "/customers/KL-1", detailLabel: "返回客户档案", list: "/customers", listLabel: "返回客户管理" });
  93. test("客户编辑页从客户清单进 ⇒ 返回清单并保留筛选与页码", () => {
  94. const entry = detailLinkState("/customers?query=跨境&status=正常&page=2", null);
  95. assert.deepEqual(editor(entry), { to: "/customers?query=跨境&status=正常&page=2", state: null, label: "返回客户管理" });
  96. });
  97. test("客户编辑页从客户档案进 ⇒ 返回档案,并带回档案自己的返回链", () => {
  98. // 清单 → 档案 → 编辑:返回档案后,档案的「返回」仍指向原来的清单(不是兜底路径)
  99. const detailState = detailLinkState("/customers?query=跨境", null);
  100. const entry = detailLinkState("/customers/KL-1", detailState);
  101. const back = editor(entry);
  102. assert.deepEqual(back, { to: "/customers/KL-1", state: detailState, label: "返回客户档案" });
  103. assert.deepEqual(detailBack(back.state, "/customers", "返回客户管理"), { to: "/customers?query=跨境", state: null, label: "返回客户管理" });
  104. });
  105. test("客户编辑页直接敲 URL 或无历史 ⇒ 兜底回客户档案", () => {
  106. for (const state of [null, undefined, {}, { from: "https://example.com" }, { from: "/customers-evil" }, { from: "/customers/KL-1/edit" }]) {
  107. assert.deepEqual(editor(state), { to: "/customers/KL-1", state: null, label: "返回客户档案" });
  108. }
  109. });