"""Batch ICDB inspection never opens PCB or dispatches Repair.""" from dataclasses import replace import json import pytest from test_maintenance_entry import case, no_native_launch from test_maintenance_diagnosis import inspection @pytest.mark.parametrize('scenario,status,category,safe', [ ('healthy', 'CHECKED', '', True), ('fault', 'FAIL', 'ICDB_REPAIR_REQUIRED', True), ('busy', 'BLOCKED', 'ICDB_DATABASE_IN_USE', True), ('unknown', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), ('unexpected_repair', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), ('missing_exit_proof', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False), ('not_launched', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), ('unreleased', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False), ('cancelled', 'CANCELLED', '', True), ]) def test_batch_check_only_loads_native_inspection(case, monkeypatch, scenario, status, category, safe): from cbb_refresh_verify import verify_runner as runner, native_repair, maintenance_entry as entry from test_native_occupancy import output request, _, address = case calls = [] monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB')) monkeypatch.setattr(entry, 'assert_native_idle', lambda: pytest.fail('check used host-wide owner gate')) def native(target, root, **kw): assert kw['inspection_only'] is True and not kw.get('maintenance_context') calls.append('inspect') record = inspection(target, fault=scenario == 'fault') if scenario == 'busy': record.update(status='IN_USE', output=output(target.project, target.database, busy=True)) if scenario in ('unknown', 'unreleased'): record.update(status='UNCONFIRMED', output='No conclusive native response') if scenario == 'unreleased': record['release']['native_exited'] = False if scenario == 'cancelled': record.update(status='CANCELLED') if scenario == 'unexpected_repair': record.update(status='REPAIRED', repair_requested=True) if scenario == 'missing_exit_proof': return {} if scenario == 'not_launched': return {'status': 'BLOCKED', 'native_started': False} return record monkeypatch.setattr(native_repair, 'run_native_repair', native) result = runner.run_verify(replace(request, auto_repair=True)) assert calls == ['inspect'] assert (result.status, result.error_category, result.safe_to_continue) == (status, category, safe) assert result.library_flag == 'UNKNOWN' and result.recovery_attempts == () assert address.read_bytes() == b'old server address - preserved' record = json.loads(result.evidence_path.read_text('utf-8')) assert record['operation'] == 'icdb_batch_check' and record['automatic_repair'] is False @pytest.mark.parametrize('case_type', ['no_layout', 'no_pcb', 'no_icdb', 'ambiguous_repair_targets']) def test_inspection_does_not_require_repair_admission(case, monkeypatch, case_type): from cbb_refresh_verify import verify_runner as runner, native_repair request, project, address = case if case_type == 'no_layout': (project.parent/'PCB/Layout/LayoutDB.lyt').unlink() if case_type == 'no_pcb': (project.parent/'PCB/IP101GR.pcb').unlink() if case_type == 'no_icdb': (project.parent/'database/icdb.dat').unlink() if case_type == 'ambiguous_repair_targets': address.unlink() (project.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "database"\n') db = project.parent/'PCB/database'; db.mkdir(); (db/'icdb.dat').touch() calls = [] def native(target, *a, **kw): assert kw['inspection_only'] is True and target.project == project calls.append('inspect') return inspection(target) monkeypatch.setattr(native_repair, 'run_native_repair', native) result = runner.run_verify(request) assert calls == ['inspect'] assert result.recovery_attempts == () @pytest.mark.parametrize('before_launch', [True, False]) def test_cancellation_never_dispatches_repair_or_reports_a_clean_check(case, monkeypatch, before_launch): from threading import Event from cbb_refresh_verify import verify_runner as runner, native_repair request, _, _ = case stop = Event(); calls = [] if before_launch: stop.set() def native(target, *a, **kw): assert kw['inspection_only'] is True calls.append('inspect'); stop.set() return inspection(target) monkeypatch.setattr(native_repair, 'run_native_repair', native) result = runner.run_verify(request, stop=stop) assert result.status == 'CANCELLED' and result.safe_to_continue assert calls == ([] if before_launch else ['inspect']) def test_released_icdb_fault_continues_the_check_batch(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair request, _, _ = case calls = [] def native(target, root, **kw): assert kw['inspection_only'] is True calls.append('inspect') return inspection(target, fault=len(calls) == 1) monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB')) results = runner.run_batch([request, request]) assert calls == ['inspect', 'inspect'] assert [result.status for result in results] == ['FAIL', 'CHECKED'] def test_inspection_ignores_other_pcb_and_skips_exclusive_repair_probe(tmp_path, monkeypatch): import win32con from test_native_repair import repair_worker_fixture from test_native_occupancy import output ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True job.write_text(json.dumps(data), encoding='utf-8') monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [(999, 'other.pcb', '')]) monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access', lambda *a: pytest.fail('inspection used exclusive Repair admission')) monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command')) monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], busy=True)) assert ui.repair_worker_main(job) == 0 assert actions == [(100, win32con.WM_CLOSE)] def test_ui_check_summary_reports_findings_and_never_claims_repair(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult from PySide6.QtWidgets import QMessageBox requests, prompts = [], [] window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda req: (requests.append(req) or VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (prompts.append(a[2]) or QMessageBox.Yes)) window.search_edit.setText('Beta') window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) assert requests[0].maintenance and not requests[0].auto_repair assert '批量检查' in window.icdb_repair_button.text() assert not any('手动备份' in text for text in prompts) assert window.summary.windowTitle() == 'ICDB 批量检查结果' assert '发现问题 1' in window.summary.text() assert '修复并校验通过' not in window.summary.detailedText() @pytest.mark.parametrize('category,label', [('ICDB_DATABASE_IN_USE', '占用 1'), ('ICDB_INSPECTION_UNCONFIRMED', '无法确认 1')]) def test_busy_or_unknown_inspection_is_not_labelled_as_database_damage(qtbot, tmp_path, monkeypatch, category, label): from test_ui_filters import load_window from cbb_refresh_verify.verify_runner import VerifyResult window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', error_category=category, stderr='暂未完成检查。'), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) window.search_edit.setText('Beta') window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) assert label in window.summary.text() assert 'ICDB 问题' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text() def test_missing_pcb_is_skipped_from_icdb_batch_check_and_restore(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.models import CbbRecord from cbb_refresh_verify.verify_runner import VerifyResult calls = [] window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('Missing', 'Power', status='PENDING'),), verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) assert not window.icdb_repair_button.isEnabled() assert not window.batch_button.isEnabled() and not window.backup_restore_button.isEnabled() assert calls == [] def test_missing_project_rows_are_excluded_from_icdb_batch_check(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.models import CbbRecord from cbb_refresh_verify.verify_runner import VerifyResult window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('Missing', 'Power', status='PENDING'), CbbRecord('Good', 'Power', status='PENDING')), verifier=lambda req: VerifyResult('CHECKED'), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) window.skipped_names.add('Missing') window._update() assert window._maintenance_names() == ['Good'] def test_check_reports_describe_inspection_without_pcb_verification(case, tmp_path): from cbb_refresh_verify.verify_runner import write_batch_report, failure_report_path, VerifyResult request, _, _ = case report = write_batch_report([request], [VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')], tmp_path/'reports') full = report.read_text('utf-8-sig') failed = failure_report_path(report).read_text('utf-8-sig') assert 'ICDB 批量检查' in full and 'Project Integration' not in full assert 'ICDB 检查异常' in failed and '发现问题: 1' in failed assert 'ICDB已修复待校验' not in failed