Files
cbb-refresh-verify-795/tests/test_pcb_restore_prompt.py

133 lines
7.3 KiB
Python

from dataclasses import replace
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
from test_pcb_version_choice import captured
from test_post_repair_consent import context, no_native_launch
@pytest.fixture
def session(context, monkeypatch):
from cbb_refresh_verify import post_repair_consent as consent
from cbb_refresh_verify.pcb_restore_prompt import RestoreController
_, _, proof, job = context
consent.prepare_launch(job, ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit'])
monkeypatch.setattr(consent, '_validate_current_processes', lambda *a: None)
snapshot = captured()
controller = RestoreController(job)
prompts, calls = [], []
def perform(snap, payload, phase):
calls.append(payload)
return SimpleNamespace(accepted_controls=(payload['target']['control_id'],),
channel='win32_button_click', error_code='', detail='')
def poll(snap=snapshot, snapshots=None):
return controller.handle(snap, snapshots or (snap,), target_pid=snapshot.pid,
launcher_pid=10, worker_pid=__import__('os').getpid(), companion_pids=(),
show=prompts.append, perform=perform)
return controller, snapshot, prompts, calls, poll, job
def test_waits_for_user_once_and_rechecks_radio_before_confirm(session):
controller, snapshot, prompts, calls, poll, job = session
assert not poll().error_code
poll()
assert len(prompts) == 1 and not calls
prompts[0].choose('last_user_saved')
assert poll().action_sent and calls[-1]['target']['control_id'] == 6213
assert not poll().action_sent and len(calls) == 1
selected = replace(snapshot, controls=tuple(replace(c, checked=c.control_id == 6213,
selected=c.control_id == 6213) if c.control_id in (6213, 6206) else c for c in snapshot.controls))
assert poll(selected).action_sent and calls[-1]['target']['control_id'] == 1
assert not poll(selected).action_sent and len(calls) == 2
result = json.loads((Path(job['root'])/'pcb-version-restoration.json').read_text(encoding='utf-8'))
assert result['choice'] == 'last_user_saved' and result['date'] == '2026/9/24 23:06'
assert result['historical_restore'] and result['confirm_sent']
def test_cancel_never_selects_or_confirms_a_version(session):
_, _, prompts, calls, poll, _ = session
poll(); prompts[0].choose('cancel')
result = poll()
assert result.error_code == 'PCB_VERSION_CHOICE_REQUIRED'
assert '取消' in result.detail and not calls
@pytest.mark.parametrize('change', ['date', 'pid', 'window', 'extra'])
def test_changed_native_choice_invalidates_user_answer(session, change):
_, snapshot, prompts, calls, poll, _ = session
poll(); prompts[0].choose('last_user_saved')
changed = snapshot
if change == 'date':
changed = replace(snapshot, controls=tuple(replace(c, name=c.name.replace('23:06', '23:07'))
if c.control_id == 6213 else c for c in snapshot.controls))
if change == 'pid': changed = replace(snapshot, pid=snapshot.pid + 1)
if change == 'window': changed = replace(snapshot, hwnd=snapshot.hwnd + 1)
result = poll(changed, (changed, changed) if change == 'extra' else None)
assert result.error_code and not calls
def test_native_action_failure_never_retries_or_confirms(session):
controller, snapshot, prompts, calls, poll, _ = session
poll(); prompts[0].choose('last_user_saved')
def failed(*a):
return SimpleNamespace(accepted_controls=(), channel='', error_code='ACTION_CHANNEL_FAILED', detail='timeout')
result = controller.handle(snapshot, (snapshot,), target_pid=snapshot.pid, launcher_pid=10,
worker_pid=__import__('os').getpid(), companion_pids=(), show=prompts.append, perform=failed)
assert result.error_code == 'ACTION_CHANNEL_FAILED'
assert poll().error_code and not calls
def test_version_dialog_shows_dates_and_explicit_choices(qtbot):
from cbb_refresh_verify.pcb_restore_prompt import RestoreChoice
from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog
from cbb_refresh_verify.dialog_rules import pcb_version_choice
choice = RestoreChoice('Block', 'library.lmc', pcb_version_choice(captured())['candidates'])
dialog = PcbRestoreDialog(choice)
qtbot.addWidget(dialog)
assert '2026/9/24 23:06' in dialog.text()
assert choice.decision is None
dialog.manual_button.click()
assert choice.decision == 'last_user_saved'
def test_closing_version_dialog_is_cancel_not_a_default_selection(qtbot):
from cbb_refresh_verify.pcb_restore_prompt import RestoreChoice
from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog
from cbb_refresh_verify.dialog_rules import pcb_version_choice
choice = RestoreChoice('Block', 'library.lmc', pcb_version_choice(captured())['candidates'])
dialog = PcbRestoreDialog(choice)
qtbot.addWidget(dialog)
dialog.reject()
assert choice.decision == 'cancel'
@pytest.mark.parametrize('change', ['', 'desktop', 'pid', 'date', 'disabled', 'selection', 'channel'])
def test_private_agent_rechecks_live_version_before_confirmation(monkeypatch, change):
import win32gui, win32process
from cbb_refresh_verify.dialog_agent import DialogActionExecutor
from cbb_refresh_verify.dialog_rules import pcb_version_choice
snapshot = replace(captured(), controls=tuple(replace(c, checked=c.control_id == 6213,
selected=c.control_id == 6213) if c.control_id in (6213, 6206) else c for c in captured().controls))
controls = {c.handle: c for c in snapshot.controls if c.handle and c.class_name in ('Static', 'Button')}
monkeypatch.setattr('cbb_refresh_verify.native_repair_ui._current_desktop_name',
lambda: 'Default' if change == 'desktop' else 'CbbVerify_fixture')
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda h: (1, snapshot.pid + (change == 'pid')))
monkeypatch.setattr(win32gui, 'GetClassName', lambda h: snapshot.window_class if h == snapshot.hwnd else controls[h].class_name)
monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: snapshot.title if h == snapshot.hwnd else
controls[h].name.replace('23:06', '23:07') if change == 'date' else controls[h].name)
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda h: True)
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda h: not (change == 'disabled' and controls[h].control_id == 6213))
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: controls[h].control_id)
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, callback, arg: [callback(c, arg) for c in controls])
monkeypatch.setattr(win32gui, 'SendMessageTimeout', lambda h, *a: int(controls[h].control_id == (6206 if change == 'selection' else 6213)))
calls = []
monkeypatch.setattr(win32gui, 'PostMessage', lambda *a: calls.append(a))
action = {'action': 'user_selected_pcb_version', 'channels': ['uia_invoke' if change == 'channel' else 'win32_button_click'],
'target': {'control_id': 1}, 'pid': snapshot.pid, 'desktop': 'CbbVerify_fixture',
'choice': 'last_user_saved', 'candidates': pcb_version_choice(snapshot)['candidates']}
result = DialogActionExecutor().perform(snapshot, action)
assert bool(result.accepted_controls) is (change == '')
assert len(calls) == (1 if change == '' else 0)