from pathlib import Path from cbb_refresh_verify.library_reader import read_library class Obj: def __init__(self, **kw): self.__dict__.update(kw) def fake_dispatch(_): class Collection: def __init__(self, values): self.values=values; self.Count=len(values) def Item(self, index): return self.values[index-1] blocks = Collection([Obj(Name='Z_CBB', PartitionName='LP'), Obj(Name='A_CBB', PartitionName='LP')]) blocks2 = Collection([]) partitions = Collection([blocks, blocks2]) return Obj(OpenLibrary=lambda _: Obj(Partitions=lambda kind: partitions)) def test_reader_filters_and_sorts_records(): result = read_library(Path('fixture.lmc'), dispatch=fake_dispatch) assert [item.name for item in result.records] == ['A_CBB', 'Z_CBB'] assert result.error is None def test_reader_surfaces_com_error(): result = read_library(Path('fixture.lmc'), dispatch=lambda _: (_ for _ in ()).throw(RuntimeError('DMS'))) assert result.records == () and 'DMS' in result.error def test_reader_accepts_com_collections_exposing_callable_count(): class Collection: def __init__(self, values): self.values = values def Count(self): return len(self.values) def Item(self, index): return self.values[index - 1] blocks = Collection([Obj(Name='CALLABLE_CBB', PartitionName='LP')]) partitions = Collection([blocks]) dispatch = lambda _: Obj(OpenLibrary=lambda _: Obj(Partitions=lambda kind: partitions)) result = read_library(Path('fixture.lmc'), dispatch=dispatch) assert [record.name for record in result.records] == ['CALLABLE_CBB'] def test_reader_fast_scans_reusable_blocks_without_com(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'B_CBB').mkdir(parents=True) (tmp_path / 'ReusableBlocksLP' / 'A_CBB').mkdir() from cbb_refresh_verify.library_reader import read_library_fast result = read_library_fast(lmc) assert [item.name for item in result.records] == ['A_CBB', 'B_CBB'] assert all(item.status == 'UNKNOWN' for item in result.records) def test_reader_fast_applies_status_reader(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'READY_CBB').mkdir(parents=True) from cbb_refresh_verify.library_reader import read_library_fast result = read_library_fast(lmc, status_reader=lambda name: 'READY' if name == 'READY_CBB' else 'PENDING') assert result.records[0].status == 'READY' def test_reader_fast_bulk_status_reader_is_called_once(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'A_CBB').mkdir(parents=True) (tmp_path / 'ReusableBlocksLP' / 'B_CBB').mkdir() calls = [] from cbb_refresh_verify.library_reader import read_library_fast def status_reader(path, names): calls.append((path, names)) return {'A_CBB': 'READY', 'B_CBB': 'PENDING'} result = read_library_fast(lmc, status_reader=status_reader) assert len(calls) == 1 assert [record.status for record in result.records] == ['READY', 'PENDING'] def test_reader_fast_preserves_unavailable_bridge_status(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'A_CBB').mkdir(parents=True) from cbb_refresh_verify.library_reader import read_library_fast result = read_library_fast(lmc, status_reader=lambda path, names: {'A_CBB': 'UNAVAILABLE'}) assert result.records[0].status == 'UNAVAILABLE' def test_reader_fast_exposes_status_reader_failure_as_unavailable(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'A_CBB').mkdir(parents=True) from cbb_refresh_verify.library_reader import read_library_fast result = read_library_fast(lmc, status_reader=lambda path, names: (_ for _ in ()).throw(RuntimeError('bridge down'))) assert result.records[0].status == 'UNAVAILABLE' def test_filesystem_status_uses_complete_fresh_cert_artifacts(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() root = tmp_path / 'ReusableBlocksLP' ready = root / 'READY_CBB' / 'Work' pending = root / 'PENDING_CBB' / 'Work' for work, name in ((ready, 'READY_CBB'), (pending, 'PENDING_CBB')): (work / 'PCB' / 'Layout').mkdir(parents=True) (work / f'{name}.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8') (work / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8') (work / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt') cert = root / 'READY_CBB' / 'Cert' (cert / 'PCB' / 'Layout').mkdir(parents=True) (cert / 'READY_CBB.prj').write_text('cert', encoding='utf-8') (cert / 'PCB' / 'actual.pcb').write_text('cert pcb', encoding='utf-8') (cert / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'cert lyt') from cbb_refresh_verify.library_reader import read_library_fast, filesystem_status_map statuses = filesystem_status_map(lmc, ('READY_CBB', 'PENDING_CBB')) assert statuses == {'READY_CBB': 'READY', 'PENDING_CBB': 'PENDING'} result = read_library_fast(lmc, status_reader=filesystem_status_map) assert [record.status for record in result.records] == ['PENDING', 'READY'] def test_reconcile_statuses_uses_persisted_pending_when_tree_is_unavailable(): from cbb_refresh_verify.library_reader import reconcile_statuses result = reconcile_statuses( {'A': 'UNKNOWN', 'B': 'READY'}, {'A': 'PENDING', 'B': 'UNKNOWN'}, ('A', 'B'), ) assert result == {'A': 'PENDING', 'B': 'READY'} def test_reconcile_statuses_prefers_calibrated_tree_sources_on_conflict(): from cbb_refresh_verify.library_reader import reconcile_statuses result = reconcile_statuses( {'A': 'READY', 'B': 'PENDING'}, {'A': 'PENDING', 'B': 'READY'}, ('A', 'B'), ) assert result == {'A': 'READY', 'B': 'PENDING'} def test_reconcile_statuses_keeps_matching_sources(): from cbb_refresh_verify.library_reader import reconcile_statuses assert reconcile_statuses({'A': 'READY'}, {'A': 'READY'}, ('A',)) == {'A': 'READY'} def test_context_menu_status_parser_is_removed(): import cbb_refresh_verify.library_reader as reader assert not hasattr(reader, 'status_from_context_menu') def test_reconcile_uses_persisted_ready_when_tree_id_is_unavailable(): from cbb_refresh_verify.library_reader import reconcile_statuses assert reconcile_statuses({'A': 'UNKNOWN'}, {'A': 'READY'}, ('A',)) == {'A': 'READY'} assert reconcile_statuses({'A': 'READY'}, {'A': 'UNKNOWN'}, ('A',)) == {'A': 'READY'} def test_reconcile_prefers_calibrated_tree_id_over_stale_persisted_status(): from cbb_refresh_verify.library_reader import reconcile_statuses assert reconcile_statuses({'A': 'PENDING'}, {'A': 'READY'}, ('A',)) == {'A': 'PENDING'} def test_tree_image_status_is_calibrated_from_filesystem_evidence(): from cbb_refresh_verify.library_reader import infer_tree_statuses result = infer_tree_statuses( {'A': 11, 'B': 11, 'C': 12}, {'A': 'PENDING', 'B': 'PENDING', 'C': 'READY'}, ('A', 'B', 'C'), ) assert result == {'A': 'PENDING', 'B': 'PENDING', 'C': 'READY'} def test_image_status_map_is_derived_from_current_ids_not_fixed_indices(): from cbb_refresh_verify.library_reader import infer_image_status_map assert infer_image_status_map( {'A': 401, 'B': 902}, {'A': 'READY', 'B': 'PENDING'}, ('A', 'B'), ) == {401: 'READY', 902: 'PENDING'} assert infer_image_status_map( {'A': 401, 'B': 401}, {'A': 'READY', 'B': 'PENDING'}, ('A', 'B'), ) == {401: 'PENDING'} def test_pending_id_overrides_stale_complete_cert_for_same_tree_icon(): from cbb_refresh_verify.library_reader import infer_tree_statuses, reconcile_statuses tree = infer_tree_statuses( {'PEF7071_PHY': 10, 'IP101GR': 10}, {'PEF7071_PHY': 'READY', 'IP101GR': 'PENDING'}, ('PEF7071_PHY', 'IP101GR'), ) assert tree == {'PEF7071_PHY': 'PENDING', 'IP101GR': 'PENDING'} assert reconcile_statuses(tree, {'PEF7071_PHY': 'READY', 'IP101GR': 'PENDING'}, ('PEF7071_PHY', 'IP101GR')) == { 'PEF7071_PHY': 'PENDING', 'IP101GR': 'PENDING'} def test_unseen_tree_id_is_pending_when_ready_id_anchor_exists(): from cbb_refresh_verify.library_reader import infer_tree_statuses assert infer_tree_statuses( {'READY_CBB_1': 11, 'READY_CBB_2': 11, 'UNVERIFIED_CBB': 10}, {'READY_CBB_1': 'READY', 'READY_CBB_2': 'READY', 'UNVERIFIED_CBB': 'READY'}, ('READY_CBB_1', 'READY_CBB_2', 'UNVERIFIED_CBB'), ) == {'READY_CBB_1': 'READY', 'READY_CBB_2': 'READY', 'UNVERIFIED_CBB': 'PENDING'} def test_status_log_records_unseen_id_inference(caplog, monkeypatch, tmp_path): import cbb_refresh_verify.library_reader as reader caplog.set_level('INFO') monkeypatch.setattr(reader, 'filesystem_evidence_map', lambda _library, names: { name: {'status': 'READY', 'project_count': 1} for name in names }) result = reader.scan_open_library_status( tmp_path / 'center.lmc', ('READY_CBB_1', 'READY_CBB_2', 'UNVERIFIED_CBB'), tree_image_reader=lambda _library, _names: {'READY_CBB_1': 11, 'READY_CBB_2': 11, 'UNVERIFIED_CBB': 10}, ) assert result == {'READY_CBB_1': 'READY', 'READY_CBB_2': 'READY', 'UNVERIFIED_CBB': 'PENDING'} assert 'unseen_tree_id_vs_ready_anchor' in caplog.text def test_cached_ready_id_classifies_new_tree_id_as_pending(caplog, monkeypatch, tmp_path): import json import cbb_refresh_verify.library_reader as reader caplog.set_level('INFO') monkeypatch.setattr(reader, 'filesystem_evidence_map', lambda _library, names: { name: {'status': 'READY', 'project_count': 1} for name in names }) store = tmp_path / 'image-map.json' store.write_text(json.dumps({'schema_version': 2, 'image_status_map': {'11': 'READY'}}), encoding='utf-8') result = reader.scan_open_library_status( tmp_path / 'center.lmc', ('READY_CBB', 'UNVERIFIED_CBB'), mapping_store=store, tree_image_reader=lambda _library, _names: {'READY_CBB': 11, 'UNVERIFIED_CBB': 10}, ) assert result == {'READY_CBB': 'READY', 'UNVERIFIED_CBB': 'PENDING'} assert 'cached_ready_anchor' in caplog.text def test_cached_ready_id_is_not_downgraded_by_transient_pending_files(caplog, monkeypatch, tmp_path): """A post-Verify scan may see Cert files before they are fully written.""" import json import cbb_refresh_verify.library_reader as reader caplog.set_level('INFO') monkeypatch.setattr(reader, 'filesystem_evidence_map', lambda _library, names: { name: {'status': 'PENDING', 'project_count': 1} for name in names }) store = tmp_path / 'image-map.json' store.write_text(json.dumps({'schema_version': 2, 'image_status_map': {'11': 'READY'}}), encoding='utf-8') result = reader.scan_open_library_status( tmp_path / 'center.lmc', ('VERIFIED_CBB',), mapping_store=store, tree_image_reader=lambda _library, _names: {'VERIFIED_CBB': 11}, ) assert result == {'VERIFIED_CBB': 'READY'} assert 'cached_image_id_authoritative' in caplog.text def test_verified_id_transition_uses_cached_ready_anchor(caplog, monkeypatch, tmp_path): """After Verify, a stale file snapshot must not downgrade the new ID.""" import json import cbb_refresh_verify.library_reader as reader caplog.set_level('INFO') monkeypatch.setattr(reader, 'filesystem_evidence_map', lambda _library, names: { name: {'status': 'PENDING', 'project_count': 1} for name in names }) store = tmp_path / 'image-map.json' store.write_text(json.dumps({'schema_version': 2, 'image_status_map': {'10': 'PENDING', '11': 'READY'}}), encoding='utf-8') result = reader.scan_open_library_status( tmp_path / 'center.lmc', ('VERIFIED_CBB',), mapping_store=store, tree_image_reader=lambda _library, _names: {'VERIFIED_CBB': 11}, ) assert result == {'VERIFIED_CBB': 'READY'} assert 'cached_image_id_authoritative' in caplog.text def test_legacy_unversioned_image_cache_is_ignored(monkeypatch, tmp_path): import json import cbb_refresh_verify.library_reader as reader monkeypatch.setattr(reader, 'filesystem_evidence_map', lambda _library, names: { name: {'status': 'PENDING', 'project_count': 1} for name in names }) store = tmp_path / 'image-map.json' store.write_text(json.dumps({'image_status_map': {'11': 'READY'}}), encoding='utf-8') result = reader.scan_open_library_status( tmp_path / 'center.lmc', ('CBB',), mapping_store=store, tree_image_reader=lambda _library, _names: {'CBB': 11}, ) assert result == {'CBB': 'PENDING'} def test_no_hardcoded_green_image_index_is_exposed(): import cbb_refresh_verify.library_reader as reader import cbb_refresh_verify.mentor_adapter as adapter assert not hasattr(reader, 'READY_IMAGE_INDEX') assert not hasattr(adapter, 'GREEN_TREE_IMAGE_INDEX') def test_unanchored_tree_image_is_unknown_instead_of_guessing(): from cbb_refresh_verify.library_reader import infer_tree_statuses assert infer_tree_statuses({'A': 11}, {'A': 'UNKNOWN'}, ('A',)) == {'A': 'UNKNOWN'} def test_unanchored_tree_image_is_not_calibrated_without_persisted_status(): from cbb_refresh_verify.library_reader import infer_tree_statuses result = infer_tree_statuses( {'A': 27, 'B': 27}, {'A': 'UNKNOWN', 'B': 'UNKNOWN'}, ('A', 'B'), ) assert result == {'A': 'UNKNOWN', 'B': 'UNKNOWN'} def test_status_scan_logs_each_cbb_image_id(caplog, tmp_path): from cbb_refresh_verify.library_reader import scan_open_library_status caplog.set_level('INFO') result = scan_open_library_status( tmp_path / 'center.lmc', ('A', 'B'), tree_image_reader=lambda _library, _names: {'A': 27, 'B': 28}, ) assert set(result) == {'A', 'B'} assert 'name=A image_id=27' in caplog.text assert 'name=B image_id=28' in caplog.text def test_status_scan_keeps_only_missing_cert_or_project_items_pending(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() root = lmc.parent / 'ReusableBlocksLP' ready = root / 'READY_CBB' / 'Work' (ready / 'PCB' / 'Layout').mkdir(parents=True) (ready / 'READY_CBB.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8') (ready / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8') (ready / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt') cert = ready.parent / 'Cert' (cert / 'PCB' / 'Layout').mkdir(parents=True) (cert / 'READY_CBB.prj').write_text('cert', encoding='utf-8') (cert / 'PCB' / 'actual.pcb').write_text('cert pcb', encoding='utf-8') (cert / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'cert lyt') pending = root / 'IP101GR' / 'Work' (pending / 'PCB' / 'Layout').mkdir(parents=True) (pending / 'IP101GR.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8') (pending / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8') (pending / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt') from cbb_refresh_verify.library_reader import scan_open_library_status result = scan_open_library_status( lmc, ('READY_CBB', 'IP101GR', 'MISSING_CBB'), tree_image_reader=lambda _library, _names: {}, ) assert result == {'READY_CBB': 'READY', 'IP101GR': 'PENDING', 'MISSING_CBB': 'PENDING'} def test_filesystem_evidence_exposes_multiple_persistent_dimensions(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() work = tmp_path / 'ReusableBlocksLP' / 'A_CBB' / 'Work' (work / 'PCB' / 'Layout').mkdir(parents=True) (work / 'A_CBB.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8') (work / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8') (work / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt') cert = work.parent / 'Cert' (cert / 'PCB' / 'Layout').mkdir(parents=True) (cert / 'A_CBB.prj').write_text('cert', encoding='utf-8') (cert / 'PCB' / 'actual.pcb').write_text('cert pcb', encoding='utf-8') (cert / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'cert lyt') from cbb_refresh_verify.library_reader import filesystem_evidence_map evidence = filesystem_evidence_map(lmc, ('A_CBB',))['A_CBB'] assert evidence['status'] == 'READY' assert all(evidence[key] for key in ( 'work_project', 'work_pcb', 'work_layout', 'cert_project', 'cert_pcb', 'cert_layout')) def test_filesystem_evidence_preserves_mapped_drive_paths(tmp_path, monkeypatch): """Legacy evidence scanning must not resolve a mapped drive back to UNC.""" lmc = tmp_path / 'center.lmc' lmc.touch() work = tmp_path / 'ReusableBlocksLP' / 'A_CBB' / 'Work' (work / 'PCB' / 'Layout').mkdir(parents=True) (work / 'A_CBB.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8') (work / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8') (work / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt') cert = work.parent / 'Cert' (cert / 'PCB' / 'Layout').mkdir(parents=True) (cert / 'A_CBB.prj').write_text('cert', encoding='utf-8') (cert / 'PCB' / 'actual.pcb').write_text('cert pcb', encoding='utf-8') (cert / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'cert lyt') monkeypatch.setattr(Path, 'resolve', lambda self: pytest.fail('mapped path was resolved')) from cbb_refresh_verify.library_reader import filesystem_evidence_map assert filesystem_evidence_map(lmc, ('A_CBB',))['A_CBB']['status'] == 'READY' def test_missing_work_project_is_a_pending_calibration_anchor(tmp_path): lmc = tmp_path / 'center.lmc' lmc.touch() (tmp_path / 'ReusableBlocksLP' / 'IMPORTED_CBB').mkdir(parents=True) from cbb_refresh_verify.library_reader import filesystem_evidence_map evidence = filesystem_evidence_map(lmc, ('IMPORTED_CBB',))['IMPORTED_CBB'] assert evidence['project_count'] == 0 assert evidence['status'] == 'PENDING'