from cbb_refresh_verify.ui import timed_prompt import json from pathlib import Path import pytest from PySide6.QtCore import Qt from PySide6.QtWidgets import QMessageBox def test_only_confirmed_maintenance_sets_explicit_flag(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', safe_to_continue=True)), status_refresher=lambda names, path: {name: 'PENDING' for name in names}) window.search_edit.setText('Beta') window.verify_all(); qtbot.waitUntil(lambda: not window.busy) assert calls and calls[-1].maintenance is False monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.No) window.repair_icdb_all(); assert len(calls) == 1 monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes) window.repair_icdb_all(); qtbot.waitUntil(lambda: not window._icdb_active()) assert len(calls) == 2 and calls[-1].maintenance is True @pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed']) def test_old_checkpoint_does_not_force_maintenance_or_disable_verify(qtbot, tmp_path, monkeypatch, marker_kind): from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.verify_runner import VerifyResult from cbb_refresh_verify.models import LibraryReadResult, CbbRecord import cbb_refresh_verify.ui.main_window as ui library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) with LibraryLock(library, safe_to_release=lambda: False) as lock: pass with _guard: owner = _owners.pop(lock.key, None) if owner: owner['handle'].Close() marker = blocker_path(library) if marker_kind == 'malformed': marker.write_text('{}') if marker_kind == 'foreign': record = json.loads(marker.read_text()); record['library'] = str(tmp_path/'foreign/mentor.lmc') marker.write_text(json.dumps(record)) monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) calls = [] window = MainWindow(reader=lambda *a: LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)), verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)), status_refresher=lambda *a: {'IP101GR': 'PENDING'}, data_root=tmp_path/'data') qtbot.addWidget(window) window.lmc_edit.setText(str(library)); window.load_library() qtbot.waitUntil(lambda: not window.busy) assert window.release_library_button.isEnabled() assert window.visible_records()[0].name == 'IP101GR' assert window.visible_records()[0].status == 'PENDING' assert not window._maintenance_recovery_view window.select_visible_button.click() assert window.icdb_repair_button.isEnabled() assert window.batch_button.isEnabled() and window.selected_batch_button.isEnabled() monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes) window.verify_selected(); qtbot.waitUntil(lambda: not window.busy) assert len(calls) == 1 and calls[0].maintenance is False assert window.selected_batch_button.isEnabled() window.repair_icdb_all(); qtbot.waitUntil(lambda: not window._icdb_active()) assert len(calls) == 2 and calls[1].maintenance is True assert marker.exists() def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path): from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.verify_runner import VerifyResult window = MainWindow(data_root=tmp_path) qtbot.addWidget(window) window._global_icdb_run = True detail = 'MAINTENANCE_TARGET_UNCONFIRMED: PCB 引用与当前工程不一致' window._batch_done(([VerifyResult('BLOCKED', stderr=detail, safe_to_continue=False)], tmp_path/'batch.log')) assert detail in window.summary.detailedText() assert detail in window.output.toPlainText() assert detail not in window.summary.text() assert str(tmp_path) not in window.summary.text() assert detail not in window.status_label.text() assert '无法确认 1' in window.summary.text() def test_open_check_preserves_verify_state_without_starting_an_extra_catalog_read(qtbot, tmp_path, monkeypatch): from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.models import LibraryReadResult, CbbRecord from cbb_refresh_verify.verify_runner import VerifyResult import cbb_refresh_verify.ui.main_window as ui library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) with LibraryLock(library, safe_to_release=lambda: False) as lease: pass with _guard: owner = _owners.pop(lease.key, None) if owner: owner['handle'].Close() reads = [] def reader(path): reads.append(path) return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)) def verify(req): assert req.maintenance blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary. return VerifyResult('CHECKED') monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes) window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data', status_refresher=lambda *a: pytest.fail('An open check must not start a second native status read')) qtbot.addWidget(window) window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy) assert len(reads) == 1 window.search_edit.setText('IP101') window.repair_icdb_all() qtbot.waitUntil(lambda: not window._icdb_active()) assert window.visible_records()[0].status == 'PENDING' and len(reads) == 1 assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101'