fix: distinguish UIA dialog chrome from native consent controls

This commit is contained in:
阿白叔 committed 2026-11-03 09:43:16 +08:00
1 parent 9806ef8f66
commit 99a708b40c
2 files changed
+61 -3

No files matched your search

+16 -3
View File
@@ -161,13 +161,26 @@ def exact_prompt(snapshot):
if (snapshot.phase != DialogPhase.PCB_STARTING or snapshot.process_name.casefold() != 'expeditionpcb.exe'
or snapshot.process_role != 'pcb' or snapshot.window_class != '#32770' or snapshot.title != 'Expedition PCB'):
return False
buttons = [c for c in snapshot.controls if c.class_name == 'Button']
static = [c for c in snapshot.controls if c.class_name == 'Static' and c.name.strip()]
controls = []
for control in snapshot.controls:
self_window = (control.handle == snapshot.hwnd and control.control_id == 0
and control.class_name == snapshot.window_class and control.name == snapshot.title
and control.control_type == 'Window' and control.patterns == ('Window',))
uia_chrome = (control.handle == 0 and control.control_id == 0
and control.class_name == '' and control.automation_id == ''
and not control.selected and not control.checked and (
control.control_type == 'TitleBar' and control.name == '' and control.patterns == ('Value',)
or control.control_type == 'Button' and not control.enabled
and control.name in ('关闭', 'Close') and control.patterns == ('Invoke',)))
if not self_window and not uia_chrome:
controls.append(control)
buttons = [c for c in controls if c.class_name == 'Button']
static = [c for c in controls if c.class_name == 'Static' and c.name.strip()]
return (len(buttons) == 2 and {c.control_id for c in buttons} == {6, 7}
and all(c.enabled for c in buttons) and len(static) == 1 and static[0].control_id == 65535
and ' '.join(static[0].name.split()) == ' '.join(PROMPT_TEXT.split())
and all(c.class_name == 'Button' or c.class_name == 'Static' and c.control_id in (20, 65535)
for c in snapshot.controls))
for c in controls))
def _validate_current_processes(job, prelaunch, target_pid, launcher_pid, worker_pid, companion_pids):
+45
View File
@@ -195,3 +195,48 @@ def test_agent_revalidates_real_window_before_single_button_message(monkeypatch,
def test_ordinary_rule_remains_hard_blocked_for_exact_recovery_prompt():
from cbb_refresh_verify.dialog_rules import decide
assert decide(snapshot(), ()).error_code == 'DATABASE_RECOVERY_REQUIRED'
def enriched_snapshot():
"""Sanitized actual aa-r2 merged Win32/UIA capture (only HWNDs changed)."""
from cbb_refresh_verify.dialog_models import ControlSnapshot
value = snapshot()
native = (replace(value.controls[0], name='是(Y)', automation_id='6', control_type='Button', patterns=('Invoke',)),
replace(value.controls[1], name='否(N)', automation_id='7', control_type='Button', patterns=('Invoke',)),
replace(value.controls[2], automation_id='20', control_type='Image'),
replace(value.controls[3], automation_id='65535', control_type='Text'))
return replace(value, controls=(*native,
ControlSnapshot(value.hwnd, 0, '#32770', 'Expedition PCB', control_type='Window', patterns=('Window',)),
ControlSnapshot(0, 0, '', '', control_type='TitleBar', patterns=('Value',)),
ControlSnapshot(0, 0, '', '关闭', control_type='Button', enabled=False, patterns=('Invoke',))))
@pytest.mark.parametrize('change', ['', 'extra_real_child', 'enabled_close', 'titlebar_text', 'other_self'])
def test_enriched_native_prompt_ignores_only_observed_uia_chrome(change):
from cbb_refresh_verify.post_repair_consent import exact_prompt
from cbb_refresh_verify.dialog_models import ControlSnapshot
value = enriched_snapshot()
if change == 'extra_real_child':
value = replace(value, controls=(*value.controls, ControlSnapshot(888, 0, '', '关闭',
control_type='Button', enabled=False, patterns=('Invoke',))))
if change == 'enabled_close': value = replace(value, controls=(*value.controls[:-1], replace(value.controls[-1], enabled=True)))
if change == 'titlebar_text': value = replace(value, controls=(*value.controls[:-2], replace(value.controls[-2], name='unexpected'), value.controls[-1]))
if change == 'other_self': value = replace(value, controls=(*value.controls[:4], replace(value.controls[4], handle=999), *value.controls[5:]))
assert exact_prompt(value) is (not change)
def test_enriched_prompt_still_reaches_live_native_child_check(monkeypatch):
import win32gui, win32process
from cbb_refresh_verify.post_repair_consent import validate_action_window
from cbb_refresh_verify import native_repair_ui
value = enriched_snapshot()
native = {c.handle: c for c in value.controls[:4]}
monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'CbbVerify_fixture')
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 20))
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: True)
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda hwnd: True)
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: value.window_class if hwnd == value.hwnd else native[hwnd].class_name)
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: value.title if hwnd == value.hwnd else native[hwnd].name)
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda hwnd: native[hwnd].control_id)
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg: [fn(child, arg) for child in native])
validate_action_window(value, {'pid':20, 'desktop':'CbbVerify_fixture'})