ticket-rows.test.mjs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. assert.deepEqual(buildOverseasIpRows([newOverseasIpRow(" 中国香港 ", "5", "2")]), [
  105. { country: "中国香港", bandwidth_mbps: 5, ip_count: 2 },
  106. ]);
  107. });
  108. test("回填网络线路:整行 12 列缺一列就报出缺哪一列(组一整行必填)", () => {
  109. assert.equal(validateRouteRows([fullRoute()], true), null);
  110. assert.match(validateRouteRows([fullRoute({ gateway: "" })], true) ?? "", /第 1 行线路缺网关/);
  111. assert.match(
  112. validateRouteRows([fullRoute({ vlan: "", vni: "" })], true) ?? "",
  113. /缺VLAN、VNI/,
  114. );
  115. // 只填了一格的半行⛔ 当空行丢掉,必须点名还缺哪些列
  116. const half = { ...newRouteRow(), sub_account_id: "SUB-000023", gateway: "" };
  117. assert.match(validateRouteRows([half], true) ?? "", /第 1 行线路缺网络账户名/);
  118. });
  119. test("回填网络线路:至少一行(开通类),空行不占名额也不入载荷", () => {
  120. assert.match(validateRouteRows([newRouteRow()], true) ?? "", /至少填写一行/);
  121. assert.equal(validateRouteRows([newRouteRow()], false), null);
  122. assert.equal(buildRouteRows([newRouteRow()]).length, 0);
  123. // 未勾「本次开通」:后端只处理 picked ⇒ 既不校验也不入库,前端同口径放行且不下发
  124. const parked = { ...fullRoute(), open_this_time: false };
  125. assert.match(validateRouteRows([parked], true) ?? "", /勾选至少一行/);
  126. assert.equal(validateRouteRows([parked, fullRoute()], true), null);
  127. assert.equal(buildRouteRows([parked, fullRoute()]).length, 1);
  128. const built = buildRouteRows([fullRoute()]);
  129. assert.equal(built.length, 1);
  130. assert.equal(built[0].down_mbps, 100);
  131. assert.equal(built[0].open_this_time, true);
  132. assert.equal(Object.keys(built[0]).length, 13, "RouteIn 十二列 + open_this_time");
  133. });
  134. test("回填网络线路:非法 IP 与非法带宽被拦", () => {
  135. assert.match(validateRouteRows([fullRoute({ intranet_ip: "10.20.30" })], true) ?? "", /不是合法 IP/);
  136. assert.equal(validateRouteRows([fullRoute({ subnet_mask: "255.255.255.0/32" })], true), null);
  137. assert.match(validateRouteRows([fullRoute({ up_mbps: "0" })], true) ?? "", /正数/);
  138. });
  139. test("回填境外 IP 明细:六项含 PIN/CPID 整行必填,IP 去重", () => {
  140. assert.equal(validateBackfillIpRows([]), null, "合同核定数为 0 时允许一行不填");
  141. assert.equal(validateBackfillIpRows([fullIp()]), null);
  142. assert.match(validateBackfillIpRows([fullIp({ pin: "" })]) ?? "", /缺PIN/);
  143. assert.match(validateBackfillIpRows([fullIp({ cpid: "" })]) ?? "", /缺CPID/);
  144. assert.match(validateBackfillIpRows([fullIp({ overseas_ip: "203.0.113" })]) ?? "", /不是合法 IP/);
  145. assert.match(
  146. validateBackfillIpRows([fullIp(), fullIp({ egress: "中国香港" })]) ?? "",
  147. /重复登记/,
  148. );
  149. const built = buildBackfillIpRows([fullIp({ pin: " " })]);
  150. assert.equal(built.length, 1);
  151. });
  152. test("建单人只接受数字身份 ID(后端 parse::<i64>,非数字等于没填 ⇒ 组二永不齐)", () => {
  153. assert.equal(normalizeBuilderUserId("377168275206836224"), "377168275206836224");
  154. assert.equal(normalizeBuilderUserId(" 377168275206836224 "), "377168275206836224");
  155. assert.equal(normalizeBuilderUserId("klt_bos_ops"), null);
  156. assert.equal(normalizeBuilderUserId(undefined), null);
  157. });
  158. test("合同核定出口解析:取不到就回空数组,让页面退回手填", () => {
  159. assert.deepEqual(parseLineExits('["中国香港","日本东京"]'), ["中国香港", "日本东京"]);
  160. assert.deepEqual(parseLineExits(null), []);
  161. assert.deepEqual(parseLineExits("{不是 JSON"), []);
  162. assert.deepEqual(parseLineExits('["", " "]'), []);
  163. });
  164. test("明细回填摊平:读侧 null 落空串,PIN 不回显", () => {
  165. const rows = routeRowsFromDetail([
  166. {
  167. id: "1", sub_account_id: null, network_account_name: null, intranet_ip: "10.0.0.1",
  168. subnet_mask: null, gateway: null, vlan: null, vni: null, src_endpoint: null,
  169. dst_endpoint: null, egress_pop: null, down_mbps: null, up_mbps: null, updated_at: null,
  170. },
  171. ]);
  172. assert.equal(rows[0].sub_account_id, "");
  173. assert.equal(rows[0].subnet_mask, "");
  174. assert.equal(rows[0].intranet_ip, "10.0.0.1");
  175. assert.equal(rows[0].open_this_time, true);
  176. const ips = backfillIpRowsFromDetail([
  177. { 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 },
  178. ]);
  179. assert.equal(ips[0].pin, "", "库内只有密文,覆盖保存前须重填 PIN");
  180. assert.equal(ips[0].cpid, "");
  181. const apps = appRowsFromDetail([{ app_category: "office", qty: "3" }]);
  182. assert.equal(apps.length, APP_CATEGORIES.length);
  183. assert.equal(apps.find((a) => a.app_category === "office")?.qty, "3");
  184. assert.deepEqual(buildAppRows(apps), [{ app_category: "office", qty: 3 }]);
  185. const hwIps = overseasIpRowsFromDetail([{ row_no: 1, country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }]);
  186. assert.deepEqual(
  187. hwIps.map(({ key, ...rest }) => rest),
  188. [{ country: "中国香港", bandwidth_mbps: "10", ip_count: "2" }],
  189. );
  190. assert.equal(validateOverseasIpRows(hwIps, true), null);
  191. });