board-logic.test.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import {createHash} from "node:crypto";
  4. import {readFileSync} from "node:fs";
  5. import {
  6. connectorPinGroups,
  7. findCandidates,
  8. generateAdcSnippet,
  9. isValidRtlPortName,
  10. sameNetPins,
  11. searchBoard,
  12. } from "../../tools/board-viewer/web/board-logic.mjs";
  13. function pin(overrides = {}) {
  14. return {
  15. id: "fixture-pin",
  16. number: "1",
  17. pad_role: "contact",
  18. net: "UART_A_TX",
  19. summary: "UART transmit",
  20. path: ["CON10.1", "UART_A_TX", "FPGA A1"],
  21. mechanical: false,
  22. electrical: true,
  23. direction: "output",
  24. external_level: "3.3V LVCMOS",
  25. function_category: "uart_tx",
  26. reserved: false,
  27. fixed_load_conflict: false,
  28. board_pull: null,
  29. adc_pulltype: null,
  30. programming: {
  31. status: "verified_fpga",
  32. conclusion: "FPGA 输出 UART_A_TX",
  33. network_explanation: "连接器网络 UART_A_TX 直达 FPGA",
  34. hdl_guidance: "RTL 端口方向应为 output",
  35. connection_guidance: ["连接到对端设备的接收端(RX)。"],
  36. constraints: ["LOCATION = A1", "IOSTANDARD = LVCMOS33"],
  37. risks: ["仍需检查当前工程约束冲突。"],
  38. evidence_tier: "cross_verified",
  39. sources: ["fixture"],
  40. },
  41. evidence_tier: "cross_verified",
  42. fpga: {
  43. ball: "A1",
  44. board_signal: "UART_A_TX",
  45. io_standard: "LVCMOS33",
  46. capabilities: ["gpio", "uart_tx"],
  47. evidence_tier: "cross_verified",
  48. sources: ["fixture"],
  49. },
  50. recommendation: {eligible: true, reasons: []},
  51. ...overrides,
  52. };
  53. }
  54. function boardData() {
  55. return {
  56. touchpoints: [
  57. {
  58. reference: "CON78",
  59. category: "CON",
  60. summary: "UART2 RS-232 connector through U23",
  61. pins: [
  62. pin({
  63. number: "1",
  64. net: "RS232_TX2",
  65. path: ["CON78.1 RS232_TX2", "U23", "UART2_TX", "FPGA M10"],
  66. external_level: "RS232",
  67. reserved: true,
  68. fpga: {
  69. ball: "M10",
  70. board_signal: "UART2_TX",
  71. io_standard: "LVCMOS33",
  72. capabilities: ["gpio", "uart_tx", "rs232_via_transceiver"],
  73. evidence_tier: "cross_verified",
  74. sources: ["fixture"],
  75. },
  76. recommendation: {eligible: false, reasons: ["reserved"]},
  77. programming: {
  78. status: "verified_fpga",
  79. conclusion: "FPGA 输出 UART2_TX 经 U23 转为 RS-232",
  80. network_explanation: "连接器侧网络 RS232_TX2 对应逻辑侧 UART2_TX",
  81. hdl_guidance: "RTL 端口方向应为 output",
  82. connection_guidance: ["连接到对端设备的接收端(RX)。", "不能直接接 3.3V TTL/CMOS 串口。"],
  83. constraints: ["LOCATION = M10", "IOSTANDARD = LVCMOS33"],
  84. risks: ["当前功能已保留。"],
  85. evidence_tier: "cross_verified",
  86. sources: ["fixture"],
  87. },
  88. }),
  89. ],
  90. },
  91. {
  92. reference: "CON10",
  93. category: "CON",
  94. summary: "Verified UART fixture",
  95. pins: [pin()],
  96. },
  97. {
  98. reference: "J2",
  99. category: "J",
  100. summary: "PWM fixture",
  101. pins: [
  102. pin({
  103. number: "7",
  104. net: "FAN_PWM_TEST",
  105. summary: "Fan PWM",
  106. function_category: "pwm",
  107. board_pull: "10k external pull-up to 3.3V",
  108. fpga: {
  109. ball: "B2",
  110. board_signal: "FAN_PWM_TEST",
  111. io_standard: "LVCMOS33",
  112. capabilities: ["gpio", "gpio_output", "pwm"],
  113. evidence_tier: "cross_verified",
  114. sources: ["fixture"],
  115. },
  116. }),
  117. pin({
  118. number: "8",
  119. net: "FAN_PWM_PULLTYPE",
  120. function_category: "pwm",
  121. adc_pulltype: "PULLUP",
  122. fpga: {
  123. ball: "B3",
  124. board_signal: "FAN_PWM_PULLTYPE",
  125. io_standard: "LVCMOS33",
  126. capabilities: ["gpio", "gpio_output", "pwm"],
  127. evidence_tier: "cross_verified",
  128. sources: ["fixture"],
  129. },
  130. }),
  131. ],
  132. },
  133. {
  134. reference: "TP3",
  135. category: "TP",
  136. summary: "Unverified test point",
  137. pins: [
  138. pin({
  139. net: "MYSTERY",
  140. external_level: "unknown",
  141. evidence_tier: "unknown",
  142. fpga: null,
  143. recommendation: {eligible: false, reasons: ["pin_evidence_not_verified"]},
  144. }),
  145. ],
  146. },
  147. ],
  148. };
  149. }
  150. test("search covers reference, pin, net, function, capability, and FPGA ball", () => {
  151. const data = boardData();
  152. assert.equal(searchBoard(data, "CON78")[0].reference, "CON78");
  153. assert.equal(searchBoard(data, "CON78.1")[0].pinNumber, "1");
  154. assert.equal(searchBoard(data, "rs232_tx2")[0].reference, "CON78");
  155. assert.equal(searchBoard(data, "uart_tx")[0].reference, "CON10");
  156. assert.equal(searchBoard(data, "rs232_via_transceiver")[0].reference, "CON78");
  157. assert.equal(searchBoard(data, "m10")[0].pinNumber, "1");
  158. assert.ok(searchBoard(data, "对端设备的接收端").some((result) => result.reference === "CON78"));
  159. assert.equal(searchBoard(data, "不能直接接 3.3v ttl")[0].reference, "CON78");
  160. assert.deepEqual(searchBoard(data, "does-not-exist"), []);
  161. });
  162. test("search covers verified MCU device, package pin, signal, and capability", () => {
  163. const data = boardData();
  164. const mcuPin = data.touchpoints.find((item) => item.reference === "TP3").pins[0];
  165. mcuPin.summary = "GD32 firmware test point";
  166. mcuPin.path = [];
  167. mcuPin.programming = {
  168. status: "verified_mcu",
  169. conclusion: "Verified MCU test point",
  170. network_explanation: "Verified MCU path",
  171. hdl_guidance: "Use from firmware",
  172. connection_guidance: [],
  173. constraints: [],
  174. risks: [],
  175. evidence_tier: "cross_verified",
  176. sources: ["fixture"],
  177. };
  178. mcuPin.mcu = {
  179. device: "U27",
  180. family: "GD32F303CGT6",
  181. pin: "10",
  182. signal: "PA0",
  183. capabilities: ["gpio", "adc_input"],
  184. evidence_tier: "cross_verified",
  185. sources: ["fixture"],
  186. };
  187. assert.equal(searchBoard(data, "u27.10")[0].reference, "TP3");
  188. assert.equal(searchBoard(data, "gd32f303")[0].reference, "TP3");
  189. assert.equal(searchBoard(data, "pa0")[0].reference, "TP3");
  190. assert.equal(searchBoard(data, "adc_input")[0].reference, "TP3");
  191. assert.ok(!findCandidates(data, "gpio").some((item) => item.reference === "TP3"));
  192. });
  193. test("same-net lookup returns every matching pad and ignores empty nets", () => {
  194. const data = boardData();
  195. data.touchpoints[1].pins.push(pin({number: "2", net: "RS232_TX2"}));
  196. assert.deepEqual(
  197. sameNetPins(data, "RS232_TX2").map((item) => `${item.reference}.${item.pinNumber}`),
  198. ["CON10.2", "CON78.1"],
  199. );
  200. assert.deepEqual(sameNetPins(data, ""), []);
  201. });
  202. test("candidate filtering is fail-closed and sorted by natural reference and pin", () => {
  203. const candidates = findCandidates(boardData(), "uart_tx");
  204. assert.deepEqual(candidates.map((item) => `${item.reference}.${item.pin.number}`), ["CON10.1"]);
  205. assert.match(candidates[0].reason, /已验证 FPGA 路径/);
  206. assert.match(candidates[0].warning, /当前工程约束冲突/);
  207. });
  208. test("candidate filters recognize canonical board capability aliases", () => {
  209. const aliases = [
  210. ["pwm", "pwm_output"],
  211. ["gpio_output", "digital_output"],
  212. ["gpio_input", "digital_input"],
  213. ["tach", "tach_input"],
  214. ["fan_tach", "tach_input"],
  215. ["presence", "presence_input"],
  216. ];
  217. for (const [filter, capability] of aliases) {
  218. const data = {
  219. touchpoints: [{
  220. reference: "CON1",
  221. category: "CON",
  222. summary: "Alias fixture",
  223. pins: [pin({
  224. fpga: {
  225. ball: "A1",
  226. board_signal: "ALIAS_SIGNAL",
  227. io_standard: "LVCMOS33",
  228. capabilities: ["gpio", capability],
  229. evidence_tier: "cross_verified",
  230. sources: ["fixture"],
  231. },
  232. })],
  233. }],
  234. };
  235. assert.equal(findCandidates(data, filter).length, 1, `${filter} -> ${capability}`);
  236. }
  237. });
  238. test("generic GPIO candidates exclude dedicated tach, PWM, presence, and protocol pins", () => {
  239. const data = {
  240. touchpoints: [{
  241. reference: "CON1",
  242. category: "CON",
  243. summary: "Mixed GPIO fixture",
  244. pins: [
  245. pin({number: "1", function_category: "gpio", summary: "Direct bidirectional GPIO"}),
  246. pin({number: "2", function_category: "fan_tach", summary: "Fan tach input"}),
  247. pin({number: "3", function_category: "pwm", summary: "Open-collector PWM"}),
  248. pin({number: "4", function_category: "presence", summary: "Presence input"}),
  249. ],
  250. }],
  251. };
  252. assert.deepEqual(
  253. findCandidates(data, "gpio").map((item) => `${item.reference}.${item.pin.number}`),
  254. ["CON1.1"],
  255. );
  256. });
  257. test("candidate reason exposes the pin purpose and non-default polarity", () => {
  258. const data = {
  259. touchpoints: [{
  260. reference: "CON31",
  261. category: "CON",
  262. summary: "Fan connector",
  263. pins: [pin({
  264. function_category: "pwm",
  265. summary: "风扇 PWM 开集电极下拉输出",
  266. active_level: "connector low",
  267. polarity: "FPGA high -> connector low",
  268. fpga: {
  269. ball: "E10",
  270. board_signal: "FAN_GPIO6",
  271. io_standard: "LVCMOS33",
  272. capabilities: ["gpio", "pwm_output", "fan_pwm"],
  273. evidence_tier: "cross_verified",
  274. sources: ["fixture"],
  275. },
  276. })],
  277. }],
  278. };
  279. const [candidate] = findCandidates(data, "pwm");
  280. assert.match(candidate.reason, /风扇 PWM 开集电极下拉输出/);
  281. assert.match(candidate.reason, /FPGA high -> connector low/);
  282. });
  283. test("product candidate filters keep dedicated functions out of generic GPIO", () => {
  284. const boardDataPath = new URL("../../tools/board-viewer/generated/board-data.json", import.meta.url);
  285. const buildInfoPath = new URL("../../tools/board-viewer/generated/build-info.json", import.meta.url);
  286. const expectationPath = new URL("./fixtures/candidate_count_expectation.json", import.meta.url);
  287. const boardDataBytes = readFileSync(boardDataPath);
  288. const buildInfoBytes = readFileSync(buildInfoPath);
  289. const data = JSON.parse(boardDataBytes);
  290. const expectation = JSON.parse(readFileSync(expectationPath, "utf8"));
  291. const boardDataSha = createHash("sha256").update(boardDataBytes).digest("hex");
  292. const buildInfoSha = createHash("sha256").update(buildInfoBytes).digest("hex");
  293. assert.equal(expectation.schema, "prod2u-board-viewer-candidate-count-expectation-v1");
  294. assert.equal(boardDataSha, expectation.bound_to.board_data_sha256);
  295. assert.equal(buildInfoSha, expectation.bound_to.build_info_sha256);
  296. assert.equal(boardDataSha, expectation.bound_to.board_data_asset_sha256);
  297. for (const [capability, expectedCount] of Object.entries(expectation.candidate_counts)) {
  298. const candidates = findCandidates(data, capability);
  299. assert.equal(candidates.length, expectedCount, `${capability} count`);
  300. if (Array.isArray(expectation.candidate_keys?.[capability])) {
  301. assert.deepEqual(
  302. candidates.map((item) => `${item.reference}.${item.pin.number}`),
  303. expectation.candidate_keys[capability],
  304. `${capability} keys`,
  305. );
  306. }
  307. }
  308. assert.ok(findCandidates(data, "gpio").every((item) => item.pin.function_category === "gpio"));
  309. });
  310. test("unknown, inferred, power, ground, mechanical, reserved, and fixed-load pins are never candidates", () => {
  311. const blockers = [
  312. {evidence_tier: "unknown"},
  313. {evidence_tier: "name_inferred"},
  314. {function_category: "power", net: "+3V3"},
  315. {function_category: "ground", net: "GND"},
  316. {mechanical: true, electrical: false},
  317. {pad_role: "shield"},
  318. {pad_role: "unknown"},
  319. {reserved: true},
  320. {fixed_load_conflict: true},
  321. ];
  322. for (const [index, override] of blockers.entries()) {
  323. const data = {touchpoints: [{reference: `J${index + 1}`, category: "J", summary: "", pins: [pin(override)]}]};
  324. assert.deepEqual(findCandidates(data, "uart_tx"), []);
  325. }
  326. });
  327. test("connector groups follow reviewed board-front order and separate auxiliary pads", () => {
  328. const touchpoint = {
  329. reference: "CON78",
  330. connector: {
  331. review_status: "cross_verified",
  332. contact_order: ["1", "2", "3", "4"],
  333. contact_grid: [["1"], ["2"], ["3"], ["4"]],
  334. },
  335. pins: [
  336. pin({id: "shield-5", number: "5", pad_role: "shield"}),
  337. pin({id: "contact-4", number: "4"}),
  338. pin({id: "contact-2", number: "2"}),
  339. pin({id: "mechanical-1", number: "", pad_role: "mechanical", mechanical: true, electrical: false}),
  340. pin({id: "contact-1", number: "1"}),
  341. pin({id: "contact-3", number: "3"}),
  342. ],
  343. };
  344. const groups = connectorPinGroups(touchpoint);
  345. assert.deepEqual(groups.contactRows.map((row) => row.map((item) => item.number)), [["1"], ["2"], ["3"], ["4"]]);
  346. assert.deepEqual(groups.contacts.map((item) => item.number), ["1", "2", "3", "4"]);
  347. assert.deepEqual(groups.auxiliary.map((item) => item.id), ["shield-5", "mechanical-1"]);
  348. });
  349. test("unknown connector layout never invents contact ordering", () => {
  350. const touchpoint = {
  351. reference: "CON1",
  352. connector: {review_status: "unknown", contact_order: [], contact_grid: []},
  353. pins: [pin({id: "unknown-1", pad_role: "unknown"})],
  354. };
  355. assert.deepEqual(connectorPinGroups(touchpoint), {
  356. contactRows: [],
  357. contacts: [],
  358. auxiliary: [touchpoint.pins[0]],
  359. });
  360. });
  361. test("RTL port names reject syntax that could escape an ADC assignment", () => {
  362. for (const name of ["uart_tx", "fan_pwm_out[3]", "_debug$signal"]) {
  363. assert.equal(isValidRtlPortName(name), true, name);
  364. }
  365. for (const name of ["", "3bad", "a b", "a;set", "name[foo]", "{signal}", "a\nnext"]) {
  366. assert.equal(isValidRtlPortName(name), false, name);
  367. }
  368. });
  369. test("ADC generation requires current verified eligible data and omits implicit PULLTYPE", () => {
  370. const data = boardData();
  371. const pwm = data.touchpoints[2].pins[0];
  372. const snippet = generateAdcSnippet(pwm, "fan_pwm_out[3]", {adcCopyEnabled: true});
  373. assert.equal(
  374. snippet,
  375. "set_pin_assignment { fan_pwm_out[3] } { LOCATION = B2; IOSTANDARD = LVCMOS33; }",
  376. );
  377. assert.doesNotMatch(snippet, /PULLTYPE/);
  378. assert.throws(() => generateAdcSnippet(pwm, "a;bad", {adcCopyEnabled: true}), /合法的 RTL 端口名/);
  379. assert.throws(() => generateAdcSnippet(pwm, "fan_pwm_out", {adcCopyEnabled: false}), /源数据已过期/);
  380. assert.throws(
  381. () => generateAdcSnippet(data.touchpoints[0].pins[0], "uart2_tx", {adcCopyEnabled: true}),
  382. /不具备 ADC 生成资格/,
  383. );
  384. });
  385. test("ADC generation includes PULLTYPE only when curated annotation explicitly supplies it", () => {
  386. const pinWithPulltype = boardData().touchpoints[2].pins[1];
  387. assert.equal(
  388. generateAdcSnippet(pinWithPulltype, "fan_pwm_aux", {adcCopyEnabled: true}),
  389. "set_pin_assignment { fan_pwm_aux } { LOCATION = B3; IOSTANDARD = LVCMOS33; PULLTYPE = PULLUP; }",
  390. );
  391. });