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

294 lines
11 KiB
Python

import json
from pathlib import Path
import pytest
from cbb_refresh_verify.dialog_models import ControlSnapshot, DialogPhase, WindowSnapshot
from cbb_refresh_verify.dialog_rules import (
DialogRule,
RuleConfigError,
decide,
load_rules,
)
ASSETS = Path(__file__).resolve().parents[1] / "src" / "cbb_refresh_verify" / "assets"
def test_diagnostic_forward_prompt_is_observed_while_ordinary_verify_still_confirms():
text = 'New changes are ready for Forward Annotation. Do you want to Forward Annotate now?'
snapshot = prompt(text)
ordinary = decide(snapshot, load_rules(ASSETS/'dialog_rules.v1.json'))
diagnostic = decide(snapshot, load_rules(ASSETS/'dialog_rules.v1.json', diagnostic_only=True))
assert ordinary.action == 'confirm_forward_annotation'
assert diagnostic.category == 'OBSERVE_PROGRESS' and not diagnostic.action
def prompt(text, *, phase=DialogPhase.PCB_STARTING, window_class="#32770",
control_types=("Text", "Button")):
controls = tuple(
ControlSnapshot(
handle=100 + index,
control_id=index,
class_name=control_type,
name=text if index == 0 else "OK",
control_type=control_type,
patterns=("Invoke",) if control_type == "Button" else (),
)
for index, control_type in enumerate(control_types)
)
return WindowSnapshot(
hwnd=10,
pid=20,
process_name="ExpeditionPCB.exe",
process_role="pcb",
window_class=window_class,
title="Expedition PCB",
phase=phase,
controls=controls,
)
def matching_rule(rule_id, priority):
return DialogRule(
rule_id=rule_id,
priority=priority,
category="OBSERVE_PROGRESS",
phases=(DialogPhase.PROJECT_INTEGRATION,),
semantic_all=("task is being processed",),
)
def test_default_save_rule_is_out_of_phase_during_license_acquisition():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"Do you want to save the Reusable Block cell?",
phase=DialogPhase.LICENSE_ACQUISITION,
)
decision = decide(snapshot, rules)
assert decision.error_code == "DIALOG_OUT_OF_PHASE"
assert decision.rule_id == "reusable-block-save"
def test_license_selector_is_allowed_after_com_attach_wait():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"Select the desired product license",
phase=DialogPhase.DOCUMENT_ATTACH,
window_class="#32770",
control_types=("GXWND", "Button"),
)
decision = decide(snapshot, rules)
assert decision.rule_id == "license-selector"
assert decision.error_code == ""
def test_open_lock_confirmation_blocks_during_pcb_starting():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"Continue to open the design with read/write access permission?",
phase=DialogPhase.PCB_STARTING,
)
decision = decide(snapshot, rules)
assert not decision.action
assert decision.error_code == "DATABASE_RECOVERY_REQUIRED"
def test_forward_annotation_confirmation_is_accepted_during_document_attach():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"New changes are ready for Forward Annotation. Do you want to Forward Annotate now?",
phase=DialogPhase.DOCUMENT_ATTACH,
)
decision = decide(snapshot, rules)
assert decision.rule_id == "forward-annotation-confirm"
assert decision.action == "confirm_forward_annotation"
assert decision.error_code == ""
def test_forward_annotation_confirmation_is_accepted_during_pcb_starting():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"New changes are ready for Forward Annotation. Do you want to Forward Annotate now?",
phase=DialogPhase.PCB_STARTING,
)
decision = decide(snapshot, rules)
assert decision.rule_id == "forward-annotation-confirm"
assert decision.action == "confirm_forward_annotation"
assert decision.error_code == ""
def test_project_integration_panel_is_observation_only():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"&Project file and Design Forward Annotation Required, connectivity changed "
"No pending schematic CES changes to load into PCB",
phase=DialogPhase.PROJECT_INTEGRATION,
)
decision = decide(snapshot, rules)
assert decision.rule_id == "project-integration-panel"
assert decision.category == "OBSERVE_PROGRESS"
assert decision.action == ""
assert decision.error_code == ""
def test_license_selector_prefers_nonblocking_ok_click_before_uia_invoke():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"Select the desired product license",
phase=DialogPhase.DOCUMENT_ATTACH,
window_class="#32770",
control_types=("GXWND", "Button"),
)
decision = decide(snapshot, rules)
assert decision.channels.index("win32_button_click") < decision.channels.index("uia_invoke")
def test_tip_of_day_prefers_nonblocking_ok_click_before_uia_invoke():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
decision = decide(prompt("Tip of the Day"), rules)
assert decision.rule_id == "tip-of-day"
assert decision.channels.index("win32_button_click") < decision.channels.index("uia_invoke")
def test_integration_progress_out_of_phase_remains_observation_only():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
decision = decide(
prompt(
"Your task is being processed. Elapsed time 10 seconds",
phase=DialogPhase.SAVE,
),
rules,
)
assert decision.rule_id == "integration-progress"
assert decision.category == "OBSERVE_PROGRESS"
assert decision.error_code == "DIALOG_OUT_OF_PHASE"
def test_equal_priority_matches_are_rejected():
snapshot = prompt(
"Your task is being processed. Elapsed time 10 seconds",
phase=DialogPhase.PROJECT_INTEGRATION,
)
decision = decide(snapshot, (matching_rule("a", 10), matching_rule("b", 10)))
assert decision.error_code == "DIALOG_RULE_CONFLICT"
assert "a" in decision.detail and "b" in decision.detail
def test_override_replaces_rule_by_id(tmp_path):
override = tmp_path / "rules.json"
override.write_text(json.dumps({
"schema_version": 1,
"rules": [{"rule_id": "tip-of-day", "enabled": False}],
}), encoding="utf-8")
rules = load_rules(ASSETS / "dialog_rules.v1.json", override)
assert next(rule for rule in rules if rule.rule_id == "tip-of-day").enabled is False
def test_unknown_schema_is_rejected(tmp_path):
path = tmp_path / "rules.json"
path.write_text(json.dumps({"schema_version": 2, "rules": []}), encoding="utf-8")
with pytest.raises(RuleConfigError, match="schema_version"):
load_rules(path)
def test_negative_semantic_evidence_prevents_match():
rule = DialogRule(
rule_id="continue-not-cancel",
priority=1,
category="SAFE_CONFIRM",
phases=(DialogPhase.PCB_STARTING,),
semantic_any=("continue",),
semantic_none=("destructive",),
action="confirm",
)
decision = decide(prompt("Continue destructive recovery"), (rule,))
assert decision.error_code == "UNKNOWN_DIALOG"
def test_known_ime_language_bar_is_observation_only_not_an_unknown_dialog():
snapshot = WindowSnapshot(hwnd=100, pid=200, process_name='ExpeditionPCB.exe',
process_role='pcb', window_class='CiceroUIWndFrame', title='TF_FloatingLangBar_WndTitle',
phase=DialogPhase.PCB_STARTING, controls=(), owner_hwnd=99)
decision = decide(snapshot, load_rules(ASSETS/'dialog_rules.v1.json'))
assert decision.category == 'OBSERVE_PROGRESS'
assert decision.error_code == ''
assert decision.action == ''
def test_language_bar_filter_does_not_ignore_a_real_dialog_with_same_caption():
snapshot = prompt('TF_FloatingLangBar_WndTitle')
decision = decide(snapshot, load_rules(ASSETS/'dialog_rules.v1.json'))
assert decision.error_code == 'UNKNOWN_DIALOG'
def test_database_open_failure_is_classified_and_declines_log_view():
snapshot = prompt('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?')
decision = decide(snapshot, load_rules(ASSETS/'dialog_rules.v1.json'))
assert decision.category == 'FATAL_ACK'
assert decision.error_code == 'MENTOR_DATABASE_LOAD_FAILED'
assert dict(decision.target)['control_id'] == 7 # IDNO, never launch another log viewer
def completed_integration_panel():
texts={1033:'No connectivity changes to be forward annotated',
1032:'No pending schematic CES changes to load into PCB',
1035:'No pending PCB changes to be back annotated',
1034:'No pending CES changes to load into PCB'}
return WindowSnapshot(hwnd=10,pid=20,process_name='ExpeditionPCB.exe',process_role='pcb',
window_class='#32770',title='Project Integration',phase=DialogPhase.SAVE,owner_hwnd=99,
controls=(ControlSnapshot(handle=12,control_id=2,class_name='Button',name='&Close'),
*(ControlSnapshot(handle=i,control_id=i,class_name='Static',name=text) for i,text in texts.items())))
@pytest.mark.parametrize('phase',[DialogPhase.SAVE,DialogPhase.CLOSE])
def test_completed_integration_panel_only_closes_its_exact_close_control(phase):
from dataclasses import replace
decision=decide(replace(completed_integration_panel(),phase=phase),load_rules(ASSETS/'dialog_rules.v1.json'))
assert decision.action=='close_completed_integration'
assert decision.channels==('win32_button_click',)
assert dict(decision.target)=={'control_id':2}
assert decision.postcondition=='window_gone' and not decision.error_code
@pytest.mark.parametrize('fault',['pending','missing','duplicate','partial','foreign','unowned','disabled','early','error'])
def test_integration_panel_without_complete_owned_idle_proof_never_closes(fault):
from dataclasses import replace
panel=completed_integration_panel()
if fault=='pending': panel=replace(panel,controls=(*panel.controls[:-1],replace(panel.controls[-1],name='CES changes are pending')))
elif fault=='missing': panel=replace(panel,controls=panel.controls[:-1])
elif fault=='duplicate': panel=replace(panel,controls=(*panel.controls,panel.controls[-1]))
elif fault=='partial': panel=replace(panel,diagnostics=('WINDOW_ENUM_INCOMPLETE',))
elif fault=='foreign': panel=replace(panel,process_name='Unrelated.exe',process_role='unknown')
elif fault=='unowned': panel=replace(panel,owner_hwnd=0)
elif fault=='disabled': panel=replace(panel,controls=(replace(panel.controls[0],enabled=False),*panel.controls[1:]))
elif fault=='early': panel=replace(panel,phase=DialogPhase.DOCUMENT_ATTACH)
elif fault=='error': panel=replace(panel,controls=(*panel.controls,
ControlSnapshot(handle=500,control_id=500,class_name='Static',name='There were errors during Project Integration.')))
assert decide(panel,load_rules(ASSETS/'dialog_rules.v1.json')).action!='close_completed_integration'