from pathlib import Path import pytest from test_maintenance_entry import case def test_verify_defaults_to_no_repair_and_does_not_retain_a_failure_lock(tmp_path, monkeypatch): from cbb_refresh_verify import verify_runner as runner, library_lock as locks, native_repair library = tmp_path / 'library/library.lmc'; library.parent.mkdir(); library.touch() req = runner.VerifyRequest(library, 'CBB', tmp_path / 'evidence') assert req.auto_repair is False marker = locks.blocker_path(library) marker.write_text('historical diagnostic record') calls = [] result = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: session lost', error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True) monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: (calls.append('verify') or result)) monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **k: pytest.fail('verify requested Repair')) monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: []) monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: []) first = runner.run_verify(req) assert first.status == result.status and first.error_category == result.error_category assert first.release_proof['status'] == 'RELEASE_CONFIRMED' assert runner.run_verify(req) is result assert calls == ['verify', 'verify'] assert not marker.exists() archive = Path(first.release_proof['evidence_path']).with_name('blocker-before-0.json') assert archive.read_text() == 'historical diagnostic record' assert locks._key(library.parent / '.library.cbb-upload.lock') not in locks._owners def test_catalog_checkpoint_does_not_disable_ordinary_verify(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult calls = [] window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: lost', error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True)), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) window._maintenance_recovery_view = True window._management_unsafe = True window.search_edit.setText('Beta'); window.select_visible_button.click(); window._update() assert window.selected_batch_button.isEnabled() window.verify_selected(); qtbot.waitUntil(lambda: not window.busy) assert len(calls) == 1 and not calls[0].maintenance and not calls[0].auto_repair assert window.selected_batch_button.isEnabled() assert 'ICDB' in window.cbb_table.item(window._row_by_name['Beta'], 3).text() def test_management_release_block_is_scoped_to_the_loaded_library(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.management import Capability adapter = type('Adapter', (), { 'capabilities': lambda self: {name: Capability(True, 'available') for name in ('add', 'update', 'remove')}})() window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter) window._management_unsafe = True window._management_unsafe_library = (tmp_path/'library-a.lmc').resolve() window.lmc_edit.setText(str(tmp_path/'library-b.lmc')) window.loaded_path = str(tmp_path/'library-b.lmc') window.mentor_lmc_path = Path(window.loaded_path) window.select_visible_button.click() window._update() assert window.add_button.isEnabled() assert '上次管理操作' not in window._management_block_reason('update') def test_loading_a_different_library_clears_old_management_block(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window window = load_window(qtbot, tmp_path, monkeypatch) old = (tmp_path/'library-a.lmc').resolve() window._management_unsafe = True window._management_unsafe_library = old window.lmc_edit.setText(str(tmp_path/'library-b.lmc')) window.load_library() qtbot.waitUntil(lambda: not window.busy) assert not window._management_unsafe assert window._management_unsafe_library is None def test_targeted_native_idle_ignores_unrelated_native_command(tmp_path, monkeypatch): from cbb_refresh_verify import maintenance_entry as entry from cbb_refresh_verify.native_repair import RepairTarget work = tmp_path/'target/Work'; work.mkdir(parents=True) library = tmp_path/'library.lmc'; library.touch() project = work/'target.prj' project.write_text(f'KEY CentralLibrary "{library}"\n', encoding='utf-8') other_pcb = tmp_path/'other/Work/PCB/other.pcb' monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: [ {'pid': 10, 'name': 'ExpeditionPCB.exe', 'command': f'ExpeditionPCB.exe "{other_pcb}"'}, ]) entry.assert_native_idle(RepairTarget(project, work/'database')) def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch): from cbb_refresh_verify import maintenance_entry as entry from cbb_refresh_verify import scoped_manifest as manifests req, project, _ = case scans = [] scan = manifests.scan_tree monkeypatch.setattr(manifests, 'scan_tree', lambda root, **kw: (scans.append(Path(root)) or scan(root, **kw))) with entry.maintenance_target_guard(req): pass assert req.lmc_file.parent not in scans assert scans.count(project.parent) == 2 def test_repair_finishes_without_calling_verify(case, monkeypatch): from cbb_refresh_verify import native_repair, verify_runner as runner from test_maintenance_entry import patch_verification_after_fault, run_legacy_maintenance from test_native_repair import complete_native_success req, _, address = case def repair(target, *a, **kw): address.unlink() return complete_native_success(target) monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('maintenance started CBB verification')) result = run_legacy_maintenance(req) assert result.status == 'REPAIRED' and result.safe_to_continue assert result.library_flag == 'UNKNOWN' assert result.recovery_attempts[0]['validation_passed'] is False @pytest.mark.parametrize('stage,expected_safe', [('PCB_STARTING', True), ('back_annotation', True)]) def test_released_failure_can_continue_from_icdb_diagnosis(tmp_path, monkeypatch, stage, expected_safe): import json from cbb_refresh_verify import verify_runner as runner library = tmp_path/'library.lmc'; library.touch() evidence = tmp_path/'evidence'; evidence.mkdir() report = evidence/'result.json'; report.write_text('{}') (evidence/'session-release.json').write_text(json.dumps({'library': str(library), 'block': 'CBB', 'worker_exited': True, 'safe_to_continue': True})) failure = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: lost session', error_stage=stage, safe_to_continue=False, evidence_path=report) monkeypatch.setattr(runner, '_run_verify', lambda *a, **k: failure) result = runner._run_verify_serialized(runner.VerifyRequest(library, 'CBB', evidence)) assert result.status == 'FAIL' and result.error_category == 'ICDB_REPAIR_REQUIRED' assert result.safe_to_continue is expected_safe def test_live_writer_keeps_execution_lock_without_a_permanent_diagnostic_blocker(tmp_path, monkeypatch): import win32con, win32file from cbb_refresh_verify import library_lock as locks, verify_runner as runner library = tmp_path/'lib.lmc'; library.touch() monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('TIMEOUT', safe_to_continue=False)) result = runner.run_verify(runner.VerifyRequest(library, 'CBB', tmp_path/'evidence')) lock = locks.LibraryLock(library) try: assert not result.safe_to_continue and not locks.blocker_path(library).exists() with pytest.raises(Exception): win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None) finally: with locks._guard: owner = locks._owners.pop(lock.key, None) if owner: owner['handle'].Close() def test_healthy_open_with_old_address_does_not_lock_management(case, monkeypatch): from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock as locks request, project, address = case monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **kw: runner.VerifyResult('PASS')) monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw: pytest.fail('scan requested Project Backup')) result = runner.run_verify(request) assert result.status == 'CHECKED' and result.safe_to_continue and address.exists() assert not result.stderr and '未发现 ICDB 错误' in result.stdout assert not locks.blocker_path(request.lmc_file).exists() with locks.LibraryLock(request.lmc_file): pass @pytest.mark.parametrize('operation', ['verify', 'maintenance']) def test_retained_execution_lease_can_retry_after_its_actual_client_exits(tmp_path, operation): from contextlib import contextmanager from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy library = tmp_path/'library.lmc'; library.touch() released = [False] with LibraryLock(library, checkpoint=False, safe_to_release=lambda: released[0]): pass with pytest.raises(LibraryBusy): with LibraryLock(library, checkpoint=False): pass released[0] = True @contextmanager def guard(): yield {'target': 'isolated fixture'} lock = (LibraryLock(library, checkpoint=False) if operation == 'verify' else LibraryLock.for_maintenance(library, tmp_path/'archive', guard, safe_to_release=lambda: True)) with lock: pass