Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
151 lines
6.5 KiB
Python
151 lines
6.5 KiB
Python
from pathlib import Path
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from cbb_refresh_verify import verify_diagnostics as diagnostics
|
|
|
|
|
|
def pcb_fixture(tmp_path):
|
|
pcb = tmp_path / 'CBB' / 'Work' / 'PCB' / 'CBB.pcb'
|
|
pcb.parent.mkdir(parents=True)
|
|
pcb.write_bytes(b'PCB')
|
|
return pcb
|
|
|
|
|
|
def test_missing_generated_jobprefs_is_warning_not_a_failure(tmp_path):
|
|
pcb = pcb_fixture(tmp_path)
|
|
record = diagnostics.collect_preflight_diagnostics(pcb, tmp_path / 'evidence')
|
|
assert record['non_blocking'] is True
|
|
assert record['directory_writable'] is True
|
|
assert len(record['paths']) == 4
|
|
assert any(item['path'].endswith(str(Path('Work/Layout_Temp/JobPrefsDB.jpf')))
|
|
for item in record['paths'])
|
|
assert len(record['warnings']) == 3
|
|
assert list(pcb.parent.iterdir()) == [pcb] # no fake dirs, lock or prefs files
|
|
assert json.loads((tmp_path/'evidence/diagnostics/preflight.json').read_text('utf-8')) == record
|
|
|
|
|
|
def test_jobprefs_readable_probe_reads_only_one_byte_and_closes_handle(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
prefs = pcb.parent/'Layout/JobPrefsDB.jpf'
|
|
prefs.parent.mkdir()
|
|
prefs.write_bytes(b'real database data')
|
|
record = diagnostics.collect_preflight_diagnostics(pcb, tmp_path/'evidence')
|
|
entry = next(item for item in record['paths'] if item['path'] == str(prefs))
|
|
assert entry['readable'] is True
|
|
assert entry['size_bytes'] == len(b'real database data')
|
|
prefs.rename(prefs.with_suffix('.moved')) # would fail on Windows if handle leaked
|
|
|
|
|
|
def test_unreadable_jobprefs_does_not_prevent_verify(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
prefs = pcb.parent/'Layout/JobPrefsDB.jpf'
|
|
prefs.parent.mkdir()
|
|
prefs.touch()
|
|
original = Path.open
|
|
def deny(path, *args, **kwargs):
|
|
if path == prefs:
|
|
raise PermissionError('simulated network share permission')
|
|
return original(path, *args, **kwargs)
|
|
monkeypatch.setattr(Path, 'open', deny)
|
|
record = diagnostics.collect_preflight_diagnostics(pcb, tmp_path/'evidence')
|
|
assert record['non_blocking'] is True
|
|
assert 'simulated network share permission' in str(record)
|
|
|
|
|
|
def test_writability_failure_is_diagnostic_only(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
def deny(**kwargs):
|
|
raise PermissionError('no create permission')
|
|
monkeypatch.setattr(diagnostics.tempfile, 'NamedTemporaryFile', deny)
|
|
record = diagnostics.collect_preflight_diagnostics(pcb, tmp_path/'evidence')
|
|
assert record['directory_writable'] is False
|
|
assert record['non_blocking'] is True
|
|
assert 'no create permission' in str(record)
|
|
assert pcb.read_bytes() == b'PCB'
|
|
|
|
|
|
def test_native_log_tail_is_bounded_and_preserves_multiline_detail(tmp_path):
|
|
pcb = pcb_fixture(tmp_path)
|
|
source = pcb.parent/'LogFiles/ForwardAnnotation.txt'
|
|
source.parent.mkdir()
|
|
raw = b'x' * 10000 + b'\nERROR: Symbol $1I1\n pin mapping explanation\r\n'
|
|
source.write_bytes(raw)
|
|
record = diagnostics.collect_native_failure_logs(pcb, tmp_path/'evidence', max_bytes=1024)
|
|
item = next(item for item in record['files'] if item['source'] == str(source))
|
|
target = Path(item['evidence'])
|
|
assert item['truncated'] is True
|
|
assert item['original_size_bytes'] == len(raw)
|
|
assert target.read_bytes() == raw[-1024:]
|
|
assert source.read_bytes() == raw
|
|
source.rename(source.with_suffix('.moved'))
|
|
assert (tmp_path/'evidence/diagnostics/native-logs.json').is_file()
|
|
|
|
|
|
def test_native_logs_include_known_root_and_logfiles_paths_without_recursing(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
sources = [pcb.parent/'LogFiles/PCBLogFile.txt', pcb.parent/'ReusableBlock.txt',
|
|
pcb.parent.parent/'ReusableBlock.txt']
|
|
for source in sources:
|
|
source.parent.mkdir(exist_ok=True)
|
|
source.write_text('native message', encoding='utf-8')
|
|
def unexpected(*args, **kwargs):
|
|
pytest.fail('diagnostics must not recursively enumerate a network CBB')
|
|
monkeypatch.setattr(Path, 'rglob', unexpected)
|
|
monkeypatch.setattr(Path, 'glob', unexpected)
|
|
record = diagnostics.collect_native_failure_logs(pcb, tmp_path/'evidence')
|
|
copied = [item for item in record['files'] if item.get('evidence')]
|
|
assert {item['source'] for item in copied} == {str(source) for source in sources}
|
|
assert len({item['evidence'] for item in copied}) == 3
|
|
|
|
|
|
def test_log_access_error_is_recorded_not_raised(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
source = pcb.parent/'LogFiles/PCBLogFile.txt'
|
|
source.parent.mkdir()
|
|
source.touch()
|
|
original = Path.open
|
|
def deny(path, *args, **kwargs):
|
|
if path == source:
|
|
raise PermissionError('native log is locked')
|
|
return original(path, *args, **kwargs)
|
|
monkeypatch.setattr(Path, 'open', deny)
|
|
record = diagnostics.collect_native_failure_logs(pcb, tmp_path/'evidence')
|
|
assert 'native log is locked' in str(record)
|
|
assert record['non_blocking'] is True
|
|
|
|
|
|
def test_local_evidence_write_error_does_not_mask_original_failure(tmp_path, monkeypatch):
|
|
pcb = pcb_fixture(tmp_path)
|
|
original = Path.mkdir
|
|
def deny(path, *args, **kwargs):
|
|
if 'evidence' in path.parts:
|
|
raise PermissionError('evidence unavailable')
|
|
return original(path, *args, **kwargs)
|
|
monkeypatch.setattr(Path, 'mkdir', deny)
|
|
record = diagnostics.collect_native_failure_logs(pcb, tmp_path/'evidence')
|
|
assert 'evidence unavailable' in str(record)
|
|
assert record['non_blocking'] is True
|
|
|
|
|
|
def test_failure_log_helper_has_hard_timeout_and_never_launches_mentor(tmp_path, monkeypatch):
|
|
import subprocess
|
|
pcb = pcb_fixture(tmp_path)
|
|
root = tmp_path/'evidence'
|
|
root.mkdir()
|
|
job_path = root/'request.json'
|
|
job_path.write_text(json.dumps({'root': str(root), 'pcb': str(pcb)}), encoding='utf-8')
|
|
calls = []
|
|
def timed_out(command, **kwargs):
|
|
calls.append((command, kwargs))
|
|
raise subprocess.TimeoutExpired(command, kwargs['timeout'])
|
|
monkeypatch.setattr(diagnostics.subprocess, 'run', timed_out)
|
|
result = diagnostics.capture_failure_logs_with_timeout(job_path, timeout_seconds=2)
|
|
assert result['status'] == 'TIMEOUT'
|
|
assert len(calls) == 1
|
|
assert '--collect-failure-logs' in calls[0][0]
|
|
assert '--verify-worker' not in calls[0][0]
|
|
assert calls[0][1]['timeout'] == 2
|
|
assert (root/'diagnostics/native-log-capture.json').is_file()
|