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 { IP_BANDWIDTH_TIERS, LINE_EXIT_MAX, CONTRACT_REQUIRED_FIELDS, appendLineExits, centsToYuanText, checkAmountCents, checkContractRequired, checkIpRows, 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", "德国"]).exits, ["美国", "US", "德国"]); const repeated = appendLineExits(["usa", "美国"], ["USA", "美 国", " 美国"]); 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"]); assert.equal(overflow.exits.length, LINE_EXIT_MAX); assert.equal(overflow.added, 1); assert.equal(overflow.overflow, 1); }); test("IP 条目要凑齐三元组,带宽档位只认 5/10/30 兆三值", () => { assert.deepEqual([...IP_BANDWIDTH_TIERS], [5, 10, 30]); assert.equal(checkIpRows([], false), null); assert.match(checkIpRows([], true) ?? "", /至少添加一条/); assert.equal(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "5", ip_count: "3" }], true), null); assert.match(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "6", ip_count: "0" }], true) ?? "", /IP 条目 1 缺:带宽档位.*IP 数量/); assert.match(checkIpRows([{ line_exit: " ", bandwidth_tier_mbps: "30", ip_count: "1" }], true) ?? "", /线路出口/); assert.match(checkIpRows([{ line_exit: "美国", bandwidth_tier_mbps: "5", ip_count: "1" }], false) ?? "", /未勾选/); }); test("出口提交校验对两种形态同一条:必须从目录选择 1–8 个", () => { const planBased = appendLineExits([], splitExitInput("中国香港、美国")).exits; assert.deepEqual(planBased, ["中国香港", "美国"]); assert.equal(checkItemExits(planBased), null); assert.equal(checkItemExits(["美国"]), null); assert.match(checkItemExits([]) ?? "", /请从套餐配置选择 1–8 个线路出口/); assert.match(checkItemExits(Array.from({ length: LINE_EXIT_MAX + 1 }, (_, index) => `出口${index}`)) ?? "", /最多 8 个,当前 9 个/); }); 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("合同金额走整数分:元⇄分⛔ 浮点误差,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("新建合同必填 5 项按后端短路顺序逐字段拦人,提示语照抄 `60004` 原话", () => { const filled = { payCycle: "yearly", amount: "1920.00", agent: "临港联通 · 张工", agentPhone: "13900000101", handoverOn: "2026-11-14" }; assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.key), ["payCycle", "amount", "agent", "agentPhone", "handoverOn"]); assert.deepEqual(CONTRACT_REQUIRED_FIELDS.map((field) => field.message), [ "请选择付款周期", "请填写合同金额", "请填写运营商经办人姓名", "请填写经办人联系电话", "请选择交接单日期", ]); assert.equal(checkContractRequired(filled), null); /** 全空时只能报出后端第一个短路的字段(付款周期),⛔ 一句话把五项混在一起。 */ const allEmpty = { payCycle: "", amount: "", 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: "agent", message: "请填写运营商经办人姓名" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", agent: "张工" }), { key: "agentPhone", message: "请填写经办人联系电话" }); assert.deepEqual(checkContractRequired({ ...allEmpty, payCycle: "monthly", amount: "1", 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} 缺原话红显`); } /** 详情页也走同一个整段替换载荷,⛔ 另写一套保存逻辑。保存形态=只提交变了的字段(键缺席=保持现值)。 */ assert.match(detail, /buildContractWrite\(draft(?:, [^)]*)?\)/); assert.match(detail, /changedWriteFields\(draft, baseline(?:, [^)]*)?\)/); assert.match(detail, /service_items: patch\.serviceItems,/); assert.match(detail, /\.\.\.\(patch\.ipEntries \? \{ ip_entries: patch\.ipEntries \} : \{\}\),/); /** ⛔ 金额再走浮点乘;⛔ 把经办人机构当必填拦人。 */ 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, /