169 lines
7.9 KiB
Python
169 lines
7.9 KiB
Python
import pytest
|
|
from PySide6.QtCore import QTimer
|
|
from PySide6.QtWidgets import QMessageBox
|
|
|
|
|
|
def restore_choice(*, manual=True):
|
|
from cbb_refresh_verify.pcb_restore_prompt import RestoreChoice
|
|
return RestoreChoice('Block', 'library.lmc', [
|
|
dict(kind='last_user_saved', date='2026/9/24 23:06', enabled=manual),
|
|
dict(kind='autosave', date='2026/9/24 23:06', enabled=True)])
|
|
|
|
|
|
@pytest.mark.parametrize('kind,decision', [('open', 'skip'), ('owner', 'skip'),
|
|
('version', 'last_user_saved')])
|
|
def test_native_choice_finishes_in_three_seconds_without_an_operator(qtbot, kind, decision):
|
|
from cbb_refresh_verify.pcb_open_consent import OpenChoice
|
|
from cbb_refresh_verify.ui.pcb_open_dialog import PcbOpenDialog
|
|
from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog
|
|
from cbb_refresh_verify.ui.write_owner_dialog import WriteOwnerDialog
|
|
from cbb_refresh_verify.write_admission import WriteOwnerChoice
|
|
if kind == 'open':
|
|
choice = OpenChoice('Block', 'library.lmc')
|
|
dialog = PcbOpenDialog(choice)
|
|
elif kind == 'owner':
|
|
choice = WriteOwnerChoice('Block', 'library.lmc', [
|
|
dict(pid=12, name='viewdraw.exe', image='viewdraw.exe')])
|
|
dialog = WriteOwnerDialog(choice)
|
|
else:
|
|
choice = restore_choice()
|
|
dialog = PcbRestoreDialog(choice)
|
|
qtbot.addWidget(dialog)
|
|
dialog.show()
|
|
qtbot.wait(3200)
|
|
assert choice.decision == decision
|
|
assert choice.decision_origin == 'timeout_default'
|
|
assert not dialog.isVisible()
|
|
|
|
|
|
def test_manual_selection_cancels_the_pending_default(qtbot, monkeypatch):
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog
|
|
monkeypatch.setattr(timed_prompt, 'DEFAULT_TIMEOUT_MS', 100)
|
|
choice = restore_choice()
|
|
dialog = PcbRestoreDialog(choice)
|
|
qtbot.addWidget(dialog)
|
|
dialog.show()
|
|
dialog.auto_button.click()
|
|
qtbot.wait(200)
|
|
assert choice.decision == 'autosave'
|
|
assert choice.decision_origin == 'user'
|
|
|
|
|
|
def test_unavailable_preferred_version_never_clicks_disabled_or_other_version(qtbot, monkeypatch):
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog
|
|
monkeypatch.setattr(timed_prompt, 'DEFAULT_TIMEOUT_MS', 100)
|
|
choice = restore_choice(manual=False)
|
|
dialog = PcbRestoreDialog(choice)
|
|
qtbot.addWidget(dialog)
|
|
dialog.show()
|
|
qtbot.waitUntil(lambda: choice.decision is not None)
|
|
assert choice.decision == 'cancel'
|
|
assert choice.decision_origin == 'timeout_default'
|
|
|
|
|
|
@pytest.mark.parametrize('default', [QMessageBox.Yes, QMessageBox.Cancel])
|
|
def test_question_runs_its_explicit_default_without_mouse_input(qtbot, monkeypatch, default):
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
monkeypatch.setattr(timed_prompt, 'DEFAULT_TIMEOUT_MS', 100)
|
|
result = timed_prompt.question(None, 'Timed fixture', 'Choose',
|
|
QMessageBox.Yes | QMessageBox.Cancel, default)
|
|
assert result == default
|
|
|
|
|
|
def test_manual_question_answer_wins_before_timeout(qtbot, monkeypatch):
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
monkeypatch.setattr(timed_prompt, 'DEFAULT_TIMEOUT_MS', 200)
|
|
def answer():
|
|
from PySide6.QtWidgets import QApplication
|
|
for box in QApplication.topLevelWidgets():
|
|
if isinstance(box, QMessageBox) and box.windowTitle() == 'Manual fixture':
|
|
box.button(QMessageBox.No).click()
|
|
QTimer.singleShot(50, answer)
|
|
assert timed_prompt.question(None, 'Manual fixture', 'Choose',
|
|
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) == QMessageBox.No
|
|
qtbot.wait(250)
|
|
|
|
|
|
def test_hidden_or_closed_notifications_never_execute_later(qtbot, monkeypatch):
|
|
from cbb_refresh_verify.ui import timed_prompt
|
|
monkeypatch.setattr(timed_prompt, 'DEFAULT_TIMEOUT_MS', 100)
|
|
box = QMessageBox()
|
|
qtbot.addWidget(box)
|
|
box.setStandardButtons(QMessageBox.Close)
|
|
clicks = []
|
|
box.buttonClicked.connect(lambda _: clicks.append(True))
|
|
timed_prompt.arm_default(box, box.button(QMessageBox.Close))
|
|
box.show()
|
|
box.hide()
|
|
qtbot.wait(200)
|
|
assert not clicks
|
|
box.show()
|
|
qtbot.waitUntil(lambda: bool(clicks))
|
|
qtbot.wait(200)
|
|
assert clicks == [True]
|
|
|
|
|
|
def test_timeout_skip_does_not_grant_unknown_clients_or_stop_the_batch(tmp_path, monkeypatch):
|
|
from threading import Event
|
|
from cbb_refresh_verify.write_admission import WriteAdmission, WriteAdmissionDeclined
|
|
monkeypatch.setattr('cbb_refresh_verify.write_admission.process_identity', lambda pid: dict(
|
|
pid=pid, created_handle='one', image='viewdraw.exe', owner_sid='user', session_id=1))
|
|
owners = [dict(pid=12, name='viewdraw.exe', executable='viewdraw.exe')]
|
|
stop = Event()
|
|
admission = WriteAdmission(stop=stop, prompt=lambda c: c.choose('skip', origin='timeout_default'))
|
|
with pytest.raises(WriteAdmissionDeclined, match='超时默认'):
|
|
admission.confirm(tmp_path/'library.lmc', owners, evidence=tmp_path/'evidence', block='Block')
|
|
assert not stop.is_set()
|
|
assert not admission.confirmed(tmp_path/'library.lmc', owners)
|
|
|
|
|
|
def test_recorded_pcb_open_choice_distinguishes_timeout_from_user(tmp_path, monkeypatch):
|
|
from contextlib import nullcontext
|
|
from types import SimpleNamespace
|
|
import json
|
|
from test_post_repair_consent import snapshot
|
|
from cbb_refresh_verify import pcb_open_consent as consent
|
|
monkeypatch.setattr(consent, 'validate_owner', lambda *_: {'target': 'owned'})
|
|
monkeypatch.setattr(consent, 'resource_guard', lambda *_: nullcontext())
|
|
controller = consent.OpenController(dict(root=str(tmp_path), block_name='Block',
|
|
lmc_file='library.lmc', project='Block.prj', pcb='Block.pcb', desktop='CbbVerify_test'))
|
|
shown = []
|
|
options = dict(target_pid=20, launcher_pid=10, worker_pid=5, show=shown.append,
|
|
perform=lambda *_: SimpleNamespace(error_code='', accepted_controls=(7,), detail='', channel='win32_button_click'))
|
|
controller.handle(snapshot(), (snapshot(),), **options)
|
|
shown[0].choose('skip', origin='timeout_default')
|
|
result = controller.handle(snapshot(), (snapshot(),), **options)
|
|
assert result.error_code == 'PCB_OPEN_SKIPPED'
|
|
record = json.loads((tmp_path/'pcb-open-user-choice.json').read_text('utf-8'))
|
|
assert record['decision_origin'] == 'timeout_default'
|
|
|
|
|
|
@pytest.mark.parametrize('decision', ['last_user_saved', 'cancel'])
|
|
def test_native_version_action_retains_automatic_choice_origin(tmp_path, monkeypatch, decision):
|
|
import json
|
|
from types import SimpleNamespace
|
|
from test_pcb_version_choice import captured
|
|
from cbb_refresh_verify.pcb_restore_prompt import RestoreController
|
|
monkeypatch.setattr('cbb_refresh_verify.pcb_open_consent.validate_owner', lambda *_: {'target': 'owned'})
|
|
controller = RestoreController(dict(root=str(tmp_path), block_name='Block', lmc_file='library.lmc',
|
|
project='Block.prj', pcb='Block.pcb', desktop='CbbVerify_test'), user_open=True)
|
|
value = captured()
|
|
shown, actions = [], []
|
|
def perform(snapshot, action, phase):
|
|
actions.append(action)
|
|
return SimpleNamespace(error_code='', accepted_controls=(6213,), detail='', channel='win32_button_click')
|
|
options = dict(target_pid=value.pid, launcher_pid=10, worker_pid=5, companion_pids=(),
|
|
show=shown.append, perform=perform)
|
|
controller.handle(value, (value,), **options)
|
|
shown[0].choose(decision, origin='timeout_default')
|
|
result = controller.handle(value, (value,), **options)
|
|
record = json.loads((tmp_path/'pcb-version-user-choice.json').read_text('utf-8'))
|
|
assert record['choice'] == decision and record['decision_origin'] == 'timeout_default'
|
|
if decision == 'cancel':
|
|
assert not actions and result.error_code == 'PCB_VERSION_CHOICE_REQUIRED'
|
|
else:
|
|
assert actions[0]['decision_origin'] == 'timeout_default'
|
|
assert actions[0]['target']['control_id'] == 6213
|