import importlib import importlib.util from pathlib import Path import pytest @pytest.fixture def diagnostic(): assert importlib.util.find_spec('cbb_refresh_verify.diagnostic_open') is not None, 'diagnostic open module is missing' return importlib.import_module('cbb_refresh_verify.diagnostic_open') @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') return {'project': str(project / 'IP101GR.prj'), 'pcb': str(pcb), 'root': str(tmp_path / 'evidence'), 'desktop': 'CbbDiagnosticTest', '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 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)