Files
cbb-refresh-verify-795/tests/test_pcb_bridge.py

86 lines
2.8 KiB
Python

import pytest
from cbb_refresh_verify.pcb_bridge import (
PcbBridgeError,
require_document_identity,
wait_for_target_document,
)
class ProjectIntegration:
def __init__(self, project):
self.ProjectFile = str(project)
class Document:
def __init__(self, name, project):
self.Name = name
self.ProjectIntegration = ProjectIntegration(project)
class Application:
def __init__(self, document):
self.ActiveDocument = document
def job(tmp_path):
return {
'project': str(tmp_path / 'Work' / 'CBB.prj'),
'pcb': str(tmp_path / 'Work' / 'PCB' / 'CBB.pcb'),
}
def test_bridge_rejects_wrong_document_before_actions(tmp_path):
current = Document('OTHER.pcb', tmp_path / 'Work' / 'OTHER.prj')
with pytest.raises(PcbBridgeError, match='身份不符') as raised:
require_document_identity(current, job(tmp_path))
assert raised.value.error_code == 'PCB_IDENTITY_MISMATCH'
def test_bridge_attaches_through_com_without_window_title_gate(tmp_path):
expected = Document('CBB.pcb', tmp_path / 'Work' / 'CBB.prj')
attempts = iter((RuntimeError('not ready'), Application(expected)))
events = []
def provider():
value = next(attempts)
if isinstance(value, Exception):
raise value
return value
app, document, integration = wait_for_target_document(
job(tmp_path), provider, process_alive=lambda: True,
emit=lambda **event: events.append(event), sleep=lambda _seconds: None,
)
assert app.ActiveDocument is document
assert integration.ProjectFile.endswith('CBB.prj')
assert not any(event['stage'] == 'com_wait' for event in events)
assert events[-1]['stage'] == 'identity_verified'
def test_bridge_stops_if_pcb_exits_before_com_is_ready(tmp_path):
alive = iter((True, False))
with pytest.raises(PcbBridgeError) as raised:
wait_for_target_document(
job(tmp_path), lambda: (_ for _ in ()).throw(RuntimeError('not ready')),
process_alive=lambda: next(alive), sleep=lambda _seconds: None,
)
assert raised.value.error_code == 'PCB_EXITED_BEFORE_COM'
def test_connected_com_without_document_remains_startup_and_checks_native_failure(tmp_path):
errors = iter(('', 'ICDB_REPAIR_REQUIRED: original lost session'))
events = []
with pytest.raises(PcbBridgeError) as raised:
wait_for_target_document(job(tmp_path), lambda: Application(None), process_alive=lambda: True,
error_reader=lambda: next(errors), emit=lambda **e: events.append(e), sleep=lambda _: None)
assert raised.value.error_code == 'PCB_STARTUP_FAILED'
assert 'original lost session' in str(raised.value)
assert [e['stage'] for e in events] == ['pcb_document_wait']
assert events[0]['error_code'] == 'PCB_DOCUMENT_NOT_READY'