"""Exercise the production diagnosis gate; do not spoof callback names.""" import json from pathlib import Path import pytest from test_maintenance_entry import case, no_native_launch def diagnostic_result(req, *, error='', database=None): from cbb_refresh_verify.verify_runner import VerifyResult root = req.evidence_dir/'probe'; root.mkdir(parents=True, exist_ok=True) result = VerifyResult('FAIL' if error else 'PASS', error_category=error, evidence_path=root/'result.json', safe_to_continue=not error) result.evidence_path.write_text('{}', encoding='utf-8') if error == 'ICDB_REPAIR_REQUIRED': record = {'error_code': error, 'scope': 'project', 'session': 'test-session', 'database_path': str(database)} (root/'diagnostics').mkdir(exist_ok=True) (root/'diagnostics/icdb-recovery.json').write_text(json.dumps(record), encoding='utf-8') return result def test_diagnosis_holds_library_lock_and_healthy_never_repair(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock request, project, address = case address.unlink() def worker(req, **kw): key = library_lock.LibraryLock(req.lmc_file).key assert key in library_lock._owners assert library_lock._owners[key]['depth'] > 0 assert req.diagnostic_only and not req.auto_repair and not req.maintenance assert not req.native_lm_verify and not req.libspec return diagnostic_result(req) monkeypatch.setattr(runner, '_run_verify_serialized', worker) monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('healthy repair')) result = runner.run_verify(request) assert result.status == 'PASS' and result.recovery_attempts == () assert result.library_flag == 'UNKNOWN' assert not library_lock.has_blocker(request.lmc_file) @pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'outside_evidence']) def test_unconfirmed_diagnosis_never_repairs(case, monkeypatch, fault): from cbb_refresh_verify import verify_runner as runner, native_repair request, project, address = case address.unlink() def worker(req, **kw): if fault == 'assertion': raise AssertionError('diagnostic failure') if fault == 'missing': return runner.VerifyResult('PASS') if fault == 'generic': return diagnostic_result(req, error='DATABASE_SAFETY_STOP') if fault == 'outside_evidence': from dataclasses import replace req = replace(req, evidence_dir=request.evidence_dir/'old-run') return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', database=project.parent/('wrong' if fault == 'wrong_database' else 'database')) monkeypatch.setattr(runner, '_run_verify_serialized', worker) monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('unconfirmed repair')) result = runner.run_verify(request) assert result.status != 'PASS' and result.recovery_attempts == () def test_diagnostic_fault_repairs_once_then_full_verification(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success request, project, address = case address.unlink() calls = [] def worker(req, **kw): calls.append('diagnose' if req.diagnostic_only else 'verify') if req.diagnostic_only: return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', database=project.parent/'database') assert req.post_repair_proof is not None and not req.auto_repair return runner.VerifyResult('PASS', library_flag='GREEN') def repair(target, *a, **kw): calls.append('repair') return complete_native_success(target) monkeypatch.setattr(runner, '_run_verify_serialized', worker) monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) result = runner.run_verify(request) assert calls == ['diagnose', 'repair', 'verify'] assert result.status == 'PASS' and len(result.recovery_attempts) == 1