Files

1049 lines
44 KiB
Python

from pathlib import Path
from threading import Event
import json
import pytest
from cbb_refresh_verify.verify_runner import (VerifyRequest, VerifyResult, build_content_check_command,
classify_output, contents_report_path, preflight,
phase_for_event, preflight_skip_reason, resolve_importer, run_batch, run_verify,
write_batch_report,
apply_post_verify_status)
from cbb_refresh_verify.status_bridge import BridgeResult
from cbb_refresh_verify.dialog_models import ControlSnapshot, DialogPhase, WindowSnapshot
from cbb_refresh_verify.mentor_adapter import reset_verify_activity_deadline, pcb_startup_timeout_seconds
from cbb_refresh_verify.verify_runner import classify_verify_dispatch
@pytest.fixture(autouse=True)
def no_real_mentor_status_bridge(monkeypatch):
monkeypatch.setattr('cbb_refresh_verify.verify_runner.read_library_flag_if_available',
lambda *_: BridgeResult('READY', 'IsVerifiedReuseBlock', 1))
def request(tmp_path, name='CBB1', **overrides):
values = dict(lmc_file=tmp_path/'lib.lmc', block_name=name, evidence_dir=tmp_path/'evidence', timeout_seconds=10)
values.update(overrides)
return VerifyRequest(**values)
def fixture(tmp_path):
(tmp_path/'lib.lmc').touch()
work = tmp_path/'ReusableBlocksLP'/'CBB1'/'Work'
(work/'PCB'/'Layout').mkdir(parents=True)
(work/'CBB1.prj').write_text('KEY PCBDesignPath "PCB\\actual.pcb"')
(work/'PCB'/'actual.pcb').write_text('pcb')
(work/'PCB'/'Layout'/'LayoutDB.lyt').write_bytes(b'data')
return work
def fake_process(exit_code=0, stdout='', stderr='', timed_out=False):
def runner(command, timeout_seconds, cwd):
return exit_code, stdout, stderr, timed_out
return runner
def test_verify_request_default_timeout_is_600_seconds(tmp_path):
request_value = VerifyRequest(tmp_path/'lib.lmc', 'CBB1', tmp_path/'evidence')
assert request_value.timeout_seconds == 600
assert request_value.interactive_input is False
def test_interactive_verify_mode_is_explicitly_serialized_into_worker_job(tmp_path, monkeypatch):
fixture(tmp_path)
captured = {}
class FinishedWorker:
desktop = None
returncode = 0
def __init__(self, command, name, **kwargs):
captured.update(json.loads(Path(command[-1]).read_text(encoding='utf-8')))
Path(command[-1]).parent.joinpath('outcome.json').write_text(
'{"status":"PASS","stdout":"test"}', encoding='utf-8')
def poll(self):
return 0
def close(self):
pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FinishedWorker)
result = run_verify(
request(tmp_path, interactive_input=True),
port_check=PORTS_OK,
)
assert result.status == 'PASS'
assert captured['interactive_input'] is True
def test_native_lm_verify_mode_is_serialized_into_worker_job(tmp_path, monkeypatch):
fixture(tmp_path)
captured = {}
class FinishedWorker:
desktop = None
returncode = 0
def __init__(self, command, name, **kwargs):
captured.update(json.loads(Path(command[-1]).read_text(encoding='utf-8')))
Path(command[-1]).parent.joinpath('outcome.json').write_text(
'{"status":"PASS","stdout":"test"}', encoding='utf-8')
def poll(self):
return 0
def close(self):
pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FinishedWorker)
result = run_verify(
request(tmp_path, native_lm_verify=True),
port_check=PORTS_OK,
)
assert result.status == 'PASS'
assert captured['native_lm_verify'] is True
def test_interactive_worker_uses_the_current_input_desktop_only_when_enabled():
from cbb_refresh_verify.desktop_worker import startup_desktop_name
assert startup_desktop_name('CbbVerify_test', interactive_input=False) == 'winsta0\\CbbVerify_test'
assert startup_desktop_name('CbbVerify_test', interactive_input=True) is None
def test_verify_click_starts_a_fresh_activity_window():
assert reset_verify_activity_deadline(100.0, 130.0, timeout_seconds=30) == 160.0
assert reset_verify_activity_deadline(100.0, 110.0, timeout_seconds=30) == 140.0
def test_pcb_startup_gets_bounded_com_registration_grace():
assert pcb_startup_timeout_seconds(30) == 60
assert pcb_startup_timeout_seconds(90) == 90
def test_verify_dispatch_is_classified_when_menu_action_returns_without_pcb():
result = classify_verify_dispatch(
verify_invoked=True,
invoke_returned=True,
target_pid=None,
observation={'processes': [], 'windows': []},
)
assert result == (
'FAIL',
'VERIFY_DISPATCH_FAILED: LMC Verify 菜单调用已返回,但未启动目标 ReusableCircuit PCB',
)
def test_verify_dispatch_is_not_failed_before_invoke_returns():
assert classify_verify_dispatch(
verify_invoked=True,
invoke_returned=False,
target_pid=None,
observation={},
) is None
def test_supervisor_cleanup_reaps_orphaned_session_processes(tmp_path, monkeypatch):
import cbb_refresh_verify.verify_runner as runner_module
calls = []
def cleanup(*args, **kwargs):
calls.append((args, kwargs))
kwargs['emit'](stage='process_cleanup', role='pcb_worker', pid=22)
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.cleanup_verify_processes', cleanup)
events = []
runner_module._cleanup_session_processes(
tmp_path,
lm_pid=11,
target_pid=None,
pcb=r'S:\lib\CBB\Work\CBB.pcb',
pcb_baseline={99},
progress=events.append,
)
assert calls == [((11, None, None), {
'expected_pcb': r'S:\lib\CBB\Work\CBB.pcb',
'baseline_pids': {99},
'emit': calls[0][1]['emit'],
})]
assert events[0]['stage'] == 'process_cleanup'
assert 'process_cleanup' in (tmp_path/'events.jsonl').read_text(encoding='utf-8')
def test_runtime_fingerprint_records_os_and_python_bitness(monkeypatch):
import cbb_refresh_verify.verify_runner as runner_module
monkeypatch.setattr(runner_module.platform, 'platform', lambda: 'Windows-test')
result = runner_module.runtime_fingerprint(r'C:\Mentor\SDD_HOME')
assert result['platform'] == 'Windows-test'
assert result['python_bitness'] in (32, 64)
assert result['mentor_bin'] == r'C:\Mentor\SDD_HOME'
def test_windows_build_family_uses_build_number_not_product_name():
import cbb_refresh_verify.verify_runner as runner_module
assert runner_module.windows_build_family('19045') == 'Windows 10'
assert runner_module.windows_build_family('22000') == 'Windows 11'
assert runner_module.windows_build_family('26200') == 'Windows 11'
assert runner_module.windows_build_family('') == 'unknown'
def test_native_lm_verify_environment_switch_is_explicit(monkeypatch):
import cbb_refresh_verify.verify_runner as runner_module
monkeypatch.delenv('CBB_NATIVE_LM_VERIFY', raising=False)
assert runner_module.native_lm_verify_enabled() is False
monkeypatch.setenv('CBB_NATIVE_LM_VERIFY', '1')
assert runner_module.native_lm_verify_enabled() is True
monkeypatch.setenv('CBB_NATIVE_LM_VERIFY', '0')
assert runner_module.native_lm_verify_enabled() is False
def test_preflight_uses_project_pcb_reference_not_block_name(tmp_path):
work = fixture(tmp_path)
assert preflight(request(tmp_path)) == (work/'CBB1.prj', work/'PCB'/'actual.pcb')
def test_preflight_uses_canonical_paths_for_safety_but_preserves_mentor_path(tmp_path, monkeypatch):
work = fixture(tmp_path)
monkeypatch.setattr(
Path,
'resolve',
lambda self: Path(str(self).replace(str(tmp_path), str(tmp_path/'canonical-share'))),
)
assert preflight(request(tmp_path)) == (work/'CBB1.prj', work/'PCB'/'actual.pcb')
def test_missing_pcb_logged_without_starting_worker(tmp_path):
fixture(tmp_path)
(tmp_path/'ReusableBlocksLP'/'CBB1'/'Work'/'PCB'/'actual.pcb').unlink()
result = run_verify(request(tmp_path))
assert result.status == 'BLOCKED'
assert 'PCB' in result.stderr
assert result.evidence_path.is_file()
def test_preflight_rejects_illegal_block_name(tmp_path):
fixture(tmp_path)
with pytest.raises(ValueError):
preflight(request(tmp_path, '../outside'))
def test_preflight_skip_reason_identifies_import_incomplete_cbb(tmp_path):
(tmp_path / 'lib.lmc').touch()
work = tmp_path / 'ReusableBlocksLP' / 'BROKEN_CBB' / 'Work'
work.mkdir(parents=True)
reason = preflight_skip_reason(tmp_path / 'lib.lmc', 'BROKEN_CBB')
assert reason and '缺少或无法唯一确定 Work 下的工程 PRJ' in reason
def test_preflight_skip_reason_is_empty_for_valid_cbb(tmp_path):
work = fixture(tmp_path)
assert preflight_skip_reason(tmp_path / 'lib.lmc', 'CBB1') == ''
def test_content_check_command_matches_documented_invocation(tmp_path):
fixture(tmp_path)
built = build_content_check_command(request(tmp_path, libspec='EE7.9', dms_login_config='login.conf'),
Path('ReuseBlockImporter.exe'))
assert built == ('ReuseBlockImporter.exe', '-lmcFile', str(tmp_path/'lib.lmc'), '-blockName', 'CBB1',
'-logicalType', 'LP', '-chkContents', '-libSpec', 'EE7.9', '-dmsLoginConfig', 'login.conf')
def test_contents_report_path_matches_mentor_safe_name(tmp_path):
report = contents_report_path(tmp_path/'lib.lmc', 'A:B/C?')
assert report.name == 'restorerb_RULP_A_B_C__contents.log'
assert report.parent == tmp_path/'LogFiles'
def test_success_requires_success_text_and_no_error_marker():
assert classify_output(0, "Reuse Block's content checking completed successfully!") == 'PASS'
assert classify_output(0, "completed successfully\nERROR : Can't check the RB contents in DMS.") == 'FAIL'
assert classify_output(1, 'Login config switch is empty!') == 'FAIL'
assert classify_output(0, 'nothing recognisable') == 'UNKNOWN'
assert classify_output(None, 'completed successfully', timed_out=True) == 'TIMEOUT'
PORTS_OK = lambda: (True, '端口 10000 可用') # noqa: E731
def test_content_check_failure_blocks_before_launching_the_pcb(tmp_path, monkeypatch):
fixture(tmp_path)
monkeypatch.setattr('cbb_refresh_verify.verify_runner.resolve_importer', lambda mentor_bin=None: Path('ReuseBlockImporter.exe'))
launched = []
import cbb_refresh_verify.desktop_worker as dw; monkeypatch.setattr(dw, 'DesktopWorker', lambda *a, **k: launched.append(a))
result = run_verify(request(tmp_path, libspec='EE7.9', dms_login_config='login.conf'),
process_runner=fake_process(1, "ERROR : Cannot connect to the DMS."),
port_check=PORTS_OK)
assert result.status == 'FAIL'
assert launched == []
assert 'Cannot connect to the DMS' in result.stderr
assert result.report_path.is_file()
def test_content_check_is_skipped_without_libspec_or_login_config(tmp_path, monkeypatch):
fixture(tmp_path)
monkeypatch.setattr('cbb_refresh_verify.verify_runner.resolve_importer',
lambda mentor_bin=None: pytest.fail('内容检查不应在未配置时运行'))
import cbb_refresh_verify.desktop_worker as dw
monkeypatch.setattr(dw, 'DesktopWorker',
lambda *a, **k: pytest.fail('已进入 PCB 刷新阶段(内容检查被正确跳过)'))
# Without configuration the runner proceeds straight to the worker stage,
# which the fake worker intercepts; a BLOCKED/FAIL result would mean the
# importer path ran instead.
with pytest.raises(BaseException, match='已进入 PCB 刷新阶段'):
run_verify(request(tmp_path), port_check=PORTS_OK)
def test_importer_resolution_prefers_configured_bin(tmp_path):
binary = tmp_path/'bin'
binary.mkdir()
(binary/'ReuseBlockImporter.exe').write_bytes(b'x')
assert resolve_importer(str(binary)) == binary/'ReuseBlockImporter.exe'
def test_importer_resolution_accepts_configured_executable(tmp_path):
binary = tmp_path / 'bin'
binary.mkdir()
executable = binary / 'ReuseBlockImporter.exe'
executable.write_bytes(b'x')
assert resolve_importer(str(executable)) == executable
def test_pcb_launch_uses_configured_sdd_home(tmp_path):
sdd_home = tmp_path / 'SDD_HOME'
common_executable = sdd_home / 'common' / 'win32' / 'bin' / 'ExpeditionPCB.exe'
wg_executable = sdd_home / 'wg' / 'win32' / 'bin' / 'ExpeditionPCB.exe'
common_executable.parent.mkdir(parents=True)
wg_executable.parent.mkdir(parents=True)
common_executable.write_bytes(b'x')
wg_executable.write_bytes(b'x')
from cbb_refresh_verify.mentor_adapter import build_pcb_launch_command
command = build_pcb_launch_command(tmp_path / 'actual.pcb', str(sdd_home / 'lm' / 'win32' / 'bin'))
assert command == (str(common_executable), str(tmp_path / 'actual.pcb'), '-ReusableCircuit')
def test_pcb_launch_preserves_lmc_reusable_circuit_arguments(tmp_path):
from cbb_refresh_verify.mentor_adapter import build_pcb_launch_command
command = build_pcb_launch_command(tmp_path / 'actual.pcb')
assert command[2:] == ('-ReusableCircuit',)
assert '-CallingApp=exppcb' not in command
assert '-automated' not in command
assert '-suppressmsgbox' not in command
def test_pcb_process_selection_prefers_wg_child_over_common_parent():
from cbb_refresh_verify.mentor_adapter import select_reusable_circuit_pcb
candidates = [
(100, r'D:\Mentor\7.9.5EE\SDD_HOME\common\win32\bin\ExpeditionPCB.exe', 'common'),
(101, r'D:\Mentor\7.9.5EE\SDD_HOME\wg\win32\bin\ExpeditionPCB.exe', 'worker'),
]
assert select_reusable_circuit_pcb(candidates) == candidates[1]
def test_pcb_process_selection_rejects_multiple_wg_children():
from cbb_refresh_verify.mentor_adapter import select_reusable_circuit_pcb
candidates = [
(101, r'D:\Mentor\7.9.5EE\SDD_HOME\wg\win32\bin\ExpeditionPCB.exe', 'worker-a'),
(102, r'D:\Mentor\7.9.5EE\SDD_HOME\wg\win32\bin\ExpeditionPCB.exe', 'worker-b'),
]
with pytest.raises(RuntimeError, match='多个 wg PCB'):
select_reusable_circuit_pcb(candidates)
def test_pcb_worker_selection_binds_launcher_child_when_commandline_is_incomplete():
from cbb_refresh_verify.mentor_adapter import select_reusable_circuit_worker
expected = r'S:\Mentor_lib_EE7.9_20250521\ReusableBlocksLP\BOB_Shield\Work\PCB\BOB_Shield.pcb'
records = [
(12588, r'C:\MentorGraphics\7.9.5EE\SDD_HOME\common\win32\bin\ExpeditionPCB.exe',
f'ExpeditionPCB.exe "{expected}" -ReusableCircuit', 0),
# This is the process that owns the license dialog in the latest data;
# some installations omit the PCB argument from the child command line.
(13132, r'C:\MentorGraphics\7.9.5EE\SDD_HOME\common\win32\bin\ExpeditionPCB.exe',
'ExpeditionPCB.exe', 12588),
]
selected = select_reusable_circuit_worker(
records, baseline_pids=set(), launcher_pid=12588, expected_pcb=expected)
assert selected[0] == 13132
assert selected[3] == 12588
def test_pcb_worker_selection_does_not_bind_unrelated_new_expedition_process():
from cbb_refresh_verify.mentor_adapter import select_reusable_circuit_worker
expected = r'S:\Mentor_lib_EE7.9_20250521\ReusableBlocksLP\BOB_Shield\Work\PCB\BOB_Shield.pcb'
records = [
(12588, r'C:\MentorGraphics\7.9.5EE\SDD_HOME\common\win32\bin\ExpeditionPCB.exe',
f'ExpeditionPCB.exe "{expected}" -ReusableCircuit', 0),
(13132, r'C:\MentorGraphics\7.9.5EE\SDD_HOME\common\win32\bin\ExpeditionPCB.exe',
'ExpeditionPCB.exe', 99999),
]
assert select_reusable_circuit_worker(
records, baseline_pids=set(), launcher_pid=12588, expected_pcb=expected) is None
def test_library_manager_verify_command_preserves_formal_entrypoint(tmp_path):
from cbb_refresh_verify.mentor_adapter import build_library_manager_verify_command
sdd_home = tmp_path / 'SDD_HOME'
executable = sdd_home / 'lm' / 'win32' / 'bin' / 'LibraryManager.exe'
executable.parent.mkdir(parents=True)
executable.write_bytes(b'x')
library = tmp_path / 'mentor_lib.lmc'
library.write_bytes(b'lmc')
command = build_library_manager_verify_command(library, str(sdd_home / 'wg' / 'win32' / 'bin'))
assert command[0] == str(executable)
assert command[1:] == (str(library),)
def test_pcb_process_keeps_private_desktop_without_no_window_flag(tmp_path, monkeypatch):
from cbb_refresh_verify import mentor_adapter
calls = []
class Process:
pid = 123
monkeypatch.setattr(
mentor_adapter.subprocess,
'Popen',
lambda *args, **kwargs: calls.append((args, kwargs)) or Process(),
)
mentor_adapter.launch_pcb_process(('ExpeditionPCB.exe', 'sample.pcb'), tmp_path)
assert calls and calls[0][1]['cwd'] == str(tmp_path)
assert 'creationflags' not in calls[0][1]
def test_importer_resolution_failure_is_blocked(tmp_path, monkeypatch):
fixture(tmp_path)
monkeypatch.setattr('cbb_refresh_verify.verify_runner.resolve_importer',
lambda mentor_bin=None: (_ for _ in ()).throw(ValueError('ReuseBlockImporter.exe 未找到')))
result = run_verify(request(tmp_path, libspec='EE7.9', dms_login_config='login.conf'))
assert result.status == 'BLOCKED' and 'ReuseBlockImporter' in result.stderr
def test_batch_continues_past_item_failure_and_logs_all_items(tmp_path):
calls = []
outcomes = [VerifyResult('BLOCKED', stderr='missing PCB'), VerifyResult('FAIL', stderr='integration failed'), VerifyResult('PASS')]
def verify(req):
calls.append(req.block_name)
return outcomes.pop(0)
results = run_batch([request(tmp_path, n) for n in ['A','B','C']], verifier=verify)
assert calls == ['A','B','C']
assert [r.status for r in results] == ['BLOCKED','FAIL','PASS']
def test_batch_stops_for_unsafe_timeout_failure(tmp_path):
calls = []
outcomes = [VerifyResult('TIMEOUT', stderr='PCB remains open', safe_to_continue=False), VerifyResult('PASS')]
def verify(req):
calls.append(req.block_name)
return outcomes.pop(0)
results = run_batch([request(tmp_path, n) for n in ['A','B']], verifier=verify)
assert calls == ['A']
assert [r.status for r in results] == ['TIMEOUT','SKIPPED']
def test_batch_stops_when_verifier_raises(tmp_path):
calls = []
def verify(req):
calls.append(req.block_name)
if req.block_name == 'A':
raise RuntimeError('unexpected worker error')
return VerifyResult('PASS')
results = run_batch([request(tmp_path, n) for n in ['A', 'B']], verifier=verify)
assert calls == ['A']
assert results[0].status == 'FAIL'
assert 'unexpected worker error' in results[0].stderr
assert results[1].status == 'SKIPPED'
def test_batch_cancel_between_items(tmp_path):
stop = Event()
def verify(req):
stop.set()
return VerifyResult('PASS')
results = run_batch([request(tmp_path,'A'),request(tmp_path,'B')], verifier=verify, stop=stop)
assert [r.status for r in results] == ['PASS','SKIPPED']
def test_parallelism_rejected_for_shared_library(tmp_path):
with pytest.raises(ValueError, match='serial'):
run_batch([], max_workers=2)
def test_timeout_without_release_evidence_preserves_native_caller(tmp_path, monkeypatch):
fixture(tmp_path)
killed = []
class FakeWorker:
def __init__(self, command, name):
self.name = name
self.desktop = None
def poll(self): return self.returncode
returncode = None
def kill(self): killed.append(True); self.returncode = 1
def wait(self, timeout=5): return 0
def close(self): pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
result = run_verify(VerifyRequest(tmp_path/'lib.lmc', 'CBB1', tmp_path/'evidence', timeout_seconds=0),
port_check=PORTS_OK)
assert result.status == 'TIMEOUT'
assert killed == []
assert result.safe_to_continue is False
def test_fatal_dialog_preserves_current_worker_and_cannot_become_pass(tmp_path, monkeypatch):
fixture(tmp_path)
killed = []
class FakeWorker:
desktop = object()
returncode = None
def __init__(self, command, name): pass
def poll(self): return self.returncode
def kill(self): killed.append(True); self.returncode = 1
def wait(self, timeout=5): return 1
def close(self): pass
class FatalSupervisor:
def poll(self):
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
return DialogPollResult(
activity=True,
action_sent=True,
error_code='SAVE_REUSABLE_BLOCK_FAILED',
detail='Error saving the Reusable Block',
)
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
result = run_verify(
request(tmp_path),
port_check=PORTS_OK,
dialog_supervisor_factory=lambda **kwargs: FatalSupervisor(),
)
assert result.status == 'FAIL'
assert result.stderr.startswith('SAVE_REUSABLE_BLOCK_FAILED')
assert killed == []
def test_worker_events_advance_dialog_phase():
assert phase_for_event({'stage': 'lm_started'}) is DialogPhase.PCB_STARTING
assert phase_for_event({'stage': 'verify_invoke'}) is DialogPhase.PCB_STARTING
assert phase_for_event({'stage': 'pcb_launch'}) is DialogPhase.PCB_STARTING
assert phase_for_event({'stage': 'pcb_started'}) is DialogPhase.PCB_STARTING
assert phase_for_event({'stage': 'identity_verified'}) is DialogPhase.IDENTITY_CHECK
assert phase_for_event({'stage': 'integration_status'}) is DialogPhase.PROJECT_INTEGRATION
assert phase_for_event({'stage': 'save_start'}) is DialogPhase.SAVE
assert phase_for_event({'stage': 'saved_and_closed'}) is DialogPhase.CLOSE
def test_batch_report_names_native_library_manager_verify(tmp_path):
req = request(tmp_path, 'CBB1')
path = write_batch_report([req], [VerifyResult('FAIL', stderr='x')], tmp_path/'reports')
assert 'LMC 原生 Verify' in path.read_text(encoding='utf-8-sig')
def test_unknown_dialog_writes_snapshot_before_current_item_fails(tmp_path, monkeypatch):
fixture(tmp_path)
killed = []
unknown = WindowSnapshot(
hwnd=900,
pid=901,
process_name='ExpeditionPCB.exe',
process_role='pcb',
window_class='GXWND',
title='Unknown recovery prompt',
phase=DialogPhase.PCB_STARTING,
controls=(ControlSnapshot(902, 1, 'Button', 'OK', control_type='Button'),),
owner_hwnd=899,
)
class FakeWorker:
desktop = object()
pid = 700
companion_pids = [701]
returncode = None
def __init__(self, command, name): self.name = name
def poll(self): return self.returncode
def kill(self): killed.append(True); self.returncode = 1
def wait(self, timeout=5): return 1
def close(self): pass
class UnknownSupervisor:
def poll(self, phase):
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
return DialogPollResult(
activity=True,
error_code='UNKNOWN_DIALOG',
detail='Unknown recovery prompt',
snapshot=unknown,
observed_snapshots=(unknown,),
)
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
result = run_verify(
request(tmp_path),
port_check=PORTS_OK,
dialog_supervisor_factory=lambda **kwargs: UnknownSupervisor(),
)
evidence = result.evidence_path.parent
records = (evidence/'window_snapshots.jsonl').read_text(encoding='utf-8')
dialog_records = (evidence/'dialogs.jsonl').read_text(encoding='utf-8')
assert result.status == 'FAIL'
assert 'UNKNOWN_DIALOG' in result.stderr
assert 'Unknown recovery prompt' in records
assert '"error_code": "UNKNOWN_DIALOG"' in dialog_records
assert 'Unknown recovery prompt' in dialog_records
assert (evidence/'process_tree.json').is_file()
assert killed == []
def test_library_flag_is_reported_verbatim_and_never_fabricated(tmp_path, monkeypatch):
fixture(tmp_path)
class FakeWorker:
def __init__(self, command, name): self.desktop = None
def poll(self): return 0
returncode = 0
def kill(self): pass
def wait(self, timeout=5): return 0
def close(self): pass
import cbb_refresh_verify.desktop_worker as desktop_worker
class FakeWorkerWriting(FakeWorker):
def __init__(self, command, name):
super().__init__(command, name)
root = Path(command[-1]).parent
(root/'outcome.json').write_text('{"status": "PASS", "stdout": "ok"}', encoding='utf-8')
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorkerWriting)
result = run_verify(VerifyRequest(tmp_path/'lib.lmc', 'CBB1', tmp_path/'evidence', timeout_seconds=5),
flag_reader=lambda library, name: ('UNKNOWN', 'Library Manager 窗口未就绪'),
port_check=PORTS_OK)
assert result.status == 'FAIL'
assert result.library_flag == 'UNKNOWN'
assert '未就绪' in result.library_flag_detail
def test_verify_worker_receives_mapped_drive_context(tmp_path, monkeypatch):
fixture(tmp_path)
captured = {}
class FakeWorker:
returncode = 0
desktop = None
def __init__(self, command, name):
captured.update(json.loads(Path(command[-1]).read_text(encoding='utf-8')))
root = Path(command[-1]).parent
(root/'outcome.json').write_text('{"status":"PASS","stdout":"ok"}', encoding='utf-8')
def poll(self): return 0
def kill(self): pass
def wait(self, timeout=5): return 0
def close(self): pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
result = run_verify(
VerifyRequest(tmp_path/'lib.lmc', 'CBB1', tmp_path/'evidence', timeout_seconds=5,
mapped_drive='S:', mapped_remote_root=r'\\server\share'),
port_check=PORTS_OK,
)
assert result.status == 'PASS'
assert captured['mapped_drive'] == 'S:'
assert captured['mapped_remote_root'] == r'\\server\share'
def test_post_verify_pending_overrides_execution_pass():
result = apply_post_verify_status(
VerifyResult('PASS', stdout='saved'),
BridgeResult('PENDING', 'IsVerifiedReuseBlock', 0),
)
assert result.status == 'FAIL'
assert result.library_flag == 'NOT_GREEN'
assert 'PENDING' in result.stderr
def test_post_verify_ready_confirms_execution_pass():
result = apply_post_verify_status(
VerifyResult('PASS', stdout='saved'),
BridgeResult('READY', 'IsVerifiedReuseBlock', 1),
)
assert result.status == 'PASS'
assert result.library_flag == 'GREEN'
def test_unknown_library_flag_probe_cannot_confirm_success(tmp_path, monkeypatch):
fixture(tmp_path)
class FakeWorker:
def __init__(self, command, name):
self.desktop = None
Path(command[-1]).parent.joinpath('outcome.json').write_text('{"status": "PASS", "stdout": "ok"}', encoding='utf-8')
def poll(self): return 0
returncode = 0
def kill(self): pass
def wait(self, timeout=5): return 0
def close(self): pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
events = []
result = run_verify(request(tmp_path), flag_reader=lambda *_: ('UNKNOWN', '状态读取失败: EnumDesktopWindows'),
port_check=PORTS_OK, progress=events.append)
assert result.status == 'FAIL'
assert result.library_flag == 'UNKNOWN'
def test_reserved_design_server_ports_block_the_item_fast(tmp_path):
fixture(tmp_path)
result = run_verify(request(tmp_path, strict_port_check=True), port_check=lambda: (False, '端口区间被系统保留'))
assert result.status == 'BLOCKED'
assert '保留' in result.stderr
assert result.evidence_path.is_file()
def test_reserved_design_server_ports_block_only_in_explicit_strict_mode(tmp_path, monkeypatch):
fixture(tmp_path)
class FakeWorker:
def __init__(self, command, name):
pytest.fail('端口被保留时不应启动 PCB worker')
def poll(self): return 0
returncode = 0
def close(self): pass
import cbb_refresh_verify.desktop_worker as desktop_worker
monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker)
result = run_verify(request(tmp_path, strict_port_check=True), port_check=lambda: (False, '端口区间被系统保留'),
progress=lambda event: None)
assert result.status == 'BLOCKED'
assert '保留' in result.stderr
def test_port_check_probe_reports_blocked_range(monkeypatch):
from cbb_refresh_verify import mentor_adapter
import socket
class BlockedSocket:
def bind(self, address): raise OSError(10013, 'WSAEACCES')
def close(self): pass
monkeypatch.setattr(socket, 'socket', lambda *a, **k: BlockedSocket())
ok, detail = mentor_adapter.icdb_ports_bindable()
assert ok is False and 'net stop winnat' in detail
def test_port_check_probe_accepts_a_free_port(monkeypatch):
from cbb_refresh_verify import mentor_adapter
import socket
class FreeSocket:
def bind(self, address): pass
def close(self): pass
monkeypatch.setattr(socket, 'socket', lambda *a, **k: FreeSocket())
ok, detail = mentor_adapter.icdb_ports_bindable()
assert ok is True and '可用' in detail
def test_pcb_log_probe_ignores_stale_server_error(tmp_path):
from cbb_refresh_verify.mentor_adapter import pcb_log_reports_missing_server
log = tmp_path / 'LogFiles' / 'PCBLogFile.txt'
log.parent.mkdir()
log.write_text('Could not run server\n', encoding='utf-8')
offset = log.stat().st_size
assert pcb_log_reports_missing_server(tmp_path / 'actual.pcb', offset=offset) == ''
with log.open('a', encoding='utf-8') as stream:
stream.write('No server is running for the project and could not bring it alive\n')
assert pcb_log_reports_missing_server(tmp_path / 'actual.pcb', offset=offset)
def test_blocked_result_records_actual_total_and_preflight_time(tmp_path):
import json
result = run_verify(request(tmp_path))
assert result.status == 'BLOCKED'
stored = json.loads(result.evidence_path.read_text(encoding='utf-8'))
assert stored['timings_seconds']['total'] >= stored['timings_seconds']['preflight'] >= 0
performance = json.loads((result.evidence_path.parent / 'performance.json').read_text(encoding='utf-8'))
assert performance['timings_seconds'] == stored['timings_seconds']
assert result.timings_seconds == stored['timings_seconds']
def test_batch_report_includes_phase_seconds_and_incomplete_label(tmp_path):
result = VerifyResult('TIMEOUT', timings_seconds={'total': 150, 'save_close': 120},
incomplete_timing_phases=('save_close',))
path = write_batch_report([request(tmp_path)], [result], tmp_path/'reports')
report = path.read_text(encoding='utf-8-sig')
assert 'total=150.000s' in report
assert 'save_close=120.000s(unfinished)' in report
def test_runner_consumes_worker_timing_even_if_it_exits_before_first_poll(tmp_path, monkeypatch):
import json
fixture(tmp_path)
class Worker:
desktop = None
returncode = 0
def __init__(self, command, name):
root = Path(command[-1]).parent
events = [
{'stage': 'pcb_launch', 'monotonic_seconds': 1000},
{'stage': 'identity_verified', 'monotonic_seconds': 1132},
{'stage': 'integration_action', 'action': 'ForwardAnnotate', 'monotonic_seconds': 1132},
{'stage': 'integration_action_complete', 'action': 'ForwardAnnotate',
'monotonic_seconds': 1473, 'elapsed_seconds': 341, 'call_elapsed_seconds': 336},
{'stage': 'integration_complete', 'monotonic_seconds': 1473},
{'stage': 'save_start', 'monotonic_seconds': 1473},
{'stage': 'save_progress', 'timings_seconds': {'save': 246, 'close': 3}},
{'stage': 'saved_and_closed', 'monotonic_seconds': 1742},
{'stage': 'complete', 'monotonic_seconds': 1743},
]
(root/'events.jsonl').write_text(''.join(json.dumps(e)+'\n' for e in events), encoding='utf-8')
(root/'outcome.json').write_text('{"status":"PASS"}', encoding='utf-8')
def poll(self): return 0
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
result = run_verify(request(tmp_path), port_check=PORTS_OK)
assert result.status == 'PASS'
assert result.timings_seconds['pcb_startup'] == 132
assert result.timings_seconds['forward_annotation'] == 341
assert result.timings_seconds['save'] == 246
assert result.timings_seconds['close'] == 3
assert not (result.evidence_path.parent/'window_snapshots.jsonl').exists()
assert 'synch_ces' not in result.timings_seconds
def test_normal_progress_ticks_do_not_create_snapshot_capture_logs(tmp_path, monkeypatch):
fixture(tmp_path)
class Worker:
desktop = None
returncode = 0
def __init__(self, command, name):
self.polls = 0
(Path(command[-1]).parent/'outcome.json').write_text('{"status":"PASS"}', encoding='utf-8')
def poll(self):
self.polls += 1
return None if self.polls <= 80 else 0
def close(self): pass
class Progress:
count = 0
def poll(self, phase):
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
self.count += 1
value = WindowSnapshot(
hwnd=10, pid=20, process_name='ExpeditionPCB.exe', process_role='pcb',
window_class='#32770', title='Database Load', phase=phase,
controls=(ControlSnapshot(11,1003,'Static',str(self.count),control_type='Text'),))
return DialogPollResult(activity=True, observed_snapshots=(value,))
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
monkeypatch.setattr('cbb_refresh_verify.verify_runner.time.sleep', lambda _: None)
result = run_verify(request(tmp_path), port_check=PORTS_OK,
dialog_supervisor_factory=lambda **kw: Progress())
assert result.status == 'PASS'
root = result.evidence_path.parent
assert not (root/'window_snapshots.jsonl').exists()
assert not (root/'dialogs.jsonl').exists()
assert (root/'performance.json').exists()
def test_bridge_pending_is_retried_before_not_green(tmp_path):
from cbb_refresh_verify.verify_runner import read_library_flag_with_retry
answers = iter([BridgeResult('PENDING', 'IsVerifiedReuseBlock', 0),
BridgeResult('READY', 'IsVerifiedReuseBlock', 1)])
waits = []
result = read_library_flag_with_retry(lambda *_: next(answers), tmp_path/'lib.lmc', 'CBB1',
delays=(0, 3), sleep=waits.append)
assert result.status == 'READY'
assert waits == [3]
def test_bridge_retry_does_not_upgrade_persistent_pending(tmp_path):
from cbb_refresh_verify.verify_runner import read_library_flag_with_retry
calls = []
result = read_library_flag_with_retry(
lambda *_: calls.append(1) or BridgeResult('PENDING', 'IsVerifiedReuseBlock', 0),
tmp_path/'lib.lmc', 'CBB1', attempts=3, delays=(0, 1, 2), sleep=lambda _: None)
assert result.status == 'PENDING'
assert len(calls) == 3
def test_failure_report_contains_only_non_pass_items(tmp_path):
requests = [request(tmp_path, 'A'), request(tmp_path, 'B'), request(tmp_path, 'C')]
results = [VerifyResult('PASS'), VerifyResult('FAIL', stderr='Mentor error', error_category='MENTOR'),
VerifyResult('TIMEOUT', stderr='timeout', error_category='TOOL')]
full = write_batch_report(requests, results, tmp_path/'reports')
failures = sorted((tmp_path/'reports').glob('failures-*.log'))
assert full.is_file() and len(failures) == 1
text = failures[0].read_text(encoding='utf-8-sig')
assert 'B' in text and 'C' in text and '\nA\t' not in text
assert 'MENTOR' in text and 'TOOL' in text
def test_failure_category_separates_ime_filter_from_mentor_error():
from cbb_refresh_verify.verify_runner import classify_failure_category
assert classify_failure_category('UNKNOWN_DIALOG: TF_FloatingLangBar_WndTitle') == 'TOOL_FALSE_DIALOG'
assert classify_failure_category('Project Integration error: ERROR: Block X, Symbol $1I1') == 'MENTOR_PROJECT_INTEGRATION'
assert classify_failure_category('执行已完成,但权威状态仍为 PENDING (IsVerifiedReuseBlock, raw=0)') == 'MENTOR_VERIFY_NOT_COMMITTED'
@pytest.mark.parametrize('bridge', [BridgeResult('UNAVAILABLE','',None),
BridgeResult('READY','tree_icon',1), BridgeResult('READY','IsVerifiedReuseBlock',0)])
def test_non_authoritative_or_unavailable_bridge_never_confirms_pass(bridge):
result = apply_post_verify_status(VerifyResult('PASS'), bridge)
assert result.status == 'FAIL'
assert result.library_flag == 'UNKNOWN'
def test_ready_bridge_never_erases_a_mentor_execution_error():
result = apply_post_verify_status(VerifyResult('FAIL',stderr='Mentor database failed'),
BridgeResult('READY','IsVerifiedReuseBlock',1))
assert result.status == 'FAIL'
assert result.stderr == 'Mentor database failed'
def test_legacy_green_tuple_is_not_the_ready_gold_standard(tmp_path):
from cbb_refresh_verify.verify_runner import read_library_flag_with_retry
result = read_library_flag_with_retry(lambda *_: ('GREEN','tree'), tmp_path/'lib.lmc', 'CBB1', sleep=lambda _: None)
assert isinstance(result, BridgeResult)
assert result.status != 'READY'
def test_failure_report_uses_same_batch_identifier_and_has_failure_stage(tmp_path):
results = [VerifyResult('PASS'), VerifyResult('FAIL',stderr='Project Integration error',error_stage='forward_annotation')]
report = write_batch_report([request(tmp_path,'passed'),request(tmp_path,'failed')],results,tmp_path/'reports')
companion = report.with_name(report.name.replace('batch-','failures-',1))
assert companion.exists()
text = companion.read_text(encoding='utf-8-sig')
assert 'forward_annotation' in text
assert 'passed' not in text
def test_failure_report_accepts_generators_and_rejects_missing_results(tmp_path):
from cbb_refresh_verify.verify_runner import write_failure_report
path = write_failure_report((request(tmp_path,n) for n in ('A','B')),
(result for result in (VerifyResult('PASS'),VerifyResult('FAIL',stderr='detail'))),tmp_path/'reports')
text = path.read_text('utf-8-sig')
assert '总数: 2 成功: 1 失败: 1' in text and '\nA\t' not in text
with pytest.raises(ValueError, match='长度不一致'):
write_failure_report([request(tmp_path)],[],tmp_path/'reports')
def test_failure_report_has_no_executed_failure_for_skips_and_preserves_bom(tmp_path):
from cbb_refresh_verify.verify_runner import failure_report_path
report = write_batch_report([request(tmp_path,'SKIP')],[VerifyResult('SKIPPED')],tmp_path/'reports')
failures = failure_report_path(report)
assert failures.read_bytes().startswith(b'\xef\xbb\xbf')
text = failures.read_text('utf-8-sig')
assert '失败: 0 未执行: 1' in text and '本批次没有失败项。' in text
assert '\nSKIP\t' not in text
def test_failure_report_escapes_multiline_errors_to_keep_one_item_per_line(tmp_path):
from cbb_refresh_verify.verify_runner import failure_report_path
reports=[]
for _ in range(2):
reports.append(write_batch_report([request(tmp_path,'失败CBB')],
[VerifyResult('FAIL',stderr='ERROR: Symbol\r\n\t完整说明')],tmp_path/'reports'))
assert len(set(reports)) == 2
text=failure_report_path(reports[0]).read_text('utf-8-sig')
assert 'ERROR: Symbol\\r\\n\\t完整说明' in text
assert len([line for line in text.splitlines() if line.startswith('失败CBB\t')]) == 1
@pytest.mark.parametrize('answers, expected, category', [
([BridgeResult('PENDING','IsVerifiedReuseBlock',0),BridgeResult('READY','IsVerifiedReuseBlock',1)],'PASS',''),
([BridgeResult('PENDING','IsVerifiedReuseBlock',0)] * 3,'FAIL','MENTOR_VERIFY_NOT_COMMITTED'),
([BridgeResult('UNAVAILABLE','',None,'bridge missing')] * 3,'FAIL','BRIDGE_UNAVAILABLE'),
])
def test_saved_closed_without_cert_uses_bridge_and_never_reexecutes_verify(
tmp_path, monkeypatch, answers, expected, category):
fixture(tmp_path)
ordering=[]
class FinishedWorker:
desktop=None
returncode=0
def __init__(self, command, *_args, **kwargs):
ordering.append('worker')
Path(command[-1]).with_name('outcome.json').write_text(
'{"status":"PASS","stdout":"Save/IsSaved/Close completed without Cert"}',encoding='utf-8')
def poll(self): return 0
def close(self): ordering.append('worker_close')
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker',FinishedWorker)
monkeypatch.setattr('cbb_refresh_verify.verify_runner._cleanup_session_processes',
lambda *a,**kw: (ordering.append('cleanup') or {'safe_to_continue': True}))
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout',
lambda *_: ordering.append('failure_logs'))
waits=[]
monkeypatch.setattr('cbb_refresh_verify.verify_runner.time.sleep',waits.append)
remaining=iter(answers)
def read(*args):
ordering.append('bridge')
return next(remaining)
monkeypatch.setattr('cbb_refresh_verify.verify_runner.read_library_flag_if_available',read)
result=run_verify(request(tmp_path),port_check=PORTS_OK)
assert result.status == expected
assert result.error_category == category
assert ordering.count('worker') == 1
assert ordering.index('cleanup') < ordering.index('bridge')
assert len(result.bridge_attempts) == len(answers)
assert waits == ([3.0] if len(answers)==2 else [3.0,5.0])
persisted=json.loads(result.evidence_path.with_name('bridge-status.json').read_text('utf-8'))
assert [item['status'] for item in persisted] == [item.status for item in answers]
if expected == 'FAIL':
assert result.error_stage == 'library_flag'
assert ordering[-1] == 'failure_logs'
@pytest.mark.parametrize('detail, category', [
('PROJECT_INTEGRATION_FAILED: There were errors during packaging', 'MENTOR_PROJECT_INTEGRATION'),
('DATABASE_LOAD_FAILED: DataBase Load is being terminated with 2 errors', 'MENTOR_DATABASE_LOAD_FAILED'),
('DATABASE_LOAD_FAILED: Could not load Job Preferences DB; Unable to create local Cell Library.', 'MENTOR_LOCAL_CELL_LIBRARY'),
('SAVE_REUSABLE_BLOCK_FAILED: Error saving the Reusable Block.', 'MENTOR_SAVE_FAILED'),
('DESIGN_SERVER_FAILED: Could not run server', 'ENVIRONMENT_DESIGN_SERVER'),
])
def test_known_native_fatal_codes_are_not_mislabeled_as_tool_or_unclassified(detail, category):
from cbb_refresh_verify.verify_runner import classify_failure_category
assert classify_failure_category(detail) == category