board-logic.test.mjs 15 KB

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