board-logic.test.mjs 14 KB

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