domain-foundation.test.mjs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { readFileSync } from "node:fs";
  4. import {
  5. getAnnualSubscriptionEndDate,
  6. getBillableStartDate,
  7. } from "../src/domain/subscription-dates.ts";
  8. import { canShowAction } from "../src/access/capabilities.ts";
  9. import { loginRoles } from "../src/access/roles.ts";
  10. import {
  11. accountStatuses,
  12. subscriptionStatuses,
  13. workOrderStatuses,
  14. } from "../src/domain/statuses.ts";
  15. import {
  16. IP_BANDWIDTH_TIERS,
  17. LINE_EXIT_MAX,
  18. CONTRACT_REQUIRED_FIELDS,
  19. appendLineExits,
  20. centsToYuanText,
  21. checkAmountCents,
  22. checkContractRequired,
  23. checkIpRows,
  24. checkItemExits,
  25. splitExitInput,
  26. yuanTextToCents,
  27. } from "../src/domain/contract-items.ts";
  28. test("年度订阅按下一周期生效日前一天结束,包括闰日边界", () => {
  29. assert.equal(getAnnualSubscriptionEndDate("2026-03-18"), "2027-03-17");
  30. assert.equal(getAnnualSubscriptionEndDate("2024-02-29"), "2025-02-27");
  31. assert.equal(getAnnualSubscriptionEndDate("0099-01-01"), "0099-12-31");
  32. assert.throws(() => getAnnualSubscriptionEndDate("2026-02-29"), RangeError);
  33. });
  34. test("正式应收从实际开通与合同生效的较晚日开始", () => {
  35. assert.equal(getBillableStartDate("2026-03-18", null), null);
  36. assert.equal(getBillableStartDate("2026-03-18", "2026-03-20"), "2026-03-20");
  37. assert.equal(getBillableStartDate("2026-03-18", "2026-03-15"), "2026-03-18");
  38. assert.throws(() => getBillableStartDate("2026-03-18", "2026-13-01"), RangeError);
  39. });
  40. test("首期状态字典沿用业务说明中的明确状态", () => {
  41. assert.deepEqual(accountStatuses, ["未开通", "在用", "停用"]);
  42. assert.deepEqual(subscriptionStatuses, ["待生效", "生效中", "已到期"]);
  43. assert.deepEqual(workOrderStatuses, ["待派单", "待处理", "处理中", "已完成", "已闭环", "已关闭"]);
  44. });
  45. test("角色能力按职责分开,运营商和代理不属于登录角色", () => {
  46. assert.equal(loginRoles.includes("operator"), false);
  47. assert.equal(loginRoles.includes("agency"), false);
  48. assert.equal(canShowAction("system-admin", "package.manage"), true);
  49. assert.equal(canShowAction("regional-partner", "package.manage"), false);
  50. assert.equal(canShowAction("regional-partner", "reconciliation.region.view"), true);
  51. assert.equal(canShowAction("regional-partner", "work-order.team.view"), false);
  52. assert.equal(canShowAction("operations-team", "work-order.team.reassign"), true);
  53. assert.equal(canShowAction("operations-team", "operations.result.record"), false);
  54. assert.equal(canShowAction("operations-member", "operations.result.record"), true);
  55. assert.equal(canShowAction("operations-member", "settlement-scope.manage"), false);
  56. });
  57. test("线路出口目录的录入辅助:分隔、去重、8 个上限", () => {
  58. assert.deepEqual(splitExitInput(" 美国、日本 ,, 韩国 、"), ["美国", "日本", "韩国"]);
  59. assert.deepEqual(appendLineExits(["美国"], [" 美国 ", "US", "德国"]).exits, ["美国", "US", "德国"]);
  60. const repeated = appendLineExits(["usa", "美国"], ["USA", "美 国", " 美国"]);
  61. assert.deepEqual(repeated.exits, ["usa", "美国"]);
  62. assert.equal(repeated.added, 0);
  63. assert.equal(repeated.duplicated, 3);
  64. const overflow = appendLineExits(["1", "2", "3", "4", "5", "6", "7"], ["7", "8", "9"]);
  65. assert.equal(overflow.exits.length, LINE_EXIT_MAX);
  66. assert.equal(overflow.added, 1);
  67. assert.equal(overflow.overflow, 1);
  68. });
  69. test("IP 条目要凑齐三元组,带宽档位只认 5/10/30 兆三值", () => {
  70. assert.deepEqual([...IP_BANDWIDTH_TIERS], [5, 10, 30]);
  71. assert.equal(checkIpRows([], false), null);
  72. assert.match(checkIpRows([], true) ?? "", /至少添加一条/);
  73. assert.equal(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "5", ip_count: "3" }], true), null);
  74. assert.match(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "6", ip_count: "0" }], true) ?? "", /IP 条目 1 缺:带宽档位.*IP 数量/);
  75. assert.match(checkIpRows([{ line_exit: " ", bandwidth_tier_mbps: "30", ip_count: "1" }], true) ?? "", /线路出口/);
  76. assert.match(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "5", ip_count: "1" }], false) ?? "", /未勾选/);
  77. });
  78. test("出口提交校验对两种形态同一条:必须从目录选择 1–8 个", () => {
  79. const planBased = appendLineExits([], splitExitInput("中国香港、美国")).exits;
  80. assert.deepEqual(planBased, ["中国香港", "美国"]);
  81. assert.equal(checkItemExits(planBased), null);
  82. assert.equal(checkItemExits(["美国"]), null);
  83. assert.match(checkItemExits([]) ?? "", /请从套餐配置选择 1–8 个线路出口/);
  84. assert.match(checkItemExits(Array.from({ length: LINE_EXIT_MAX + 1 }, (_, index) => `出口${index}`)) ?? "", /最多 8 个,当前 9 个/);
  85. });
  86. test("线路出口统一在套餐配置中配置,合同页只做选择(⛔ 自由录入)", () => {
  87. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  88. assert.match(contract, /toggleExit/);
  89. assert.equal(contract.includes("exitInput"), false);
  90. assert.equal(contract.includes("commitExits"), false);
  91. assert.match(contract, /useLineExits\(\)/);
  92. const catalog = readFileSync(new URL("../src/pages/PackageManagementPage.tsx", import.meta.url), "utf8");
  93. assert.match(catalog, /lineExitSave/);
  94. assert.match(catalog, /useLineExits\(\)/);
  95. assert.equal(catalog.includes("出口不在目录侧登记"), false);
  96. });
  97. test("合同金额走整数分:元⇄分⛔ 浮点误差,0 元与非法格式拦下", () => {
  98. assert.equal(yuanTextToCents("328.53"), 32853);
  99. assert.equal(yuanTextToCents("0.07"), 7);
  100. assert.equal(yuanTextToCents("1920"), 192000);
  101. assert.equal(yuanTextToCents(" 12.3 "), 1230);
  102. assert.equal(yuanTextToCents("1.999"), null);
  103. assert.equal(yuanTextToCents("1e3"), null);
  104. assert.equal(centsToYuanText(32853), "328.53");
  105. assert.equal(centsToYuanText(7), "0.07");
  106. assert.equal(centsToYuanText(192000), "1920.00");
  107. assert.equal(centsToYuanText(0), "0.00");
  108. for (const cents of [1, 7, 99, 100, 32853, 900719925474088]) {
  109. assert.equal(yuanTextToCents(centsToYuanText(cents)), cents);
  110. }
  111. assert.match(checkAmountCents("") ?? "", /请填写合同金额/);
  112. assert.match(checkAmountCents("0") ?? "", /须大于 0/);
  113. assert.match(checkAmountCents("0.00") ?? "", /须大于 0/);
  114. assert.match(checkAmountCents("-5") ?? "", /格式不对/);
  115. assert.match(checkAmountCents("1.999") ?? "", /格式不对/);
  116. assert.equal(checkAmountCents("1920.00"), null);
  117. });
  118. test("新建合同必填 5 项按后端短路顺序逐字段拦人,提示语照抄 `60004` 原话", () => {
  119. const filled = { payCycle: "yearly", amount: "1920.00", agent: "临港联通 · 张工", agentPhone: "13900000101", handoverOn: "2026-11-14" };
  120. assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.key), ["payCycle", "amount", "agent", "agentPhone", "handoverOn"]);
  121. assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.message), [
  122. "请选择付款周期",
  123. "请填写合同金额",
  124. "请填写运营商经办人姓名",
  125. "请填写经办人联系电话",
  126. "请选择交接单日期",
  127. ]);
  128. assert.equal(checkContractRequired(filled), null);
  129. /** 全空时只能报出后端第一个短路的字段(付款周期),⛔ 一句话把五项混在一起。 */
  130. const allEmpty = { payCycle: "", amount: "", agent: "", agentPhone: "", handoverOn: "" };
  131. assert.deepEqual(checkContractRequired(allEmpty), { key: "payCycle", message: "请选择付款周期" });
  132. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly" }), { key: "amount", message: "请填写合同金额" });
  133. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1" }), { key: "agent", message: "请填写运营商经办人姓名" });
  134. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", agent: "张工" }), { key: "agentPhone", message: "请填写经办人联系电话" });
  135. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", agent: "张工", agentPhone: "13900000101" }), { key: "handoverOn", message: "请选择交接单日期" });
  136. /** 金额非空但非法 ⇒ 同一字段换成更具体的口径(0 元不是合法合同金额)。 */
  137. const payCycleOk = { ...allEmpty, payCycle: "quarterly" };
  138. assert.deepEqual(checkContractRequired({ ...payCycleOk, amount: "0" }), { key: "amount", message: "合同金额须大于 0(0 元不是合法合同金额)" });
  139. assert.match(checkContractRequired({ ...payCycleOk, amount: "1.999" })?.message ?? "", /格式不对/);
  140. /** `agent_org`(经办人机构)实测选填 ⇒ ⛔ 出现在必填清单里。 */
  141. assert.equal(Object.keys(allEmpty).includes("agentOrg"), false);
  142. assert.equal(CONTRACT_REQUIRED_FIELDS.some((field) => field.key === "agentOrg" || field.label.includes("机构")), false);
  143. });
  144. test("合同页载荷补齐后端必填键,命中的必填项红显该字段", () => {
  145. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  146. assert.match(contract, /contract_amount_cents: amountCents,/);
  147. assert.match(contract, /pay_cycle: draft\.payCycle,/);
  148. assert.match(contract, /agent_mobile: draft\.agentPhone\.trim\(\),/);
  149. assert.match(contract, /agent_name: draft\.agent\.trim\(\),/);
  150. assert.match(contract, /agent_handover_on: draft\.handoverOn,/);
  151. assert.match(contract, /agent_org: draft\.agentOrg\.trim\(\),/);
  152. assert.match(contract, /const missing = checkContractRequired\(\{[\s\S]{0,160}\}\);[\s\S]{0,60}setFieldError\(missing\)/);
  153. for (const key of ["payCycle", "amount", "agent", "agentPhone", "handoverOn"]) {
  154. assert.match(contract, new RegExp(`aria-invalid=\\{invalidOf\\("${key}"\\)\\}`));
  155. assert.ok(contract.includes(`{requiredErrorOf("${key}")}`), `字段 ${key} 缺原话红显`);
  156. }
  157. /** ⛔ 金额再走浮点乘;⛔ 把经办人机构当必填拦人。 */
  158. assert.equal(/contract_amount_cents: yuanToCents/.test(contract), false);
  159. assert.equal(/checkContractRequired\([\s\S]{0,200}agentOrg/.test(contract), false);
  160. });