| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- import test from "node:test";
- import assert from "node:assert/strict";
- import {createHash} from "node:crypto";
- import {existsSync, readFileSync} from "node:fs";
- import {fileURLToPath} from "node:url";
- import {
- connectorPinGroups,
- findCandidates,
- generateAdcSnippet,
- isValidRtlPortName,
- sameNetPins,
- searchBoard,
- } from "../../tools/board-viewer/web/board-logic.mjs";
- function resolveGeneratedAsset(name) {
- // Live repo: tools/board-viewer/generated; compact package: evidence/board-viewer.
- const candidates = [
- new URL(`../../tools/board-viewer/generated/${name}`, import.meta.url),
- new URL(`../../evidence/board-viewer/${name}`, import.meta.url),
- new URL(`../evidence/board-viewer/${name}`, import.meta.url),
- ];
- for (const url of candidates) {
- const path = fileURLToPath(url);
- if (existsSync(path)) {
- return url;
- }
- }
- return candidates[0];
- }
- function pin(overrides = {}) {
- return {
- id: "fixture-pin",
- number: "1",
- pad_role: "contact",
- net: "UART_A_TX",
- summary: "UART transmit",
- path: ["CON10.1", "UART_A_TX", "FPGA A1"],
- mechanical: false,
- electrical: true,
- direction: "output",
- external_level: "3.3V LVCMOS",
- function_category: "uart_tx",
- reserved: false,
- fixed_load_conflict: false,
- board_pull: null,
- adc_pulltype: null,
- programming: {
- status: "verified_fpga",
- conclusion: "FPGA 输出 UART_A_TX",
- network_explanation: "连接器网络 UART_A_TX 直达 FPGA",
- hdl_guidance: "RTL 端口方向应为 output",
- connection_guidance: ["连接到对端设备的接收端(RX)。"],
- constraints: ["LOCATION = A1", "IOSTANDARD = LVCMOS33"],
- risks: ["仍需检查当前工程约束冲突。"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- evidence_tier: "cross_verified",
- fpga: {
- ball: "A1",
- board_signal: "UART_A_TX",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", "uart_tx"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- recommendation: {eligible: true, reasons: []},
- ...overrides,
- };
- }
- function boardData() {
- return {
- touchpoints: [
- {
- reference: "CON78",
- category: "CON",
- summary: "UART2 RS-232 connector through U23",
- pins: [
- pin({
- number: "1",
- net: "RS232_TX2",
- path: ["CON78.1 RS232_TX2", "U23", "UART2_TX", "FPGA M10"],
- external_level: "RS232",
- reserved: true,
- fpga: {
- ball: "M10",
- board_signal: "UART2_TX",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", "uart_tx", "rs232_via_transceiver"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- recommendation: {eligible: false, reasons: ["reserved"]},
- programming: {
- status: "verified_fpga",
- conclusion: "FPGA 输出 UART2_TX 经 U23 转为 RS-232",
- network_explanation: "连接器侧网络 RS232_TX2 对应逻辑侧 UART2_TX",
- hdl_guidance: "RTL 端口方向应为 output",
- connection_guidance: ["连接到对端设备的接收端(RX)。", "不能直接接 3.3V TTL/CMOS 串口。"],
- constraints: ["LOCATION = M10", "IOSTANDARD = LVCMOS33"],
- risks: ["当前功能已保留。"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- }),
- ],
- },
- {
- reference: "CON10",
- category: "CON",
- summary: "Verified UART fixture",
- pins: [pin()],
- },
- {
- reference: "J2",
- category: "J",
- summary: "PWM fixture",
- pins: [
- pin({
- number: "7",
- net: "FAN_PWM_TEST",
- summary: "Fan PWM",
- function_category: "pwm",
- board_pull: "10k external pull-up to 3.3V",
- fpga: {
- ball: "B2",
- board_signal: "FAN_PWM_TEST",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", "gpio_output", "pwm"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- }),
- pin({
- number: "8",
- net: "FAN_PWM_PULLTYPE",
- function_category: "pwm",
- adc_pulltype: "PULLUP",
- fpga: {
- ball: "B3",
- board_signal: "FAN_PWM_PULLTYPE",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", "gpio_output", "pwm"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- }),
- ],
- },
- {
- reference: "TP3",
- category: "TP",
- summary: "Unverified test point",
- pins: [
- pin({
- net: "MYSTERY",
- external_level: "unknown",
- evidence_tier: "unknown",
- fpga: null,
- recommendation: {eligible: false, reasons: ["pin_evidence_not_verified"]},
- }),
- ],
- },
- ],
- };
- }
- test("search covers reference, pin, net, function, capability, and FPGA ball", () => {
- const data = boardData();
- assert.equal(searchBoard(data, "CON78")[0].reference, "CON78");
- assert.equal(searchBoard(data, "CON78.1")[0].pinNumber, "1");
- assert.equal(searchBoard(data, "rs232_tx2")[0].reference, "CON78");
- assert.equal(searchBoard(data, "uart_tx")[0].reference, "CON10");
- assert.equal(searchBoard(data, "rs232_via_transceiver")[0].reference, "CON78");
- assert.equal(searchBoard(data, "m10")[0].pinNumber, "1");
- assert.ok(searchBoard(data, "对端设备的接收端").some((result) => result.reference === "CON78"));
- assert.equal(searchBoard(data, "不能直接接 3.3v ttl")[0].reference, "CON78");
- assert.deepEqual(searchBoard(data, "does-not-exist"), []);
- });
- test("search covers verified MCU device, package pin, signal, and capability", () => {
- const data = boardData();
- const mcuPin = data.touchpoints.find((item) => item.reference === "TP3").pins[0];
- mcuPin.summary = "GD32 firmware test point";
- mcuPin.path = [];
- mcuPin.programming = {
- status: "verified_mcu",
- conclusion: "Verified MCU test point",
- network_explanation: "Verified MCU path",
- hdl_guidance: "Use from firmware",
- connection_guidance: [],
- constraints: [],
- risks: [],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- };
- mcuPin.mcu = {
- device: "U27",
- family: "GD32F303CGT6",
- pin: "10",
- signal: "PA0",
- capabilities: ["gpio", "adc_input"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- };
- assert.equal(searchBoard(data, "u27.10")[0].reference, "TP3");
- assert.equal(searchBoard(data, "gd32f303")[0].reference, "TP3");
- assert.equal(searchBoard(data, "pa0")[0].reference, "TP3");
- assert.equal(searchBoard(data, "adc_input")[0].reference, "TP3");
- assert.ok(!findCandidates(data, "gpio").some((item) => item.reference === "TP3"));
- });
- test("same-net lookup returns every matching pad and ignores empty nets", () => {
- const data = boardData();
- data.touchpoints[1].pins.push(pin({number: "2", net: "RS232_TX2"}));
- assert.deepEqual(
- sameNetPins(data, "RS232_TX2").map((item) => `${item.reference}.${item.pinNumber}`),
- ["CON10.2", "CON78.1"],
- );
- assert.deepEqual(sameNetPins(data, ""), []);
- });
- test("candidate filtering is fail-closed and sorted by natural reference and pin", () => {
- const candidates = findCandidates(boardData(), "uart_tx");
- assert.deepEqual(candidates.map((item) => `${item.reference}.${item.pin.number}`), ["CON10.1"]);
- assert.match(candidates[0].reason, /已验证 FPGA 路径/);
- assert.match(candidates[0].warning, /当前工程约束冲突/);
- });
- test("candidate filters recognize canonical board capability aliases", () => {
- const aliases = [
- ["pwm", "pwm_output"],
- ["gpio_output", "digital_output"],
- ["gpio_input", "digital_input"],
- ["tach", "tach_input"],
- ["fan_tach", "tach_input"],
- ["presence", "presence_input"],
- ];
- for (const [filter, capability] of aliases) {
- const data = {
- touchpoints: [{
- reference: "CON1",
- category: "CON",
- summary: "Alias fixture",
- pins: [pin({
- fpga: {
- ball: "A1",
- board_signal: "ALIAS_SIGNAL",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", capability],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- })],
- }],
- };
- assert.equal(findCandidates(data, filter).length, 1, `${filter} -> ${capability}`);
- }
- });
- test("generic GPIO candidates exclude dedicated tach, PWM, presence, and protocol pins", () => {
- const data = {
- touchpoints: [{
- reference: "CON1",
- category: "CON",
- summary: "Mixed GPIO fixture",
- pins: [
- pin({number: "1", function_category: "gpio", summary: "Direct bidirectional GPIO"}),
- pin({number: "2", function_category: "fan_tach", summary: "Fan tach input"}),
- pin({number: "3", function_category: "pwm", summary: "Open-collector PWM"}),
- pin({number: "4", function_category: "presence", summary: "Presence input"}),
- ],
- }],
- };
- assert.deepEqual(
- findCandidates(data, "gpio").map((item) => `${item.reference}.${item.pin.number}`),
- ["CON1.1"],
- );
- });
- test("candidate reason exposes the pin purpose and non-default polarity", () => {
- const data = {
- touchpoints: [{
- reference: "CON31",
- category: "CON",
- summary: "Fan connector",
- pins: [pin({
- function_category: "pwm",
- summary: "风扇 PWM 开集电极下拉输出",
- active_level: "connector low",
- polarity: "FPGA high -> connector low",
- fpga: {
- ball: "E10",
- board_signal: "FAN_GPIO6",
- io_standard: "LVCMOS33",
- capabilities: ["gpio", "pwm_output", "fan_pwm"],
- evidence_tier: "cross_verified",
- sources: ["fixture"],
- },
- })],
- }],
- };
- const [candidate] = findCandidates(data, "pwm");
- assert.match(candidate.reason, /风扇 PWM 开集电极下拉输出/);
- assert.match(candidate.reason, /FPGA high -> connector low/);
- });
- test("product candidate filters keep dedicated functions out of generic GPIO", () => {
- const boardDataPath = resolveGeneratedAsset("board-data.json");
- const buildInfoPath = resolveGeneratedAsset("build-info.json");
- const expectationPath = new URL("./fixtures/candidate_count_expectation.json", import.meta.url);
- const boardDataBytes = readFileSync(boardDataPath);
- const buildInfoBytes = readFileSync(buildInfoPath);
- const data = JSON.parse(boardDataBytes);
- const expectation = JSON.parse(readFileSync(expectationPath, "utf8"));
- const boardDataSha = createHash("sha256").update(boardDataBytes).digest("hex");
- const buildInfoSha = createHash("sha256").update(buildInfoBytes).digest("hex");
- assert.equal(expectation.schema, "prod2u-board-viewer-candidate-count-expectation-v1");
- assert.equal(boardDataSha, expectation.bound_to.board_data_sha256);
- assert.equal(buildInfoSha, expectation.bound_to.build_info_sha256);
- assert.equal(boardDataSha, expectation.bound_to.board_data_asset_sha256);
- for (const [capability, expectedCount] of Object.entries(expectation.candidate_counts)) {
- const candidates = findCandidates(data, capability);
- assert.equal(candidates.length, expectedCount, `${capability} count`);
- if (Array.isArray(expectation.candidate_keys?.[capability])) {
- assert.deepEqual(
- candidates.map((item) => `${item.reference}.${item.pin.number}`),
- expectation.candidate_keys[capability],
- `${capability} keys`,
- );
- }
- }
- assert.ok(findCandidates(data, "gpio").every((item) => item.pin.function_category === "gpio"));
- });
- test("unknown, inferred, power, ground, mechanical, reserved, and fixed-load pins are never candidates", () => {
- const blockers = [
- {evidence_tier: "unknown"},
- {evidence_tier: "name_inferred"},
- {function_category: "power", net: "+3V3"},
- {function_category: "ground", net: "GND"},
- {mechanical: true, electrical: false},
- {pad_role: "shield"},
- {pad_role: "unknown"},
- {reserved: true},
- {fixed_load_conflict: true},
- ];
- for (const [index, override] of blockers.entries()) {
- const data = {touchpoints: [{reference: `J${index + 1}`, category: "J", summary: "", pins: [pin(override)]}]};
- assert.deepEqual(findCandidates(data, "uart_tx"), []);
- }
- });
- test("connector groups follow reviewed board-front order and separate auxiliary pads", () => {
- const touchpoint = {
- reference: "CON78",
- connector: {
- review_status: "cross_verified",
- contact_order: ["1", "2", "3", "4"],
- contact_grid: [["1"], ["2"], ["3"], ["4"]],
- },
- pins: [
- pin({id: "shield-5", number: "5", pad_role: "shield"}),
- pin({id: "contact-4", number: "4"}),
- pin({id: "contact-2", number: "2"}),
- pin({id: "mechanical-1", number: "", pad_role: "mechanical", mechanical: true, electrical: false}),
- pin({id: "contact-1", number: "1"}),
- pin({id: "contact-3", number: "3"}),
- ],
- };
- const groups = connectorPinGroups(touchpoint);
- assert.deepEqual(groups.contactRows.map((row) => row.map((item) => item.number)), [["1"], ["2"], ["3"], ["4"]]);
- assert.deepEqual(groups.contacts.map((item) => item.number), ["1", "2", "3", "4"]);
- assert.deepEqual(groups.auxiliary.map((item) => item.id), ["shield-5", "mechanical-1"]);
- });
- test("unknown connector layout never invents contact ordering", () => {
- const touchpoint = {
- reference: "CON1",
- connector: {review_status: "unknown", contact_order: [], contact_grid: []},
- pins: [pin({id: "unknown-1", pad_role: "unknown"})],
- };
- assert.deepEqual(connectorPinGroups(touchpoint), {
- contactRows: [],
- contacts: [],
- auxiliary: [touchpoint.pins[0]],
- });
- });
- test("RTL port names reject syntax that could escape an ADC assignment", () => {
- for (const name of ["uart_tx", "fan_pwm_out[3]", "_debug$signal"]) {
- assert.equal(isValidRtlPortName(name), true, name);
- }
- for (const name of ["", "3bad", "a b", "a;set", "name[foo]", "{signal}", "a\nnext"]) {
- assert.equal(isValidRtlPortName(name), false, name);
- }
- });
- test("ADC generation requires current verified eligible data and omits implicit PULLTYPE", () => {
- const data = boardData();
- const pwm = data.touchpoints[2].pins[0];
- const snippet = generateAdcSnippet(pwm, "fan_pwm_out[3]", {adcCopyEnabled: true});
- assert.equal(
- snippet,
- "set_pin_assignment { fan_pwm_out[3] } { LOCATION = B2; IOSTANDARD = LVCMOS33; }",
- );
- assert.doesNotMatch(snippet, /PULLTYPE/);
- assert.throws(() => generateAdcSnippet(pwm, "a;bad", {adcCopyEnabled: true}), /合法的 RTL 端口名/);
- assert.throws(() => generateAdcSnippet(pwm, "fan_pwm_out", {adcCopyEnabled: false}), /源数据已过期/);
- assert.throws(
- () => generateAdcSnippet(data.touchpoints[0].pins[0], "uart2_tx", {adcCopyEnabled: true}),
- /不具备 ADC 生成资格/,
- );
- });
- test("ADC generation includes PULLTYPE only when curated annotation explicitly supplies it", () => {
- const pinWithPulltype = boardData().touchpoints[2].pins[1];
- assert.equal(
- generateAdcSnippet(pinWithPulltype, "fan_pwm_aux", {adcCopyEnabled: true}),
- "set_pin_assignment { fan_pwm_aux } { LOCATION = B3; IOSTANDARD = LVCMOS33; PULLTYPE = PULLUP; }",
- );
- });
|