Files
cbb-refresh-verify-795/tests/test_ui.py
T

367 lines
16 KiB
Python

from cbb_refresh_verify.ui import timed_prompt
import os
import pytest
from pathlib import Path
os.environ.setdefault('QT_QPA_PLATFORM','offscreen')
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
from threading import Event
from PySide6.QtCore import Qt
from PySide6.QtGui import QPalette
def test_application_palette_keeps_table_text_readable(qtbot):
from PySide6.QtWidgets import QApplication
from cbb_refresh_verify.application import configure_qt_application
app = QApplication.instance()
configure_qt_application(app)
text = app.palette().color(QPalette.ColorRole.Text)
base = app.palette().color(QPalette.ColorRole.Base)
assert text.value() < 100
assert base.value() > 180
def fake_reader(_): return LibraryReadResult((CbbRecord('CBB1','Reusable Blocks'),))
@pytest.fixture(autouse=True)
def isolate_authoritative_native_refresh(monkeypatch):
from PySide6.QtWidgets import QMessageBox
monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes)
# 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((
CbbRecord('READY', 'LP', status='READY'), CbbRecord('PENDING', 'LP'))),
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
data_root=tmp_path)
qtbot.addWidget(window)
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.No)
window.icdb_repair_button.click()
assert calls == []
monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window._icdb_active())
assert calls == ['PENDING']
def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot):
window = MainWindow(reader=fake_reader)
qtbot.addWidget(window)
assert not window.icdb_repair_button.isEnabled()
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window.icdb_repair_button.isEnabled()
window.busy = True; window._update()
assert not window.icdb_repair_button.isEnabled()
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('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)
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
monkeypatch.setattr(timed_prompt, 'question', lambda *a, **k: QMessageBox.Yes)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window._icdb_active())
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() == '未检查'
def test_global_icdb_progress_and_check_summary(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
confirmations = []
def confirm(*args, **kwargs):
confirmations.append(args[2])
return QMessageBox.Yes
monkeypatch.setattr(timed_prompt, 'question', confirm)
window = MainWindow(reader=fake_reader, data_root=tmp_path,
verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False, recovery_attempts=(
{'status': 'RESTORED_REVIEW_REQUIRED', 'detail': 'restored old backup'},)))
qtbot.addWidget(window)
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window._icdb_active())
assert 'lib.lmc' 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 '恢复备份待复核' not in window.status_label.text()
assert window.summary.windowTitle() == 'ICDB 批量检查结果'
reports = list((tmp_path/'reports').glob('icdb-*.json'))
assert len(reports) == 1
def test_ready_row_missing_project_is_skipped_by_global_maintenance(qtbot, tmp_path):
library = tmp_path/'library.lmc'
library.touch()
window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('MISSING', 'LP', status='READY'),)),
data_root=tmp_path)
qtbot.addWidget(window)
window.lmc_edit.setText(str(library)); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window.skipped_names == {'MISSING'}
assert 'MISSING' not in window.ready_names
assert not window.icdb_repair_button.isEnabled()
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)
assert not window.batch_button.isEnabled()
window.lmc_edit.setText('lib.lmc'); window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
# LibSpec / DMS config are optional: they only add the contents check.
assert hasattr(window, 'libspec_edit') and hasattr(window, 'dms_edit')
window.lmc_edit.setText('different.lmc')
assert not window.batch_button.isEnabled()
assert window.cbb_table.rowCount() == 1
assert '失效' in window.library_notice.text()
def test_ui_removes_current_cbb_controls_and_hides_advanced_settings(qtbot):
window = MainWindow(reader=fake_reader, verifier=lambda _: VerifyResult('PASS'))
qtbot.addWidget(window)
assert not hasattr(window, 'cbb_combo')
assert not hasattr(window, 'verify_button')
assert window.advanced_panel.isHidden()
window.advanced_toggle.click()
assert not window.advanced_panel.isHidden()
def test_window_title_and_action_buttons_are_polished(qtbot):
window = MainWindow(reader=fake_reader, verifier=lambda _: VerifyResult('PASS'))
qtbot.addWidget(window)
assert '后台串行' not in window.windowTitle()
assert not window.windowIcon().isNull()
for button in (window.load_button, window.browse_button, window.selected_batch_button,
window.batch_button, window.stop_button, window.logs_button):
assert not button.icon().isNull()
def test_import_incomplete_rows_are_skipped_and_disabled(qtbot, tmp_path):
lmc = tmp_path / 'center.lmc'
lmc.touch()
broken = tmp_path / 'ReusableBlocksLP' / 'BROKEN_CBB' / 'Work'
broken.mkdir(parents=True)
valid = tmp_path / 'ReusableBlocksLP' / 'VALID_CBB' / 'Work'
(valid / 'PCB' / 'Layout').mkdir(parents=True)
(valid / 'VALID_CBB.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"', encoding='utf-8')
(valid / 'PCB' / 'actual.pcb').write_text('pcb', encoding='utf-8')
(valid / 'PCB' / 'Layout' / 'LayoutDB.lyt').write_bytes(b'lyt')
calls = []
def reader(_):
return LibraryReadResult((CbbRecord('BROKEN_CBB', 'LP'), CbbRecord('VALID_CBB', 'LP')))
window = MainWindow(reader=reader,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
data_root=tmp_path)
qtbot.addWidget(window)
window.lmc_edit.setText(str(lmc))
window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
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']
def test_batch_does_not_block_ui_and_stop_skips_next_item(qtbot,tmp_path):
entered, release = Event(), Event()
calls=[]
def verify(req):
calls.append(req.block_name); entered.set()
release.wait(3)
return VerifyResult('PASS')
def reader(_): return LibraryReadResult((CbbRecord('A','LP'),CbbRecord('B','LP')))
window=MainWindow(reader=reader,verifier=verify,data_root=tmp_path)
qtbot.addWidget(window); window.lmc_edit.setText('lib.lmc');window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
window.verify_all()
try:
qtbot.waitUntil(entered.is_set)
assert window.stop_button.isEnabled()
window.stop_button.click()
finally:
release.set()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['A']
assert list((tmp_path/'reports').glob('*.log'))
def test_loaded_library_shows_selectable_rows_and_selected_batch_action(qtbot, tmp_path):
def reader(_): return LibraryReadResult((CbbRecord('A', 'LP'), CbbRecord('B', 'LP')))
window = MainWindow(reader=reader, verifier=lambda _: VerifyResult('PASS'), data_root=tmp_path)
qtbot.addWidget(window)
window.lmc_edit.setText('lib.lmc')
window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
assert window.cbb_table.rowCount() == 2
assert not window.selected_batch_button.isEnabled()
window.cbb_table.item(0, 0).setCheckState(Qt.Checked)
window.selected_batch_button.click()
qtbot.waitUntil(lambda: not window.busy)
assert (tmp_path / 'reports').exists()
assert window.cbb_table.item(0, 2).text() == '绿色 / Ready'
assert window.cbb_table.item(0, 3).text() == 'PASS'
def test_large_library_load_does_not_update_controls_for_every_table_cell(qtbot, tmp_path):
records = tuple(CbbRecord(f'CBB_{index:04d}', 'LP') for index in range(300))
window = MainWindow(reader=lambda _: LibraryReadResult(records), data_root=tmp_path)
qtbot.addWidget(window)
calls = []
original_update = window._update
window._update = lambda: (calls.append(window.cbb_table.rowCount()), original_update())[1]
window.lmc_edit.setText(str(tmp_path / 'center.lmc'))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window.cbb_table.rowCount() == 300
assert window._row_by_name['CBB_0299'] == 299
assert len(calls) < 20
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 = []
window = MainWindow(reader=reader, verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')), data_root=tmp_path)
qtbot.addWidget(window)
window.lmc_edit.setText('lib.lmc')
window.load_library()
qtbot.waitUntil(lambda: window.batch_button.isEnabled())
assert window.cbb_table.columnCount() == 4
assert window.cbb_table.item(0, 2).text() == '绿色 / Ready'
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' 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 window.cbb_table.item(1, 0).flags() & Qt.ItemIsUserCheckable
assert window.cbb_table.item(1, 0).background().color().name().lower() == '#e8f5e9'
def test_unc_library_uses_mentor_path_for_read_and_verify_and_releases_on_close(qtbot, tmp_path):
calls = []
class FakeLease:
display_path = Path(r'\\server\share\mentor_lib.lmc')
mentor_path = Path(r'Y:\mentor_lib.lmc')
closed = False
def close(self):
self.closed = True
lease = FakeLease()
def lease_factory(path):
assert str(path) == str(lease.display_path)
return lease
def reader(path):
calls.append(path)
return LibraryReadResult((CbbRecord('CBB1', 'LP', status='PENDING'),))
window = MainWindow(reader=reader, data_root=tmp_path, path_lease_factory=lease_factory)
qtbot.addWidget(window)
window.lmc_edit.setText(str(lease.display_path))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert calls == [lease.mentor_path]
assert window._request('CBB1').lmc_file == lease.mentor_path
window.close()
qtbot.waitUntil(lambda: lease.closed)
def test_advanced_options_are_copied_to_verify_request(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
window.loaded_path = 'display.lmc'
window.mentor_lmc_path = Path(r'Y:\lib\mentor_lib.lmc')
window.libspec_edit.setText('EE7.9_CUSTOM')
window.dms_edit.setText(r'Y:\config\login.conf')
window.mentor_bin_edit.setText(r'D:\Mentor\bin')
window.timeout_spin.setValue(45)
request = window._request('CBB1')
assert request.lmc_file == Path(r'Y:\lib\mentor_lib.lmc')
assert request.libspec == 'EE7.9_CUSTOM'
assert request.dms_login_config == r'Y:\config\login.conf'
assert request.mentor_bin == r'D:\Mentor\bin'
assert request.timeout_seconds == 45
def test_ui_request_keeps_background_trigger_mode(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
window.loaded_path = 'display.lmc'
window.mentor_lmc_path = Path(r'Y:\lib\mentor_lib.lmc')
assert window._request('CBB1').interactive_input is False
def test_ui_request_exposes_native_lm_verify_compatibility_switch(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
window.loaded_path = 'display.lmc'
window.mentor_lmc_path = Path(r'Y:\lib\mentor_lib.lmc')
assert window._request('CBB1').native_lm_verify is False
window.native_lm_verify_check.setChecked(True)
assert window._request('CBB1').native_lm_verify is True
def test_library_directory_input_uses_its_only_lmc(qtbot, tmp_path):
lmc = tmp_path / 'mentor_lib.lmc'
lmc.touch()
calls = []
window = MainWindow(
reader=lambda path: (
calls.append(path) or LibraryReadResult((CbbRecord('CBB1', 'LP'),))
),
data_root=tmp_path / 'data',
)
qtbot.addWidget(window)
window.lmc_edit.setText(str(tmp_path))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert calls == [lmc]
assert window._request('CBB1').lmc_file == lmc
def test_ui_default_timeout_matches_request_and_labels_inactivity(qtbot):
from cbb_refresh_verify.verify_runner import VerifyRequest
window = MainWindow(reader=fake_reader)
qtbot.addWidget(window)
assert window.timeout_spin.value() == 600
assert window.timeout_spin.value() == VerifyRequest(Path('lib.lmc'), 'CBB1', Path('data')).timeout_seconds
assert '无进展' in window.timeout_spin.suffix()
def test_batch_summary_exposes_failures_only_report(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
report=tmp_path/'reports/batch-20260929-test.log'
window._batch_done(([VerifyResult('FAIL',stderr='Mentor error')],report))
assert '仅失败日志' in window.summary.detailedText()
assert str(report.with_name('failures-20260929-test.log')) in window.summary.detailedText()