Files
cbb-refresh-verify-795/tests/test_integration.py
T

575 lines
21 KiB
Python

from pathlib import Path
import hashlib
import os
import pytest
from cbb_refresh_verify.integration import IntegrationError, refresh_integration
from cbb_refresh_verify.mentor_adapter import (pcb_log_reports_integration_error,
save_log_has_core_markers,
build_backup_command, best_effort_backup,
backup_timeout_seconds,
choose_dialog_action, cleanup_evidence_backup,
ActivityDeadline, cleanup_verify_processes,
ensure_pcb_central_library, is_network_backed_path)
from cbb_refresh_verify.library_manager_verify import (_fresh_library_pids, _menu_click_lparam,
_invoke_interactive_verify)
class FakeProjectIntegration:
ForwardAnnotationStatus = 1
SynchCESStatus = 2
LoadCESStatus = 2
BackAnnotationStatus = 2
IsForwardAnnotationAllowed = True
IsSynchCESAllowed = True
IsLoadCESAllowed = True
IsBackAnnotationAllowed = True
forward_calls = 0
def ForwardAnnotate(self):
self.forward_calls += 1
return True
def test_database_restoration_dialog_requires_manual_recovery():
action = choose_dialog_action(
'Database Restoration',
['Restore Database', 'Load the last user saved database dated 2026/9/3 19:32.',
'Load the autosave database dated 2026/9/3 19:32.', 'OK', 'Cancel'],
)
assert action is None
def test_library_manager_fresh_pid_filter_rejects_stale_sessions():
assert _fresh_library_pids({10, 11}, {10, 11, 12, 13}, 12) == {12, 13}
def test_library_manager_menu_click_lparam_is_relative_to_popup_window():
assert _menu_click_lparam((50, 150, 300, 500), (140, 280, 260, 320)) == (150 << 16) | 150
def test_library_manager_menu_click_lparam_accepts_uia_rect_objects():
class Rect:
left = 140
top = 280
right = 260
bottom = 320
assert _menu_click_lparam((50, 150, 300, 500), Rect()) == (150 << 16) | 150
def test_interactive_verify_focuses_library_manager_before_real_right_click():
calls = []
class Target:
def set_focus(self):
calls.append('focus')
class Item:
def select(self):
calls.append('select')
def click_input(self, *, button):
calls.append(('click', button))
_invoke_interactive_verify(Target(), Item())
assert calls == ['focus', 'select', ('click', 'right')]
def test_cleanup_verify_processes_confirms_absent_worker_parent_and_lm(monkeypatch):
from cbb_refresh_verify import mentor_adapter
terminated = []
monkeypatch.setattr(mentor_adapter, '_terminate_pid',
lambda pid, **_kwargs: terminated.append(int(pid)))
monkeypatch.setattr(mentor_adapter, '_process_details', lambda _name: ())
result = cleanup_verify_processes(101, 202, 303, emit=lambda **event: None)
assert terminated == []
assert result['safe_to_continue']
def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeypatch):
from cbb_refresh_verify import mentor_adapter
terminated = []
target = r'S:\Mentor_lib\ReusableBlocksLP\CBB\Work\CBB.pcb'
command = rf'"C:\Mentor\common\ExpeditionPCB.exe" "{target}" -ReusableCircuit'
monkeypatch.setattr(
mentor_adapter,
'_process_details',
lambda _name: [
(202, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 303),
(303, r'C:\Mentor\common\ExpeditionPCB.exe', command, 101),
(204, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 777),
(999, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 0),
],
)
monkeypatch.setattr(mentor_adapter, '_terminate_pid',
lambda pid, **_kwargs: terminated.append(int(pid)))
result = mentor_adapter.cleanup_verify_processes(
101,
None,
None,
expected_pcb=target,
baseline_pids={999},
emit=lambda **event: None,
)
assert terminated == []
assert result['remaining_pids'] == [202, 204, 303]
assert not result['safe_to_continue']
def test_verify_cleanup_is_present_on_all_exit_paths():
from cbb_refresh_verify import mentor_adapter
import inspect
source = inspect.getsource(mentor_adapter.execute_job)
assert 'cleanup_verify_processes(' in source
assert 'expected_pcb=job.get(\'pcb\')' in source
def test_real_verify_execution_no_longer_calls_legacy_dialog_handler():
from cbb_refresh_verify import mentor_adapter
import inspect
source = inspect.getsource(mentor_adapter.execute_job)
assert 'handle_known_dialogs(' not in source
assert 'choose_dialog_action(' not in source
def test_real_verify_execution_uses_observed_reusable_circuit_route():
from cbb_refresh_verify import mentor_adapter
import inspect
source = inspect.getsource(mentor_adapter.execute_job)
assert 'native_lm_verify' in source
assert "job['lmc_file']" in source
assert 'build_pcb_launch_command' in source
assert 'launch_pcb_process' in source
def test_real_verify_keeps_licensing_in_32bit_save_helper():
from cbb_refresh_verify import mentor_adapter
import inspect
source = inspect.getsource(mentor_adapter.execute_job)
assert 'validate_document(' not in source
assert "save_verified_block.vbs" in source
def test_unknown_modal_prefers_safe_positive_action():
action = choose_dialog_action('Expedition PCB', ['Continue', 'Cancel'])
assert action == 'positive'
def test_unknown_modal_uses_fallback_action():
action = choose_dialog_action('Expedition PCB', ['Proceed with design recovery', 'Dismiss'])
assert action == 'fallback'
def test_evidence_backup_is_reclaimed(tmp_path):
backup = tmp_path/'backup'
(backup/'CBB'/'Cert').mkdir(parents=True)
(backup/'CBB'/'Cert'/'sample.prj').write_text('x', encoding='utf-8')
cleanup_evidence_backup(tmp_path)
assert not backup.exists()
def test_activity_deadline_is_extended_by_progress(monkeypatch):
now = [100.0]
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.time.monotonic', lambda: now[0])
deadline = ActivityDeadline(30)
assert not deadline.expired()
now[0] = 120.0
deadline.touch()
now[0] = 129.9
assert not deadline.expired()
assert deadline.remaining() == pytest.approx(20.1, abs=0.1)
now[0] = 149.9
assert not deadline.expired()
now[0] = 150.1
assert deadline.expired()
def test_refresh_integration_stops_on_new_pcb_error_before_action():
pi = FakeProjectIntegration()
with pytest.raises(IntegrationError, match='Job Preferences DB'):
refresh_integration(pi, error_reader=lambda: 'ERROR: Could not load Job Preferences DB')
assert pi.forward_calls == 0
class GreenProjectIntegration:
ForwardAnnotationStatus = 2
SynchCESStatus = 2
LoadCESStatus = 2
BackAnnotationStatus = 2
IsForwardAnnotationAllowed = True
IsSynchCESAllowed = True
IsLoadCESAllowed = True
IsBackAnnotationAllowed = True
def __init__(self):
self.actions = []
def ForwardAnnotate(self):
self.actions.append('ForwardAnnotate')
return True
def SynchCES(self):
self.actions.append('SynchCES')
return True
def LoadCES(self):
self.actions.append('LoadCES')
return True
def BackAnnotate(self):
self.actions.append('BackAnnotate')
return True
def test_refresh_integration_only_refreshes_first_action_when_already_green():
pi = GreenProjectIntegration()
events = []
assert refresh_integration(pi, settle_seconds=0, emit=lambda **e: events.append(e)) == {
'ForwardAnnotation': 2,
'SynchCES': 2,
'LoadCES': 2,
'BackAnnotation': 2,
}
assert pi.actions == ['ForwardAnnotate']
assert [e['action'] for e in events if e['stage'] == 'integration_action'] == pi.actions
def test_refresh_integration_waits_for_action_to_turn_green_before_next_action():
class PendingProjectIntegration(GreenProjectIntegration):
def ForwardAnnotate(self):
super().ForwardAnnotate()
self.ForwardAnnotationStatus = 1
return True
pi = PendingProjectIntegration()
waits = []
def complete_pending_action(_seconds):
assert pi.actions == ['ForwardAnnotate']
waits.append(True)
pi.ForwardAnnotationStatus = 2
refresh_integration(pi, settle_seconds=0, sleep=complete_pending_action)
assert waits
assert pi.actions == ['ForwardAnnotate']
@pytest.mark.parametrize('failure', ['not_allowed', 'returned_false', 'new_error'])
def test_refresh_integration_never_accepts_initial_green_when_refresh_fails(failure):
pi = GreenProjectIntegration()
if failure == 'not_allowed':
pi.ForwardAnnotationStatus = 1
pi.IsForwardAnnotationAllowed = False
elif failure == 'returned_false':
pi.ForwardAnnotate = lambda: False
with pytest.raises(IntegrationError):
refresh_integration(
pi, settle_seconds=0,
error_reader=lambda: 'refresh error' if failure == 'new_error' and pi.actions else '',
)
assert 'SynchCES' not in pi.actions
def test_refresh_integration_skips_native_disabled_green_action():
pi = GreenProjectIntegration()
pi.IsForwardAnnotationAllowed = False
refresh_integration(pi, settle_seconds=0)
assert pi.actions == []
def test_refresh_integration_never_counts_rejected_green_action_as_completed():
class VerifiedProjectIntegration(GreenProjectIntegration):
def _blocked(self, action):
self.actions.append(action)
raise RuntimeError(
f'{action} is not allowed on verified (or once verified) Reusable Blocks.')
def ForwardAnnotate(self): return self._blocked('ForwardAnnotate')
def SynchCES(self): return self._blocked('SynchCES')
def LoadCES(self): return self._blocked('LoadCES')
def BackAnnotate(self): return self._blocked('BackAnnotate')
pi = VerifiedProjectIntegration()
events = []
with pytest.raises(IntegrationError, match='not allowed on verified'):
refresh_integration(pi, settle_seconds=0, emit=lambda **e: events.append(e))
assert pi.actions == ['ForwardAnnotate']
assert not any(e['stage'] == 'integration_complete' for e in events)
def test_refresh_integration_does_not_retry_rejected_action_after_status_changes():
class VerifiedAfterFirstAction(GreenProjectIntegration):
def ForwardAnnotate(self):
self.actions.append('ForwardAnnotate')
self.BackAnnotationStatus = 1
return True
def BackAnnotate(self):
self.actions.append('BackAnnotate')
raise RuntimeError('Back Annotation is not allowed on verified (or once verified) Reusable Blocks.')
pi = VerifiedAfterFirstAction()
with pytest.raises(IntegrationError, match='not allowed on verified'):
refresh_integration(pi, settle_seconds=0, timeout_seconds=None)
assert pi.actions == ['ForwardAnnotate', 'BackAnnotate']
def test_refresh_integration_waits_for_real_state_change_without_internal_timeout():
class SlowProjectIntegration(GreenProjectIntegration):
SynchCESStatus = 2
LoadCESStatus = 2
BackAnnotationStatus = 2
IsForwardAnnotationAllowed = True
def __init__(self):
super().__init__()
self.reads = 0
self.forward_calls = 0
@property
def ForwardAnnotationStatus(self):
self.reads += 1
return 2 if self.reads >= 6 else 1
def ForwardAnnotate(self):
self.forward_calls += 1
return True
pi = SlowProjectIntegration()
result = refresh_integration(pi, settle_seconds=0, timeout_seconds=None,
sleep=lambda _seconds: None)
assert result['ForwardAnnotation'] == 2
assert pi.forward_calls == 1
def test_pcb_log_reports_only_new_integration_errors(tmp_path):
pcb = tmp_path / 'Work' / 'PCB' / 'sample.pcb'
log = pcb.parent / 'LogFiles' / 'ForwardAnnotation.txt'
log.parent.mkdir(parents=True)
log.write_text('No errors in Existing Schematic Packaging.\n', encoding='utf-8')
offset = log.stat().st_size
assert pcb_log_reports_integration_error(pcb, offsets={log: offset}) == ''
with log.open('a', encoding='utf-8') as stream:
stream.write('ERROR: Could not load Job Preferences DB\n')
stream.write('ERROR: Unable to create local Cell Library.\n')
stream.write('DataBase Load is being terminated with 2 errors and 0 warnings.\n')
detail = pcb_log_reports_integration_error(pcb, offsets={log: offset})
assert 'Job Preferences DB' in detail
assert 'local Cell Library' in detail
def test_pcb_log_ignores_nonfatal_packager_compatibility_warnings(tmp_path):
pcb = tmp_path / 'Work' / 'PCB' / 'sample.pcb'
log = pcb.parent / 'LogFiles' / 'PartPkg.log'
log.parent.mkdir(parents=True)
log.write_text(
'Unable to determine the Disable Repackage status.\n'
'Unable to successfully read the Property Definition Editor data.\n'
'No errors found. Proceeding with packaging...\n'
'Packager finished successfully.\n'
'!THE iCDB IS UP-TO-DATE!\n',
encoding='utf-8',
)
assert pcb_log_reports_integration_error(pcb, offsets={log: 0}) == ''
def test_pcb_log_reports_rewritten_same_size_log(tmp_path):
pcb = tmp_path / 'Work' / 'PCB' / 'sample.pcb'
log = pcb.parent / 'LogFiles' / 'ForwardAnnotation.txt'
log.parent.mkdir(parents=True)
old = 'No errors in Existing Schematic Packaging..\n'
log.write_text(old, encoding='utf-8')
baseline = {
'size': log.stat().st_size,
'mtime_ns': log.stat().st_mtime_ns,
'digest': hashlib.sha256(log.read_bytes()).hexdigest(),
}
new = 'ERROR: Unable to create local Cell Library.\n'
assert len(new.encode('utf-8')) == len(old.encode('utf-8'))
log.write_text(new, encoding='utf-8')
detail = pcb_log_reports_integration_error(pcb, offsets={log: baseline})
assert 'local Cell Library' in detail
def test_ensure_pcb_central_library_rewrites_stale_mapped_path(tmp_path):
pcb = tmp_path / 'Work' / 'PCB' / 'sample.pcb'
pcb.parent.mkdir(parents=True)
project = pcb.parent / 'pcb.prj'
project.write_text(
'KEY CentralLibrary "S:\\mentor_lib.lmc"\n'
'KEY CentralLibrary "S:\\mentor_lib.lmc"\n',
encoding='utf-8',
)
lmc = tmp_path / 'mentor_lib.lmc'
lmc.write_text('', encoding='utf-8')
evidence = tmp_path / 'evidence'
changed = ensure_pcb_central_library(pcb, lmc, evidence_dir=evidence)
assert changed is True
relative_lmc = os.path.relpath(lmc, project.parent).replace('/', '\\')
assert project.read_text(encoding='utf-8').count(relative_lmc) == 2
assert (evidence / 'pcb.prj.before-central-library').is_file()
def test_save_log_core_markers_allow_quit_recovery():
assert save_log_has_core_markers(
'LICENSE_OK\nSAVE_OK\nIS_SAVED\nPOST_SAVE_SYNCHRONIZED\nCLOSE_OK\n')
assert not save_log_has_core_markers('LICENSE_OK\nSAVE_OK\nIS_SAVED\n')
def test_backup_command_excludes_volatile_logs_and_databases(tmp_path):
command = build_backup_command(tmp_path/'source', tmp_path/'backup')
assert '/XD' in command and 'database' in command and 'logfiles' in command
assert '/XF' in command and '*.log' in command and '*.lck' in command
def test_backup_timeout_is_non_blocking_and_reported(tmp_path, monkeypatch):
source = tmp_path/'source'
source.mkdir()
terminated = []
class HangingProcess:
returncode = None
def wait(self, timeout):
raise __import__('subprocess').TimeoutExpired('robocopy', timeout)
import cbb_refresh_verify.mentor_adapter as adapter
monkeypatch.setattr(adapter.subprocess, 'Popen', lambda *args, **kwargs: HangingProcess())
monkeypatch.setattr(adapter, '_terminate', lambda process, **kwargs: terminated.append(process))
result = best_effort_backup(source, tmp_path/'backup', timeout_seconds=.1)
assert result['status'] == 'TIMEOUT'
assert terminated
def test_backup_timeout_scales_with_workload_and_is_independent_of_verify_timeout(tmp_path):
source = tmp_path/'source'
source.mkdir()
(source/'small.bin').write_bytes(b'x' * (1 * 1024 * 1024))
small = backup_timeout_seconds(source, verify_timeout_seconds=30)
large = tmp_path/'large'
large.mkdir()
for index in range(1200):
(large/f'{index:04d}.bin').write_bytes(b'x' * (64 * 1024))
large_budget = backup_timeout_seconds(large, verify_timeout_seconds=30)
assert small >= 3
assert large_budget > small
assert large_budget <= 30
assert backup_timeout_seconds(source, verify_timeout_seconds=1800) == small
def test_backup_timeout_falls_back_to_minimum_for_unreadable_source(tmp_path):
missing = tmp_path/'missing'
assert backup_timeout_seconds(missing, verify_timeout_seconds=30) == 3
def test_backup_timeout_ignores_volatile_directories_excluded_from_copy(tmp_path):
source = tmp_path/'source'
(source/'stable').mkdir(parents=True)
(source/'logfiles').mkdir()
(source/'stable'/'small.bin').write_bytes(b'x')
for index in range(1500):
(source/'logfiles'/f'{index:04d}.bin').write_bytes(b'x' * 1024)
budget = backup_timeout_seconds(source)
assert budget == pytest.approx(3.003)
def test_backup_timeout_uses_bounded_kill_budget(tmp_path, monkeypatch):
source = tmp_path/'source'
source.mkdir()
observed = {}
class HangingProcess:
returncode = None
def wait(self, timeout):
raise __import__('subprocess').TimeoutExpired('robocopy', timeout)
import cbb_refresh_verify.mentor_adapter as adapter
monkeypatch.setattr(adapter.subprocess, 'Popen', lambda *args, **kwargs: HangingProcess())
def terminate(process, **kwargs):
observed.update(kwargs)
monkeypatch.setattr(adapter, '_terminate', terminate)
result = best_effort_backup(source, tmp_path/'backup', timeout_seconds=3)
assert result['status'] == 'TIMEOUT'
assert observed['grace_seconds'] == 1
assert observed['kill_timeout_seconds'] <= 5
def test_network_backed_path_detects_unc_and_matching_mapped_drive(tmp_path):
assert is_network_backed_path(r'\\server\share\CBB', mapped_drive='Z:')
assert is_network_backed_path(r'Z:\CBB', mapped_drive='Z:')
assert not is_network_backed_path(tmp_path / 'CBB', mapped_drive='Z:')
def test_green_refresh_only_runs_newly_required_actions():
class NeedsBackAnnotation(GreenProjectIntegration):
def ForwardAnnotate(self):
super().ForwardAnnotate()
self.BackAnnotationStatus = 1
return True
def BackAnnotate(self):
super().BackAnnotate()
self.BackAnnotationStatus = 2
return True
pi = NeedsBackAnnotation()
result = refresh_integration(pi, settle_seconds=0)
assert pi.actions == ['ForwardAnnotate', 'BackAnnotate']
assert set(result.values()) == {2}
def test_initial_required_keeps_existing_full_sequence():
class Required(GreenProjectIntegration):
ForwardAnnotationStatus = 1
def ForwardAnnotate(self):
super().ForwardAnnotate()
self.ForwardAnnotationStatus = 2
return True
pi = Required()
refresh_integration(pi, settle_seconds=0)
assert pi.actions == ['ForwardAnnotate', 'SynchCES', 'LoadCES', 'BackAnnotate']
def test_integration_times_actual_call_and_completion_without_wall_clock():
now = [100.0]
events = []
class SlowForward(GreenProjectIntegration):
def ForwardAnnotate(self):
super().ForwardAnnotate()
now[0] += 341
return True
refresh_integration(SlowForward(), settle_seconds=0, clock=lambda: now[0],
emit=lambda **e: events.append(e))
complete = next(e for e in events if e['stage'] == 'integration_action_complete')
assert complete['elapsed_seconds'] == 341
assert complete['call_elapsed_seconds'] == 341
assert len([e for e in events if e['stage'] == 'integration_status']) == 1