domain-foundation.test.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { readFileSync } from "node:fs";
  4. import {
  5. getAnnualOrderEndDate,
  6. getBillableStartDate,
  7. } from "../src/domain/order-dates.ts";
  8. import { canShowAction } from "../src/access/capabilities.ts";
  9. import { loginRoles } from "../src/access/roles.ts";
  10. import {
  11. accountStatuses,
  12. orderStatuses,
  13. workOrderStatuses,
  14. } from "../src/domain/statuses.ts";
  15. import {
  16. LINE_EXIT_MAX,
  17. CONTRACT_REQUIRED_FIELDS,
  18. appendLineExits,
  19. centsToYuanText,
  20. checkAmountCents,
  21. checkContractRequired,
  22. checkItemExits,
  23. splitExitInput,
  24. yuanTextToCents,
  25. } from "../src/domain/contract-items.ts";
  26. test("年度订单按下一周期生效日前一天结束,包括闰日边界", () => {
  27. assert.equal(getAnnualOrderEndDate("2026-03-18"), "2027-03-17");
  28. assert.equal(getAnnualOrderEndDate("2024-02-29"), "2025-02-27");
  29. assert.equal(getAnnualOrderEndDate("0099-01-01"), "0099-12-31");
  30. assert.throws(() => getAnnualOrderEndDate("2026-02-29"), RangeError);
  31. });
  32. test("正式应收从实际开通与合同生效的较晚日开始", () => {
  33. assert.equal(getBillableStartDate("2026-03-18", null), null);
  34. assert.equal(getBillableStartDate("2026-03-18", "2026-03-20"), "2026-03-20");
  35. assert.equal(getBillableStartDate("2026-03-18", "2026-03-15"), "2026-03-18");
  36. assert.throws(() => getBillableStartDate("2026-03-18", "2026-13-01"), RangeError);
  37. });
  38. test("首期状态字典沿用业务说明中的明确状态", () => {
  39. assert.deepEqual(accountStatuses, ["未开通", "在用", "停用"]);
  40. assert.deepEqual(orderStatuses, ["待生效", "生效中", "已到期"]);
  41. assert.deepEqual(workOrderStatuses, ["待派单", "待处理", "处理中", "已完成", "已闭环", "已关闭"]);
  42. });
  43. test("角色能力按职责分开,运营商和代理不属于登录角色", () => {
  44. assert.equal(loginRoles.includes("operator"), false);
  45. assert.equal(loginRoles.includes("agency"), false);
  46. assert.equal(canShowAction("system-admin", "package.manage"), true);
  47. assert.equal(canShowAction("regional-partner", "package.manage"), false);
  48. assert.equal(canShowAction("regional-partner", "reconciliation.region.view"), true);
  49. assert.equal(canShowAction("regional-partner", "work-order.team.view"), false);
  50. assert.equal(canShowAction("operations-team", "work-order.team.reassign"), true);
  51. assert.equal(canShowAction("operations-team", "operations.result.record"), false);
  52. assert.equal(canShowAction("operations-member", "operations.result.record"), true);
  53. assert.equal(canShowAction("operations-member", "settlement-scope.manage"), false);
  54. });
  55. test("线路出口目录的录入辅助:分隔、去重、8 条上限", () => {
  56. assert.deepEqual(splitExitInput(" 美国、日本 ,, 韩国 、"), ["美国", "日本", "韩国"]);
  57. assert.deepEqual(appendLineExits(["美国"], [" 美国 ", "US", "德国"], 8).exits, ["美国", "US", "德国"]);
  58. const repeated = appendLineExits(["usa", "美国"], ["USA", "美 国", " 美国"], 8);
  59. assert.deepEqual(repeated.exits, ["usa", "美国"]);
  60. assert.equal(repeated.added, 0);
  61. assert.equal(repeated.duplicated, 3);
  62. const overflow = appendLineExits(["1", "2", "3", "4", "5", "6", "7"], ["7", "8", "9"], 8);
  63. assert.equal(overflow.exits.length, 8);
  64. assert.equal(overflow.added, 1);
  65. assert.equal(overflow.overflow, 1);
  66. // 条目级缺省上限=1(2026-11-17 出口改单选)。
  67. assert.deepEqual(appendLineExits([], ["中国香港", "美国"]).exits, ["中国香港"]);
  68. });
  69. test("合同级固定 IP 条目已下线(2026-11-17):不再导出三元组校验口径", async () => {
  70. // 境外 IP 改由「套餐档位 × 订阅份数」表达 ⇒ 合同不再登记 (出口, 档位, 数量) 三元组;
  71. // 对应 `checkIpRows`/`checkIpExitWithinItems`/`IP_BANDWIDTH_TIERS` 一并退役。
  72. const mod = await import("../src/domain/contract-items.ts");
  73. for (const name of ["checkIpRows", "checkIpExitWithinItems", "IP_BANDWIDTH_TIERS"]) {
  74. assert.equal(name in mod, false, `${name} 应已随配置下线从模块导出中移除`);
  75. }
  76. });
  77. test("出口提交校验:条目只能选 1 个(2026-11-17 由多选改单选)", () => {
  78. assert.equal(LINE_EXIT_MAX, 1);
  79. assert.equal(checkItemExits(["美国"]), null);
  80. assert.match(checkItemExits([]) ?? "", /请从套餐配置选择 1 个线路出口/);
  81. const multi = appendLineExits([], splitExitInput("中国香港、美国"), 8).exits;
  82. assert.deepEqual(multi, ["中国香港", "美国"]);
  83. assert.match(checkItemExits(multi) ?? "", /线路出口为单选:当前有 2 个.*只保留 1 个/);
  84. });
  85. test("线路出口统一在套餐配置中配置,合同页只做选择(⛔ 自由录入)", () => {
  86. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  87. assert.match(contract, /toggleExit/);
  88. assert.equal(contract.includes("exitInput"), false);
  89. assert.equal(contract.includes("commitExits"), false);
  90. assert.match(contract, /useLineExits\(\)/);
  91. const catalog = readFileSync(new URL("../src/pages/PackageManagementPage.tsx", import.meta.url), "utf8");
  92. assert.match(catalog, /lineExitSave/);
  93. assert.match(catalog, /useLineExits\(\)/);
  94. assert.equal(catalog.includes("出口不在目录侧登记"), false);
  95. });
  96. test("带宽档位=套餐带宽权益的快捷预设(2026-11-17 改用途,⛔ 合同侧不再选档)", () => {
  97. const catalog = readFileSync(new URL("../src/pages/PackageManagementPage.tsx", import.meta.url), "utf8");
  98. /** ① 档位清单的下拉顺序已不再服务合同页。 */
  99. assert.equal(catalog.includes("合同页下拉顺序"), false);
  100. /** ② 固定档/自定义档编辑器共用同一个带宽档位快捷控件。 */
  101. assert.match(catalog, /tiers=\{tiers\}/);
  102. assert.match(catalog, /catalog-bandwidth-presets/);
  103. assert.match(catalog, /applyTier/);
  104. /** ③ 合同页不再出现档位快捷(合同只按套餐与份数登记)。 */
  105. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  106. assert.equal(contract.includes("catalog-bandwidth-preset"), false);
  107. });
  108. test("合同金额走整数分:元⇄分⛔ 浮点误差,0 元与非法格式拦下", () => {
  109. assert.equal(yuanTextToCents("328.53"), 32853);
  110. assert.equal(yuanTextToCents("0.07"), 7);
  111. assert.equal(yuanTextToCents("1920"), 192000);
  112. assert.equal(yuanTextToCents(" 12.3 "), 1230);
  113. assert.equal(yuanTextToCents("1.999"), null);
  114. assert.equal(yuanTextToCents("1e3"), null);
  115. assert.equal(centsToYuanText(32853), "328.53");
  116. assert.equal(centsToYuanText(7), "0.07");
  117. assert.equal(centsToYuanText(192000), "1920.00");
  118. assert.equal(centsToYuanText(0), "0.00");
  119. for (const cents of [1, 7, 99, 100, 32853, 900719925474088]) {
  120. assert.equal(yuanTextToCents(centsToYuanText(cents)), cents);
  121. }
  122. assert.match(checkAmountCents("") ?? "", /请填写合同金额/);
  123. assert.match(checkAmountCents("0") ?? "", /须大于 0/);
  124. assert.match(checkAmountCents("0.00") ?? "", /须大于 0/);
  125. assert.match(checkAmountCents("-5") ?? "", /格式不对/);
  126. assert.match(checkAmountCents("1.999") ?? "", /格式不对/);
  127. assert.equal(checkAmountCents("1920.00"), null);
  128. });
  129. test("新建合同必填 6 项按后端短路顺序逐字段拦人,提示语照抄 `60004` 原话", () => {
  130. const filled = { payCycle: "yearly", amount: "1920.00", start: "2026-11-14", agent: "临港联通 · 张工", agentPhone: "13900000101", handoverOn: "2026-11-14" };
  131. assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.key), ["payCycle", "amount", "start", "agent", "agentPhone", "handoverOn"]);
  132. assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.message), [
  133. "请选择付款周期",
  134. "请填写合同金额",
  135. "请选择生效日",
  136. "请填写运营商经办人姓名",
  137. "请填写经办人联系电话",
  138. "请选择交接日期",
  139. ]);
  140. assert.equal(checkContractRequired(filled), null);
  141. /** 全空时只能报出后端第一个短路的字段(付款周期),⛔ 一句话把六项混在一起。 */
  142. const allEmpty = { payCycle: "", amount: "", start: "", agent: "", agentPhone: "", handoverOn: "" };
  143. assert.deepEqual(checkContractRequired(allEmpty), { key: "payCycle", message: "请选择付款周期" });
  144. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly" }), { key: "amount", message: "请填写合同金额" });
  145. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1" }), { key: "start", message: "请选择生效日" });
  146. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14" }), { key: "agent", message: "请填写运营商经办人姓名" });
  147. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14", agent: "张工" }), { key: "agentPhone", message: "请填写经办人联系电话" });
  148. assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14", agent: "张工", agentPhone: "13900000101" }), { key: "handoverOn", message: "请选择交接日期" });
  149. /** 金额非空但非法 ⇒ 同一字段换成更具体的口径(0 元不是合法合同金额)。 */
  150. const payCycleOk = { ...allEmpty, payCycle: "quarterly" };
  151. assert.deepEqual(checkContractRequired({ ...payCycleOk, amount: "0" }), { key: "amount", message: "合同金额须大于 0(0 元不是合法合同金额)" });
  152. assert.match(checkContractRequired({ ...payCycleOk, amount: "1.999" })?.message ?? "", /格式不对/);
  153. /** `agent_org`(经办人机构)实测选填 ⇒ ⛔ 出现在必填清单里。 */
  154. assert.equal(Object.keys(allEmpty).includes("agentOrg"), false);
  155. assert.equal(CONTRACT_REQUIRED_FIELDS.some((field) => field.key === "agentOrg" || field.label.includes("机构")), false);
  156. });
  157. test("合同页载荷补齐后端必填键,命中的必填项红显该字段", () => {
  158. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  159. const detail = readFileSync(new URL("../src/pages/ContractRecordPage.tsx", import.meta.url), "utf8");
  160. const draft = readFileSync(new URL("../src/domain/contract-draft.ts", import.meta.url), "utf8");
  161. /** 写入口径已抽到 `domain/contract-draft`:新建与详情内联编辑共用同一份校验与载荷。 */
  162. assert.match(draft, /contractAmountCents: yuanTextToCents\(draft\.amount\) \?\? 0,/);
  163. assert.match(draft, /payCycle: draft\.payCycle,/);
  164. assert.match(draft, /agentMobile: draft\.agentPhone\.trim\(\),/);
  165. assert.match(draft, /agentName: draft\.agent\.trim\(\),/);
  166. assert.match(draft, /agentHandoverOn: draft\.handoverOn,/);
  167. assert.match(draft, /agentOrg: draft\.agentOrg\.trim\(\),/);
  168. assert.match(draft, /const missing = checkContractRequired\(\{[\s\S]{0,200}\}\);[\s\S]{0,100}fieldError: missing/);
  169. for (const key of ["payCycle", "amount", "agent", "agentPhone", "handoverOn"]) {
  170. assert.match(contract, new RegExp(`aria-invalid=\\{invalidOf\\("${key}"\\)\\}`));
  171. assert.ok(contract.includes(`{requiredErrorOf("${key}")}`), `字段 ${key} 缺原话红显`);
  172. assert.match(detail, new RegExp(`invalidOf\\("${key}"\\)`));
  173. assert.ok(detail.includes(`requiredErrorOf("${key}")`), `详情内联编辑字段 ${key} 缺原话红显`);
  174. }
  175. /** 2026-11-17:结束日锁成只读推算值 ⇒ 生效日成了合同期间唯一的人工入口,表单侧同样红显原话。 */
  176. assert.match(contract, /aria-invalid=\{invalidOf\("start"\)\}/);
  177. assert.ok(contract.includes('{requiredErrorOf("start")}'), "生效日缺原话红显");
  178. /** 详情页也走同一个整段替换载荷,⛔ 另写一套保存逻辑。保存形态=只提交变了的字段(键缺席=保持现值)。 */
  179. assert.match(detail, /buildContractWrite\(draft(?:, [^)]*)?\)/);
  180. assert.match(detail, /changedWriteFields\(draft, baseline(?:, [^)]*)?\)/);
  181. assert.match(detail, /service_items: patch\.serviceItems,/);
  182. /** 2026-11-17:固定 IP 条目下线 ⇒ 保存载荷不再拼 `ip_entries` 段。 */
  183. assert.equal(/ip_entries: patch\.ipEntries/.test(detail), false);
  184. /** ⛔ 金额再走浮点乘;⛔ 把经办人机构当必填拦人。 */
  185. assert.equal(/contract_amount_cents: yuanToCents/.test(contract), false);
  186. assert.equal(/checkContractRequired\([\s\S]{0,200}agentOrg/.test(`${contract}\n${draft}`), false);
  187. });
  188. test("合同详情与录入表单:两枚经办人都按机构/姓名/联系方式三段呈现", () => {
  189. const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8");
  190. const detail = readFileSync(new URL("../src/pages/ContractRecordPage.tsx", import.meta.url), "utf8");
  191. /** 运营商经办人:三段各自一枚 `contract-inline-seg`,顺序=机构 → 姓名 → 联系方式。 */
  192. assert.match(detail, /<dt>运营商经办人<\/dt><dd className="contract-inline-group">[\s\S]{0,900}?<small>机构<\/small>[\s\S]{0,1000}?<small>姓名<\/small>[\s\S]{0,1000}?<small>联系方式<\/small>/);
  193. assert.match(detail, /id="contract-agent-org"/);
  194. assert.match(detail, /id="contract-agent"/);
  195. assert.match(detail, /id="contract-agent-phone"/);
  196. /** 录入经办人:三段全部来自账号派生列(机构/姓名/联系方式),且恒只读。 */
  197. assert.match(detail, /<dt>录入经办人<\/dt><dd className="contract-inline-group">[\s\S]{0,900}?created_by_org[\s\S]{0,400}?created_by_name[\s\S]{0,400}?created_by_mobile/);
  198. const createdRow = /<dt>录入经办人<\/dt>([\s\S]*?)<\/dd><\/div>/.exec(detail);
  199. assert.ok(createdRow, "详情页缺「录入经办人」行");
  200. assert.equal(/<InlineField/.test(createdRow[1]), false, "录入经办人 ⛔ 内联编辑");
  201. /** 录入表单:同一份三段只读回显(新建态姓名/联系方式取当前登录身份,机构待服务端按账号带入)。 */
  202. assert.match(contract, /录入经办人[\s\S]{0,600}?created_by_org[\s\S]{0,300}?created_by_name[\s\S]{0,300}?created_by_mobile/);
  203. assert.match(contract, /提交时自动带入/);
  204. /** 两枚经办人各占一整行:详情 facts 是两列网格 ⇒ 必须跨满两列,⛔ 与邻项并排。 */
  205. assert.match(detail, /<div className="contract-facts-row-full"><dt>运营商经办人<\/dt>/);
  206. assert.match(detail, /<div className="contract-facts-row-full"><dt>录入经办人<\/dt>/);
  207. /** 录入表单:运营商经办人三段装在同一块三列栅格里(`contract-agent-grid`),录入经办人随后的整块另起一行。 */
  208. assert.match(contract, /contract-agent-block[\s\S]{0,900}?contract-agent-grid[\s\S]{0,1200}?id="agent-org"[\s\S]{0,900}?id="agent-name"[\s\S]{0,900}?id="agent-phone"/);
  209. assert.match(contract, /ops-form-span-full contract-agent-readonly/);
  210. });