main
John Lauer Published limits by default, preferences as profiles: PCBWay 4L back to its published split limits (4 mil inner, 0.30 m… f738814
"""PCBWay standard 4-layer 1 oz: the PUBLISHED limits, split the way PCBWay splits them, must appear in the generated KiCad rules, each as an explicit error.
Source archive SHA-256 values are the ones recorded in the PCBDS r7 proof. Syntax-only passes are NOT accepted as proof: see DRC coupon evidence."""
import unittest, importlib.util, json, re
from pathlib import Path
BASE = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('rules', BASE / 'tools' / 'generate.py')
g = importlib.util.module_from_spec(spec); spec.loader.exec_module(g)
REC = [r for r in json.loads((BASE / 'rules/records/pcbway.json').read_text())['records'] if r['process'] == 'standard' and r['layerCount'] == 4][0]
TXT = g.gen_kicad_dru('PCBWay', REC)
STD_SHA = '694ce30266e8c0bde8173d590c3ac71e6d928eb730fad3555e6a1dd298e0a980'
LIM_SHA = '9cebdeed859c3c61d4e104185aa5e6a54fb1a5c6ac5864591fbd5993479145e1'
TOL_SHA = '51142afe99832cc0ee41af19bf7d4c552a2658da466f04850523fab09bb66948'
def rule(name):
    m = re.search(r'\(rule "%s"\n(.*?)(?:\n\n|\Z)' % re.escape(name), TXT, flags=re.S)
    return m.group(1) if m else None
class PCBWay4L(unittest.TestCase):
    def test_no_invalid_keyword_or_layer_qualifier(self):
        self.assertNotIn('(constraint hole ', TXT)
        self.assertNotIn('(layer "inner")', TXT)  # KiCad's layer keywords are bare
        self.assertNotIn('(layer "outer")', TXT)
    def test_every_rule_is_explicit_error(self):
        blocks = re.findall(r'\(rule "[^"]+"\n(.*?)(?:\n\n|\Z)', TXT, flags=re.S)
        self.assertTrue(blocks)
        for b in blocks: self.assertIn('(severity error)', b)
    def test_track_width_published_per_layer(self):
        # outer 1 oz 5 mil, inner 1 oz 4 mil (capabilities table)
        self.assertIn('(constraint track_width (min 0.127mm))', rule('min-track-width-outer'))
        self.assertIn('(layer outer)', rule('min-track-width-outer'))
        self.assertIn('(constraint track_width (min 0.1016mm))', rule('min-track-width-inner'))
        self.assertIn('(layer inner)', rule('min-track-width-inner'))
        bs = json.loads(g.gen_kicad_board_setup('PCBWay', REC))
        self.assertEqual(bs['boardSetupConstraints']['minTrackWidthMm'], 0.1016)
    def test_no_unconditional_clearance_rule_and_board_floor_carries_0_1524(self):
        self.assertIsNone(rule('min-clearance'))  # would override netclass clearance (measured)
        bs = json.loads(g.gen_kicad_board_setup('PCBWay', REC))
        self.assertEqual(bs['boardSetupConstraints']['minClearanceMm'], 0.1524)
    def test_via_and_pth_annular_rings_are_separate(self):
        self.assertIn("A.Type == 'Via'", rule('min-via-annular-width'))
        self.assertIn('(constraint annular_width (min 0.15mm))', rule('min-via-annular-width'))
        self.assertIn("A.Pad_Type == 'Through-hole'", rule('min-pad-annular-width'))
        self.assertIn('(constraint annular_width (min 0.254mm))', rule('min-pad-annular-width'))
    def test_hole_spacing_published_per_hole_type(self):
        # limited_holes_spacing page: 0.30 mm via to via, 0.45 mm where a component hole is involved; the
        # component rule comes after the via rule because the later matching KiCad rule wins
        self.assertIn('(constraint hole_to_hole (min 0.3mm))', rule('min-hole-to-hole-via'))
        self.assertIn('(constraint hole_to_hole (min 0.45mm))', rule('min-hole-to-hole-component'))
        self.assertIn("A.Type == 'Pad' || B.Type == 'Pad'", rule('min-hole-to-hole-component'))
        self.assertLess(TXT.index('min-hole-to-hole-via'), TXT.index('min-hole-to-hole-component'))
    def test_inner_hole_to_copper_7mil_is_emitted(self):
        self.assertIn('(constraint hole_clearance (min 0.1778mm))', rule('min-hole-to-inner-copper'))
    def test_silk_height_and_width(self):
        self.assertIn('(constraint text_height (min 0.8mm))', rule('min-silk-height'))
        self.assertIn('(constraint text_thickness (min 0.15mm))', rule('min-silk-thickness'))
    def test_copper_edge_and_drill_and_via_diameter(self):
        self.assertIn('(constraint edge_clearance (min 0.25mm))', rule('min-edge-clearance'))
        self.assertIn('(constraint hole_size (min 0.2mm))', rule('min-hole'))
        self.assertIn('(constraint via_diameter (min 0.5mm))', rule('min-via-diameter'))
    def test_record_carries_source_archive_hashes(self):
        shas = [s.get('archiveSha256') for s in REC.get('sources', [])]
        for need in (STD_SHA, LIM_SHA, TOL_SHA): self.assertIn(need, shas)
    def test_pth_ring_is_strictest_of_sources(self):
        self.assertGreaterEqual(REC['annularRing']['padMm'], 0.254)  # tolerances page 0.20 is weaker and is not used
class CommittedOutputsMatchGenerator(unittest.TestCase):
    """Every committed PCBWay 4L output must be exactly what the generator produces for its record (no stale 4-layer files)."""
    def test_all_pcbway_4l_outputs_match_generator(self):
        pairs = [
            (BASE/'kicad'/'pcbway-4L.kicad_dru', g.gen_kicad_dru('PCBWay', REC)),
            (BASE/'kicad'/'pcbway-4L.board-setup.json', g.gen_kicad_board_setup('PCBWay', REC)),
            (BASE/'fusion'/'pcbway-4L.dru', g.gen_dru('PCBWay', REC)),
            (BASE/'fusion'/'pcbway-4L.edru', g.gen_edru('PCBWay', REC)),
            (BASE/'altium'/'pcbway-4L.RUL', g.gen_rul('PCBWay', REC)),
        ]
        for path, expected in pairs:
            self.assertEqual(path.read_text(encoding='utf-8'), expected, path.name)
if __name__ == '__main__': unittest.main(verbosity=2)