detail-navigation.test.mjs 6.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/queue?q=LG" }, "/work-orders", "返回工单列表").to, "/workspace/queue?q=LG");
  8. assert.equal(detailBack({ from: "/workspace/plan" }, "/orders", "返回订单管理").label, "返回我的套餐");
  9. // 页码在 URL 上:返回时连页带筛选一起送回(⛔ 退回第一页)
  10. assert.equal(detailBack({ from: "/customers?query=跨境&page=3" }, "/customers", "返回客户管理").to, "/customers?query=跨境&page=3");
  11. });
  12. test("合同、客户、订单详情逐级返回保留前一级历史", () => {
  13. const list = { from: "/contracts?q=CL" };
  14. const contract = detailLinkState("/contracts/HT-CL", list);
  15. const customer = detailLinkState("/customers/KL-1", contract);
  16. const back = detailBack(customer, "/orders", "返回订单管理");
  17. assert.equal(back.to, "/customers/KL-1");
  18. const previous = detailBack(back.state, "/customers", "返回客户清单");
  19. assert.equal(previous.to, "/contracts/HT-CL");
  20. assert.deepEqual(previous.state, list);
  21. });
  22. test("返回路径只接受已知业务列表与详情,拒绝外部或任意路径", () => {
  23. for (const from of ["https://example.com", "//example.com", "/login", "/workspace/other", "/contracts-evil?q=x", 123, null]) {
  24. assert.equal(detailBack({ from }, "/contracts", "返回合同管理").to, "/contracts");
  25. }
  26. });
  27. test("工单组四个新列表入口可返回并保留筛选,且不被当成详情", () => {
  28. const cases = [
  29. ["/work-orders/overview", "返回工单总览"],
  30. ["/work-orders/mine?type=fault", "返回我的待办"],
  31. ["/work-orders/queue?page=2", "返回待分派队列"],
  32. ["/work-orders/delivery?tab=rejected", "返回交付与回执"],
  33. ["/work-orders?status=processing&page=3", "返回工单列表"],
  34. ];
  35. for (const [from, label] of cases) {
  36. const back = detailBack({ from }, "/work-orders", "返回工单列表");
  37. assert.equal(back.to, from, from);
  38. assert.equal(back.label, label, from);
  39. }
  40. // 四个保留段不是详情:从总览进详情再「返回」应回总览,⛔ 反被当成「回工单详情」。
  41. for (const path of ["/work-orders/overview", "/work-orders/mine", "/work-orders/queue", "/work-orders/delivery"]) {
  42. const state = detailLinkState(path, null);
  43. assert.deepEqual(detailBack(state, "/work-orders", "返回工单列表").to, path, path);
  44. }
  45. // 真正的详情仍被识别:`/work-orders/1024` 返回后落在详情,而不是列表。
  46. const state = detailLinkState("/work-orders/1024", detailLinkState("/work-orders?page=2", null));
  47. assert.equal(detailBack(state, "/work-orders", "返回工单列表").to, "/work-orders/1024");
  48. });
  49. /**
  50. * 客户侧工单详情:客户的工单列表只有 `/workspace/progress` 一张。
  51. * 来路是运营台账(`/work-orders` 及四个子列表)时⛔ 送客户去那张表——列集与筛选器都不同,
  52. * 且旧实现正是这样让「返回工单列表」回到了另一张列表。
  53. */
  54. test("客户工单详情:来路是客户工单进度 ⇒ 原样带回筛选与页码", () => {
  55. const state = detailLinkState("/workspace/progress?q=办公&status=processing&page=2", null);
  56. assert.deepEqual(customerTicketBack(state), { to: "/workspace/progress?q=办公&status=processing&page=2", state: null, label: "返回工单进度" });
  57. });
  58. test("客户工单详情:来路是运营台账口径 ⇒ 改回客户自己的工单列表", () => {
  59. 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"]) {
  60. assert.deepEqual(customerTicketBack(detailLinkState(from, null)), { to: "/workspace/progress", state: null, label: "返回工单进度" }, from);
  61. }
  62. });
  63. test("客户工单详情:无来路或来路是套餐详情 ⇒ 不被改成工单列表", () => {
  64. assert.deepEqual(customerTicketBack(null), { to: "/workspace/progress", state: null, label: "返回工单进度" });
  65. assert.deepEqual(customerTicketBack(detailLinkState("/orders/KL-1", null)), { to: "/orders/KL-1", state: null, label: "返回订阅详情" });
  66. });
  67. const editor = (state) => formBack({ state, detail: "/customers/KL-1", detailLabel: "返回客户档案", list: "/customers", listLabel: "返回客户管理" });
  68. test("客户编辑页从客户清单进 ⇒ 返回清单并保留筛选与页码", () => {
  69. const entry = detailLinkState("/customers?query=跨境&status=正常&page=2", null);
  70. assert.deepEqual(editor(entry), { to: "/customers?query=跨境&status=正常&page=2", state: null, label: "返回客户管理" });
  71. });
  72. test("客户编辑页从客户档案进 ⇒ 返回档案,并带回档案自己的返回链", () => {
  73. // 清单 → 档案 → 编辑:返回档案后,档案的「返回」仍指向原来的清单(不是兜底路径)
  74. const detailState = detailLinkState("/customers?query=跨境", null);
  75. const entry = detailLinkState("/customers/KL-1", detailState);
  76. const back = editor(entry);
  77. assert.deepEqual(back, { to: "/customers/KL-1", state: detailState, label: "返回客户档案" });
  78. assert.deepEqual(detailBack(back.state, "/customers", "返回客户管理"), { to: "/customers?query=跨境", state: null, label: "返回客户管理" });
  79. });
  80. test("客户编辑页直接敲 URL 或无历史 ⇒ 兜底回客户档案", () => {
  81. for (const state of [null, undefined, {}, { from: "https://example.com" }, { from: "/customers-evil" }, { from: "/customers/KL-1/edit" }]) {
  82. assert.deepEqual(editor(state), { to: "/customers/KL-1", state: null, label: "返回客户档案" });
  83. }
  84. });