feat: scope CBB search selection and maintenance to visible non-READY rows

This commit is contained in:
阿白叔 committed 2026-11-02 18:47:25 +08:00
1 parent c823a4b92d
commit 747ce1add2
5 files changed
+572 -106

No files matched your search

+8 -7
View File
@@ -51,11 +51,12 @@ def test_backup_dialog_keeps_ui_responsive_and_cannot_close_during_restore(qtbot
assert dialog.close_button.isEnabled()
def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_path):
def test_main_window_opens_non_ready_dialog_and_failure_retains_ready_untargeted(qtbot, tmp_path):
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.library_reader import CbbRecord, LibraryReadResult
window = MainWindow(reader=lambda _: LibraryReadResult((
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path)
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path,
status_refresher=lambda names, _: {name: 'UNKNOWN' for name in names})
qtbot.addWidget(window)
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
@@ -63,12 +64,12 @@ def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_pat
window.backup_restore_button.click()
dialog = window.backup_dialog
assert dialog.isVisible()
assert dialog.block_combo.currentText() == 'A'
dialog.started.emit('A')
assert dialog.block_combo.currentText() == 'B'
dialog.started.emit('B')
assert window.busy
assert 'A' not in window.ready_names
dialog.completed.emit('A', VerifyResult('FAIL', stderr='损坏'))
assert window.cbb_table.item(0, 2).text() == '未知 / 需确认'
assert 'A' in window.ready_names
dialog.completed.emit('B', VerifyResult('FAIL', stderr='无法确认', safe_to_continue=False))
assert window.cbb_table.item(1, 2).text() == '未知 / 需确认'
dialog.reject()
qtbot.waitUntil(lambda: not window.busy)
+27 -17
View File
@@ -1,4 +1,5 @@
import os
import pytest
from pathlib import Path
os.environ.setdefault('QT_QPA_PLATFORM','offscreen')
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
@@ -21,7 +22,15 @@ def test_application_palette_keeps_table_text_readable(qtbot):
def fake_reader(_): return LibraryReadResult((CbbRecord('CBB1','Reusable Blocks'),))
def test_global_icdb_action_confirms_and_includes_ready_rows(qtbot, tmp_path, monkeypatch):
@pytest.fixture(autouse=True)
def isolate_authoritative_native_refresh(monkeypatch):
# Production refresh crosses the native Mentor boundary; these are UI tests.
monkeypatch.setattr('cbb_refresh_verify.ui.main_window.authoritative_status_map',
lambda names, *_: {name: 'READY' for name in names})
def test_global_icdb_action_confirms_and_excludes_ready_rows(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
calls = []
window = MainWindow(reader=lambda _: LibraryReadResult((
@@ -37,7 +46,7 @@ def test_global_icdb_action_confirms_and_includes_ready_rows(qtbot, tmp_path, mo
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['READY', 'PENDING']
assert calls == ['PENDING']
def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot):
window = MainWindow(reader=fake_reader)
@@ -49,11 +58,11 @@ def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot):
window.busy = True; window._update()
assert not window.icdb_repair_button.isEnabled()
def test_global_icdb_failure_clears_ready_and_stops_queue(qtbot, tmp_path, monkeypatch):
def test_global_icdb_failure_preserves_untargeted_ready_and_stops_queue(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
calls = []
window = MainWindow(reader=lambda _: LibraryReadResult((
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))),
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'), CbbRecord('C', 'LP'))),
verifier=lambda req: (calls.append(req.block_name) or VerifyResult(
'FAIL', stderr='unconfirmed', safe_to_continue=False)), data_root=tmp_path)
qtbot.addWidget(window)
@@ -62,10 +71,10 @@ def test_global_icdb_failure_clears_ready_and_stops_queue(qtbot, tmp_path, monke
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['A']
assert 'A' not in window.ready_names
assert window.cbb_table.item(0, 2).text() == '未知 / 需确认'
assert window.cbb_table.item(1, 3).text().startswith('SKIPPED')
assert calls == ['B']
assert 'A' in window.ready_names
assert window.cbb_table.item(1, 2).text() == '未知 / 需确认'
assert window.cbb_table.item(2, 3).text().startswith('SKIPPED')
def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
@@ -83,11 +92,11 @@ def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch):
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window.busy)
assert 'lib.lmc' in confirmations[0]
assert 'Ready' in confirmations[0]
assert '非 READY' in confirmations[0]
assert window.icdb_progress.value() == 1
assert window.icdb_progress.maximum() == 1
assert '恢复备份待复核 1' in window.status_label.text()
assert window.summary.windowTitle() == '全库 ICDB 检查与修复结果'
assert window.summary.windowTitle() == '非 READY ICDB 检查与修复结果'
reports = list((tmp_path/'reports').glob('icdb-*.json'))
assert len(reports) == 1
@@ -102,7 +111,7 @@ def test_ready_row_missing_project_is_skipped_by_global_maintenance(qtbot, tmp_p
assert window.skipped_names == {'MISSING'}
assert 'MISSING' not in window.ready_names
assert not window.icdb_repair_button.isEnabled()
assert window.cbb_table.item(0, 2).text().startswith('跳过')
assert '缺少工程' in window.cbb_table.item(0, 2).text()
def test_verify_requires_loaded_library_and_invalidates_on_path_change(qtbot):
window=MainWindow(reader=fake_reader, verifier=lambda _: VerifyResult('PASS')); qtbot.addWidget(window)
@@ -156,8 +165,8 @@ def test_import_incomplete_rows_are_skipped_and_disabled(qtbot, tmp_path):
window.lmc_edit.setText(str(lmc))
window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
assert window.cbb_table.item(0, 2).text().startswith('跳过:')
assert not (window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable)
assert '缺少工程' in window.cbb_table.item(0, 2).text()
assert window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['VALID_CBB']
@@ -218,7 +227,7 @@ def test_large_library_load_does_not_update_controls_for_every_table_cell(qtbot,
assert window._row_by_name['CBB_0299'] == 299
assert len(calls) < 20
def test_ready_rows_show_status_and_cannot_be_selected_or_verified(qtbot, tmp_path):
def test_ready_rows_allow_management_selection_but_cannot_be_verified(qtbot, tmp_path):
def reader(_):
return LibraryReadResult((CbbRecord('READY_CBB', 'LP', status='READY'), CbbRecord('PENDING_CBB', 'LP', status='PENDING')))
calls = []
@@ -229,15 +238,16 @@ def test_ready_rows_show_status_and_cannot_be_selected_or_verified(qtbot, tmp_pa
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
assert window.cbb_table.columnCount() == 4
assert window.cbb_table.item(0, 2).text() == '绿色 / Ready'
assert not (window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable)
assert window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable
assert window.cbb_table.item(0, 0).background().color().name().lower() == '#e8f5e9'
window.cbb_table.item(0, 0).setCheckState(Qt.Checked)
assert 'READY_CBB' not in window._selected_names()
assert 'READY_CBB' in window._selected_names()
assert not window.selected_batch_button.isEnabled()
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['PENDING_CBB']
assert window.cbb_table.item(1, 0).checkState() == Qt.Unchecked
assert not (window.cbb_table.item(1, 0).flags() & Qt.ItemIsUserCheckable)
assert window.cbb_table.item(1, 0).flags() & Qt.ItemIsUserCheckable
assert window.cbb_table.item(1, 0).background().color().name().lower() == '#e8f5e9'
+290
View File
@@ -0,0 +1,290 @@
"""Local catalog scope and authoritative status refresh regression tests."""
import json
import time
from threading import Event
from pathlib import Path
from types import SimpleNamespace
import pytest
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
MIXED = (
CbbRecord('Alpha', 'Power', status='READY'),
CbbRecord('Beta', 'Power', status='PENDING'),
CbbRecord('Gamma', 'Analog', status='UNKNOWN'),
CbbRecord('Delta', 'Analog', status='ERROR'),
CbbRecord('Missing', 'Power', status='READY'),
)
def load_window(qtbot, tmp_path, monkeypatch, records=MIXED, **kwargs):
import cbb_refresh_verify.ui.main_window as ui
monkeypatch.setattr(ui, 'preflight_skip_reason',
lambda _, name: 'missing project' if name == 'Missing' else None)
lease = SimpleNamespace(mentor_path=Path('test.lmc'), close=lambda: None,
retain_for_recovery=lambda: None)
window = MainWindow(reader=lambda _: LibraryReadResult(records), data_root=tmp_path,
path_lease_factory=lambda _: lease, **kwargs)
qtbot.addWidget(window)
window.lmc_edit.setText('test.lmc')
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
return window
def names(records):
return [record.name for record in records]
def test_search_and_filters_use_cached_catalog_and_count_visible_states(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.reader = lambda _: pytest.fail('filter reread Mentor')
assert window.visible_counts() == {'visible': 5, 'ready': 2, 'error': 3}
expected = {
'ready': ['Alpha', 'Missing'], 'non_ready': ['Beta', 'Gamma', 'Delta'],
'pending': ['Beta'], 'errors': ['Gamma', 'Delta'], 'missing': ['Missing'],
}
for mode, wanted in expected.items():
window.status_filter.setCurrentIndex(window.status_filter.findData(mode))
assert names(window.visible_records()) == wanted
window.status_filter.setCurrentIndex(window.status_filter.findData('all'))
window.search_edit.setText(' ANALOG ')
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_counts() == {'visible': 2, 'ready': 0, 'error': 2}
window.search_edit.setText('aLpH')
assert names(window.visible_records()) == ['Alpha']
assert '1' in window.counts_label.text()
def test_management_selection_includes_ready_and_missing_but_hiding_clears_checks(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.select_visible_button.click()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Gamma', 'Delta', 'Missing']
assert window.selected_records()[0].logical_type == 'LP'
window.search_edit.setText('power')
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.search_edit.clear()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.clear_selection_button.click()
assert window.selected_records() == ()
def test_verify_all_and_selected_only_target_visible_non_ready_with_projects(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda names, _: {name: 'READY' for name in names})
window.search_edit.setText('power')
window.select_visible_button.click()
window.verify_selected()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta']
window.search_edit.setText('analog')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta', 'Gamma', 'Delta']
def test_maintenance_confirmation_progress_and_report_use_visible_checked_subset(qtbot, tmp_path, monkeypatch):
calls, confirmations, refreshes = [], [], []
def refresh(targets, _):
refreshes.append(tuple(targets))
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=refresh)
window.search_edit.setText('analog')
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
monkeypatch.setattr(QMessageBox, 'question', lambda *args, **_: (
confirmations.append(args[2]) or QMessageBox.Yes))
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Delta']
assert refreshes == [('Delta',)]
assert '1' in confirmations[0] and '非 READY' in confirmations[0]
assert window.icdb_progress.maximum() == 1 and window.icdb_progress.value() == 1
report = json.loads(next((tmp_path/'reports').glob('icdb-*.json')).read_text(encoding='utf-8'))
assert report['includes_ready'] is False
assert report['target_names'] == ['Delta']
assert report['preflight_skipped'] == []
assert report['counts']['total'] == 1
assert window.search_edit.text() == 'analog'
assert window.cbb_table.item(3, 3).text() == 'PASS'
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_records()[1].status == 'PENDING'
def test_maintenance_defaults_to_visible_eligible_and_ready_only_checks_do_not_fallback(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda targets, _: {name: 'PENDING' for name in targets})
monkeypatch.setattr(QMessageBox, 'question', lambda *_, **__: QMessageBox.Yes)
window.cbb_table.item(0, 0).setCheckState(Qt.Checked)
window.repair_icdb_all()
assert not window.busy and calls == []
window.clear_selection_button.click()
window.search_edit.setText('power')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta']
def test_recovery_defaults_to_visible_checked_non_ready_and_skips_ready(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.search_edit.setText('power')
window.restore_backup()
assert [window.backup_dialog.block_combo.itemText(i)
for i in range(window.backup_dialog.block_combo.count())] == ['Beta']
window.backup_dialog.reject()
window.search_edit.clear()
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
window.restore_backup()
assert window.backup_dialog.block_combo.currentText() == 'Delta'
assert window.backup_dialog.block_combo.count() == 1
window.backup_dialog.reject()
def test_action_pass_does_not_promote_pending_without_authoritative_ready(qtbot, tmp_path, monkeypatch):
entered = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: (entered.extend(targets) or {'Beta': 'READY'}))
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.cbb_table.item(1, 0).setCheckState(Qt.Checked)
window._progress({'block': 'Beta', 'stage': 'PASS'})
assert names(window.visible_records()) == ['Beta']
assert window.visible_records()[0].status == 'PENDING'
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
assert entered == ['Beta']
assert window.visible_records() == ()
assert window.selected_records() == ()
assert window.status_filter.currentData() == 'pending'
window.status_filter.setCurrentIndex(window.status_filter.findData('ready'))
assert names(window.visible_records()) == ['Alpha', 'Beta', 'Missing']
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_refresh_failure_demotes_affected_without_losing_other_statuses(qtbot, tmp_path, monkeypatch):
def fail(*_):
raise OSError('bridge unavailable')
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=fail)
window.refresh_affected_records(['Alpha'])
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.visible_records()[1].status == 'PENDING'
assert 'Alpha' not in window.ready_names
assert 'bridge unavailable' in window.output.toPlainText()
def test_unsafe_result_defers_native_refresh_and_retains_action_evidence(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', stderr='owner release unconfirmed',
safe_to_continue=False, evidence_path=tmp_path/'evidence.json'),
status_refresher=lambda *_: pytest.fail('unsafe owner was reopened'))
window.search_edit.setText('beta')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.cbb_table.item(1, 3).text() == 'FAIL: owner release unconfirmed'
assert str(tmp_path/'evidence.json') in window.output.toPlainText()
assert '暂缓原生状态复核' in window.cbb_table.item(1, 2).toolTip()
def test_refresh_callback_uses_only_known_affected_records_and_preserves_logical_type(qtbot, tmp_path, monkeypatch):
observed, callbacks = [], []
def refresh(targets, path):
observed.append((targets, path))
return {'X': 'READY'}
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('X', 'Display label', logical_type='RULP', status='PENDING'),),
status_refresher=refresh)
assert window.refresh_affected_records(['unknown']) is False
assert window.refresh_affected_records(['X', 'X', 'unknown'], callbacks.append) is True
assert window.refresh_affected_records(['X']) is False
qtbot.waitUntil(lambda: not window.busy)
assert observed == [(('X',), Path('test.lmc'))]
assert callbacks == [(CbbRecord('X', 'Display label', logical_type='RULP', status='READY'),)]
def test_backup_completion_refreshes_status_without_dialog_close_unlocking_pending_refresh(qtbot, tmp_path, monkeypatch):
entered, release = Event(), Event()
def refresh(targets, _):
entered.set()
release.wait(3)
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=refresh)
window.search_edit.setText('beta')
window.restore_backup()
window.backup_dialog.started.emit('Beta')
window.backup_dialog.completed.emit('Beta', VerifyResult('PASS'))
try:
qtbot.waitUntil(entered.is_set)
window.backup_dialog.reject()
assert window.busy
finally:
release.set()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'PENDING'
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_missing_project_keeps_explicit_authoritative_status(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
assert 'Ready' in window.cbb_table.item(4, 2).text()
assert '缺少工程' in window.cbb_table.item(4, 2).text()
def test_open_backup_dialog_cannot_restore_target_that_became_ready(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: {name: 'READY' for name in targets})
window.search_edit.setText('beta')
window.restore_backup()
dialog = window.backup_dialog
source = tmp_path/'Beta.prj'
source.touch()
dialog.source_edit.setText(str(source))
dialog.recoverer = lambda *_, **__: (calls.append('restore') or VerifyResult('PASS'))
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
dialog.restore_button.click()
qtbot.waitUntil(lambda: not dialog.busy)
assert calls == []
assert '非 READY' in dialog.status_label.text()
dialog.reject()
@pytest.mark.parametrize('status', ['UNKNOWN', 'UNAVAILABLE', 'ERROR', 'TIMEOUT', 'MALFORMED', 'CRASHED',
'UNRECOGNIZED'])
def test_unconfirmed_statuses_are_non_ready_and_error_filter_candidates(qtbot, tmp_path, monkeypatch, status):
window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('X', 'LP', status=status),))
window.status_filter.setCurrentIndex(window.status_filter.findData('non_ready'))
assert names(window.visible_records()) == ['X']
window.status_filter.setCurrentIndex(window.status_filter.findData('errors'))
assert names(window.visible_records()) == ['X']
@pytest.mark.parametrize('size', [381, 3000])
def test_large_catalog_local_filter_and_selection_remain_responsive(qtbot, tmp_path, monkeypatch, size):
records = tuple(CbbRecord(f'CBB{i:04}', 'Power' if i % 2 else 'Analog',
status='READY' if i % 3 == 0 else 'PENDING') for i in range(size))
start = time.perf_counter()
window = load_window(qtbot, tmp_path, monkeypatch, records=records)
loaded = time.perf_counter() - start
window.reader = lambda _: pytest.fail('local actions reread Mentor')
start = time.perf_counter()
window.search_edit.setText('power')
window.select_visible_button.click()
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.clear_selection_button.click()
elapsed = time.perf_counter() - start
print(f'catalog rows={size} load={loaded:.3f}s local_actions={elapsed:.3f}s')
assert elapsed < 2.0
assert window.selected_records() == ()