ticket-rows.test.mjs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env node
  2. /**
  3. * 工单明细行校验(交接单开通应用/境外 IP、回填网络线路/境外 IP 明细)。
  4. *
  5. * 存在理由:开户工单的处理链此前在这两步被界面卡死——交接单只收声明四项,
  6. * 提交必回 `60408 handover_all_zero`;施工回填既没有 `route_rows` 也没有 `ip_rows`
  7. * 与建单人,提交必回 `60404` 必填组不齐。这两张表的「整行必填」口径⛔ 散在组件里,
  8. * 本测试把后端 `handlers/handle.rs` 的逐行必填口径钉死在这里。
  9. */
  10. import test from "node:test";
  11. import assert from "node:assert/strict";
  12. import {
  13. APP_CATEGORIES,
  14. appRowsFromDetail,
  15. backfillIpRowsFromDetail,
  16. buildAppRows,
  17. buildBackfillIpRows,
  18. buildOverseasIpRows,
  19. buildRouteRows,
  20. defaultAppRows,
  21. newAppRow,
  22. newBackfillIpRow,
  23. newOverseasIpRow,
  24. newRouteRow,
  25. normalizeBuilderUserId,
  26. overseasIpRowsFromDetail,
  27. parseLineExits,
  28. routeRowsFromDetail,
  29. validateAppRows,
  30. validateBackfillIpRows,
  31. validateOverseasIpRows,
  32. validateRouteRows,
  33. } from "../src/lib/ticketRows.ts";
  34. const fullRoute = (over = {}) => ({
  35. ...newRouteRow(),
  36. sub_account_id: "SUB-000023",
  37. network_account_name: "hailan-office-01",
  38. intranet_ip: "10.20.30.11",
  39. subnet_mask: "255.255.255.0",
  40. gateway: "10.20.30.1",
  41. vlan: "120",
  42. vni: "50120",
  43. src_endpoint: "上海临港POP",
  44. dst_endpoint: "中国香港POP",
  45. egress_pop: "中国香港",
  46. down_mbps: "100",
  47. up_mbps: "50",
  48. ...over,
  49. });
  50. const fullIp = (over = {}) => ({
  51. ...newBackfillIpRow(),
  52. egress: "中国香港",
  53. overseas_ip: "203.0.113.7",
  54. ip_type: "fixed_ip",
  55. bandwidth_mbps: "10",
  56. pin: "PIN-8842",
  57. cpid: "CPID-8842",
  58. ...over,
  59. });
  60. test("交接单应用类别固定五类,编码与后端 APP_CATEGORIES 逐项对齐", () => {
  61. assert.deepEqual(
  62. APP_CATEGORIES.map((c) => c.code),
  63. ["browse", "office", "operation", "livestream", "custom"],
  64. );
  65. assert.equal(APP_CATEGORIES.every((c) => c.label.length > 0), true);
  66. });
  67. test("开通应用:整行留空的行静默丢弃,但至少要有数量为正整数的一行(后端 60408)", () => {
  68. assert.match(validateAppRows(defaultAppRows()) ?? "", /至少为一个应用类别/);
  69. assert.match(validateAppRows([newAppRow("office", "0")]) ?? "", /至少为一个应用类别/);
  70. assert.match(validateAppRows([newAppRow("office", "-3")]) ?? "", /须为整数/);
  71. assert.match(validateAppRows([newAppRow("office", "2.5")]) ?? "", /须为整数/);
  72. assert.equal(validateAppRows([newAppRow("office", "2")]), null);
  73. // 只填数量没选类别 ⇒ 半填行,⛔ 当成空行提交
  74. assert.match(validateAppRows([newAppRow("", "2")]) ?? "", /请填写应用类别/);
  75. });
  76. test("开通应用:同一类别重复登记被拦(库内 uq_handover_app 唯一键)", () => {
  77. assert.match(
  78. validateAppRows([newAppRow("office", "2"), newAppRow("office", "3")]) ?? "",
  79. /重复登记/,
  80. );
  81. });
  82. test("开通应用载荷:空行与零数量不入载荷,数量收敛为数字", () => {
  83. const rows = [newAppRow("office", "2"), newAppRow("browse", ""), newAppRow("custom", "0")];
  84. assert.equal(validateAppRows(rows), null);
  85. assert.deepEqual(buildAppRows(rows), [{ app_category: "office", qty: 2 }]);
  86. });
  87. test("交接单境外 IP:整行必填且至少一行", () => {
  88. const noCountry = { ...newOverseasIpRow(), country: "", ip_count: "1" };
  89. assert.match(validateOverseasIpRows([noCountry], true) ?? "", /国家/地区/);
  90. assert.match(
  91. validateOverseasIpRows([newOverseasIpRow("中国香港", "7", "1")], true) ?? "",
  92. /5/10/30/,
  93. );
  94. assert.match(
  95. validateOverseasIpRows([newOverseasIpRow("中国香港", "10", "0")], true) ?? "",
  96. /正整数/,
  97. );
  98. assert.equal(validateOverseasIpRows([newOverseasIpRow("中国香港", "10", "1")], true), null);
  99. // 整行一个字节没动过(把预填的带宽档位也清掉)⇒ 不算一行:报「至少登记一行」,也不入载荷
  100. const untouched = { ...newOverseasIpRow(), bandwidth_mbps: "" };
  101. assert.match(validateOverseasIpRows([untouched], true) ?? "", /至少登记一行/);
  102. assert.equal(validateOverseasIpRows([untouched], false), null);
  103. assert.equal(buildOverseasIpRows([untouched]).length, 0);
  104. // 合同未核定境外 IP ⇒ 交接单允许整行删空(0 行):校验放行且载荷为空数组,⛔ 逼运维编一条假 IP
  105. assert.equal(validateOverseasIpRows([], false), null);
  106. assert.deepEqual(buildOverseasIpRows([]), []);
  107. assert.deepEqual(buildOverseasIpRows([newOverseasIpRow(" 中国香港 ", "5", "2")]), [
  108. { country: "中国香港", bandwidth_mbps: 5, ip_count: 2 },
  109. ]);
  110. });
  111. test("回填网络线路:整行 12 列缺一列就报出缺哪一列(组一整行必填)", () => {
  112. assert.equal(validateRouteRows([fullRoute()], true), null);
  113. assert.match(validateRouteRows([fullRoute({ gateway: "" })], true) ?? "", /第 1 行线路缺网关/);
  114. assert.match(
  115. validateRouteRows([fullRoute({ vlan: "", vni: "" })], true) ?? "",
  116. /缺VLAN、VNI/,
  117. );
  118. // 只填了一格的半行⛔ 当空行丢掉,必须点名还缺哪些列
  119. const half = { ...newRouteRow(), sub_account_id: "SUB-000023", gateway: "" };
  120. assert.match(validateRouteRows([half], true) ?? "", /第 1 行线路缺网络账户名/);
  121. });
  122. test("回填网络线路:至少一行(开通类),空行不占名额也不入载荷", () => {
  123. assert.match(validateRouteRows([newRouteRow()], true) ?? "", /至少填写一行/);
  124. assert.equal(validateRouteRows([newRouteRow()], false), null);
  125. assert.equal(buildRouteRows([newRouteRow()]).length, 0);
  126. // 未勾「本次开通」:后端只处理 picked ⇒ 既不校验也不入库,前端同口径放行且不下发
  127. const parked = { ...fullRoute(), open_this_time: false };
  128. assert.match(validateRouteRows([parked], true) ?? "", /勾选至少一行/);
  129. assert.equal(validateRouteRows([parked, fullRoute()], true), null);
  130. assert.equal(buildRouteRows([parked, fullRoute()]).length, 1);
  131. const built = buildRouteRows([fullRoute()]);
  132. assert.equal(built.length, 1);
  133. assert.equal(built[0].down_mbps, 100);
  134. assert.equal(built[0].open_this_time, true);
  135. assert.equal(Object.keys(built[0]).length, 13, "RouteIn 十二列 + open_this_time");
  136. });
  137. test("回填网络线路:非法 IP 与非法带宽被拦", () => {
  138. assert.match(validateRouteRows([fullRoute({ intranet_ip: "10.20.30" })], true) ?? "", /不是合法 IP/);
  139. assert.equal(validateRouteRows([fullRoute({ subnet_mask: "255.255.255.0/32" })], true), null);
  140. assert.match(validateRouteRows([fullRoute({ up_mbps: "0" })], true) ?? "", /正数/);
  141. });
  142. test("回填境外 IP 明细:六项含 PIN/CPID 整行必填,IP 去重", () => {
  143. assert.equal(validateBackfillIpRows([]), null, "合同核定数为 0 时允许一行不填");
  144. assert.equal(validateBackfillIpRows([fullIp()]), null);
  145. assert.match(validateBackfillIpRows([fullIp({ pin: "" })]) ?? "", /缺PIN/);
  146. assert.match(validateBackfillIpRows([fullIp({ cpid: "" })]) ?? "", /缺CPID/);
  147. assert.match(validateBackfillIpRows([fullIp({ overseas_ip: "203.0.113" })]) ?? "", /不是合法 IP/);
  148. assert.match(
  149. validateBackfillIpRows([fullIp(), fullIp({ egress: "中国香港" })]) ?? "",
  150. /重复登记/,
  151. );
  152. const built = buildBackfillIpRows([fullIp({ pin: " " })]);
  153. assert.equal(built.length, 1);
  154. });
  155. test("建单人只接受数字身份 ID(后端 parse::<i64>,非数字等于没填 ⇒ 组二永不齐)", () => {
  156. assert.equal(normalizeBuilderUserId("377168275206836224"), "377168275206836224");
  157. assert.equal(normalizeBuilderUserId(" 377168275206836224 "), "377168275206836224");
  158. assert.equal(normalizeBuilderUserId("klt_bos_ops"), null);
  159. assert.equal(normalizeBuilderUserId(undefined), null);
  160. });
  161. test("合同核定出口解析:取不到就回空数组,让页面退回手填", () => {
  162. assert.deepEqual(parseLineExits('["中国香港","日本东京"]'), ["中国香港", "日本东京"]);
  163. assert.deepEqual(parseLineExits(null), []);
  164. assert.deepEqual(parseLineExits("{不是 JSON"), []);
  165. assert.deepEqual(parseLineExits('["", " "]'), []);
  166. });
  167. test("明细回填摊平:读侧 null 落空串,PIN 不回显", () => {
  168. const rows = routeRowsFromDetail([
  169. {
  170. id: "1", sub_account_id: null, network_account_name: null, intranet_ip: "10.0.0.1",
  171. subnet_mask: null, gateway: null, vlan: null, vni: null, src_endpoint: null,
  172. dst_endpoint: null, egress_pop: null, down_mbps: null, up_mbps: null, updated_at: null,
  173. },
  174. ]);
  175. assert.equal(rows[0].sub_account_id, "");
  176. assert.equal(rows[0].subnet_mask, "");
  177. assert.equal(rows[0].intranet_ip, "10.0.0.1");
  178. assert.equal(rows[0].open_this_time, true);
  179. const ips = backfillIpRowsFromDetail([
  180. { 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 },
  181. ]);
  182. assert.equal(ips[0].pin, "", "库内只有密文,覆盖保存前须重填 PIN");
  183. assert.equal(ips[0].cpid, "");
  184. const apps = appRowsFromDetail([{ app_category: "office", qty: "3" }]);
  185. assert.equal(apps.length, APP_CATEGORIES.length);
  186. assert.equal(apps.find((a) => a.app_category === "office")?.qty, "3");
  187. assert.deepEqual(buildAppRows(apps), [{ app_category: "office", qty: 3 }]);
  188. const hwIps = overseasIpRowsFromDetail([{ row_no: 1, country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }]);
  189. assert.deepEqual(
  190. hwIps.map(({ key, ...rest }) => rest),
  191. [{ country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }],
  192. );
  193. assert.equal(validateOverseasIpRows(hwIps, true), null);
  194. });