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.lmc'; 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')) assert runner.run_verify(req) is result assert runner.run_verify(req) is result assert calls == ['verify', 'verify'] assert marker.read_text() == 'historical diagnostic record' assert locks._key(tmp_path / '.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_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 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 = runner.run_verify(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', False)]) def test_only_released_startup_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_native_inspection_with_old_address_does_not_lock_management(case, monkeypatch): from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock as locks from test_native_occupancy import output request, project, address = case def inspect(target, *a, **kw): assert kw['inspection_only'] return {'inspection_only': True, 'repair_requested': False, 'repair_required': False, 'status': 'NOT_IN_USE', 'native_pid': 777, 'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database), 'output': output(target.project, target.database), 'release': {'native_exited': True}} monkeypatch.setattr(native_repair, 'run_native_repair', inspect) result = runner.run_verify(request) assert result.status == 'CHECKED' and result.safe_to_continue and address.exists() assert not result.stderr and '无需修复' 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