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(QMessageBox, 'question', lambda *a, **k: QMessageBox.No) window.repair_icdb_all(); assert len(calls) == 1 monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy) assert len(calls) == 2 and calls[-1].maintenance is True @pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed']) def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(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 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: _owners.pop(lock.key)['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: pytest.fail('blocked view called native reader'), verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)), status_refresher=lambda *a: pytest.fail('unsafe view refreshed native status'), 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() if marker_kind != 'valid': assert window.loaded_path is None and not window.icdb_repair_button.isEnabled() return assert window.visible_records()[0].name == 'IP101GR' assert window.visible_records()[0].status == 'UNKNOWN' assert '未确认' in window.status_label.text() assert 'ICDB' in window.management_capability_label.text() assert '会话' in window.add_button.toolTip() window.select_visible_button.click() assert window.icdb_repair_button.isEnabled() assert not any(button.isEnabled() for button in (window.batch_button, window.selected_batch_button, window.backup_restore_button, window.add_button, window.update_button, window.remove_button)) window.verify_all(); window.verify_selected(); window.restore_backup(); window.manage_cbb('remove') assert window.refresh_affected_records(['IP101GR']) is False assert calls == [] and not window.busy monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy) assert len(calls) == 1 and calls[0].maintenance is True 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.text() assert detail in window.status_label.text() def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(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: _owners.pop(lease.key)['handle'].Close() reads = [] def reader(path): reads.append(path) return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='READY'),)) def verify(req): assert req.maintenance blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary. return VerifyResult('PASS', library_flag='GREEN') monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data', status_refresher=lambda *a: pytest.fail('recovery view must reload after worker completion')) qtbot.addWidget(window) window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy) assert reads == [] window.search_edit.setText('IP101') window.repair_icdb_all() qtbot.waitUntil(lambda: not window.busy and bool(reads)) assert window.visible_records()[0].status == 'READY' assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101'