import json from pathlib import Path from types import ModuleType, SimpleNamespace import pytest def test_raw_dispatch_diagnostics_preserve_hresult_and_known_member_ids(tmp_path): from cbb_refresh_verify.native_management import _dispatch_diagnostics class Error(RuntimeError): hresult = -2147221008 class Raw: def GetIDsOfNames(self, name): if name == 'Quit': raise Error('COM apartment unavailable') return 17 if name == 'Close' else 21 obj = SimpleNamespace(_oleobj_=Raw()) _dispatch_diagnostics(tmp_path, 'before-import', obj, obj) record = json.loads((tmp_path/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8')) assert record['members']['application.OnReusableBlocksEditor']['dispid'] == 21 assert record['members']['library.Close']['dispid'] == 17 error = record['members']['application.Quit']['error'] assert error['hresult'] == -2147221008 and error['args'] == ['COM apartment unavailable'] assert error['type'].endswith('Error') def test_probe_records_dispatch_before_after_import_and_after_source_alignment(tmp_path, monkeypatch): import cbb_refresh_verify.native_management as native import sys source = tmp_path/'source/Block.prj' source.parent.mkdir() source.write_text('KEY RootBlock "Block"\nKEY PCBDesignPath "PCB/Block.pcb"\n' 'KEY iCDBDir "database"\nKEY CentralLibrary "old.lmc"\n') (source.parent/'database').mkdir(); (source.parent/'database/icdb.dat').write_bytes(b'fixture') pcb = source.parent/'PCB/Block.pcb'; (pcb.parent/'Layout').mkdir(parents=True) pcb.write_text('KEY ProjectFile "../Block.prj"\n') (pcb.parent/'pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') (pcb.parent/'Layout/LayoutDB.lyt').write_bytes(b'fixture') root = tmp_path/'native-add'; root.mkdir() library = root/'fixture/mentor_lib.lmc'; library.parent.mkdir(); library.touch() phase = ['before'] class Raw: def GetIDsOfNames(self, name): return 1 if phase[0] == 'before' else 2 class App: _oleobj_ = Raw() def OnReusableBlocksEditor(self): raise RuntimeError('stop before native UI') class Uia(ModuleType): def __getattr__(self, name): if name == 'Desktop': phase[0] = 'after' return None raise AttributeError(name) monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto')) job = {'operation': 'probe_create_add', 'source': str(source), 'library': str(library), 'name': 'Block', 'timeout_seconds': 0} with pytest.raises(RuntimeError, match='stop before native UI'): native._probe_add_dialog_impl(App(), job, None, set(), root, library=SimpleNamespace(_oleobj_=Raw())) records = [json.loads(line) for line in (root/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8').splitlines()] assert [r['stage'] for r in records] == ['before-pywinauto-import', 'after-pywinauto-import', 'after-source-alignment'] assert records[0]['members']['application.Quit']['dispid'] == 1 assert records[1]['members']['application.Quit']['dispid'] == 2 assert records[2]['paths'] == {'source': str(source), 'pcb': str(pcb), 'library_argument': str(library)} assert 'old.lmc' not in source.read_text()