ticket-rows.test.mjs 7.7 KB

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