import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { getAnnualOrderEndDate, getBillableStartDate, } from "../src/domain/order-dates.ts"; import { canShowAction } from "../src/access/capabilities.ts"; import { loginRoles } from "../src/access/roles.ts"; import { accountStatuses, orderStatuses, workOrderStatuses, } from "../src/domain/statuses.ts"; import { LINE_EXIT_MAX, CONTRACT_REQUIRED_FIELDS, appendLineExits, centsToYuanText, checkAmountCents, checkContractRequired, checkItemExits, splitExitInput, yuanTextToCents, } from "../src/domain/contract-items.ts"; test("年度订单按下一周期生效日前一天结束,包括闰日边界", () => { assert.equal(getAnnualOrderEndDate("2026-03-18"), "2027-03-17"); assert.equal(getAnnualOrderEndDate("2024-02-29"), "2025-02-27"); assert.equal(getAnnualOrderEndDate("0099-01-01"), "0099-12-31"); assert.throws(() => getAnnualOrderEndDate("2026-02-29"), RangeError); }); test("正式应收从实际开通与合同生效的较晚日开始", () => { assert.equal(getBillableStartDate("2026-03-18", null), null); assert.equal(getBillableStartDate("2026-03-18", "2026-03-20"), "2026-03-20"); assert.equal(getBillableStartDate("2026-03-18", "2026-03-15"), "2026-03-18"); assert.throws(() => getBillableStartDate("2026-03-18", "2026-13-01"), RangeError); }); test("首期状态字典沿用业务说明中的明确状态", () => { assert.deepEqual(accountStatuses, ["未开通", "在用", "停用"]); assert.deepEqual(orderStatuses, ["待生效", "生效中", "已到期"]); assert.deepEqual(workOrderStatuses, ["待派单", "待处理", "处理中", "已完成", "已办结", "已关闭"]); }); test("角色能力按职责分开,运营商和代理不属于登录角色", () => { assert.equal(loginRoles.includes("operator"), false); assert.equal(loginRoles.includes("agency"), false); assert.equal(canShowAction("system-admin", "package.manage"), true); assert.equal(canShowAction("regional-partner", "package.manage"), false); assert.equal(canShowAction("regional-partner", "reconciliation.region.view"), true); assert.equal(canShowAction("regional-partner", "work-order.team.view"), false); assert.equal(canShowAction("operations-team", "work-order.team.reassign"), true); assert.equal(canShowAction("operations-team", "operations.result.record"), false); assert.equal(canShowAction("operations-member", "operations.result.record"), true); assert.equal(canShowAction("operations-member", "settlement-scope.manage"), false); }); test("线路出口目录的录入辅助:分隔、去重、8 条上限", () => { assert.deepEqual(splitExitInput(" 美国、日本 ,, 韩国 、"), ["美国", "日本", "韩国"]); assert.deepEqual(appendLineExits(["美国"], [" 美国 ", "US", "德国"], 8).exits, ["美国", "US", "德国"]); const repeated = appendLineExits(["usa", "美国"], ["USA", "美 国", " 美国"], 8); assert.deepEqual(repeated.exits, ["usa", "美国"]); assert.equal(repeated.added, 0); assert.equal(repeated.duplicated, 3); const overflow = appendLineExits(["1", "2", "3", "4", "5", "6", "7"], ["7", "8", "9"], 8); assert.equal(overflow.exits.length, 8); assert.equal(overflow.added, 1); assert.equal(overflow.overflow, 1); // 条目级缺省上限=1(2026-11-17 出口改单选)。 assert.deepEqual(appendLineExits([], ["中国香港", "美国"]).exits, ["中国香港"]); }); test("合同级固定 IP 条目已下线(2026-11-17):不再导出三元组校验口径", async () => { // 境外 IP 改由「套餐档位 × 订阅份数」表达 ⇒ 合同不再登记 (出口, 档位, 数量) 三元组; // 对应 `checkIpRows`/`checkIpExitWithinItems`/`IP_BANDWIDTH_TIERS` 一并退役。 const mod = await import("../src/domain/contract-items.ts"); for (const name of ["checkIpRows", "checkIpExitWithinItems", "IP_BANDWIDTH_TIERS"]) { assert.equal(name in mod, false, `${name} 应已随配置下线从模块导出中移除`); } }); test("出口提交校验:条目只能选 1 个(2026-11-17 由多选改单选)", () => { assert.equal(LINE_EXIT_MAX, 1); assert.equal(checkItemExits(["美国"]), null); assert.match(checkItemExits([]) ?? "", /请从套餐配置选择 1 个线路出口/); const multi = appendLineExits([], splitExitInput("中国香港、美国"), 8).exits; assert.deepEqual(multi, ["中国香港", "美国"]); assert.match(checkItemExits(multi) ?? "", /线路出口为单选:当前有 2 个.*只保留 1 个/); }); test("线路出口统一在套餐配置中配置,合同页只做选择(⛔ 自由录入)", () => { const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8"); assert.match(contract, /toggleExit/); assert.equal(contract.includes("exitInput"), false); assert.equal(contract.includes("commitExits"), false); assert.match(contract, /useLineExits\(\)/); const catalog = readFileSync(new URL("../src/pages/PackageManagementPage.tsx", import.meta.url), "utf8"); assert.match(catalog, /lineExitSave/); assert.match(catalog, /useLineExits\(\)/); assert.equal(catalog.includes("出口不在目录侧登记"), false); }); test("带宽档位=套餐带宽权益的快捷预设(2026-11-17 改用途,⛔ 合同侧不再选档)", () => { const catalog = readFileSync(new URL("../src/pages/PackageManagementPage.tsx", import.meta.url), "utf8"); /** ① 档位清单的下拉顺序已不再服务合同页。 */ assert.equal(catalog.includes("合同页下拉顺序"), false); /** ② 固定档/自定义档编辑器共用同一个带宽档位快捷控件(2026-11-19 抽到 `PackagePlanEditor`)。 */ const editor = readFileSync(new URL("../src/components/PackagePlanEditor.tsx", import.meta.url), "utf8"); assert.match(catalog, /PackagePlanEditor/); assert.match(editor, /tierPresets/); assert.match(editor, /catalog-bandwidth-presets/); assert.match(editor, /applyTier/); /** ③ 合同页不再出现档位快捷(合同只按套餐与份数登记)。 */ const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8"); assert.equal(contract.includes("catalog-bandwidth-preset"), false); }); test("合同金额走整数分:元⇄分⛔ 浮点误差,0 元与非法格式拦下", () => { assert.equal(yuanTextToCents("328.53"), 32853); assert.equal(yuanTextToCents("0.07"), 7); assert.equal(yuanTextToCents("1920"), 192000); assert.equal(yuanTextToCents(" 12.3 "), 1230); assert.equal(yuanTextToCents("1.999"), null); assert.equal(yuanTextToCents("1e3"), null); assert.equal(centsToYuanText(32853), "328.53"); assert.equal(centsToYuanText(7), "0.07"); assert.equal(centsToYuanText(192000), "1920.00"); assert.equal(centsToYuanText(0), "0.00"); for (const cents of [1, 7, 99, 100, 32853, 900719925474088]) { assert.equal(yuanTextToCents(centsToYuanText(cents)), cents); } assert.match(checkAmountCents("") ?? "", /请填写合同金额/); assert.match(checkAmountCents("0") ?? "", /须大于 0/); assert.match(checkAmountCents("0.00") ?? "", /须大于 0/); assert.match(checkAmountCents("-5") ?? "", /格式不对/); assert.match(checkAmountCents("1.999") ?? "", /格式不对/); assert.equal(checkAmountCents("1920.00"), null); }); test("新建合同必填 6 项按后端短路顺序逐字段拦人,提示语照抄 `60004` 原话", () => { const filled = { payCycle: "yearly", amount: "1920.00", start: "2026-11-14", agent: "临港联通 · 张工", agentPhone: "13900000101", handoverOn: "2026-11-14" }; assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.key), ["payCycle", "amount", "start", "agent", "agentPhone", "handoverOn"]); assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.message), [ "请选择付款周期", "请填写合同金额", "请选择生效日", "请填写运营商经办人姓名", "请填写经办人联系电话", "请选择交接日期", ]); assert.equal(checkContractRequired(filled), null); /** 全空时只能报出后端第一个短路的字段(付款周期),⛔ 一句话把六项混在一起。 */ const allEmpty = { payCycle: "", amount: "", start: "", agent: "", agentPhone: "", handoverOn: "" }; assert.deepEqual(checkContractRequired(allEmpty), { key: "payCycle", message: "请选择付款周期" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly" }), { key: "amount", message: "请填写合同金额" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1" }), { key: "start", message: "请选择生效日" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14" }), { key: "agent", message: "请填写运营商经办人姓名" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14", agent: "张工" }), { key: "agentPhone", message: "请填写经办人联系电话" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", start: "2026-11-14", agent: "张工", agentPhone: "13900000101" }), { key: "handoverOn", message: "请选择交接日期" }); /** 金额非空但非法 ⇒ 同一字段换成更具体的口径(0 元不是合法合同金额)。 */ const payCycleOk = { ...allEmpty, payCycle: "quarterly" }; assert.deepEqual(checkContractRequired({ ...payCycleOk, amount: "0" }), { key: "amount", message: "合同金额须大于 0(0 元不是合法合同金额)" }); assert.match(checkContractRequired({ ...payCycleOk, amount: "1.999" })?.message ?? "", /格式不对/); /** `agent_org`(经办人机构)实测选填 ⇒ ⛔ 出现在必填清单里。 */ assert.equal(Object.keys(allEmpty).includes("agentOrg"), false); assert.equal(CONTRACT_REQUIRED_FIELDS.some((field) => field.key === "agentOrg" || field.label.includes("机构")), false); }); test("合同页载荷补齐后端必填键,命中的必填项红显该字段", () => { const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8"); const detail = readFileSync(new URL("../src/pages/ContractRecordPage.tsx", import.meta.url), "utf8"); const draft = readFileSync(new URL("../src/domain/contract-draft.ts", import.meta.url), "utf8"); /** 写入口径已抽到 `domain/contract-draft`:新建与详情内联编辑共用同一份校验与载荷。 */ assert.match(draft, /contractAmountCents: yuanTextToCents\(draft\.amount\) \?\? 0,/); assert.match(draft, /payCycle: draft\.payCycle,/); assert.match(draft, /agentMobile: draft\.agentPhone\.trim\(\),/); assert.match(draft, /agentName: draft\.agent\.trim\(\),/); assert.match(draft, /agentHandoverOn: draft\.handoverOn,/); assert.match(draft, /agentOrg: draft\.agentOrg\.trim\(\),/); assert.match(draft, /const missing = checkContractRequired\(\{[\s\S]{0,200}\}\);[\s\S]{0,100}fieldError: missing/); for (const key of ["payCycle", "amount", "agent", "agentPhone", "handoverOn"]) { assert.match(contract, new RegExp(`aria-invalid=\\{invalidOf\\("${key}"\\)\\}`)); assert.ok(contract.includes(`{requiredErrorOf("${key}")}`), `字段 ${key} 缺原话红显`); assert.match(detail, new RegExp(`invalidOf\\("${key}"\\)`)); assert.ok(detail.includes(`requiredErrorOf("${key}")`), `详情内联编辑字段 ${key} 缺原话红显`); } /** 2026-11-17:结束日锁成只读推算值 ⇒ 生效日成了合同期间唯一的人工入口,表单侧同样红显原话。 */ assert.match(contract, /aria-invalid=\{invalidOf\("start"\)\}/); assert.ok(contract.includes('{requiredErrorOf("start")}'), "生效日缺原话红显"); /** 详情页也走同一个整段替换载荷,⛔ 另写一套保存逻辑。保存形态=只提交变了的字段(键缺席=保持现值)。 */ assert.match(detail, /buildContractWrite\(draft(?:, [^)]*)?\)/); assert.match(detail, /changedWriteFields\(draft, baseline(?:, [^)]*)?\)/); assert.match(detail, /service_items: patch\.serviceItems,/); /** 2026-11-17:固定 IP 条目下线 ⇒ 保存载荷不再拼 `ip_entries` 段。 */ assert.equal(/ip_entries: patch\.ipEntries/.test(detail), false); /** ⛔ 金额再走浮点乘;⛔ 把经办人机构当必填拦人。 */ assert.equal(/contract_amount_cents: yuanToCents/.test(contract), false); assert.equal(/checkContractRequired\([\s\S]{0,200}agentOrg/.test(`${contract}\n${draft}`), false); }); test("合同详情与录入表单:两枚经办人都按机构/姓名/联系方式三段呈现", () => { const contract = readFileSync(new URL("../src/pages/ContractDetailPages.tsx", import.meta.url), "utf8"); const detail = readFileSync(new URL("../src/pages/ContractRecordPage.tsx", import.meta.url), "utf8"); /** 运营商经办人:三段各自一枚 `contract-inline-seg`,顺序=机构 → 姓名 → 联系方式。 */ assert.match(detail, /