"""Native occupancy must precede repair even when old addresses remain.""" import pytest from test_maintenance_entry import case, no_native_launch, startup_failure, run_legacy_maintenance from test_native_occupancy import output def inspection(target, *, fault=False): return {'inspection_only': True, 'repair_requested': False, 'native_pid': 777, 'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database), 'output': output(target.project, target.database, fault=fault), 'status': 'NOT_IN_USE', 'repair_required': fault, 'release': {'native_exited': True}} @pytest.mark.parametrize('old_address', [False, True]) def test_diagnosed_native_inspection_holds_lock_after_opening_pcb(case, monkeypatch, old_address): from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock request, project, address = case if not old_address: address.unlink() calls = [] def startup(req, **kw): assert req.diagnostic_only calls.append('open') return startup_failure(req) def native(target, root, **kw): assert calls == ['open'] key = library_lock.LibraryLock(request.lmc_file).key assert library_lock._owners[key]['depth'] > 0 assert kw['inspection_only'] is True assert kw['timeout_seconds'] <= 30 return inspection(target) monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(runner, '_run_verify_serialized', startup) result = run_legacy_maintenance(request) assert result.status == 'CHECKED' and result.recovery_attempts == () assert result.library_flag == 'UNKNOWN' assert result.safe_to_continue is True @pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'stale_verdict', 'not_exited']) def test_unconfirmed_native_inspection_never_repairs(case, monkeypatch, fault): from cbb_refresh_verify import verify_runner as runner, native_repair request, project, address = case def native(target, root, **kw): assert kw['inspection_only'] is True if fault == 'assertion': raise AssertionError('probe failed') if fault == 'missing': return {} record = inspection(target, fault=True) if fault == 'generic': record['output'] = 'DATABASE_SAFETY_STOP' if fault == 'wrong_database': record['database'] = str(project.parent/'other') if fault == 'stale_verdict': record['output'] += 'Checking if project is in use.' if fault == 'not_exited': record['release']['native_exited'] = False return record monkeypatch.setattr(native_repair, 'run_native_repair', native) result = run_legacy_maintenance(request) assert result.status == 'BLOCKED' and not result.recovery_attempts def test_native_fault_repairs_once_without_running_separate_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 calls = [] def native(target, root, **kw): if kw.get('inspection_only'): assert address.exists() calls.append('inspect') return inspection(target, fault=True) calls.append('repair') address.unlink() # Simulated native repair release only. return complete_native_success(target) def verify(req, **kw): if req.diagnostic_only: calls.append('open') return startup_failure(req) assert req.post_repair_proof is not None and not req.diagnostic_only and not req.auto_repair calls.append('verify') return runner.VerifyResult('PASS', library_flag='GREEN') monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) monkeypatch.setattr(runner, '_run_verify_serialized', verify) result = run_legacy_maintenance(request) assert calls == ['open', 'inspect', 'repair'] assert result.status == 'REPAIRED' and len(result.recovery_attempts) == 1 assert result.library_flag == 'UNKNOWN' and not result.recovery_attempts[0]['validation_passed']