domain-foundation.test.mjs 14 KB

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