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

476 lines
16 KiB
Python

from cbb_refresh_verify.dialog_supervisor import (
AgentDialogBackend,
DialogControl,
DialogObservationError,
DialogSnapshot,
DialogSupervisor,
Win32DialogBackend,
)
from cbb_refresh_verify.dialog_agent import ActionResult
from cbb_refresh_verify.dialog_models import (
ControlSnapshot as EvidenceControl,
DialogPhase,
WindowSnapshot,
)
from cbb_refresh_verify.dialog_rules import load_rules
from pathlib import Path
def dialog(text, *, title="Expedition PCB", hwnd=100, pid=200, buttons=("OK", "Cancel")):
controls = [DialogControl(1000, 0, "Static", text, True)]
controls.extend(
DialogControl(1100 + index, control_id, "Button", caption, True)
for index, (control_id, caption) in enumerate(zip((1, 2, 6, 7), buttons))
)
return DialogSnapshot(hwnd, pid, title, tuple(controls))
def license_dialog():
return dialog("Select the desired product license", title="", buttons=("OK", "Cancel"))
def save_error_dialog():
return dialog("Error saving the Reusable Block. See log file, ReusableBlock.txt.",
buttons=("OK",))
class FakeDialogs:
def __init__(self, observations):
self.observations = list(observations)
self.actions = []
def observe(self):
if not self.observations:
return ()
return tuple(self.observations.pop(0))
def perform(self, snapshot, action):
self.actions.append((snapshot.hwnd, action))
return (1,)
def test_click_is_not_resolved_until_dialog_disappears():
backend = FakeDialogs([[license_dialog()], [license_dialog()], []])
supervisor = DialogSupervisor(backend, retry_seconds=0, max_attempts=3)
first = supervisor.poll()
second = supervisor.poll()
third = supervisor.poll()
assert first.action_sent
assert not first.resolved
assert second.action_sent
assert not second.resolved
assert third.resolved
assert len(backend.actions) == 2
def test_stuck_dialog_fails_after_bounded_retries():
backend = FakeDialogs([[license_dialog()]] * 4)
supervisor = DialogSupervisor(backend, retry_seconds=0, max_attempts=2)
supervisor.poll()
supervisor.poll()
result = supervisor.poll()
assert result.error_code == "DIALOG_STUCK"
assert len(backend.actions) == 2
def test_unknown_dialog_is_recorded_but_not_clicked():
backend = FakeDialogs([[dialog("Unrecognized destructive prompt")]])
result = DialogSupervisor(backend).poll()
assert result.error_code == "UNKNOWN_DIALOG"
assert backend.actions == []
def test_save_error_is_fatal_even_with_ok_button():
backend = FakeDialogs([[save_error_dialog()]])
result = DialogSupervisor(backend).poll()
assert result.error_code == "SAVE_REUSABLE_BLOCK_FAILED"
assert backend.actions == [(100, "acknowledge_fatal")]
def test_changed_text_on_reused_handle_is_a_new_dialog_state():
backend = FakeDialogs([
[license_dialog()],
[dialog("Do you want to save the Reusable Block cell?", buttons=("Yes", "No"))],
])
supervisor = DialogSupervisor(backend, retry_seconds=60, max_attempts=3)
assert supervisor.poll().action_sent
result = supervisor.poll()
assert result.action_sent
assert backend.actions == [(100, "accept_license"), (100, "save_reusable_block")]
def test_progress_dialog_with_only_cancel_is_observed_not_clicked():
backend = FakeDialogs([[dialog("Your task is being processed. Elapsed time 10 seconds",
buttons=("Cancel",))]])
result = DialogSupervisor(backend).poll()
assert not result.action_sent
assert not result.error_code
assert backend.actions == []
def test_progress_dialog_text_change_counts_as_activity_without_clicking():
backend = FakeDialogs([
[dialog("Your task is being processed. Elapsed time 10 seconds", buttons=("Cancel",))],
[dialog("Your task is being processed. Elapsed time 11 seconds", buttons=("Cancel",))],
])
supervisor = DialogSupervisor(backend)
first = supervisor.poll()
second = supervisor.poll()
assert first.activity
assert second.activity
assert backend.actions == []
def test_win32_enumeration_callbacks_always_continue():
callback_results = []
class FakeGui:
def EnumWindows(self, callback, context):
callback_results.extend((callback(10, context), callback(20, context)))
def IsWindowVisible(self, hwnd):
return True
def GetClassName(self, hwnd):
return "#32770" if hwnd == 10 else "Other"
def EnumChildWindows(self, hwnd, callback, context):
callback_results.extend((callback(101, context), callback(102, context)))
def GetWindowText(self, hwnd):
return {10: "", 101: "Select the desired product license", 102: "OK"}.get(hwnd, "")
def GetDlgCtrlID(self, hwnd):
return 1 if hwnd == 102 else 0
def IsWindowEnabled(self, hwnd):
return True
def SendMessage(self, hwnd, message, wparam, lparam):
return 0
class FakeProcess:
@staticmethod
def GetWindowThreadProcessId(hwnd):
return 1, 200
snapshots = Win32DialogBackend(
{200}, win32gui=FakeGui(), win32process=FakeProcess(), win32con=object()
).observe()
assert callback_results == [True, True, True, True]
assert len(snapshots) == 1
assert snapshots[0].pid == 200
def test_database_restoration_selects_choice_before_confirming():
clicks = []
class Gui:
@staticmethod
def PostMessage(hwnd, *_args):
clicks.append(hwnd)
backend = Win32DialogBackend(
set(), win32gui=Gui(), win32process=object(), win32con=object()
)
choice = DialogControl(10, 6213, 'Button', 'Load the last user saved database', True)
ok = DialogControl(11, 1, 'Button', 'OK', True)
snapshot = DialogSnapshot(1, 2, 'Database Restoration', (choice, ok))
assert backend.perform(snapshot, 'restore_last_saved') == (6213,)
selected = DialogControl(10, 6213, 'Button', 'Load the last user saved database', True,
checked=True)
snapshot = DialogSnapshot(1, 2, 'Database Restoration', (selected, ok))
assert backend.perform(snapshot, 'restore_last_saved') == (1,)
assert clicks == [10, 11]
def test_license_waits_for_list_selection_before_clicking_ok():
clicks = []
class Gui:
@staticmethod
def PostMessage(hwnd, *_args):
clicks.append(hwnd)
backend = Win32DialogBackend(
set(), win32gui=Gui(), win32process=object(), win32con=object()
)
ok = DialogControl(11, 1, 'Button', 'OK', True)
unselected = DialogControl(12, 20, 'ComboBox', '', True, selected=False)
selected = DialogControl(12, 20, 'ComboBox', '', True, selected=True)
assert backend.perform(DialogSnapshot(1, 2, '', (unselected, ok)), 'accept_license') == ()
assert backend.perform(DialogSnapshot(1, 2, '', (selected, ok)), 'accept_license') == (1,)
assert clicks == [11]
def test_license_dialog_opens_product_selector_before_waiting_for_selection():
clicks = []
class Gui:
@staticmethod
def PostMessage(hwnd, *_args):
clicks.append(hwnd)
backend = Win32DialogBackend(
set(), win32gui=Gui(), win32process=object(), win32con=object()
)
selector = DialogControl(10, 5225, 'Button', 'Select the desired product license', True)
custom_list = DialogControl(11, 1522, 'GXWND', '', True)
ok = DialogControl(12, 1, 'Button', 'OK', True)
snapshot = DialogSnapshot(1, 2, '', (selector, custom_list, ok))
assert backend.perform(snapshot, 'accept_license') == (5225,)
assert clicks == [10]
def test_window_enumeration_failure_is_structured_not_silent():
class BrokenGui:
@staticmethod
def EnumWindows(_callback, _context):
raise RuntimeError('enum failed')
class Backend:
@staticmethod
def observe():
raise DialogObservationError('WINDOW_ENUM_ERROR', 'enum failed')
result = DialogSupervisor(Backend()).poll()
assert result.error_code == 'WINDOW_ENUM_ERROR'
backend = Win32DialogBackend(
set(), win32gui=BrokenGui(), win32process=object(), win32con=object()
)
result = DialogSupervisor(backend).poll()
assert result.error_code == 'WINDOW_ENUM_ERROR'
def test_private_desktop_without_windows_is_transient_not_fatal():
class EmptyDesktop:
@staticmethod
def EnumDesktopWindows():
import pywintypes
raise pywintypes.error(2, 'EnumDesktopWindows', 'file not found')
backend = Win32DialogBackend(
set(), desktop=EmptyDesktop(), win32gui=object(),
win32process=object(), win32con=object()
)
assert backend.observe() == ()
def test_private_desktop_access_failure_remains_structured():
class BrokenDesktop:
@staticmethod
def EnumDesktopWindows():
import pywintypes
raise pywintypes.error(5, 'EnumDesktopWindows', 'access denied')
backend = Win32DialogBackend(
set(), desktop=BrokenDesktop(), win32gui=object(),
win32process=object(), win32con=object()
)
result = DialogSupervisor(backend).poll()
assert result.error_code == 'WINDOW_ENUM_ERROR'
RULES = load_rules(
Path(__file__).resolve().parents[1] / 'src' / 'cbb_refresh_verify' / 'assets' /
'dialog_rules.v1.json'
)
def evidence_window(text, *, phase, window_class='#32770', selected=False):
return WindowSnapshot(
hwnd=500,
pid=600,
process_name='ExpeditionPCB.exe',
process_role='pcb',
window_class=window_class,
title='Expedition PCB',
phase=phase,
controls=(
EvidenceControl(501, 0, 'Static', text, control_type='Text'),
EvidenceControl(502, 1, 'Button', 'OK', control_type='Button',
enabled=True, selected=selected, patterns=('Invoke',)),
),
owner_hwnd=400,
)
class PhaseBackend:
def __init__(self, observations):
self.observations = list(observations)
self.actions = []
def observe(self, phase):
if not self.observations:
return ()
return tuple(self.observations.pop(0))
def perform(self, snapshot, action, phase):
self.actions.append((snapshot.hwnd, action, phase))
return ActionResult(channel='uia_invoke', accepted_controls=(1,))
def test_phase_aware_unknown_window_fails_without_action():
backend = PhaseBackend([[
evidence_window('Unrecognized destructive prompt', phase=DialogPhase.PCB_STARTING,
window_class='GXWND')
]])
result = DialogSupervisor(backend, rules=RULES).poll(DialogPhase.PCB_STARTING)
assert result.error_code == 'UNKNOWN_DIALOG'
assert backend.actions == []
def test_phase_aware_action_is_pending_until_window_disappears():
restore = evidence_window(
'Do you want to save the reusable block cell',
phase=DialogPhase.SAVE,
)
backend = PhaseBackend([[restore], []])
supervisor = DialogSupervisor(backend, rules=RULES, retry_seconds=60)
first = supervisor.poll(DialogPhase.SAVE)
second = supervisor.poll(DialogPhase.SAVE)
assert first.action_sent and not first.resolved
assert second.resolved
assert len(backend.actions) == 1
def test_phase_aware_changed_content_is_not_window_gone():
first = evidence_window(
'Do you want to save the reusable block cell',
phase=DialogPhase.SAVE,
)
changed = evidence_window(
'Do you want to save the reusable block cell - selected',
phase=DialogPhase.SAVE,
selected=True,
)
backend = PhaseBackend([[first], [changed]])
supervisor = DialogSupervisor(backend, rules=RULES, retry_seconds=60)
first_result = supervisor.poll(DialogPhase.SAVE)
second_result = supervisor.poll(DialogPhase.SAVE)
assert first_result.action_sent and not first_result.resolved
assert not second_result.resolved
def test_phase_aware_fatal_error_cannot_become_success():
fatal = evidence_window(
'There were errors during Project Integration',
phase=DialogPhase.PROJECT_INTEGRATION,
)
backend = PhaseBackend([[fatal]])
result = DialogSupervisor(backend, rules=RULES).poll(DialogPhase.PROJECT_INTEGRATION)
assert result.error_code == 'PROJECT_INTEGRATION_FAILED'
assert result.rule_id == 'integration-error'
assert backend.actions
def test_agent_backend_forwards_phase_fingerprint_and_action():
calls = []
class Client:
def observe(self, phase):
calls.append(('observe', phase))
return ()
def perform(self, **values):
calls.append(('perform', values))
return ActionResult(channel='uia_invoke', accepted_controls=(1,))
backend = AgentDialogBackend(Client())
window = evidence_window('Tip of the Day', phase=DialogPhase.PCB_STARTING)
backend.observe(DialogPhase.PCB_STARTING)
backend.perform(window, {'action': 'dismiss_tip'}, DialogPhase.PCB_STARTING)
assert calls[0] == ('observe', DialogPhase.PCB_STARTING)
assert calls[1][1]['hwnd'] == 500
assert calls[1][1]['fingerprint']
def native_database_open_error():
return DialogSnapshot(100, 200, 'Expedition PCB', (
DialogControl(1000, 0, 'Static',
'Problem encountered while opening one of the database files. '
'See PCBLogFile.txt for details. Loading of the job has failed. '
'Do you wish to view the log file now?', True),
DialogControl(1100, 6, 'Button', 'Yes', True),
DialogControl(1101, 7, 'Button', 'No', True),
))
def test_legacy_native_database_failure_is_classified_and_declines_log_viewer():
backend = FakeDialogs([[native_database_open_error()]])
result = DialogSupervisor(backend).poll()
assert result.error_code == 'MENTOR_DATABASE_LOAD_FAILED'
assert backend.actions == [(100, 'decline_database_log')]
def test_legacy_database_failure_clicks_idno_never_yes():
clicks = []
class Gui:
def PostMessage(self, handle, *args):
clicks.append(handle)
backend = Win32DialogBackend({200}, win32gui=Gui(), win32process=object(), win32con=object())
assert backend.perform(native_database_open_error(), 'decline_database_log') == (7,)
assert clicks == [1101]
only_yes = DialogSnapshot(100, 200, 'Expedition PCB', native_database_open_error().controls[:-1])
assert backend.perform(only_yes, 'decline_database_log') == ()
assert clicks == [1101]
def test_versioned_supervision_ignores_ime_and_still_handles_next_real_dialog():
phase = DialogPhase.PCB_STARTING
ime = WindowSnapshot(hwnd=800, pid=600, process_name='ExpeditionPCB.exe', process_role='pcb',
window_class='CiceroUIWndFrame', title='TF_FloatingLangBar_WndTitle', phase=phase,
owner_hwnd=400, controls=())
from dataclasses import replace
license_prompt = evidence_window('Select the desired product license', phase=phase)
license_prompt = replace(license_prompt, controls=(
replace(license_prompt.controls[0], class_name='GXWND'), license_prompt.controls[1]))
backend = PhaseBackend([[ime, license_prompt]])
result = DialogSupervisor(backend, rules=RULES).poll(phase)
assert not result.error_code
assert result.action_sent
assert len(backend.actions) == 1 and backend.actions[0][0] == license_prompt.hwnd
def test_versioned_database_open_failure_action_targets_no_button():
from dataclasses import replace
window = evidence_window(native_database_open_error().texts[0], phase=DialogPhase.DOCUMENT_ATTACH)
window = replace(window, controls=(window.controls[0],
EvidenceControl(502, 6, 'Button', 'Yes', control_type='Button'),
EvidenceControl(503, 7, 'Button', 'No', control_type='Button')))
backend = PhaseBackend([[window]])
result = DialogSupervisor(backend, rules=RULES).poll(window.phase)
assert result.error_code == 'MENTOR_DATABASE_LOAD_FAILED'
assert dict(backend.actions[0][1]['target']) == {'control_id': 7}