from pathlib import Path from types import SimpleNamespace import subprocess import pytest from cbb_refresh_verify import mentor_adapter as adapter def exercise_worker(tmp_path, monkeypatch, *, integration_error=None): work = tmp_path/'ReusableBlocksLP/CBB/Work' pcb = work/'PCB/CBB.pcb' pcb.parent.mkdir(parents=True) pcb.touch() project = work/'CBB.prj' project.touch() evidence = tmp_path/'evidence' evidence.mkdir() events = [] monkeypatch.setattr(adapter, 'backup_timeout_seconds', lambda _: .1) monkeypatch.setattr(adapter, 'best_effort_backup', lambda *a, **kw: {'status':'SKIPPED'}) monkeypatch.setattr(adapter, 'prepare_icdb_address', lambda *a, **kw: {'status':'ABSENT'}) monkeypatch.setattr(adapter, 'ensure_pcb_central_library', lambda *a, **kw: False) monkeypatch.setattr(adapter, 'pcb_processes', lambda: ()) monkeypatch.setattr(adapter, 'build_pcb_launch_command', lambda *a: ('ExpeditionPCB.exe',)) monkeypatch.setattr(adapter, 'launch_pcb_process', lambda *a: SimpleNamespace(pid=1)) monkeypatch.setattr(adapter, '_process_details', lambda _: ()) monkeypatch.setattr(adapter, 'select_reusable_circuit_worker', lambda *a, **kw: (2,'pcb','command',1)) monkeypatch.setattr(adapter, 'cleanup_verify_processes', lambda *a, **kw: {'safe_to_continue': True, 'remaining_pids': []}) monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', lambda *a, **kw: (None, None, object())) def integration(*args, **kwargs): if integration_error: raise RuntimeError(integration_error) monkeypatch.setattr('cbb_refresh_verify.integration.refresh_integration', integration) # The x86 helper finished; the pre-existing Save/Close gates remain exercised. def save_helper(command, **kw): kw['stdout'].write('LICENSE_OK\nSAVE_OK\nIS_SAVED\nPOST_SAVE_SYNCHRONIZED\nCLOSE_OK\nQUIT_OK\n') kw['stdout'].flush() return SimpleNamespace(pid=3, poll=lambda: 0, returncode=0) monkeypatch.setattr(adapter.subprocess, 'Popen', save_helper) job = {'root':str(evidence),'project':str(project),'pcb':str(pcb), 'lmc_file':str(tmp_path/'lib.lmc'),'block_name':'CBB','timeout':600} return job, events, lambda: adapter.execute_job(job, lambda **e: events.append(e)) def test_missing_cert_cannot_block_saved_and_closed_worker(tmp_path, monkeypatch): job, events, execute = exercise_worker(tmp_path, monkeypatch) result = execute() work = Path(job['project']).parent assert result['status'] == 'PASS' # execution only; parent must still ask bridge assert not (work.parent/'Cert').exists() assert any(e['stage'] == 'saved_and_closed' for e in events) assert any(e['stage'] == 'cert_diagnostics' and e['non_blocking'] for e in events) def test_cert_diagnostic_io_failure_is_nonblocking(tmp_path, monkeypatch): from cbb_refresh_verify.mentor_adapter import collect_cert_evidence original = Path.stat def deny(path, *args, **kwargs): if 'Cert' in path.parts: raise PermissionError('simulated SMB error on Cert') return original(path,*args,**kwargs) monkeypatch.setattr(Path,'stat',deny) result = collect_cert_evidence(tmp_path, tmp_path/'Cert/CBB.prj', tmp_path/'Cert/PCB/CBB.pcb', probe_files=True) assert result['non_blocking'] is True assert 'simulated SMB error' in str(result) def test_worker_records_diagnostics_once_before_pcb_launch(tmp_path, monkeypatch): import json job, events, execute = exercise_worker(tmp_path, monkeypatch) assert execute()['status'] == 'PASS' stages = [event['stage'] for event in events] assert stages.count('preflight_diagnostics') == 1 assert stages.index('preflight_diagnostics') < stages.index('pcb_launch') record = json.loads((Path(job['root'])/'diagnostics/preflight.json').read_text('utf-8')) assert record['non_blocking'] is True assert record['directory_writable'] is True assert not (Path(job['root'])/'diagnostics/native-logs.json').exists() def test_worker_failure_preserves_native_multiline_logs_and_original_error(tmp_path, monkeypatch): job, events, execute = exercise_worker(tmp_path, monkeypatch, integration_error='Project Integration error: symbol $1I1') source = Path(job['pcb']).parent/'LogFiles/ForwardAnnotation.txt' source.parent.mkdir() source.write_bytes(b'ERROR: symbol $1I1\n pin mapping detail\n') with pytest.raises(RuntimeError, match='Project Integration error: symbol'): execute() target = Path(job['root'])/'diagnostics/native-logs/pcb-logfiles-ForwardAnnotation.txt' assert target.read_bytes() == source.read_bytes() assert any(event['stage'] == 'native_failure_logs' for event in events) def test_default_cert_diagnostics_never_stat_network_files(tmp_path, monkeypatch): original = Path.stat def unexpected(path, *args, **kwargs): if 'Cert' in path.parts: pytest.fail('Cert stat must not delay successful Save/Close on SMB') return original(path,*args,**kwargs) monkeypatch.setattr(Path,'stat',unexpected) result=adapter.collect_cert_evidence(tmp_path,tmp_path/'Cert/CBB.prj',tmp_path/'Cert/PCB/CBB.pcb') assert result['non_blocking'] is True assert result['probed'] is False assert result['project_exists'] is None