538 lines
26 KiB
Python
538 lines
26 KiB
Python
import importlib
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def diagnostic(monkeypatch):
|
|
assert importlib.util.find_spec('cbb_refresh_verify.diagnostic_open') is not None, 'diagnostic open module is missing'
|
|
module = importlib.import_module('cbb_refresh_verify.diagnostic_open')
|
|
check = module.check_target_window
|
|
def acknowledged(job, pid, *, emit, **kwargs):
|
|
def parent(**event):
|
|
emit(**event)
|
|
if event['stage'] == 'diagnostic_close_requested':
|
|
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps(
|
|
{key:value for key,value in event.items() if key != 'stage'}))
|
|
return check(job, pid, emit=parent, **kwargs)
|
|
monkeypatch.setattr(module, 'check_target_window', acknowledged)
|
|
return module
|
|
|
|
|
|
@pytest.fixture
|
|
def job(tmp_path):
|
|
project = tmp_path / 'IP101GR'
|
|
pcb = project / 'PCB/IP101GR.pcb'
|
|
pcb.parent.mkdir(parents=True)
|
|
pcb.write_bytes(b'pcb')
|
|
(tmp_path/'evidence').mkdir()
|
|
return {'project': str(project / 'IP101GR.prj'), 'pcb': str(pcb), 'root': str(tmp_path / 'evidence'),
|
|
'desktop': 'CbbDiagnosticTest', 'operation_id': 'verify-fixture', 'timeout': .03}
|
|
|
|
|
|
def write_logs(job, *, pid=42, foreign=False, success=True):
|
|
project = Path(job['project']).parent
|
|
logdir = Path(job['pcb']).parent / 'LogFiles'
|
|
logdir.mkdir(parents=True, exist_ok=True)
|
|
pcb = str(Path(job['pcb'])) if not foreign else str(project.parent / 'other/PCB/IP101GR.pcb')
|
|
(logdir / 'PCBAutoSave.txt').write_text('Job Name: ' + pcb + '\n', encoding='utf-8')
|
|
(logdir / 'PCBLogFile.txt').write_text('Successfully read the design.\n' if success else 'Loading\n', encoding='utf-8')
|
|
database = str(project / 'database') if not foreign else str(project.parent / 'other/database')
|
|
logs = project / 'database/cdbsvr/log/current'
|
|
logs.mkdir(parents=True, exist_ok=True)
|
|
(logs / 'current.txt').write_text(
|
|
f'S: Project path [{database}] on file system [NTFS]\n'
|
|
f'S: Project file (.prj) path [{project / "IP101GR.prj"}] on file system [NTFS]\n'
|
|
f'S: Started up automatically by iCDB Client: ABS:ABS:{pid}\n'
|
|
'S: iCDB database loaded\n'
|
|
f'SC[455]: Registered sub-client [Expedition PCB_123] with ID [1] for session ID [455] host [Abs] user [ABS] process ID [{pid}]\n',
|
|
encoding='utf-8')
|
|
|
|
|
|
def test_old_success_logs_are_not_current_open_evidence(diagnostic, job):
|
|
write_logs(job)
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
assert diagnostic._load_evidence(job, 42, baseline) is None
|
|
|
|
|
|
@pytest.mark.parametrize('pid,foreign,success', [(99, False, True), (42, True, True), (42, False, False)])
|
|
def test_foreign_pid_path_or_incomplete_load_cannot_pass(diagnostic, job, pid, foreign, success):
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job, pid=pid, foreign=foreign, success=success)
|
|
assert diagnostic._load_evidence(job, 42, baseline) is None
|
|
|
|
|
|
def test_current_full_path_and_pcb_pid_logs_prove_open_only(diagnostic, job):
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
evidence = diagnostic._load_evidence(job, 42, baseline)
|
|
assert evidence['pcb'] == job['pcb']
|
|
assert evidence['pcb_pid'] == 42
|
|
|
|
|
|
def test_old_prefix_in_appended_log_cannot_supply_target_identity(diagnostic, job):
|
|
write_logs(job)
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
for path in Path(job['project']).parent.rglob('*.txt'):
|
|
with path.open('a', encoding='utf-8') as output:
|
|
output.write('unrelated new record\n')
|
|
assert diagnostic._load_evidence(job, 42, baseline) is None
|
|
|
|
|
|
def frame(job, *, title='IP101GR'):
|
|
return {'hwnd': 10, 'pid': 42, 'class': 'Afx:123',
|
|
'text': f'Expedition PCB - Reusable Block - [{title}]', 'enabled': True,
|
|
'children': [{'hwnd': 11, 'class': 'MDIClient', 'text': '', 'parent': 10},
|
|
{'hwnd': 12, 'class': 'Afx:document', 'text': title, 'parent': 11}]}
|
|
|
|
|
|
def test_target_title_without_target_mdi_does_not_pass(diagnostic, job):
|
|
window = frame(job)
|
|
window['children'][1]['text'] = 'another_project'
|
|
assert diagnostic._target_frame([window], Path(job['pcb']).stem) is None
|
|
|
|
|
|
def test_visible_loading_progress_is_not_ready_for_diagnostic_close(diagnostic, job):
|
|
window = frame(job)
|
|
progress = {'hwnd': 13, 'class': 'msctls_progress32', 'text': '', 'visible': True}
|
|
window['children'].append(progress)
|
|
assert diagnostic._target_frame([window], 'IP101GR') is None
|
|
progress['visible'] = False
|
|
assert diagnostic._target_frame([window], 'IP101GR') == window
|
|
|
|
|
|
@pytest.mark.parametrize('cancel', [False, True])
|
|
def test_persistent_loading_progress_keeps_deadline_and_never_closes(diagnostic, job, monkeypatch, cancel):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
signal = Path(job['root']).parent/'cancel-progress'
|
|
job['cancel_signal'] = str(signal)
|
|
target = frame(job)
|
|
target['children'].append({'hwnd':13,'class':'msctls_progress32','text':'','visible':True})
|
|
def windows(pid):
|
|
if cancel: signal.touch()
|
|
return [target]
|
|
monkeypatch.setattr(diagnostic,'_NativeSession',Session)
|
|
monkeypatch.setattr(diagnostic,'_windows',windows)
|
|
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED' if cancel else 'DIAGNOSTIC_OPEN_UNCONFIRMED'):
|
|
diagnostic.check_target_window(job,42,emit=lambda **event:None,error_reader=lambda:None)
|
|
assert Session.instances[-1].posted == []
|
|
|
|
|
|
def test_cancel_from_window_scan_wins_expired_open_deadline(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
signal = Path(job['root']).parent/'cancel-after-scan'
|
|
job['cancel_signal'] = str(signal)
|
|
clock = [0.0]
|
|
monkeypatch.setattr(diagnostic.time, 'monotonic', lambda: clock[0])
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
target = frame(job)
|
|
target['children'].append({'hwnd':13,'class':'msctls_progress32','text':'','visible':True})
|
|
def windows(pid):
|
|
signal.touch()
|
|
clock[0] = 10.0
|
|
return [target]
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == []
|
|
|
|
|
|
def test_diagnostic_waits_for_startup_forward_no_before_close(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job); job['timeout'] = 1
|
|
class ForwardSession(Session):
|
|
declined = False
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd,message))
|
|
if hwnd == 31: self.declined = True
|
|
else: self.alive = False
|
|
def windows(pid):
|
|
state = Session.instances[-1]
|
|
target = frame(job)
|
|
if state.declined: return [target] if state.alive else []
|
|
target['enabled'] = False
|
|
target['children'].append({'hwnd':13,'class':'msctls_progress32','text':'','visible':True})
|
|
return [target, {'hwnd':20,'pid':42,'class':'#32770','text':'Expedition PCB','owner':10,
|
|
'children':[{'hwnd':30,'class':'Static','id':6172,'text':'New changes are ready for Forward Annotation. Do you want to Forward Annotate now?'},
|
|
{'hwnd':31,'class':'Button','id':7,'text':'&No','enabled':True},
|
|
{'hwnd':32,'class':'Button','id':6,'text':'&Yes','enabled':True}]}]
|
|
monkeypatch.setattr(diagnostic,'_NativeSession',ForwardSession)
|
|
monkeypatch.setattr(diagnostic,'_windows',windows)
|
|
assert diagnostic.check_target_window(job,42,emit=lambda **event:None,error_reader=lambda:None)['status']=='PASS'
|
|
assert Session.instances[-1].posted == [(31,0xF5),(10,0x10)]
|
|
|
|
|
|
class Session:
|
|
instances = []
|
|
def __init__(self, job, pid, launcher):
|
|
self.alive = True
|
|
self.closed = False
|
|
self.posted = []
|
|
self.instances.append(self)
|
|
def pcb_alive(self):
|
|
return self.alive
|
|
def launcher_alive(self):
|
|
return self.alive
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd, message))
|
|
self.alive = False
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
|
|
def test_unknown_prompt_is_preserved_and_never_closed(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job),
|
|
{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Unknown question', 'children': []}])
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == []
|
|
assert Session.instances[-1].closed
|
|
|
|
|
|
def test_success_posts_one_exact_close_and_reports_only_open_check(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
|
|
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert outcome['status'] == 'PASS'
|
|
assert outcome['check'] == 'native_open_only'
|
|
assert outcome['native_processes_exited'] is True
|
|
assert outcome['library_ownership'] == 'NOT_ASSESSED'
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
assert Session.instances[-1].closed
|
|
|
|
|
|
@pytest.mark.parametrize('incomplete_scan', [1, 2, 3])
|
|
def test_transient_incomplete_scan_is_retried_without_acting_on_partial_windows(diagnostic, job, monkeypatch, incomplete_scan):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
job['timeout'] = 1
|
|
write_logs(job)
|
|
class ClosingSession(Session):
|
|
closing = False
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd, message))
|
|
self.closing = True
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', ClosingSession)
|
|
scans = []
|
|
def windows(pid):
|
|
scans.append(True)
|
|
if len(scans) == incomplete_scan:
|
|
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
|
|
if Session.instances[-1].closing:
|
|
Session.instances[-1].alive = False
|
|
return []
|
|
return [frame(job)]
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
events = []
|
|
result = diagnostic.check_target_window(job, 42, emit=lambda **event:events.append(event), error_reader=lambda:None)
|
|
assert result['status'] == 'PASS'
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
assert any(e['stage'] == 'diagnostic_window_retry' for e in events)
|
|
|
|
|
|
def test_incomplete_scans_keep_original_open_deadline_and_never_close(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
calls = []
|
|
def windows(pid):
|
|
calls.append(True)
|
|
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_WINDOW_LIMIT'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
|
|
assert len(calls) <= 3
|
|
assert Session.instances[-1].posted == []
|
|
|
|
|
|
def test_cancel_during_incomplete_scan_stops_retry_without_close(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
job['timeout'] = 10
|
|
cancel = Path(job['root']).parent/'cancel-scan'
|
|
job['cancel_signal'] = str(cancel)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
calls = []
|
|
def windows(pid):
|
|
calls.append(True)
|
|
cancel.touch()
|
|
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
|
|
assert len(calls) == 1 and Session.instances[-1].posted == []
|
|
|
|
|
|
def test_incomplete_close_scan_cannot_extend_thirty_second_deadline(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
clock, offset = diagnostic.time.monotonic, [0]
|
|
monkeypatch.setattr(diagnostic.time, 'monotonic', lambda:clock()+offset[0])
|
|
class ClosingSession(Session):
|
|
closing = False
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd, message))
|
|
self.closing = True
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', ClosingSession)
|
|
def windows(pid):
|
|
if Session.instances[-1].closing:
|
|
offset[0] = 31
|
|
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
|
|
return [frame(job)]
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_WINDOW_LIMIT'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
|
|
|
|
def test_transient_startup_window_cannot_abort_before_target_is_loaded(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
calls = []
|
|
def windows(pid):
|
|
calls.append(True)
|
|
if len(calls) == 1:
|
|
return [{'hwnd': 20, 'pid': 42, 'class': 'VendorSplash', 'text': '', 'children': []}]
|
|
return [frame(job)] if Session.instances[-1].alive else []
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
job['timeout'] = 1
|
|
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
|
|
|
|
def test_native_caption_variant_uses_proven_target_mdi(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
target = frame(job)
|
|
target['text'] = 'Expedition PCB - [IP101GR]'
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [target] if Session.instances[-1].alive else [])
|
|
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
|
|
|
|
|
|
def test_cancel_does_not_wait_for_open_timeout(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
cancel = Path(job['root']).parent / 'cancel'
|
|
cancel.touch()
|
|
job['cancel_signal'] = str(cancel)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [])
|
|
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == []
|
|
assert Session.instances[-1].closed
|
|
|
|
|
|
def test_cancel_after_close_request_still_waits_for_own_launcher_exit(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
cancel = Path(job['root']).parent / 'cancel'
|
|
job['cancel_signal'] = str(cancel)
|
|
class DelayedLauncher(Session):
|
|
polls = 0
|
|
def post(self, hwnd, message):
|
|
super().post(hwnd, message)
|
|
cancel.touch()
|
|
def launcher_alive(self):
|
|
self.polls += 1
|
|
return self.polls < 3
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', DelayedLauncher)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
|
|
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
|
|
assert Session.instances[-1].polls >= 3
|
|
|
|
|
|
@pytest.mark.parametrize('owner,window_class', [(10, 'VendorModal'), (0, 'VendorModal'),
|
|
(0, 'Internet Explorer_Hidden')])
|
|
def test_unknown_custom_owned_window_blocks_close(diagnostic, job, monkeypatch, owner, window_class):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job),
|
|
{'hwnd': 20, 'pid': 42, 'class': window_class, 'text': 'Unclassified prompt',
|
|
'owner': owner, 'enabled': True, 'children': []}])
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == []
|
|
|
|
|
|
def test_empty_desktop_resets_stale_win32_last_error(diagnostic, monkeypatch):
|
|
import win32api
|
|
import win32gui
|
|
import pywintypes
|
|
seen = []
|
|
def empty(callback, context):
|
|
seen.append(win32api.GetLastError())
|
|
raise pywintypes.error(0, 'EnumWindows', 'empty desktop')
|
|
monkeypatch.setattr(win32gui, 'EnumWindows', empty)
|
|
win32api.SetLastError(170)
|
|
assert diagnostic._windows(42) == []
|
|
assert seen == [0]
|
|
|
|
|
|
def test_missing_launcher_cannot_claim_verified_native_release(diagnostic, job):
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_LAUNCHER_REQUIRED'):
|
|
diagnostic._NativeSession(job, 42, None)
|
|
|
|
|
|
def test_buffered_full_pcb_path_must_be_verified_after_normal_close(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
autosave = Path(job['pcb']).parent / 'LogFiles/PCBAutoSave.txt'
|
|
autosave.write_text('', encoding='utf-8')
|
|
class BufferedSession(Session):
|
|
def post(self, hwnd, message):
|
|
super().post(hwnd, message)
|
|
autosave.write_text('Job Name: ' + job['pcb'] + '\n', encoding='utf-8')
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', BufferedSession)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
|
|
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert outcome['status'] == 'PASS'
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
|
|
|
|
def test_large_unrelated_history_does_not_block_current_evidence(diagnostic, job):
|
|
logs = Path(job['project']).parent / 'database/cdbsvr/log'
|
|
for index in range(270):
|
|
directory = logs / f'2000-01-01 00.00.00-{index:05}'
|
|
directory.mkdir(parents=True)
|
|
(directory / '2000.01.01.txt').write_bytes(b'old unrelated history\n' * 14000)
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
assert diagnostic._load_evidence(job, 42, baseline) is not None
|
|
|
|
|
|
def test_pcb_logs_follow_actual_pcb_directory(diagnostic, job):
|
|
pcb = Path(job['pcb']).parent.parent / 'AlternativePCB/IP101GR.pcb'
|
|
pcb.parent.mkdir()
|
|
pcb.write_bytes(b'pcb')
|
|
job['pcb'] = str(pcb)
|
|
baseline = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
assert diagnostic._load_evidence(job, 42, baseline)['pcb'] == str(pcb)
|
|
|
|
|
|
def test_only_exact_owned_save_prompt_gets_no_after_diagnostic_close(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
class SaveSession(Session):
|
|
closing = False
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd, message))
|
|
if message == 0x10:
|
|
self.closing = True
|
|
else:
|
|
self.alive = False
|
|
closing_scans = []
|
|
def windows(pid):
|
|
state = Session.instances[-1]
|
|
if not state.closing:
|
|
return [frame(job)]
|
|
closing_scans.append(True)
|
|
if len(closing_scans) == 1:
|
|
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': '',
|
|
'owner': 10, 'children': []}]
|
|
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB',
|
|
'owner': 10, 'children': [
|
|
{'hwnd': 30, 'class': 'Static', 'text': 'Do you want to save the reusable block cell?', 'id': -1},
|
|
{'hwnd': 31, 'class': 'Button', 'text': '&No', 'id': 7}]}]
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', SaveSession)
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert outcome['status'] == 'PASS'
|
|
assert Session.instances[-1].posted == [(10, 0x10), (31, 0xF5)]
|
|
|
|
|
|
def test_no_pass_when_normal_close_never_flushes_full_pcb_path(diagnostic, job, monkeypatch):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
(Path(job['pcb']).parent / 'LogFiles/PCBAutoSave.txt').write_bytes(b'')
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
|
|
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
|
|
with pytest.raises(RuntimeError, match='fresh full PCB path not confirmed after close'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
|
|
|
|
@pytest.mark.parametrize('filename,accepted', [('IP101GR.pcb', True), ('Foreign.pcb', False)])
|
|
def test_standard_save_question_requires_exact_target_filename(diagnostic, job, monkeypatch, filename, accepted):
|
|
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
|
|
write_logs(job)
|
|
class SaveSession(Session):
|
|
closing = False
|
|
def post(self, hwnd, message):
|
|
self.posted.append((hwnd, message))
|
|
if message == 0x10:
|
|
self.closing = True
|
|
else:
|
|
self.alive = False
|
|
def windows(pid):
|
|
if not Session.instances[-1].closing:
|
|
return [frame(job)]
|
|
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB', 'owner': 10,
|
|
'children': [{'hwnd': 30, 'class': 'Static', 'text': f'Save changes to {filename}?', 'id': -1},
|
|
{'hwnd': 31, 'class': 'Button', 'text': '否(&N)', 'id': 7},
|
|
{'hwnd': 32, 'class': 'Button', 'text': '是(&Y)', 'id': 6},
|
|
{'hwnd': 33, 'class': 'Button', 'text': '取消', 'id': 2}]}]
|
|
monkeypatch.setattr(diagnostic, '_NativeSession', SaveSession)
|
|
monkeypatch.setattr(diagnostic, '_windows', windows)
|
|
if accepted:
|
|
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
|
|
assert Session.instances[-1].posted == [(10, 0x10), (31, 0xF5)]
|
|
else:
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
|
|
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
|
|
assert Session.instances[-1].posted == [(10, 0x10)]
|
|
|
|
|
|
def test_recycled_window_from_another_pid_never_receives_close(diagnostic, monkeypatch):
|
|
import win32process
|
|
session = diagnostic._NativeSession.__new__(diagnostic._NativeSession)
|
|
session.pid = 42
|
|
session.pcb_alive = lambda: True
|
|
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 99))
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_STALE_WINDOW'):
|
|
session.post(10, 0x10)
|
|
|
|
|
|
@pytest.mark.parametrize('fault', ['', 'foreign_pid', 'cycle', 'other_frame', 'visible', 'enabled', 'class', 'title', 'desktop', 'incomplete', 'filename'])
|
|
def test_diagnostic_save_accepts_only_complete_hidden_owner_chain(diagnostic, job, fault):
|
|
target = frame(job)
|
|
owner = {'hwnd': 15, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB',
|
|
'owner': 10, 'visible': False, 'enabled': False, 'same_desktop': True}
|
|
end = {**target, 'same_desktop': True}
|
|
window = {'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB',
|
|
'owner': 15, 'owner_chain': [owner, end], 'children': [
|
|
{'hwnd': 30, 'class': 'Static', 'text': 'Save changes to IP101GR.pcb?', 'id': -1},
|
|
{'hwnd': 31, 'class': 'Button', 'text': '否(&N)', 'id': 7},
|
|
{'hwnd': 32, 'class': 'Button', 'text': '是(&Y)', 'id': 6},
|
|
{'hwnd': 33, 'class': 'Button', 'text': '取消', 'id': 2}]}
|
|
if fault == 'foreign_pid': owner['pid'] = 99
|
|
if fault == 'cycle': owner['owner'] = 15
|
|
if fault == 'other_frame': end['hwnd'] = 99
|
|
if fault == 'visible': owner['visible'] = True
|
|
if fault == 'enabled': owner['enabled'] = True
|
|
if fault == 'class': owner['class'] = 'VendorDialog'
|
|
if fault == 'title': owner['text'] = 'Unknown recovery'
|
|
if fault == 'desktop': owner['same_desktop'] = False
|
|
if fault == 'incomplete': window['owner_chain'] = [owner]
|
|
if fault == 'filename': window['children'][0]['text'] = 'Save changes to Foreign.pcb?'
|
|
session = Session(job, 42, None)
|
|
action = lambda:diagnostic._pending_prompt([window], closing=True, frame=target,
|
|
session=session, clicked=set(), pcb_name='IP101GR.pcb')
|
|
if not fault:
|
|
assert action() is False
|
|
assert session.posted == [(31, 0xF5)]
|
|
else:
|
|
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
|
|
action()
|
|
assert session.posted == []
|