board-logic.test.mjs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. BACK_MIRROR_AXIS,
  8. buildBoardIndex,
  9. connectorPinGroups,
  10. displayBounds,
  11. displayPoint,
  12. extractPathResistors,
  13. findCandidates,
  14. generateAdcSnippet,
  15. identityMatch,
  16. isValidRtlPortName,
  17. itemVisibleOnSide,
  18. mirrorBoardX,
  19. parseBoardQuery,
  20. resolveExactNetName,
  21. sameNetMembers,
  22. sameNetPins,
  23. searchBoard,
  24. } from "../../tools/board-viewer/web/board-logic.mjs";
  25. function resolveGeneratedAsset(name) {
  26. // Live repo: tools/board-viewer/generated; compact package: evidence/board-viewer.
  27. const candidates = [
  28. new URL(`../../tools/board-viewer/generated/${name}`, import.meta.url),
  29. new URL(`../../evidence/board-viewer/${name}`, import.meta.url),
  30. new URL(`../evidence/board-viewer/${name}`, import.meta.url),
  31. ];
  32. for (const url of candidates) {
  33. const path = fileURLToPath(url);
  34. if (existsSync(path)) {
  35. return url;
  36. }
  37. }
  38. return candidates[0];
  39. }
  40. function pin(overrides = {}) {
  41. return {
  42. id: "fixture-pin",
  43. number: "1",
  44. pad_role: "contact",
  45. net: "UART_A_TX",
  46. summary: "UART transmit",
  47. path: ["CON10.1", "UART_A_TX", "FPGA A1"],
  48. mechanical: false,
  49. electrical: true,
  50. direction: "output",
  51. external_level: "3.3V LVCMOS",
  52. function_category: "uart_tx",
  53. reserved: false,
  54. fixed_load_conflict: false,
  55. board_pull: null,
  56. adc_pulltype: null,
  57. programming: {
  58. status: "verified_fpga",
  59. conclusion: "FPGA 输出 UART_A_TX",
  60. network_explanation: "连接器网络 UART_A_TX 直达 FPGA",
  61. hdl_guidance: "RTL 端口方向应为 output",
  62. connection_guidance: ["连接到对端设备的接收端(RX)。"],
  63. constraints: ["LOCATION = A1", "IOSTANDARD = LVCMOS33"],
  64. risks: ["仍需检查当前工程约束冲突。"],
  65. evidence_tier: "cross_verified",
  66. sources: ["fixture"],
  67. },
  68. evidence_tier: "cross_verified",
  69. fpga: {
  70. ball: "A1",
  71. board_signal: "UART_A_TX",
  72. io_standard: "LVCMOS33",
  73. capabilities: ["gpio", "uart_tx"],
  74. evidence_tier: "cross_verified",
  75. sources: ["fixture"],
  76. },
  77. recommendation: {eligible: true, reasons: []},
  78. ...overrides,
  79. };
  80. }
  81. function resistor(overrides = {}) {
  82. return {
  83. reference: "R12",
  84. category: "R",
  85. component_value: "22R",
  86. value_ohms: 22,
  87. value_tokens: ["22r", "22", "22ohm"],
  88. summary: "电阻 22R · NET_A↔NET_B",
  89. position: {x: 1, y: 1},
  90. view_position: {x: 1, y: 1},
  91. rotation: 0,
  92. bounds: {x: 0, y: 0, width: 1, height: 1},
  93. view_bounds: {x: 0, y: 0, width: 1, height: 1},
  94. side: "front",
  95. pads: [
  96. {
  97. number: "1",
  98. net: "NET_A",
  99. position: {x: 0, y: 0},
  100. view_position: {x: 0, y: 0},
  101. pad_role: "electrical",
  102. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  103. },
  104. {
  105. number: "2",
  106. net: "NET_B",
  107. position: {x: 1, y: 0},
  108. view_position: {x: 1, y: 0},
  109. pad_role: "electrical",
  110. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  111. },
  112. ],
  113. ...overrides,
  114. };
  115. }
  116. function boardData() {
  117. return {
  118. resistors: [
  119. resistor(),
  120. resistor({
  121. reference: "R1225",
  122. component_value: "10k",
  123. value_ohms: 10000,
  124. value_tokens: ["10k", "10000", "10000r"],
  125. pads: [
  126. {
  127. number: "1",
  128. net: "OTHER",
  129. position: {x: 2, y: 0},
  130. view_position: {x: 2, y: 0},
  131. pad_role: "electrical",
  132. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  133. },
  134. {
  135. number: "2",
  136. net: "OTHER2",
  137. position: {x: 3, y: 0},
  138. view_position: {x: 3, y: 0},
  139. pad_role: "electrical",
  140. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  141. },
  142. ],
  143. }),
  144. resistor({
  145. reference: "R7",
  146. component_value: "4.7k",
  147. value_ohms: 4700,
  148. value_tokens: ["4.7k", "4k7", "4700", "4700r"],
  149. pads: [
  150. {
  151. number: "1",
  152. net: "PUL1",
  153. position: {x: 4, y: 0},
  154. view_position: {x: 4, y: 0},
  155. pad_role: "electrical",
  156. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  157. },
  158. {
  159. number: "2",
  160. net: "GND",
  161. position: {x: 5, y: 0},
  162. view_position: {x: 5, y: 0},
  163. pad_role: "electrical",
  164. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  165. },
  166. ],
  167. }),
  168. resistor({
  169. reference: "R674",
  170. component_value: "",
  171. value_ohms: null,
  172. value_tokens: [],
  173. pads: [
  174. {
  175. number: "1",
  176. net: "BANK0_GPIO15",
  177. position: {x: 6, y: 0},
  178. view_position: {x: 6, y: 0},
  179. pad_role: "electrical",
  180. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  181. },
  182. {
  183. number: "2",
  184. net: "PUL1",
  185. position: {x: 7, y: 0},
  186. view_position: {x: 7, y: 0},
  187. pad_role: "electrical",
  188. size: {width: 0.5, height: 0.5, drill_width: 0, drill_height: 0},
  189. },
  190. ],
  191. }),
  192. ],
  193. touchpoints: [
  194. {
  195. reference: "CON78",
  196. category: "CON",
  197. summary: "UART2 RS-232 connector through U23",
  198. pins: [
  199. pin({
  200. number: "1",
  201. net: "RS232_TX2",
  202. path: ["CON78.1 RS232_TX2", "U23", "UART2_TX", "FPGA M10"],
  203. external_level: "RS232",
  204. reserved: true,
  205. fpga: {
  206. ball: "M10",
  207. board_signal: "UART2_TX",
  208. io_standard: "LVCMOS33",
  209. capabilities: ["gpio", "uart_tx", "rs232_via_transceiver"],
  210. evidence_tier: "cross_verified",
  211. sources: ["fixture"],
  212. },
  213. recommendation: {eligible: false, reasons: ["reserved"]},
  214. programming: {
  215. status: "verified_fpga",
  216. conclusion: "FPGA 输出 UART2_TX 经 U23 转为 RS-232",
  217. network_explanation: "连接器侧网络 RS232_TX2 对应逻辑侧 UART2_TX",
  218. hdl_guidance: "RTL 端口方向应为 output",
  219. connection_guidance: ["连接到对端设备的接收端(RX)。", "不能直接接 3.3V TTL/CMOS 串口。"],
  220. constraints: ["LOCATION = M10", "IOSTANDARD = LVCMOS33"],
  221. risks: ["当前功能已保留。"],
  222. evidence_tier: "cross_verified",
  223. sources: ["fixture"],
  224. },
  225. }),
  226. ],
  227. },
  228. {
  229. reference: "CON10",
  230. category: "CON",
  231. summary: "Verified UART fixture",
  232. pins: [pin()],
  233. },
  234. {
  235. reference: "J2",
  236. category: "J",
  237. summary: "PWM fixture",
  238. pins: [
  239. pin({
  240. number: "7",
  241. net: "FAN_PWM_TEST",
  242. summary: "Fan PWM",
  243. function_category: "pwm",
  244. board_pull: "10k external pull-up to 3.3V",
  245. fpga: {
  246. ball: "B2",
  247. board_signal: "FAN_PWM_TEST",
  248. io_standard: "LVCMOS33",
  249. capabilities: ["gpio", "gpio_output", "pwm"],
  250. evidence_tier: "cross_verified",
  251. sources: ["fixture"],
  252. },
  253. }),
  254. pin({
  255. number: "8",
  256. net: "FAN_PWM_PULLTYPE",
  257. function_category: "pwm",
  258. adc_pulltype: "PULLUP",
  259. fpga: {
  260. ball: "B3",
  261. board_signal: "FAN_PWM_PULLTYPE",
  262. io_standard: "LVCMOS33",
  263. capabilities: ["gpio", "gpio_output", "pwm"],
  264. evidence_tier: "cross_verified",
  265. sources: ["fixture"],
  266. },
  267. }),
  268. ],
  269. },
  270. {
  271. reference: "TP3",
  272. category: "TP",
  273. summary: "Unverified test point",
  274. pins: [
  275. pin({
  276. net: "MYSTERY",
  277. external_level: "unknown",
  278. evidence_tier: "unknown",
  279. fpga: null,
  280. recommendation: {eligible: false, reasons: ["pin_evidence_not_verified"]},
  281. }),
  282. ],
  283. },
  284. ],
  285. };
  286. }
  287. test("search covers reference, pin, net, function, capability, and FPGA ball", () => {
  288. const data = boardData();
  289. assert.equal(searchBoard(data, "CON78")[0].reference, "CON78");
  290. assert.equal(searchBoard(data, "CON78.1")[0].pinNumber, "1");
  291. assert.equal(searchBoard(data, "rs232_tx2")[0].reference, "CON78");
  292. assert.equal(searchBoard(data, "uart_tx")[0].reference, "CON10");
  293. assert.equal(searchBoard(data, "rs232_via_transceiver")[0].reference, "CON78");
  294. assert.equal(searchBoard(data, "m10")[0].pinNumber, "1");
  295. assert.ok(searchBoard(data, "对端设备的接收端").some((result) => result.reference === "CON78"));
  296. assert.equal(searchBoard(data, "不能直接接 3.3v ttl")[0].reference, "CON78");
  297. assert.deepEqual(searchBoard(data, "does-not-exist"), []);
  298. });
  299. test("back view mirrors X about the vertical/Y axis for KiCad --mirror SVGs", () => {
  300. assert.equal(BACK_MIRROR_AXIS, "y");
  301. assert.equal(mirrorBoardX(233.275, 430), 196.725);
  302. assert.deepEqual(displayPoint({x: 10, y: 5}, 100, "front"), {x: 10, y: 5});
  303. assert.deepEqual(displayPoint({x: 10, y: 5}, 100, "back"), {x: 90, y: 5});
  304. assert.deepEqual(
  305. displayBounds({x: 10, y: 5, width: 4, height: 2}, 100, "back"),
  306. {x: 86, y: 5, width: 4, height: 2},
  307. );
  308. assert.equal(itemVisibleOnSide({side: "back"}, "back"), true);
  309. assert.equal(itemVisibleOnSide({side: "back"}, "front"), false);
  310. assert.equal(itemVisibleOnSide({side: "front"}, "front"), true);
  311. });
  312. test("pass D/E: exact resistor queries never false-positive on FPGA balls or R1225", () => {
  313. const data = boardData();
  314. data.touchpoints[0].pins[0].fpga.ball = "R7";
  315. data.touchpoints[0].pins[0].path = ["CON78.1", "R63 22R", "FPGA R7"];
  316. const r7 = searchBoard(data, "R7");
  317. assert.equal(r7[0].type, "resistor");
  318. assert.equal(r7[0].reference, "R7");
  319. assert.ok(!r7.some((item) => item.type === "pin" && item.matchedField === "fpga_ball"));
  320. const r12 = searchBoard(data, "R12");
  321. assert.equal(r12[0].type, "resistor");
  322. assert.equal(r12[0].reference, "R12");
  323. assert.ok(!r12.some((item) => item.reference === "R1225"));
  324. assert.equal(searchBoard(data, "R674")[0].type, "resistor");
  325. assert.equal(searchBoard(data, "r674.2")[0].type, "resistor_pad");
  326. assert.equal(searchBoard(data, "r674.2")[0].pinNumber, "2");
  327. assert.equal(identityMatch("R1225", "R12"), 0);
  328. assert.equal(identityMatch("R12", "R12"), 3);
  329. });
  330. test("pass C/F: value and net search cover resistors; sameNetMembers includes resistor pads", () => {
  331. const data = boardData();
  332. assert.equal(parseBoardQuery("4k7").kind, "value");
  333. assert.equal(parseBoardQuery("R674").kind, "resistor_ref");
  334. assert.equal(parseBoardQuery("ball:R7").kind, "ball");
  335. assert.ok(searchBoard(data, "4k7").some((item) => item.reference === "R7"));
  336. assert.ok(searchBoard(data, "22R").some((item) => item.reference === "R12"));
  337. assert.ok(searchBoard(data, "PUL1").some((item) => item.reference === "R7" || item.reference === "R674"));
  338. assert.ok(searchBoard(data, "net:PUL1").some((item) => item.reference === "R674"));
  339. const members = sameNetMembers(data, "PUL1");
  340. assert.ok(members.some((item) => item.kind === "resistor_pad" && item.reference === "R7"));
  341. assert.ok(members.some((item) => item.kind === "resistor_pad" && item.reference === "R674"));
  342. // Compatibility: sameNetPins stays touchpoint-only.
  343. assert.ok(sameNetPins(data, "PUL1").every((item) => item.kind === "touchpoint"));
  344. assert.ok(buildBoardIndex(data).resistorByReference.has("R674"));
  345. });
  346. test("pass D: ball:R7 and U3.R7 disambiguate FPGA balls from resistor R7", () => {
  347. const data = boardData();
  348. data.touchpoints[0].pins[0].fpga.ball = "R7";
  349. data.touchpoints[0].pins[0].path = ["CON78.1", "BANK0", "U3.R7"];
  350. assert.equal(searchBoard(data, "R7")[0].type, "resistor");
  351. const ball = searchBoard(data, "ball:R7");
  352. assert.ok(ball.some((item) => item.type === "pin" && item.pin?.fpga?.ball === "R7"));
  353. assert.ok(searchBoard(data, "U3.R7").some((item) => item.reference === "CON78"));
  354. });
  355. test("pass G path jump extracts annotation resistors and fails closed on missing index", () => {
  356. const data = boardData();
  357. const found = extractPathResistors(data, ["CON78.1", "R7 4.7k", "U3.R4", "FPGA M10"], ["R12 series"]);
  358. assert.deepEqual(found.map((item) => item.reference), ["R7", "R12"]);
  359. assert.throws(
  360. () => extractPathResistors(data, ["R99999 missing"]),
  361. /R99999 missing from resistor index/,
  362. );
  363. });
  364. test("search covers verified MCU device, package pin, signal, and capability", () => {
  365. const data = boardData();
  366. const mcuPin = data.touchpoints.find((item) => item.reference === "TP3").pins[0];
  367. mcuPin.summary = "GD32 firmware test point";
  368. mcuPin.path = [];
  369. mcuPin.programming = {
  370. status: "verified_mcu",
  371. conclusion: "Verified MCU test point",
  372. network_explanation: "Verified MCU path",
  373. hdl_guidance: "Use from firmware",
  374. connection_guidance: [],
  375. constraints: [],
  376. risks: [],
  377. evidence_tier: "cross_verified",
  378. sources: ["fixture"],
  379. };
  380. mcuPin.mcu = {
  381. device: "U27",
  382. family: "GD32F303CGT6",
  383. pin: "10",
  384. signal: "PA0",
  385. capabilities: ["gpio", "adc_input"],
  386. evidence_tier: "cross_verified",
  387. sources: ["fixture"],
  388. };
  389. assert.equal(searchBoard(data, "u27.10")[0].reference, "TP3");
  390. assert.equal(searchBoard(data, "gd32f303")[0].reference, "TP3");
  391. assert.equal(searchBoard(data, "pa0")[0].reference, "TP3");
  392. assert.equal(searchBoard(data, "adc_input")[0].reference, "TP3");
  393. assert.ok(!findCandidates(data, "gpio").some((item) => item.reference === "TP3"));
  394. });
  395. test("same-net lookup returns every matching pad and ignores empty nets", () => {
  396. const data = boardData();
  397. data.touchpoints[1].pins.push(pin({number: "2", net: "RS232_TX2"}));
  398. assert.deepEqual(
  399. sameNetPins(data, "RS232_TX2").map((item) => `${item.reference}.${item.pinNumber}`),
  400. ["CON10.2", "CON78.1"],
  401. );
  402. assert.deepEqual(sameNetPins(data, ""), []);
  403. });
  404. test("candidate filtering is fail-closed and sorted by natural reference and pin", () => {
  405. const candidates = findCandidates(boardData(), "uart_tx");
  406. assert.deepEqual(candidates.map((item) => `${item.reference}.${item.pin.number}`), ["CON10.1"]);
  407. assert.match(candidates[0].reason, /已验证 FPGA 路径/);
  408. assert.match(candidates[0].warning, /当前工程约束冲突/);
  409. });
  410. test("candidate filters recognize canonical board capability aliases", () => {
  411. const aliases = [
  412. ["pwm", "pwm_output"],
  413. ["gpio_output", "digital_output"],
  414. ["gpio_input", "digital_input"],
  415. ["tach", "tach_input"],
  416. ["fan_tach", "tach_input"],
  417. ["presence", "presence_input"],
  418. ];
  419. for (const [filter, capability] of aliases) {
  420. const data = {
  421. touchpoints: [{
  422. reference: "CON1",
  423. category: "CON",
  424. summary: "Alias fixture",
  425. pins: [pin({
  426. fpga: {
  427. ball: "A1",
  428. board_signal: "ALIAS_SIGNAL",
  429. io_standard: "LVCMOS33",
  430. capabilities: ["gpio", capability],
  431. evidence_tier: "cross_verified",
  432. sources: ["fixture"],
  433. },
  434. })],
  435. }],
  436. };
  437. assert.equal(findCandidates(data, filter).length, 1, `${filter} -> ${capability}`);
  438. }
  439. });
  440. test("generic GPIO candidates exclude dedicated tach, PWM, presence, and protocol pins", () => {
  441. const data = {
  442. touchpoints: [{
  443. reference: "CON1",
  444. category: "CON",
  445. summary: "Mixed GPIO fixture",
  446. pins: [
  447. pin({number: "1", function_category: "gpio", summary: "Direct bidirectional GPIO"}),
  448. pin({number: "2", function_category: "fan_tach", summary: "Fan tach input"}),
  449. pin({number: "3", function_category: "pwm", summary: "Open-collector PWM"}),
  450. pin({number: "4", function_category: "presence", summary: "Presence input"}),
  451. ],
  452. }],
  453. };
  454. assert.deepEqual(
  455. findCandidates(data, "gpio").map((item) => `${item.reference}.${item.pin.number}`),
  456. ["CON1.1"],
  457. );
  458. });
  459. test("candidate reason exposes the pin purpose and non-default polarity", () => {
  460. const data = {
  461. touchpoints: [{
  462. reference: "CON31",
  463. category: "CON",
  464. summary: "Fan connector",
  465. pins: [pin({
  466. function_category: "pwm",
  467. summary: "风扇 PWM 开集电极下拉输出",
  468. active_level: "connector low",
  469. polarity: "FPGA high -> connector low",
  470. fpga: {
  471. ball: "E10",
  472. board_signal: "FAN_GPIO6",
  473. io_standard: "LVCMOS33",
  474. capabilities: ["gpio", "pwm_output", "fan_pwm"],
  475. evidence_tier: "cross_verified",
  476. sources: ["fixture"],
  477. },
  478. })],
  479. }],
  480. };
  481. const [candidate] = findCandidates(data, "pwm");
  482. assert.match(candidate.reason, /风扇 PWM 开集电极下拉输出/);
  483. assert.match(candidate.reason, /FPGA high -> connector low/);
  484. });
  485. test("product candidate filters keep dedicated functions out of generic GPIO", () => {
  486. const boardDataPath = resolveGeneratedAsset("board-data.json");
  487. const buildInfoPath = resolveGeneratedAsset("build-info.json");
  488. const expectationPath = new URL("./fixtures/candidate_count_expectation.json", import.meta.url);
  489. const boardDataBytes = readFileSync(boardDataPath);
  490. const buildInfoBytes = readFileSync(buildInfoPath);
  491. const data = JSON.parse(boardDataBytes);
  492. const expectation = JSON.parse(readFileSync(expectationPath, "utf8"));
  493. const boardDataSha = createHash("sha256").update(boardDataBytes).digest("hex");
  494. const buildInfoSha = createHash("sha256").update(buildInfoBytes).digest("hex");
  495. assert.equal(expectation.schema, "prod2u-board-viewer-candidate-count-expectation-v1");
  496. assert.equal(boardDataSha, expectation.bound_to.board_data_sha256);
  497. assert.equal(buildInfoSha, expectation.bound_to.build_info_sha256);
  498. assert.equal(boardDataSha, expectation.bound_to.board_data_asset_sha256);
  499. for (const [capability, expectedCount] of Object.entries(expectation.candidate_counts)) {
  500. const candidates = findCandidates(data, capability);
  501. assert.equal(candidates.length, expectedCount, `${capability} count`);
  502. if (Array.isArray(expectation.candidate_keys?.[capability])) {
  503. assert.deepEqual(
  504. candidates.map((item) => `${item.reference}.${item.pin.number}`),
  505. expectation.candidate_keys[capability],
  506. `${capability} keys`,
  507. );
  508. }
  509. }
  510. assert.ok(findCandidates(data, "gpio").every((item) => item.pin.function_category === "gpio"));
  511. });
  512. test("unknown, inferred, power, ground, mechanical, reserved, and fixed-load pins are never candidates", () => {
  513. const blockers = [
  514. {evidence_tier: "unknown"},
  515. {evidence_tier: "name_inferred"},
  516. {function_category: "power", net: "+3V3"},
  517. {function_category: "ground", net: "GND"},
  518. {mechanical: true, electrical: false},
  519. {pad_role: "shield"},
  520. {pad_role: "unknown"},
  521. {reserved: true},
  522. {fixed_load_conflict: true},
  523. ];
  524. for (const [index, override] of blockers.entries()) {
  525. const data = {touchpoints: [{reference: `J${index + 1}`, category: "J", summary: "", pins: [pin(override)]}]};
  526. assert.deepEqual(findCandidates(data, "uart_tx"), []);
  527. }
  528. });
  529. test("connector groups follow reviewed board-front order and separate auxiliary pads", () => {
  530. const touchpoint = {
  531. reference: "CON78",
  532. connector: {
  533. review_status: "cross_verified",
  534. contact_order: ["1", "2", "3", "4"],
  535. contact_grid: [["1"], ["2"], ["3"], ["4"]],
  536. },
  537. pins: [
  538. pin({id: "shield-5", number: "5", pad_role: "shield"}),
  539. pin({id: "contact-4", number: "4"}),
  540. pin({id: "contact-2", number: "2"}),
  541. pin({id: "mechanical-1", number: "", pad_role: "mechanical", mechanical: true, electrical: false}),
  542. pin({id: "contact-1", number: "1"}),
  543. pin({id: "contact-3", number: "3"}),
  544. ],
  545. };
  546. const groups = connectorPinGroups(touchpoint);
  547. assert.deepEqual(groups.contactRows.map((row) => row.map((item) => item.number)), [["1"], ["2"], ["3"], ["4"]]);
  548. assert.deepEqual(groups.contacts.map((item) => item.number), ["1", "2", "3", "4"]);
  549. assert.deepEqual(groups.auxiliary.map((item) => item.id), ["shield-5", "mechanical-1"]);
  550. });
  551. test("unknown connector layout never invents contact ordering", () => {
  552. const touchpoint = {
  553. reference: "CON1",
  554. connector: {review_status: "unknown", contact_order: [], contact_grid: []},
  555. pins: [pin({id: "unknown-1", pad_role: "unknown"})],
  556. };
  557. assert.deepEqual(connectorPinGroups(touchpoint), {
  558. contactRows: [],
  559. contacts: [],
  560. auxiliary: [touchpoint.pins[0]],
  561. });
  562. });
  563. test("RTL port names reject syntax that could escape an ADC assignment", () => {
  564. for (const name of ["uart_tx", "fan_pwm_out[3]", "_debug$signal"]) {
  565. assert.equal(isValidRtlPortName(name), true, name);
  566. }
  567. for (const name of ["", "3bad", "a b", "a;set", "name[foo]", "{signal}", "a\nnext"]) {
  568. assert.equal(isValidRtlPortName(name), false, name);
  569. }
  570. });
  571. test("ADC generation requires current verified eligible data and omits implicit PULLTYPE", () => {
  572. const data = boardData();
  573. const pwm = data.touchpoints[2].pins[0];
  574. const snippet = generateAdcSnippet(pwm, "fan_pwm_out[3]", {adcCopyEnabled: true});
  575. assert.equal(
  576. snippet,
  577. "set_pin_assignment { fan_pwm_out[3] } { LOCATION = B2; IOSTANDARD = LVCMOS33; }",
  578. );
  579. assert.doesNotMatch(snippet, /PULLTYPE/);
  580. assert.throws(() => generateAdcSnippet(pwm, "a;bad", {adcCopyEnabled: true}), /合法的 RTL 端口名/);
  581. assert.throws(() => generateAdcSnippet(pwm, "fan_pwm_out", {adcCopyEnabled: false}), /源数据已过期/);
  582. assert.throws(
  583. () => generateAdcSnippet(data.touchpoints[0].pins[0], "uart2_tx", {adcCopyEnabled: true}),
  584. /不具备 ADC 生成资格/,
  585. );
  586. });
  587. test("ADC generation includes PULLTYPE only when curated annotation explicitly supplies it", () => {
  588. const pinWithPulltype = boardData().touchpoints[2].pins[1];
  589. assert.equal(
  590. generateAdcSnippet(pinWithPulltype, "fan_pwm_aux", {adcCopyEnabled: true}),
  591. "set_pin_assignment { fan_pwm_aux } { LOCATION = B3; IOSTANDARD = LVCMOS33; PULLTYPE = PULLUP; }",
  592. );
  593. });
  594. test("pass C/D: 0805 is not a resistance query; 0R/120R grammar; bare PUL1 exact net", () => {
  595. const data = boardData();
  596. // Inject path text noise that must NOT match bare exact-net PUL1.
  597. data.touchpoints[0].pins[0].path = ["mentions PUL1 in prose only", "FPGA M10"];
  598. data.touchpoints[0].pins[0].net = "RS232_TX2";
  599. assert.notEqual(parseBoardQuery("0805").kind, "value");
  600. assert.equal(parseBoardQuery("0R").kind, "value");
  601. assert.equal(parseBoardQuery("120R").kind, "value");
  602. assert.equal(resolveExactNetName(data, "PUL1"), "PUL1");
  603. const bare = searchBoard(data, "PUL1");
  604. const prefixed = searchBoard(data, "net:PUL1");
  605. assert.deepEqual(
  606. bare.map((item) => `${item.type}:${item.reference}.${item.pinNumber}`).sort(),
  607. prefixed.map((item) => `${item.type}:${item.reference}.${item.pinNumber}`).sort(),
  608. );
  609. assert.ok(bare.every((item) => item.matchedField === "net"));
  610. assert.ok(!bare.some((item) => item.reference === "CON78"));
  611. // Fixture 4k7 grammar still works on synthetic R7.
  612. assert.ok(searchBoard(data, "4k7").some((item) => item.reference === "R7"));
  613. });