| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- // @vitest-environment jsdom
- import { describe, expect, it } from "vitest";
- import { buildContractWrite, businessToday, changedWriteFields, contractEndFromMonths, emptyDraft, emptyItem, frozenChanges, renewalPrefill, renewalStartOf, resolveReplacementSelection } from "../contract-draft";
- import type { ContractEditScope, ReplacementCandidate } from "../../lib/api/types";
- /** 基线:一份可正常保存的未开通合同草稿。 */
- const baseline = () => {
- const draft = emptyDraft();
- return {
- ...draft,
- amount: "10800.00",
- contractType: "new",
- serviceMonths: "12",
- start: "2026-01-01",
- end: "2026-12-31",
- agent: "张三",
- agentPhone: "13800000000",
- /** 交接日期=签署日期(2026-11-19):草稿只留 signDate,写口按 agent_handover_on 同值落库。 */
- signDate: "2026-01-05",
- items: [{ ...emptyItem(), planId: "CP-000001", planName: "标准套餐", quantity: 1, exits: ["香港"] }],
- };
- };
- /** 两轴全冻(已开通/已到期且超出更正期)。 */
- const allLocked: ContractEditScope = {
- state: "opened",
- billing: { editable: false, reason: "opened" },
- archive: { editable: false, reason: "opened" },
- grace_until: null,
- };
- /** 只冻计费轴(已生效未开通)。 */
- const billingOnlyLocked: ContractEditScope = {
- state: "effective_unopened",
- billing: { editable: false, reason: "grace_expired" },
- archive: { editable: true, reason: null },
- grace_until: null,
- };
- /** 两轴全开(未生效/录后 72h 内)。 */
- const allOpen: ContractEditScope = {
- state: "pending",
- billing: { editable: true, reason: null },
- archive: { editable: true, reason: null },
- grace_until: "2026-11-18T21:00:00+08:00",
- };
- describe("frozenChanges · 编辑口径守卫(两轴独立判定)", () => {
- it("草稿与基线一致 ⇒ 不算越界(其余字段照样可改)", () => {
- const base = baseline();
- expect(frozenChanges(base, base, allLocked)).toEqual([]);
- expect(frozenChanges({ ...base, agent: "张三" }, base, allLocked)).toEqual([]);
- });
- it("金额/服务期/期间各自命中,且一次列出全部越界项", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, amount: "12000.00" }, base, allLocked)).toEqual(["合同金额"]);
- expect(frozenChanges({ ...base, serviceMonths: "3" }, base, allLocked)).toEqual(["服务期(月)"]);
- expect(frozenChanges({ ...base, start: "2026-02-01" }, base, allLocked)).toEqual(["合同生效日"]);
- expect(frozenChanges({ ...base, end: "2027-12-31" }, base, allLocked)).toEqual(["合同结束日"]);
- expect(frozenChanges({ ...base, amount: "1", start: "2026-02-01", end: "2027-01-01" }, base, allLocked)).toEqual([
- "合同金额",
- "合同生效日",
- "合同结束日",
- ]);
- });
- it("服务构成按条目内容比较:改档位/数量/年价被拦", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, items: [{ ...base.items[0], quantity: 2 }] }, base, allLocked)).toEqual(["服务构成"]);
- expect(frozenChanges({ ...base, items: [{ ...base.items[0], planFamily: "addon" }] }, base, allLocked)).toEqual(["服务构成"]);
- // 2026-11-17:IP 条目不属草稿(配置已下线)⇒ 只按服务构成判计费轴。
- });
- it("档案轴:已冻结时外部单号/签署日期/运营商经办人被拦", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, externalNo: "WB-NEW" }, base, allLocked)).toEqual(["外部单号"]);
- expect(frozenChanges({ ...base, contractType: "renew" }, base, allLocked)).toEqual(["合同类型"]);
- expect(frozenChanges({ ...base, signDate: "2026-02-02" }, base, allLocked)).toEqual(["签署日期"]);
- // 运营商经办人三件套=交接单上的商务档案,与签署日期同轴;⛔ 与「录入账号」混用。
- expect(frozenChanges({ ...base, agent: "李四" }, base, allLocked)).toEqual(["运营商经办人姓名"]);
- expect(frozenChanges({ ...base, agentPhone: "13900000000" }, base, allLocked)).toEqual(["运营商经办人电话"]);
- expect(frozenChanges({ ...base, agentOrg: "华东运营中心" }, base, allLocked)).toEqual(["运营商经办人机构"]);
- });
- it("已生效未开通:只冻计费轴,外部单号与运营商经办人仍可改", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, amount: "1" }, base, billingOnlyLocked)).toEqual(["合同金额"]);
- expect(frozenChanges({ ...base, items: [{ ...base.items[0], quantity: 2 }] }, base, billingOnlyLocked)).toEqual(["服务构成"]);
- expect(frozenChanges({ ...base, externalNo: "WB-NEW" }, base, billingOnlyLocked)).toEqual([]);
- expect(frozenChanges({ ...base, agent: "李四" }, base, billingOnlyLocked)).toEqual([]);
- });
- it("两轴全开:任何改动都不拦", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, amount: "1", externalNo: "WB-NEW" }, base, allOpen)).toEqual([]);
- });
- it("口径缺席(旧后端未下发 edit_scope)⇒ 不在前端臆断冻结", () => {
- const base = baseline();
- expect(frozenChanges({ ...base, amount: "1" }, base, null)).toEqual([]);
- });
- });
- describe("changedWriteFields · 只提交变了的字段", () => {
- it("整份未改 ⇒ 空载荷(后端按键缺席=保持现值)", () => {
- const base = baseline();
- expect(changedWriteFields(base, base)).toEqual({});
- });
- it("只改外部单号 ⇒ 载荷只带 external_no", () => {
- const base = baseline();
- expect(changedWriteFields({ ...base, externalNo: "WB-NEW" }, base)).toEqual({ externalNo: "WB-NEW" });
- });
- it("只有条目变了才带 service_items", () => {
- const base = baseline();
- const items = changedWriteFields({ ...base, items: [{ ...base.items[0], quantity: 2 }] }, base);
- expect(Object.keys(items)).toEqual(["serviceItems"]);
- });
- });
- /** 候选行(只填与选择矩阵相关的两列,其余置空)。 */
- const candidate = (id: string, customized = false): ReplacementCandidate => ({
- subscription_id: id,
- status: "active",
- customized,
- });
- describe("resolveReplacementSelection · 候选选择矩阵(2026-11-17)", () => {
- it("0 个候选 ⇒ 不替换(按新订阅开通)", () => {
- expect(resolveReplacementSelection([], "SUB-1", false)).toEqual({ id: "", customized: false });
- });
- it("1 个候选 ⇒ 自动选中;显式退出后保持不替换", () => {
- expect(resolveReplacementSelection([candidate("SUB-1")], "", false)).toEqual({ id: "SUB-1", customized: false });
- expect(resolveReplacementSelection([candidate("SUB-1")], "SUB-1", false)).toEqual({ id: "SUB-1", customized: false });
- expect(resolveReplacementSelection([candidate("SUB-1")], "SUB-1", true)).toEqual({ id: "", customized: false });
- });
- it("多个候选 ⇒ 不预选,只沿用仍有效的已选", () => {
- const list = [candidate("SUB-1"), candidate("SUB-2", true)];
- expect(resolveReplacementSelection(list, "", false)).toEqual({ id: "", customized: false });
- expect(resolveReplacementSelection(list, "SUB-2", false)).toEqual({ id: "SUB-2", customized: true });
- /** 已选掉出候选(换了档位/出口/客户)⇒ 归零,⛔ 带一个不存在的订阅号提交。 */
- expect(resolveReplacementSelection(list, "SUB-9", false)).toEqual({ id: "", customized: false });
- });
- });
- describe("contractEndFromMonths · 合同结束日推算(2026-11-19:生效日+服务期月数−1 天)", () => {
- it("结束日=生效日+服务期(月)−1 天", () => {
- expect(contractEndFromMonths("2026-11-17", 12)).toBe("2027-11-16");
- expect(contractEndFromMonths("2026-11-17", "12")).toBe("2027-11-16");
- expect(contractEndFromMonths("2026-11-17", 3)).toBe("2027-02-16");
- expect(contractEndFromMonths("2026-11-17", 1)).toBe("2026-12-16");
- expect(contractEndFromMonths("2026-11-17", 36)).toBe("2029-11-16");
- expect(contractEndFromMonths("2026-11-17", 60)).toBe("2031-11-16");
- });
- it("该月无对应日先取月末再减一天(与后端 add_months_clamped 同式)", () => {
- expect(contractEndFromMonths("2026-01-31", 1)).toBe("2026-02-27");
- expect(contractEndFromMonths("2028-02-29", 12)).toBe("2029-02-27");
- expect(contractEndFromMonths("2026-12-01", 3)).toBe("2027-02-28");
- });
- it("服务期非法(0/601/小数/空)或生效日缺失 ⇒ 空串(⛔ 留陈旧值)", () => {
- expect(contractEndFromMonths("2026-11-17", 0)).toBe("");
- expect(contractEndFromMonths("2026-11-17", 601)).toBe("");
- expect(contractEndFromMonths("2026-11-17", 1.5)).toBe("");
- expect(contractEndFromMonths("2026-11-17", "")).toBe("");
- expect(contractEndFromMonths("", 12)).toBe("");
- expect(contractEndFromMonths("2026/11/17", 12)).toBe("");
- });
- });
- describe("businessToday · 业务当天按 Asia/Shanghai", () => {
- it("UTC 晚间仍按北京时间取日(⛔ 用 toISOString 跳日)", () => {
- expect(businessToday(new Date("2026-11-17T15:59:00Z"))).toBe("2026-11-17");
- expect(businessToday(new Date("2026-11-17T16:30:00Z"))).toBe("2026-11-18");
- });
- });
- describe("renewalPrefill · 续签带入(2026-11-17)", () => {
- const oldRow = {
- contract_id: "CT-000003",
- customer_id: "KL-0001-000001",
- contract_no: "KL-0001-000001-C1",
- external_no: "EXT-9",
- status: "effective",
- contract_amount_cents: 1_080_000,
- contract_type: "renew",
- sign_date: "2024-12-28",
- service_months: 12,
- start_date: "2025-01-01",
- end_date: "2025-12-31",
- agent_name: "李四",
- agent_mobile: "13900000000",
- agent_handover_on: "2025-01-03",
- agent_org: "省公司",
- attachment_url: "klt/contract/attachment/old.pdf",
- row_version: "3",
- created_at: "2025-01-01T00:00:00Z",
- service_items: [
- { item_id: "SI-000009", plan_id: "CP-000001", plan_name: "标准套餐", item_family: "link", quantity: 1, line_exits: ["香港"] },
- { item_id: "SI-000010", plan_id: "CP-000009", plan_name: "单独境外 IP", item_family: "addon", quantity: 2, line_exits: ["新加坡"] },
- ],
- };
- it("带出金额/服务期/条目/经办人;合同类型置为续签;条目主键清空(新建态由服务端发号)", () => {
- const p = renewalPrefill(oldRow as never);
- expect(p).not.toBeNull();
- if (!p) return;
- expect(p.amount).toBe("10800.00");
- expect(p.serviceMonths).toBe("12");
- expect(p.contractType).toBe("renew");
- expect(p.agent).toBe("李四");
- expect(p.agentPhone).toBe("13900000000");
- expect(p.agentOrg).toBe("省公司");
- expect(p.items).toHaveLength(2);
- expect(p.items.every((item) => item.itemId === null)).toBe(true);
- expect(p.items.map((item) => item.planFamily)).toEqual(["link", "addon"]);
- expect(p.items[1].quantity).toBe(2);
- });
- it("⛔ 带编号/外部单号/结束日/附件/签署日期;生效日取旧止期次日(不早于业务当天)", () => {
- const p = renewalPrefill(oldRow as never) as unknown as Record<string, unknown>;
- expect(p).not.toBeNull();
- for (const key of ["contractNo", "externalNo", "end", "attachmentUrl", "signDate"]) {
- expect(p[key]).toBeUndefined();
- }
- /** 2026-11-17 四轮:新合同接在旧期间之后,旧合同已过期 ⇒ 取业务当天(⛔ 倒签)。 */
- expect(renewalStartOf("2025-12-31", "2026-11-17")).toBe("2026-11-17");
- expect(renewalStartOf("2026-11-30", "2026-11-17")).toBe("2026-12-01");
- expect(typeof p.start).toBe("string");
- });
- it("旧合同读不到 ⇒ 返回 null,由页面退回手工录入", () => {
- expect(renewalPrefill(null)).toBeNull();
- });
- });
- describe("buildContractWrite · 合同期间(2026-11-17:结束日锁定为推算值)", () => {
- it("缺生效日 ⇒ 红显生效日并给原话(⛔ 报一句「生效日与结束日」)", () => {
- const built = buildContractWrite({ ...baseline(), start: "", end: "" });
- expect(built.ok).toBe(false);
- if (built.ok) return;
- expect(built.error).toBe("请选择生效日");
- expect(built.fieldError?.key).toBe("start");
- });
- it("结束日按草稿原样提交(页面只在生效日/服务期变化时重算)", () => {
- const draft = { ...baseline(), start: "2026-11-17", serviceMonths: "12", end: contractEndFromMonths("2026-11-17", 12) };
- const built = buildContractWrite(draft);
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- expect(built.write.endDate).toBe("2027-11-16");
- });
- });
- describe("buildContractWrite · 逐条替换 succession 载荷", () => {
- const withReplacement = (patch: Partial<ReturnType<typeof emptyItem>> = {}) => {
- const base = baseline();
- return {
- ...base,
- items: [{ ...base.items[0], replacesSubscriptionId: "SUB-000041", ...patch }],
- };
- };
- it("选中原订阅 ⇒ 条目带 succession(只带订阅号,起止日由后端派生)", () => {
- const built = buildContractWrite(withReplacement());
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- expect(built.write.serviceItems[0].succession).toEqual({ replaces_subscription_id: "SUB-000041" });
- });
- it("未选中 ⇒ 条目不带 succession(按新订阅开通)", () => {
- const built = buildContractWrite(baseline());
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- expect(built.write.serviceItems[0].succession).toBeUndefined();
- });
- it("定制档须显式确认;确认后放行", () => {
- const blocked = buildContractWrite(withReplacement({ replacementCustomized: true }));
- expect(blocked.ok).toBe(false);
- if (!blocked.ok) expect(blocked.error).toContain("定制档");
- const ok = buildContractWrite(withReplacement({ replacementCustomized: true, replacementConfirmed: true }));
- expect(ok.ok).toBe(true);
- });
- it("替换时一条条目只生一份订阅:档位数量须为 1", () => {
- const blocked = buildContractWrite(withReplacement({ quantity: 2 }));
- expect(blocked.ok).toBe(false);
- if (!blocked.ok) expect(blocked.error).toContain("数量须为 1");
- });
- });
- describe("checkItemExits · 出口单选(2026-11-17)", () => {
- it("多出口 ⇒ 点名条目并提示只保留 1 个", () => {
- const base = baseline();
- const built = buildContractWrite({ ...base, items: [{ ...base.items[0], exits: ["香港", "德国"] }] });
- expect(built.ok).toBe(false);
- if (!built.ok) {
- expect(built.error).toContain("条目 1");
- expect(built.error).toContain("单选");
- }
- });
- });
- describe("buildContractWrite · 交付形态只决定份数(D-15/D-18)", () => {
- /**
- * P1 起附加项必须挂在跨境链路条目下(D-7)⇒ 造「父链路 + 子附加」两行。
- * `parentKey` 是草稿的本地身份,不是数组下标。
- */
- const withAddon = (quantity: number) => {
- const base = baseline();
- const parent = { ...base.items[0], planFamily: "link" };
- const child = { ...emptyItem(parent.key), planFamily: "addon", planId: "CP-ADDON", quantity, exits: ["香港"] };
- return { ...base, items: [parent, child] };
- };
- const withFamily = (family: string, quantity: number) => {
- const base = baseline();
- return { ...base, items: [{ ...base.items[0], planFamily: family, quantity }] };
- };
- it("link 档份数恒 1:填 2 即拦下并点名跨境链路", () => {
- const blocked = buildContractWrite(withFamily("link", 2));
- expect(blocked.ok).toBe(false);
- if (!blocked.ok) expect(blocked.error).toContain("跨境链路档:份数恒为 1");
- });
- it("addon 档份数可 > 1:3 份放行,等于 3 个 IP 权益", () => {
- const built = buildContractWrite(withAddon(3));
- expect(built.ok).toBe(true);
- if (built.ok) expect(built.write.serviceItems[1].quantity).toBe(3);
- });
- it("份数 0/小数一律拦(两种形态同一句)", () => {
- for (const quantity of [0, 1.5]) {
- const blocked = buildContractWrite(withAddon(quantity));
- expect(blocked.ok).toBe(false);
- if (!blocked.ok) expect(blocked.error).toContain("正整数");
- }
- });
- it("条目载荷只有 item_id/plan_id/quantity/line_exits:⛔ 带价格、带宽、账号数、item_kind", () => {
- const base = baseline();
- const built = buildContractWrite({
- ...base,
- items: [{ ...base.items[0], planFamily: "link", quantity: 1, exits: ["美国"] }],
- });
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- const item = built.write.serviceItems[0] as Record<string, unknown>;
- expect(Object.keys(item).sort()).toEqual(["item_id", "line_exits", "plan_id", "quantity"]);
- for (const banned of [
- "item_kind",
- "item_family",
- "annual_price_cents",
- "monthly_settle_cents",
- "account_count",
- "bandwidth_up_mbps",
- "bandwidth_down_mbps",
- ]) {
- expect(item[banned]).toBeUndefined();
- }
- });
- });
- describe("buildContractWrite · P1 树形与增量(D-7/D-9)", () => {
- it("附加项未挂父 ⇒ 拦下并点名 D-7", () => {
- const base = baseline();
- const built = buildContractWrite({ ...base, items: [{ ...base.items[0], planFamily: "addon", exits: ["香港"] }] });
- expect(built.ok).toBe(false);
- if (!built.ok) expect(built.error).toContain("必须挂在某条跨境链路条目下");
- });
- it("父不是链路(附加挂附加)⇒ 拦下", () => {
- const base = baseline();
- const link = { ...base.items[0], planFamily: "link", exits: ["香港"] };
- const addon = { ...emptyItem(link.key), planFamily: "addon", planId: "CP-A", exits: ["香港"] };
- const child = { ...emptyItem(addon.key), planFamily: "addon", planId: "CP-B", exits: ["香港"] };
- const built = buildContractWrite({ ...base, items: [link, addon, child] });
- expect(built.ok).toBe(false);
- if (!built.ok) expect(built.error).toContain("必须是跨境链路");
- });
- it("子行出口不在父行出口内 ⇒ 拦下", () => {
- const base = baseline();
- const parent = { ...base.items[0], planFamily: "link", exits: ["香港"] };
- const child = { ...emptyItem(parent.key), planFamily: "addon", planId: "CP-A", exits: ["美国"] };
- const built = buildContractWrite({ ...base, items: [parent, child] });
- expect(built.ok).toBe(false);
- if (!built.ok) expect(built.error).toContain("不在父条目");
- });
- it("父是本次新建 ⇒ 用 0 基 serves_item_index(父已落库才用 serves_item_id)", () => {
- const base = baseline();
- const parent = { ...base.items[0], planFamily: "link", itemId: null };
- const child = { ...emptyItem(parent.key), planFamily: "addon", planId: "CP-A", exits: ["香港"] };
- const built = buildContractWrite({ ...base, items: [parent, child] });
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- expect(built.write.serviceItems[1].serves_item_index).toBe(0);
- expect(built.write.serviceItems[1].serves_item_id).toBeUndefined();
- const saved = buildContractWrite({ ...base, items: [{ ...parent, itemId: "SI-000001" }, child] });
- expect(saved.ok).toBe(true);
- if (saved.ok) expect(saved.write.serviceItems[1].serves_item_id).toBe("SI-000001");
- });
- it("增量条目:带 item_mode/ref_subscription_id,且 ⛔ 挂 serves_item_id", () => {
- const base = baseline();
- const item = { ...emptyItem(null, "increment"), planFamily: "addon", planId: "CP-A", quantity: 3, exits: ["香港"], refSubscriptionId: "SUB-1" };
- const built = buildContractWrite({ ...base, increment: true, contractType: "expand", items: [item] });
- expect(built.ok).toBe(true);
- if (!built.ok) return;
- const payload = built.write.serviceItems[0];
- expect(payload.item_mode).toBe("increment");
- expect(payload.ref_subscription_id).toBe("SUB-1");
- expect(payload.serves_item_id).toBeUndefined();
- expect(payload.serves_item_index).toBeUndefined();
- });
- it("增量条目缺引用订阅 ⇒ 拦下(D-9)", () => {
- const base = baseline();
- const item = { ...emptyItem(null, "increment"), planFamily: "addon", planId: "CP-A", exits: ["香港"] };
- const built = buildContractWrite({ ...base, increment: true, contractType: "expand", items: [item] });
- expect(built.ok).toBe(false);
- if (!built.ok) expect(built.error).toContain("须引用本客户一条在期跨境链路订阅");
- });
- it("增量合同 ⛔ 把链路条目改成 increment(D-9:增量只补附加项)", () => {
- const base = baseline();
- const bad = buildContractWrite({ ...base, increment: true, contractType: "expand", items: [{ ...base.items[0], planFamily: "link", itemMode: "increment", refSubscriptionId: "SUB-1" }] });
- expect(bad.ok).toBe(false);
- if (!bad.ok) expect(bad.error).toContain("只能补附加项");
- });
- });
|