from __future__ import annotations import copy import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] VIEWER_ROOT = ROOT / "tools" / "board-viewer" sys.path.insert(0, str(VIEWER_ROOT)) from board_viewer.regression import BoardRegressionError, validate_product_regression # noqa: E402 FOUR_CONTACT_REFS = { "CON12", "CON30", "CON31", "CON32", "CON33", "CON34", "CON35", "CON36", "CON37", "CON38", "CON39", "CON40", "CON54", "CON58", "CON59", "CON60", "CON61", "CON62", "CON63", "CON64", "CON65", "CON66", "CON67", "CON68", "CON69", "CON70", "CON77", "CON78", } THREE_CONTACT_REFS = { "CON53", "CON55", "CON56", "CON57", "CON79", "CON80", "CON81", "CON82", "CON83", "CON84", "CON85", "CON86", "CON87", "CON88", "CON89", "CON90", "CON91", "CON92", "CON93", "CON94", "CON95", "CON96", "CON97", "CON98", } FOURTEEN_CONTACT_REFS = {"CON18", "CON27", "CON28", "CON29"} POWER_CONTACT_REFS = {"CON2", "CON23"} FAN_CONTACT_REFS = {"CON8", "CON24", "CON25", "CON26"} J3_GRID = [["6", "1"], ["7", "2"], ["8", "3"], ["9", "4"], ["10", "5"]] J3_NETS = { "1": "N9532014", "2": "GND", "3": "N9532092", "4": "N9532034", "5": "N9532142", "6": "Net_649", "7": "Net_650", "8": "Net_648", "9": "N9532240", "10": "GND", } J3_SIGNALS = { "1": ("FPGA_TCK", "A7", "input", "jtag_tck", "R63"), "3": ("FPGA_TDO", "C6", "output", "jtag_tdo", "R65"), "5": ("FPGA_TMS", "B8", "input", "jtag_tms", "R67"), "9": ("FPGA_TDI", "A6", "input", "jtag_tdi", "R69"), } def expected_connector_contract(reference: str) -> tuple[str, list[list[str]], list[str], int]: if reference in FOUR_CONTACT_REFS: order = ["1", "2", "3", "4"] if reference in {"CON54", "CON77", "CON78"} else ["4", "3", "2", "1"] return "CON_11251W00_NPX_SX_4P", [[number] for number in order], ["5", "6"], 0 if reference in THREE_CONTACT_REFS: return "CON_51541_003_P1D25", [["3", "2", "1"]], ["4", "5"], 0 if reference in FOURTEEN_CONTACT_REFS: return ( "CON_3110_14P_P2D54", [ ["13", "11", "9", "7", "5", "3", "1"], ["14", "12", "10", "8", "6", "4", "2"], ], [], 0, ) if reference == "CON23": return "CON_C22930_1084N_X", [["5", "1"], ["6", "2"], ["7", "3"], ["8", "4"]], ["9", "10"], 2 if reference == "CON2": return "CON_C22930_1084N_X", [["4", "8"], ["3", "7"], ["2", "6"], ["1", "5"]], ["9", "10"], 2 if reference in FAN_CONTACT_REFS: return "CON_PZGM_B03_08P", [[str(number)] for number in range(8, 0, -1)], [], 1 raise AssertionError(f"missing test connector contract for {reference}") def connector_fixture(reference: str) -> dict[str, object]: profile, grid, shields, mechanical_count = expected_connector_contract(reference) contact_order = [number for row in grid for number in row] pins: list[dict[str, object]] = [] for board_order, number in enumerate(contact_order, start=1): row = next(index for index, values in enumerate(grid, start=1) if number in values) pins.append( { "id": f"{reference}.contact-{number}", "number": number, "pad_role": "contact", "row": row, "column": grid[row - 1].index(number) + 1, "board_order": board_order, "net": f"{reference}_{number}", "path": [], "fpga": None, "mechanical": False, "electrical": True, "reserved": False, "function_category": "unknown", "evidence_tier": "board_database", "programming": { "status": "board_only", "conclusion": f"Board-only {reference}.{number}", "network_explanation": f"Raw network {reference}_{number}", "hdl_guidance": "No verified FPGA path", "connection_guidance": ["Verify before connecting"], "constraints": [], "risks": ["Name is not proof"], "evidence_tier": "board_database", "sources": ["fixture"], }, "recommendation": {"eligible": False, "reasons": ["pin_evidence_not_verified"]}, } ) pins.extend( { "id": f"{reference}.shield-{number}", "number": number, "pad_role": "shield", "row": None, "column": None, "board_order": None, "net": "GND", "path": [], "fpga": None, "mechanical": False, "electrical": True, "reserved": False, "function_category": "ground", "evidence_tier": "board_database", "programming": { "status": "not_programmable", "conclusion": "Shield pad", "network_explanation": "Grounded shell", "hdl_guidance": "No ADC", "connection_guidance": ["Not an external contact"], "constraints": [], "risks": ["Do not count as a pin"], "evidence_tier": "board_database", "sources": ["fixture"], }, "recommendation": {"eligible": False, "reasons": ["not_connector_contact"]}, } for number in shields ) pins.extend( { "id": f"{reference}.mechanical-{index}", "number": "", "pad_role": "mechanical", "row": None, "column": None, "board_order": None, "net": None, "path": [], "fpga": None, "mechanical": True, "electrical": False, "reserved": False, "function_category": "unknown", "evidence_tier": "board_database", "programming": { "status": "not_programmable", "conclusion": "Mechanical hole", "network_explanation": "No electrical net", "hdl_guidance": "No ADC", "connection_guidance": ["Do not wire"], "constraints": [], "risks": ["Mechanical only"], "evidence_tier": "board_database", "sources": ["fixture"], }, "recommendation": {"eligible": False, "reasons": ["mechanical_or_non_electrical"]}, } for index in range(1, mechanical_count + 1) ) return { "reference": reference, "side": "front", "connector": { "profile": profile, "review_status": "cross_verified", "view": "board_front", "contact_count": len(contact_order), "auxiliary_pad_count": len(shields) + mechanical_count, "rows": len(grid), "columns": len(grid[0]), "contact_grid": grid, "contact_order": contact_order, "sources": ["test cross-check"], }, "pins": pins, } def j3_fixture() -> dict[str, object]: order = [number for row in J3_GRID for number in row] pins: list[dict[str, object]] = [] for board_order, number in enumerate(order, start=1): row = next(index for index, values in enumerate(J3_GRID, start=1) if number in values) signal = J3_SIGNALS.get(number) fpga = None if signal is not None: board_signal, ball, direction, function_category, series_resistor = signal fpga = { "ball": ball, "board_signal": board_signal, "io_standard": "Dedicated JTAG (VREF 3.3V)", "capabilities": ["jtag", function_category], "evidence_tier": "cross_verified", "sources": ["fixture schematic and PCB"], } programming = { "status": "verified_fpga", "conclusion": f"{board_signal} dedicated JTAG path to U3.{ball}", "network_explanation": f"J3.{number} reaches {board_signal} through reviewed protection and series parts", "hdl_guidance": "Dedicated FPGA configuration pin; do not declare it as a normal RTL GPIO or ADC pin.", "connection_guidance": ["Connect only to the matching JTAG probe signal."], "constraints": ["No user ADC assignment"], "risks": ["Check cable orientation and target VREF before programming."], "evidence_tier": "cross_verified", "sources": ["fixture schematic and PCB"], } summary = board_signal.removeprefix("FPGA_") path = [f"J3.{number}", f"{series_resistor} 22R", board_signal, f"U3.{ball}"] elif number in {"2", "10"}: direction, function_category, summary = "passive", "ground", "GND" path = [f"J3.{number}", "GND"] programming = { "status": "not_programmable", "conclusion": "JTAG ground reference", "network_explanation": f"J3.{number} is board GND", "hdl_guidance": "No RTL or ADC assignment", "connection_guidance": ["Connect to probe ground."], "constraints": [], "risks": ["Maintain a common reference."], "evidence_tier": "cross_verified", "sources": ["fixture schematic and PCB"], } elif number == "4": direction, function_category, summary = "passive", "reference_voltage", "VREF" path = ["J3.4", "N9532034", "R61 0R", "+3V3"] programming = { "status": "not_programmable", "conclusion": "VREF is a 3.3V target reference, not board power.", "network_explanation": "J3.4 reaches +3V3 through R61 0R and local decoupling.", "hdl_guidance": "No RTL or ADC assignment", "connection_guidance": ["Probe senses this voltage; do not use it to power the board."], "constraints": [], "risks": ["Back-powering can damage hardware."], "evidence_tier": "cross_verified", "sources": ["fixture schematic and PCB"], } else: direction, function_category, summary = "passive", "not_connected", "NC" path = [f"J3.{number}", "NC"] programming = { "status": "not_programmable", "conclusion": "Unconnected JTAG header position", "network_explanation": f"J3.{number} has no path beyond the connector pad.", "hdl_guidance": "No RTL or ADC assignment", "connection_guidance": ["Leave unconnected."], "constraints": [], "risks": ["Do not repurpose without a board revision."], "evidence_tier": "cross_verified", "sources": ["fixture schematic and PCB"], } pins.append( { "id": f"J3.pad-{number}", "number": number, "pad_role": "contact", "row": row, "column": J3_GRID[row - 1].index(number) + 1, "board_order": board_order, "net": J3_NETS[number], "path": path, "fpga": fpga, "mechanical": False, "electrical": True, "direction": direction, "external_level": "0V" if function_category == "ground" else "3.3V JTAG", "reserved": True, "fixed_load_conflict": False, "function_category": function_category, "evidence_tier": "cross_verified", "programming": programming, "recommendation": {"eligible": False, "reasons": ["reserved"]}, "summary": summary, } ) return { "reference": "J3", "side": "front", "summary": "FPGA JTAG 调试/配置接口(专用配置脚)", "connector": { "profile": "J3_FPGA_JTAG_2X5", "review_status": "cross_verified", "view": "board_front", "contact_count": 10, "auxiliary_pad_count": 0, "rows": 5, "columns": 2, "contact_grid": J3_GRID, "contact_order": order, "sources": ["fixture schematic and PCB"], }, "pins": pins, } def touchpoint(payload: dict[str, object], reference: str) -> dict[str, object]: return next(item for item in payload["touchpoints"] if item["reference"] == reference) def pin(payload: dict[str, object], reference: str, number: str) -> dict[str, object]: return next(item for item in touchpoint(payload, reference)["pins"] if item["number"] == number) def product_payload() -> dict[str, object]: connector_references = sorted( FOUR_CONTACT_REFS | THREE_CONTACT_REFS | FOURTEEN_CONTACT_REFS | POWER_CONTACT_REFS | FAN_CONTACT_REFS ) touchpoints = [connector_fixture(reference) for reference in connector_references] con78 = next(item for item in touchpoints if item["reference"] == "CON78") pins = {pin["number"]: pin for pin in con78["pins"] if pin["number"]} pins["1"].update( { "net": "RS232_TX2", "path": ["CON78.1 RS232_TX2", "U23 TRSF3232EIPWR", "UART2_TX", "FPGA M10"], "fpga": {"ball": "M10", "evidence_tier": "cross_verified"}, "reserved": True, "function_category": "uart_tx", "evidence_tier": "cross_verified", "recommendation": {"eligible": False, "reasons": ["reserved"]}, "programming": { "status": "verified_fpga", "conclusion": "FPGA output UART2_TX M10 through U23", "network_explanation": "RS232_TX2 through U23 to UART2_TX", "hdl_guidance": "output LVCMOS33", "connection_guidance": ["Connect to peer RX", "Do not connect to 3.3V TTL"], "constraints": ["LOCATION = M10", "IOSTANDARD = LVCMOS33"], "risks": ["reserved", "check current constraints"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) pins["2"].update( { "net": "RS232_RX2", "path": ["CON78.2 RS232_RX2", "U23 TRSF3232EIPWR", "UART2_RX", "FPGA N11"], "fpga": {"ball": "N11", "evidence_tier": "cross_verified"}, "reserved": True, "function_category": "uart_rx", "evidence_tier": "cross_verified", "recommendation": {"eligible": False, "reasons": ["reserved"]}, "programming": { "status": "verified_fpga", "conclusion": "FPGA input UART2_RX N11 through U23", "network_explanation": "RS232_RX2 through U23 to UART2_RX", "hdl_guidance": "input LVCMOS33", "connection_guidance": ["Connect to peer TX", "Do not connect to 3.3V TTL"], "constraints": ["LOCATION = N11", "IOSTANDARD = LVCMOS33"], "risks": ["reserved", "check current constraints"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) for number in ("3", "4"): pins[number].update({"net": "GND", "path": [f"CON78.{number} GND", "Board ground"], "function_category": "ground"}) con30 = next(item for item in touchpoints if item["reference"] == "CON30") con30_pin4 = next(pin for pin in con30["pins"] if pin["number"] == "4") con30_pin4.update( { "net": "FAN_GPIO4", "summary": "MOTOR_EN_N", "direction": "output", "external_level": "3.3V LVCMOS", "active_level": "low", "board_pull": "R937 4.7K to +3V3", "evidence_tier": "cross_verified", "fpga": {"ball": "A10", "board_signal": "MOTOR_EN_N", "io_standard": "LVCMOS33", "evidence_tier": "cross_verified"}, "programming": { "status": "verified_fpga", "conclusion": "FPGA output MOTOR_EN_N A10", "network_explanation": "FAN_GPIO4 maps to MOTOR_EN_N", "hdl_guidance": "output LVCMOS33 low active", "connection_guidance": ["board output"], "constraints": ["LOCATION = A10", "IOSTANDARD = LVCMOS33", "有效电平:低电平"], "risks": ["reserved", "check current constraints"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) con31 = next(item for item in touchpoints if item["reference"] == "CON31") con31_pin4 = next(pin for pin in con31["pins"] if pin["number"] == "4") con31_pin4.update( { "net": "N9971986", "summary": "fan PWM open-collector output", "path": ["CON31.4 N9971986", "Q105 collector", "Q105 base = FAN_GPIO6", "R665", "U3.E10"], "evidence_tier": "cross_verified", "external_level": "open-collector fan control", "function_category": "pwm", "reserved": False, "fixed_load_conflict": False, "polarity": "FPGA high -> Q105 on -> connector low", "fpga": { "ball": "E10", "board_signal": "FAN_GPIO6", "io_standard": "LVCMOS33", "capabilities": ["gpio", "pwm_output", "fan_pwm"], "evidence_tier": "cross_verified", }, "programming": { "status": "verified_fpga", "conclusion": "FPGA E10 drives FAN_GPIO6 through Q105 to CON31.4", "network_explanation": "N9971986 is the Q105 collector side, not the FPGA logic net", "hdl_guidance": "output LVCMOS33; FPGA high pulls connector low", "connection_guidance": ["use as open-collector fan PWM"], "constraints": ["LOCATION = E10", "IOSTANDARD = LVCMOS33"], "risks": ["inverted at Q105", "check current constraints"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) con58 = next(item for item in touchpoints if item["reference"] == "CON58") con58_pin4 = next(pin for pin in con58["pins"] if pin["number"] == "4") con58_pin4.update( { "net": "FAN_GPIO38", "summary": "MOTOR_STEP rising-edge pulse output", "direction": "output", "external_level": "3.3V LVCMOS", "function_category": "motor_step", "active_level": "rising_edge", "reserved": True, "fixed_load_conflict": True, "evidence_tier": "cross_verified", "fpga": { "ball": "D15", "board_signal": "MOTOR_STEP", "io_standard": "LVCMOS33", "evidence_tier": "cross_verified", }, "programming": { "status": "verified_fpga", "conclusion": "MOTOR_STEP D15 is a pulse output, not a level enable", "network_explanation": "FAN_GPIO38 maps to MOTOR_STEP through R698", "hdl_guidance": "output pulses; each 上升沿 is one step event", "connection_guidance": ["connect to STEP input"], "constraints": ["LOCATION = D15", "IOSTANDARD = LVCMOS33"], "risks": ["verify pulse timing"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) con65 = next(item for item in touchpoints if item["reference"] == "CON65") con65_pin4 = next(pin for pin in con65["pins"] if pin["number"] == "4") con65_pin4.update( { "net": "FAN_GPIO32", "summary": "MOTOR_DIR direction state output", "direction": "output", "external_level": "3.3V LVCMOS", "function_category": "motor_direction", "active_level": "state", "reserved": True, "fixed_load_conflict": True, "evidence_tier": "cross_verified", "fpga": { "ball": "H14", "board_signal": "MOTOR_DIR", "io_standard": "LVCMOS33", "evidence_tier": "cross_verified", }, "programming": { "status": "verified_fpga", "conclusion": "MOTOR_DIR H14 selects a direction state", "network_explanation": "FAN_GPIO32 maps to MOTOR_DIR through R692", "hdl_guidance": "output 方向状态; stabilize before STEP", "connection_guidance": ["connect to DIR input"], "constraints": ["LOCATION = H14", "IOSTANDARD = LVCMOS33"], "risks": ["0/1 mechanical direction depends on wiring"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) con53 = next(item for item in touchpoints if item["reference"] == "CON53") con53_pin1 = next(pin for pin in con53["pins"] if pin["number"] == "1") con53_pin1.update( { "function_category": "remote_temperature", "reserved": True, "fixed_load_conflict": True, "evidence_tier": "cross_verified", "programming": { "status": "board_only", "conclusion": "CT7428 remote-diode analog input", "network_explanation": "temperature is read through TEMP_SCL/TEMP_SDA", "hdl_guidance": "TEMP I2C must be open-drain and 只拉低或释放", "connection_guidance": ["connect a remote diode pair"], "constraints": [], "risks": ["R811/R812 and other option links require BOM review"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, } ) j_touchpoints = [{"reference": f"J{number}", "side": "front", "pins": []} for number in range(1, 17)] j_touchpoints[2] = j3_fixture() touchpoints.extend(j_touchpoints) touchpoints.extend({"reference": f"PL{number}", "side": "front", "pins": []} for number in range(1, 4)) touchpoints.extend( {"reference": f"TP{number}", "side": "front", "pins": []} for number in [*range(1, 8), *range(9, 31)] ) tp9 = next(item for item in touchpoints if item["reference"] == "TP9") tp9["pins"] = [ { "number": "1", "pad_role": "contact", "mechanical": False, "electrical": True, "function_category": "mcu_gpio", "evidence_tier": "cross_verified", "fpga": None, "mcu": { "device": "U27", "family": "GD32F303CGT6", "pin": "10", "signal": "PA0", "evidence_tier": "cross_verified", }, "programming": { "status": "verified_mcu", "conclusion": "GD32F303CGT6 U27.10 PA0 test point", "network_explanation": "TP9 maps to U27.10 PA0", "hdl_guidance": "configure in GD32 firmware", "connection_guidance": ["use a high-impedance probe"], "constraints": [], "risks": ["check current MCU mux"], "evidence_tier": "cross_verified", "sources": ["fixture"], }, "recommendation": {"eligible": False, "reasons": ["not_fpga"]}, } ] return { "counts": { "footprints": 1052, "touchpoints": 110, "pads": 472, "connector_contacts": 288, "connector_auxiliary_pads": 116, "categories": {"CON": 62, "J": 16, "PL": 3, "TP": 29}, "sides": {"front": 110, "back": 0}, }, "touchpoints": touchpoints, } def test_current_product_regression_accepts_verified_con78_anchor() -> None: validate_product_regression(product_payload()) @pytest.mark.parametrize( "mutation, expected", [ (lambda payload: payload["counts"].update({"connector_contacts": 287}), "connector_contacts=287"), (lambda payload: payload["touchpoints"][0]["connector"].update({"review_status": "unknown"}), "not cross-verified"), (lambda payload: payload["touchpoints"][0]["connector"]["contact_order"].reverse(), "contact order"), (lambda payload: payload["touchpoints"][0]["pins"][0].update({"pad_role": "shield"}), "contact pin role"), ], ) def test_all_connector_profiles_and_board_front_orders_are_regression_anchors(mutation, expected: str) -> None: payload = copy.deepcopy(product_payload()) mutation(payload) with pytest.raises(BoardRegressionError, match=expected): validate_product_regression(payload) @pytest.mark.parametrize( "mutation, expected", [ (lambda payload: payload["counts"].update({"pads": 471}), "pads=471"), (lambda payload: payload["counts"]["categories"].update({"CON": 61}), "CON=61"), (lambda payload: payload["counts"]["sides"].update({"back": 1}), "back=1"), (lambda payload: payload["touchpoints"][1].update({"reference": "CON5"}), "CON5"), (lambda payload: pin(payload, "CON78", "1").update({"net": "WRONG"}), "CON78.1"), (lambda payload: pin(payload, "CON78", "2")["fpga"].update({"ball": "M10"}), "N11"), (lambda payload: pin(payload, "CON78", "1").update({"path": ["CON78.1", "FPGA M10"]}), "U23"), ], ) def test_product_regression_fails_closed_on_anchor_drift(mutation, expected: str) -> None: payload = copy.deepcopy(product_payload()) mutation(payload) with pytest.raises(BoardRegressionError, match=expected): validate_product_regression(payload) def test_any_recommendable_pin_must_satisfy_fail_closed_contract() -> None: payload = product_payload() pin = payload["touchpoints"][0]["pins"][2] pin["recommendation"] = {"eligible": True, "reasons": []} with pytest.raises(BoardRegressionError, match="recommendable pin violates contract"): validate_product_regression(payload) @pytest.mark.parametrize( "mutation, expected", [ (lambda payload: pin(payload, "CON78", "1")["programming"].update({"status": "board_only"}), "CON78.1 programming"), (lambda payload: pin(payload, "CON30", "4")["fpga"].update({"board_signal": "FAN_GPIO4"}), "MOTOR_EN_N"), (lambda payload: pin(payload, "CON30", "4").update({"active_level": "high"}), "low active"), (lambda payload: pin(payload, "CON31", "4")["programming"].update({"status": "board_only"}), "CON31.4 programming"), (lambda payload: pin(payload, "CON58", "4").update({"active_level": "high"}), "MOTOR_STEP"), (lambda payload: pin(payload, "CON65", "4").update({"active_level": "high"}), "MOTOR_DIR"), (lambda payload: pin(payload, "CON53", "1")["programming"].update({"hdl_guidance": "push-pull"}), "CON53.1"), (lambda payload: pin(payload, "TP9", "1")["mcu"].update({"family": "GD32F303CCT6"}), "TP9.1"), ], ) def test_programming_guidance_regression_anchors_fail_closed(mutation, expected: str) -> None: payload = product_payload() mutation(payload) with pytest.raises(BoardRegressionError, match=expected): validate_product_regression(payload) @pytest.mark.parametrize( "mutation", [ lambda payload: touchpoint(payload, "J3")["connector"].update( {"contact_grid": [["1", "6"], ["2", "7"], ["3", "8"], ["4", "9"], ["5", "10"]]} ), lambda payload: pin(payload, "J3", "1")["fpga"].update({"ball": "A6"}), lambda payload: pin(payload, "J3", "4")["programming"].update( {"conclusion": "Generic 3.3V power pin"} ), lambda payload: pin(payload, "J3", "6").update({"function_category": "gpio"}), ], ) def test_j3_jtag_pinout_and_programming_semantics_are_regression_anchors(mutation) -> None: payload = product_payload() mutation(payload) with pytest.raises(BoardRegressionError, match="J3"): validate_product_regression(payload)