#!/usr/bin/env node /** * 工单明细行校验(交接单开通应用/境外 IP、回填网络线路/境外 IP 明细)。 * * 存在理由:开户工单的处理链此前在这两步被界面卡死——交接单只收声明四项, * 提交必回 `60408 handover_all_zero`;施工回填既没有 `route_rows` 也没有 `ip_rows` * 与建单人,提交必回 `60404` 必填组不齐。这两张表的「整行必填」口径⛔ 散在组件里, * 本测试把后端 `handlers/handle.rs` 的逐行必填口径钉死在这里。 */ import test from "node:test"; import assert from "node:assert/strict"; import { APP_CATEGORIES, appRowsFromDetail, backfillIpRowsFromDetail, buildAppRows, buildBackfillIpRows, buildOverseasIpRows, buildRouteRows, defaultAppRows, newAppRow, newBackfillIpRow, newOverseasIpRow, newRouteRow, normalizeBuilderUserId, overseasIpRowsFromDetail, parseLineExits, routeRowsFromDetail, validateAppRows, validateBackfillIpRows, validateOverseasIpRows, validateRouteRows, } from "../src/lib/ticketRows.ts"; const fullRoute = (over = {}) => ({ ...newRouteRow(), sub_account_id: "SUB-000023", network_account_name: "hailan-office-01", intranet_ip: "10.20.30.11", subnet_mask: "255.255.255.0", gateway: "10.20.30.1", vlan: "120", vni: "50120", src_endpoint: "上海临港POP", dst_endpoint: "中国香港POP", egress_pop: "中国香港", down_mbps: "100", up_mbps: "50", ...over, }); const fullIp = (over = {}) => ({ ...newBackfillIpRow(), egress: "中国香港", overseas_ip: "203.0.113.7", ip_type: "fixed_ip", bandwidth_mbps: "10", pin: "PIN-8842", cpid: "CPID-8842", ...over, }); test("交接单应用类别固定五类,编码与后端 APP_CATEGORIES 逐项对齐", () => { assert.deepEqual( APP_CATEGORIES.map((c) => c.code), ["browse", "office", "operation", "livestream", "custom"], ); assert.equal(APP_CATEGORIES.every((c) => c.label.length > 0), true); }); test("开通应用:整行留空的行静默丢弃,但至少要有数量为正整数的一行(后端 60408)", () => { assert.match(validateAppRows(defaultAppRows()) ?? "", /至少为一个应用类别/); assert.match(validateAppRows([newAppRow("office", "0")]) ?? "", /至少为一个应用类别/); assert.match(validateAppRows([newAppRow("office", "-3")]) ?? "", /须为整数/); assert.match(validateAppRows([newAppRow("office", "2.5")]) ?? "", /须为整数/); assert.equal(validateAppRows([newAppRow("office", "2")]), null); // 只填数量没选类别 ⇒ 半填行,⛔ 当成空行提交 assert.match(validateAppRows([newAppRow("", "2")]) ?? "", /请填写应用类别/); }); test("开通应用:同一类别重复登记被拦(库内 uq_handover_app 唯一键)", () => { assert.match( validateAppRows([newAppRow("office", "2"), newAppRow("office", "3")]) ?? "", /重复登记/, ); }); test("开通应用载荷:空行与零数量不入载荷,数量收敛为数字", () => { const rows = [newAppRow("office", "2"), newAppRow("browse", ""), newAppRow("custom", "0")]; assert.equal(validateAppRows(rows), null); assert.deepEqual(buildAppRows(rows), [{ app_category: "office", qty: 2 }]); }); test("交接单境外 IP:整行必填且至少一行", () => { const noCountry = { ...newOverseasIpRow(), country: "", ip_count: "1" }; assert.match(validateOverseasIpRows([noCountry], true) ?? "", /国家/地区/); assert.match( validateOverseasIpRows([newOverseasIpRow("中国香港", "7", "1")], true) ?? "", /5/10/30/, ); assert.match( validateOverseasIpRows([newOverseasIpRow("中国香港", "10", "0")], true) ?? "", /正整数/, ); assert.equal(validateOverseasIpRows([newOverseasIpRow("中国香港", "10", "1")], true), null); // 整行一个字节没动过(把预填的带宽档位也清掉)⇒ 不算一行:报「至少登记一行」,也不入载荷 const untouched = { ...newOverseasIpRow(), bandwidth_mbps: "" }; assert.match(validateOverseasIpRows([untouched], true) ?? "", /至少登记一行/); assert.equal(validateOverseasIpRows([untouched], false), null); assert.equal(buildOverseasIpRows([untouched]).length, 0); assert.deepEqual(buildOverseasIpRows([newOverseasIpRow(" 中国香港 ", "5", "2")]), [ { country: "中国香港", bandwidth_mbps: 5, ip_count: 2 }, ]); }); test("回填网络线路:整行 12 列缺一列就报出缺哪一列(组一整行必填)", () => { assert.equal(validateRouteRows([fullRoute()], true), null); assert.match(validateRouteRows([fullRoute({ gateway: "" })], true) ?? "", /第 1 行线路缺网关/); assert.match( validateRouteRows([fullRoute({ vlan: "", vni: "" })], true) ?? "", /缺VLAN、VNI/, ); // 只填了一格的半行⛔ 当空行丢掉,必须点名还缺哪些列 const half = { ...newRouteRow(), sub_account_id: "SUB-000023", gateway: "" }; assert.match(validateRouteRows([half], true) ?? "", /第 1 行线路缺网络账户名/); }); test("回填网络线路:至少一行(开通类),空行不占名额也不入载荷", () => { assert.match(validateRouteRows([newRouteRow()], true) ?? "", /至少填写一行/); assert.equal(validateRouteRows([newRouteRow()], false), null); assert.equal(buildRouteRows([newRouteRow()]).length, 0); // 未勾「本次开通」:后端只处理 picked ⇒ 既不校验也不入库,前端同口径放行且不下发 const parked = { ...fullRoute(), open_this_time: false }; assert.match(validateRouteRows([parked], true) ?? "", /勾选至少一行/); assert.equal(validateRouteRows([parked, fullRoute()], true), null); assert.equal(buildRouteRows([parked, fullRoute()]).length, 1); const built = buildRouteRows([fullRoute()]); assert.equal(built.length, 1); assert.equal(built[0].down_mbps, 100); assert.equal(built[0].open_this_time, true); assert.equal(Object.keys(built[0]).length, 13, "RouteIn 十二列 + open_this_time"); }); test("回填网络线路:非法 IP 与非法带宽被拦", () => { assert.match(validateRouteRows([fullRoute({ intranet_ip: "10.20.30" })], true) ?? "", /不是合法 IP/); assert.equal(validateRouteRows([fullRoute({ subnet_mask: "255.255.255.0/32" })], true), null); assert.match(validateRouteRows([fullRoute({ up_mbps: "0" })], true) ?? "", /正数/); }); test("回填境外 IP 明细:六项含 PIN/CPID 整行必填,IP 去重", () => { assert.equal(validateBackfillIpRows([]), null, "合同核定数为 0 时允许一行不填"); assert.equal(validateBackfillIpRows([fullIp()]), null); assert.match(validateBackfillIpRows([fullIp({ pin: "" })]) ?? "", /缺PIN/); assert.match(validateBackfillIpRows([fullIp({ cpid: "" })]) ?? "", /缺CPID/); assert.match(validateBackfillIpRows([fullIp({ overseas_ip: "203.0.113" })]) ?? "", /不是合法 IP/); assert.match( validateBackfillIpRows([fullIp(), fullIp({ egress: "中国香港" })]) ?? "", /重复登记/, ); const built = buildBackfillIpRows([fullIp({ pin: " " })]); assert.equal(built.length, 1); }); test("建单人只接受数字身份 ID(后端 parse::,非数字等于没填 ⇒ 组二永不齐)", () => { assert.equal(normalizeBuilderUserId("377168275206836224"), "377168275206836224"); assert.equal(normalizeBuilderUserId(" 377168275206836224 "), "377168275206836224"); assert.equal(normalizeBuilderUserId("klt_bos_ops"), null); assert.equal(normalizeBuilderUserId(undefined), null); }); test("合同核定出口解析:取不到就回空数组,让页面退回手填", () => { assert.deepEqual(parseLineExits('["中国香港","日本东京"]'), ["中国香港", "日本东京"]); assert.deepEqual(parseLineExits(null), []); assert.deepEqual(parseLineExits("{不是 JSON"), []); assert.deepEqual(parseLineExits('["", " "]'), []); }); test("明细回填摊平:读侧 null 落空串,PIN 不回显", () => { const rows = routeRowsFromDetail([ { id: "1", sub_account_id: null, network_account_name: null, intranet_ip: "10.0.0.1", subnet_mask: null, gateway: null, vlan: null, vni: null, src_endpoint: null, dst_endpoint: null, egress_pop: null, down_mbps: null, up_mbps: null, updated_at: null, }, ]); assert.equal(rows[0].sub_account_id, ""); assert.equal(rows[0].subnet_mask, ""); assert.equal(rows[0].intranet_ip, "10.0.0.1"); assert.equal(rows[0].open_this_time, true); const ips = backfillIpRowsFromDetail([ { id: "2", row_no: 1, egress: null, overseas_ip: "203.0.113.9", ip_type: "fixed_ip", bandwidth_mbps: "5", pin_registered: true, cpid_registered: false }, ]); assert.equal(ips[0].pin, "", "库内只有密文,覆盖保存前须重填 PIN"); assert.equal(ips[0].cpid, ""); const apps = appRowsFromDetail([{ app_category: "office", qty: "3" }]); assert.equal(apps.length, APP_CATEGORIES.length); assert.equal(apps.find((a) => a.app_category === "office")?.qty, "3"); assert.deepEqual(buildAppRows(apps), [{ app_category: "office", qty: 3 }]); const hwIps = overseasIpRowsFromDetail([{ row_no: 1, country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }]); assert.deepEqual( hwIps.map(({ key, ...rest }) => rest), [{ country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }], ); assert.equal(validateOverseasIpRows(hwIps, true), null); });