fix: bind manual recovery entries and current native diagnostics

This commit is contained in:
阿白叔 committed 2026-11-16 20:38:17 +08:00
1 parent 8374cabe5c
commit 601dd7b6b2
14 files changed
+694 -69

No files matched your search

+5 -5
View File
@@ -54,7 +54,7 @@ def test_preflight_rejects_pcb_junction_outside_current_block(tmp_path):
preflight(VerifyRequest(library, 'CBB', tmp_path/'evidence'))
def test_serial_guard_covers_preflight_and_content_check(tmp_path, monkeypatch):
def test_nonnative_preparation_leaves_real_host_channel_available(tmp_path, monkeypatch):
from cbb_refresh_verify import verify_runner as runner
entered, release = Event(), Event()
calls, results = [], []
@@ -70,14 +70,14 @@ def test_serial_guard_covers_preflight_and_content_check(tmp_path, monkeypatch):
thread.start()
assert entered.wait(3)
try:
second = runner.run_verify(VerifyRequest(tmp_path/'lib.lmc', 'second', tmp_path/'evidence'))
assert second.status == 'BLOCKED'
assert second.safe_to_continue
assert second.error_category == 'NATIVE_CHANNEL_BUSY'
from cbb_refresh_verify.operation_queue import native_channel
with native_channel():
pass
assert calls == ['first']
finally:
release.set()
thread.join(6)
assert results[0].status == 'PASS'
def test_recovery_retains_temporary_network_mapping():
+11 -5
View File
@@ -277,7 +277,8 @@ def test_contents_session_loss_records_each_item_without_launching_pcb_or_repair
assert results[1].error_category == 'ICDB_REPAIR_REQUIRED'
def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch):
@pytest.mark.parametrize('fault', ['session_loss', 'inconsistent'])
def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch, fault):
from test_verify_runner import fixture, request
from cbb_refresh_verify.icdb_recovery import snapshot_log
from cbb_refresh_verify.verify_diagnostics import failure_logs_main
@@ -292,7 +293,8 @@ def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, mon
(root/'diagnostics').mkdir()
(root/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
'source': str(log), 'baseline': snapshot_log(log)}), encoding='utf-8')
log.write_text(ERROR, encoding='utf-8')
from test_issue15_native_entry import inconsistency
log.write_text(ERROR if fault == 'session_loss' else inconsistency(work/'database'), encoding='utf-8')
(root/'outcome.json').write_text(json.dumps({
'status': 'FAIL', 'stderr': 'MENTOR_DATABASE_LOAD_FAILED: opening failed',
'safe_to_continue': False}), encoding='utf-8')
@@ -305,7 +307,11 @@ def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, mon
assert result.error_category == 'ICDB_REPAIR_REQUIRED'
assert result.safe_to_continue is True
assert 'opening failed' in result.stderr
assert '200091-CBB' in result.stderr
assert '__ReusableBlocksSymb' in result.stderr
assert '2026-10-24 08.22.29-10308' in result.stderr
if fault == 'session_loss':
assert '200091-CBB' in result.stderr
assert '__ReusableBlocksSymb' in result.stderr
assert '2026-10-24 08.22.29-10308' in result.stderr
else:
assert str(work/'database') in result.stderr
assert '未修复' in result.stderr
assert json.loads(result.evidence_path.read_text('utf-8'))['error_category'] == 'ICDB_REPAIR_REQUIRED'
+380
View File
@@ -0,0 +1,380 @@
"""Issue 15 recovery entry regressions. Native boundaries never launch Mentor."""
from contextlib import contextmanager
import json
import os
from pathlib import Path
from types import SimpleNamespace
import pytest
@pytest.fixture(autouse=True)
def no_real_owner_probe(monkeypatch):
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: [])
def test_ordinary_worker_aligns_both_bound_projects_and_retains_each_original(tmp_path, monkeypatch):
from test_verify_worker_stability import exercise_worker
from cbb_refresh_verify import mentor_adapter as adapter
job, events, execute = exercise_worker(tmp_path, monkeypatch)
main = Path(job['project']); aux = Path(job['pcb']).parent/'pcb.prj'
original_main = b'KEY CentralLibrary "old-main.lmc"\r\n#\x81\x40\r\n'
original_aux = b'\xff\xfe'+ 'KEY CentralLibrary "old-pcb.lmc"\r\n'.encode('utf-16-le')
main.write_bytes(original_main); aux.write_bytes(original_aux)
assert execute()['status'] == 'PASS'
from cbb_refresh_verify.backup_recovery import _project_text
for path in (main, aux):
import re
reference = re.search(r'KEY CentralLibrary "([^"]+)"', _project_text(path))[1]
assert (path.parent/reference).resolve() == Path(job['lmc_file']).resolve()
saved = [p.read_bytes() for p in (Path(job['root'])/'diagnostics').rglob('*.before-central-library')]
assert original_main in saved and original_aux in saved
def test_ordinary_legacy_binding_never_rewrites_template(tmp_path, monkeypatch):
from test_verify_worker_stability import exercise_worker
job, _, execute = exercise_worker(tmp_path, monkeypatch)
main = Path(job['project']); pcb = Path(job['pcb'])
main.write_text('KEY CentralLibrary "old.lmc"\n')
pcb.write_text('KEY ProjectFile "../CBB.prj"\n')
template = pcb.parent/'template.prj'; template.write_text('KEY CentralLibrary "do-not-touch.lmc"\n')
original = template.read_bytes()
assert execute()['status'] == 'PASS'
assert template.read_bytes() == original
assert 'old.lmc' not in main.read_text()
def inconsistency(database):
return (f'Project database [{database}] is inconsistent. It has been manually copied while the iCDB Server was running. '
"Please use iCDB Project Backup 'Repair project' functionality to fix this issue.")
def test_current_inconsistency_has_structured_database_and_old_error_is_not_reused(tmp_path):
from cbb_refresh_verify import icdb_recovery as recovery, mentor_adapter as adapter
source = tmp_path/'LogFiles/PCBLogFile.txt'; source.parent.mkdir()
db = tmp_path/'database'; message = inconsistency(db)
source.write_text(message+'\n')
baseline = recovery.snapshot_log(source)
assert recovery.read_log_failure(source, baseline)[1] is None
source.write_text('new header\n'+message+'\n')
assert recovery.read_log_failure(source, baseline)[1] is None
baseline = recovery.snapshot_log(source)
with source.open('a') as stream: stream.write(message+'\n')
text, record = recovery.read_log_failure(source, baseline)
assert record['database_path'] == str(db) and record['scope'] == 'project'
assert record['kind'] == 'database_inconsistent'
detail = adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline)
assert 'ICDB_REPAIR_REQUIRED' in detail and str(db) in detail and '未修复' in detail
def test_manual_repair_selects_diagnosed_bound_database_without_layout_or_healthy_header(tmp_path):
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native
request, main = project_fixture(tmp_path)
pcb = main.parent/'PCB/Block.pcb'; pcb.parent.mkdir()
pcb.write_text('KEY ProjectFile "../Block.prj"\n')
aux = pcb.parent/'pcb.prj'; aux.write_text('KEY iCDBDir "database"\n')
for prj in (main, aux):
(prj.parent/'database').mkdir(); (prj.parent/'database/icdb.dat').write_bytes(b'not-a-healthy-header')
assert native.native_backup_target(request, mode='repair')[0] == main
assert native.native_backup_target(request, mode='repair', target_database=aux.parent/'database')[0] == aux
with pytest.raises(ValueError, match='TARGET'):
native.native_backup_target(request, mode='repair', target_database=tmp_path/'foreign')
def test_versions_launcher_rechecks_parent_retained_lease_and_persists_before_gate(tmp_path, monkeypatch):
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, library_lock, session_record
request, project = project_fixture(tmp_path)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
calls = []
monkeypatch.setattr(library_lock, 'recheck_previous_release', lambda *a, **k: calls.append('recheck'))
monkeypatch.setattr(session_record, 'process_identity', lambda pid: dict(pid=pid, created='now', executable='python', owner='test'))
def spawn(command, **kw):
job = json.loads(Path(command[-1]).read_text())
assert calls == ['recheck'] and not Path(job['start_gate']).exists()
calls.append('spawn')
return SimpleNamespace(pid=123, poll=lambda: None)
monkeypatch.setattr(native.subprocess, 'Popen', spawn)
session = native.launch_native_backup(request, mode='repair')
record = json.loads(session.job.read_text())
assert Path(record['start_gate']).is_file()
assert Path(record['session_record_path']).is_file()
assert session.mode == 'repair' and session.database == project.parent/'database'
assert session.operation_id == record['operation_id']
assert record['automatic_repair'] is False
@pytest.mark.parametrize('failure', ['spawn', 'registration'])
def test_backup_failure_leaves_start_gate_closed(tmp_path, monkeypatch, failure):
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, session_record
request, _ = project_fixture(tmp_path)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
def spawn(*a, **k):
if failure == 'spawn': raise OSError('start failed')
return SimpleNamespace(pid=123, poll=lambda: None)
monkeypatch.setattr(native.subprocess, 'Popen', spawn)
monkeypatch.setattr(session_record, 'register_worker_session', lambda *a: (_ for _ in ()).throw(OSError('register failed')))
with pytest.raises(OSError): native.launch_native_backup(request)
job = next(request.evidence_dir.glob('native-backup-*/request.json'))
record = json.loads(job.read_text())
assert not Path(record['start_gate']).exists()
assert Path(record['cancel_signal']).exists()
def test_changed_prj_bytes_refused_before_recovery_tool_dispatch(tmp_path, monkeypatch):
import hashlib
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native
request, project = project_fixture(tmp_path)
job = tmp_path/'request.json'
record = dict(library=str(request.lmc_file), block=request.block_name, project=str(project),
project_sha256=hashlib.sha256(project.read_bytes()).hexdigest())
job.write_text(json.dumps(record))
project.write_text(project.read_text()+'# changed identity\n')
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
assert native.native_backup_worker_main(job, process_factory=lambda *a: pytest.fail('changed PRJ dispatched')) == 1
assert '身份' in json.loads(job.with_name('result.json').read_text('utf-8'))['detail']
def test_real_parent_retained_sidecar_is_reconciled_before_child_acquires_it_and_reopens(tmp_path, monkeypatch):
import subprocess, sys
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, library_lock as locks
from cbb_refresh_verify.file_probe import hold_release_files
request, _ = project_fixture(tmp_path)
with locks.LibraryLock(request.lmc_file, safe_to_release=lambda: False, block_name='Block') as retained:
pass
@contextmanager
def proof(library, **kwargs):
with hold_release_files(library, (library.parent/'ReusableBlocksLP/Block/Work',)) as files:
yield {**files, 'native_owners': []}
monkeypatch.setattr(locks, '_prove_library_released', proof)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'not-launched.exe')
popen = subprocess.Popen
script = (
'import json,sys\nfrom pathlib import Path\n'
'from cbb_refresh_verify.desktop_worker import run_native_worker\n'
'from cbb_refresh_verify.library_lock import LibraryLock\n'
'def action(path):\n'
' j=json.loads(Path(path).read_text())\n'
' with LibraryLock(j["library"],checkpoint=False):\n'
' Path(path).with_name("child-acquired").write_text("owned")\n'
' return 0\n'
'raise SystemExit(run_native_worker(action,sys.argv[1]))\n')
def spawn(command, **kwargs):
if '--native-backup-worker' in command:
assert not locks.has_retained_library(request.lmc_file)
return popen([sys.executable, '-c', script, command[-1]], **kwargs)
return popen(command, **kwargs)
monkeypatch.setattr(native.subprocess, 'Popen', spawn)
try:
for mode in ('versions', 'repair'):
session = native.launch_native_backup(request, mode=mode)
assert session.process.wait(10) == 0
assert session.job.with_name('child-acquired').read_text() == 'owned'
assert retained.path.is_file()
finally:
owner = locks._owners.pop(retained.key, None)
if owner: owner['handle'].Close()
def test_native_registration_failure_never_resumes_suspended_child(tmp_path):
import sys
from cbb_refresh_verify.native_backup import _NativeBackupProcess
marker = tmp_path/'must-not-run'
def refuse(process): raise OSError('required identity journal failed')
with pytest.raises(OSError, match='journal'):
_NativeBackupProcess([sys.executable, '-c', f'from pathlib import Path;Path({str(marker)!r}).touch()'],
tmp_path, before_resume=refuse)
assert not marker.exists()
def test_independent_backup_job_registration_retains_descendants_after_caller_handles_close(tmp_path):
import sys, time
from cbb_refresh_verify.native_backup import _NativeBackupProcess, _BackupJobWriter
script = tmp_path/'parent.py'; marker = tmp_path/'child.pid'
script.write_text('import subprocess,sys,time\nfrom pathlib import Path\n'
'p=subprocess.Popen([sys.executable,"-c","import time;time.sleep(1)"])\n'
f'Path({str(marker)!r}).write_text(str(p.pid))\ntime.sleep(.1)\n')
process = _NativeBackupProcess([sys.executable, str(script)], tmp_path)
registered = _BackupJobWriter(process.job)
try:
deadline = time.monotonic()+5
while process.poll() is None and time.monotonic()<deadline: time.sleep(.02)
assert marker.exists() and process.poll() == 0
process.close()
assert registered.poll() is None
while registered.poll() is None and time.monotonic()<deadline: time.sleep(.02)
assert registered.poll() == 0
finally:
if registered.job is not None: registered.job.Close()
def test_common_admission_refusal_publishes_bound_receipt_and_ui_can_reopen(qtbot, tmp_path, monkeypatch):
from threading import Event, Thread
from test_native_backup import project_fixture
from test_ui_filters import load_window
from cbb_refresh_verify import native_backup as native, desktop_worker
from cbb_refresh_verify.application import main
request, _ = project_fixture(tmp_path)
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
monkeypatch.setattr(native.subprocess, 'Popen', lambda *a, **k: SimpleNamespace(pid=os.getpid(), poll=lambda: None))
session = native.launch_native_backup(request)
entered, release = Event(), Event()
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
def held_writer():
with desktop_worker.worker_native_channel():
entered.set(); release.wait(5)
holder = Thread(target=held_writer); holder.start()
assert entered.wait(2)
monkeypatch.setattr(native, 'native_backup_worker_main', lambda *a: pytest.fail('refused action entered'))
try:
assert main(['--native-backup-worker', str(session.job)]) == 1
finally:
release.set(); holder.join(3)
record = json.loads(session.job.with_name('result.json').read_text('utf-8'))
assert record['safe_to_continue'] is True and record['native_started'] is False
assert record['operation_id'] == json.loads(session.job.read_text('utf-8'))['operation_id']
assert record['worker_identity']['pid'] == os.getpid()
session.process.poll = lambda: 1
launched = []
window = load_window(qtbot, tmp_path, monkeypatch,
native_backup_launcher=lambda req: (launched.append(req) or session))
session.block = 'Beta'; record['block'] = 'Beta'
session.job.with_name('result.json').write_text(json.dumps(record), encoding='utf-8')
window._native_backup_session = session
window._poll_native_backup(); qtbot.waitUntil(lambda: not window.busy)
assert window._native_backup_session is None
assert 'NATIVE_WRITER_ACTIVE' in window.status_label.text()
window.search_edit.setText('Beta'); window.select_visible_button.click()
window.open_native_backup(); qtbot.waitUntil(lambda: not window.busy)
assert len(launched) == 1
window._native_backup_session = None # The test adapter has no live native client.
def test_unbound_admission_refusal_cannot_claim_release(tmp_path, monkeypatch):
from cbb_refresh_verify import native_backup as native, desktop_worker
job = tmp_path/'request.json'
job.write_text(json.dumps(dict(library=str(tmp_path/'lib.lmc'), block='A', operation_id='foreign',
session_record_path=str(tmp_path/'session.json'), project=str(tmp_path/'A.prj'))))
monkeypatch.setattr(desktop_worker, 'run_native_worker', lambda *a: (_ for _ in ()).throw(RuntimeError('refused')))
assert native.run_native_backup_worker(job) == 1
result = json.loads(job.with_name('result.json').read_text('utf-8'))
assert result['safe_to_continue'] is False
def test_legacy_unregistered_backup_job_cannot_enter_native_action(tmp_path, monkeypatch):
from cbb_refresh_verify import native_backup as native, desktop_worker
job = tmp_path/'request.json'
job.write_text(json.dumps(dict(library=str(tmp_path/'lib.lmc'), block='A', project=str(tmp_path/'A.prj'))))
monkeypatch.setattr(desktop_worker, '_automation_clients', [])
monkeypatch.setattr(native, 'native_backup_worker_main', lambda *a: pytest.fail('unregistered action entered'))
assert native.run_native_backup_worker(job) == 1
assert json.loads(job.with_name('result.json').read_text('utf-8'))['safe_to_continue'] is False
@pytest.mark.parametrize('owner_scope', ['target', 'unrelated'])
def test_recovery_entry_scopes_external_ownership_before_launch(tmp_path, monkeypatch, owner_scope):
from test_native_backup import project_fixture
from cbb_refresh_verify import native_backup as native, owner_probe
request, project = project_fixture(tmp_path)
job = tmp_path/'request.json'
job.write_text(json.dumps(dict(library=str(request.lmc_file), block=request.block_name,
project=str(project), mode='repair')))
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: tmp_path/'native.exe')
external = project if owner_scope == 'target' else tmp_path/'other/Work/other.prj'
monkeypatch.setattr(owner_probe, 'query_owners', lambda names: [dict(pid=99999,
name='ExpeditionPCB.exe', command=f'ExpeditionPCB.exe "{external}"', executable='ExpeditionPCB.exe')])
def process(*args):
if owner_scope == 'target': pytest.fail('external target owner bypassed')
return SimpleNamespace(pid=456, poll=lambda: 0, session_pids=lambda: [], close=lambda: None)
assert native.native_backup_worker_main(job, process_factory=process) == (1 if owner_scope == 'target' else 0)
result = json.loads(job.with_name('result.json').read_text('utf-8'))
assert result['safe_to_continue'] is True
if owner_scope == 'target': assert 'OWNER_ACTIVE' in result['detail']
else: assert result['status'] == 'CLOSED'
@pytest.mark.parametrize('user_timeout', [5, 600])
def test_lm_menu_return_without_pcb_uses_dispatch_budget_and_closes_lm(tmp_path, monkeypatch, user_timeout):
from test_verify_worker_stability import exercise_worker
from cbb_refresh_verify import mentor_adapter as adapter, library_manager_verify as lm
job, events, execute = exercise_worker(tmp_path, monkeypatch)
job['native_lm_verify'] = True; job['timeout'] = user_timeout
(Path(job['pcb']).parent/'pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n')
clock = [0.0]; closed = []
monkeypatch.setattr(adapter.time, 'monotonic', lambda: clock[0])
monkeypatch.setattr(adapter.time, 'sleep', lambda seconds: clock.__setitem__(0, clock[0]+seconds))
monkeypatch.setattr(adapter, 'select_reusable_circuit_worker', lambda *a, **k: None)
def launch(*args, **kw):
assert args[3] <= min(45, user_timeout)
return SimpleNamespace(process=SimpleNamespace(pid=123), close=lambda: closed.append(True))
monkeypatch.setattr(lm, 'launch_library_verify', launch)
with pytest.raises(RuntimeError, match='VERIFY_DISPATCH_FAILED'): execute()
assert clock[0] <= min(45, user_timeout)+1 and closed == [True]
def test_inconsistency_preserves_shared_database_scope():
from cbb_refresh_verify.icdb_recovery import parse_recovery_request
database = r'C:\\library\\SymbolLibs\\__ReusableBlocksSymb'
assert parse_recovery_request(inconsistency(database))['scope'] == 'central_library'
@pytest.mark.parametrize('case', ['not_in_use', 'repair_required', 'in_use', 'unconfirmed', 'wrong_target',
'no_identity', 'changed_address', 'changed_project', 'shared_address', 'exclusive'])
def test_explicit_continue_requires_exact_fresh_native_and_exclusive_proof(tmp_path, monkeypatch, case):
from test_native_repair import target_fixture
from test_native_occupancy import output
from cbb_refresh_verify import pcb_open_consent as consent, native_repair, owner_probe, session_record
from cbb_refresh_verify.verify_runner import preflight
request, _, target = target_fixture(tmp_path)
request.evidence_dir.mkdir()
project, pcb = preflight(request)
project.write_text('KEY PCBDesignPath "PCB/actual.pcb"\nKEY iCDBDir "database"\n')
pcb.write_text('KEY ProjectFile "../'+project.name+'"\n')
address = target.database/'cdbsvr/sAddress.adr'; address.parent.mkdir(); address.write_bytes(b'unchanged address')
if case == 'shared_address':
shared = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
shared.parent.mkdir(parents=True); shared.write_bytes(b'unknown shared owner')
monkeypatch.setattr(owner_probe, 'query_process_snapshot', lambda: [])
calls = []
def inspect(inspected, root, **kwargs):
assert kwargs['inspection_only'] is True
calls.append(inspected)
record = dict(status='NOT_IN_USE', project=str(target.project), database=str(target.database),
native_pid=os.getpid(), native_identity=session_record.process_identity(os.getpid()),
inspection_only=True, repair_requested=False, repair_required=case == 'repair_required',
native_exit_code=0, release={'native_exited': True},
output=output(target.project, target.database, fault=case == 'repair_required'))
if case == 'in_use': record.update(status='IN_USE', output=output(target.project, target.database, busy=True))
if case == 'unconfirmed': record.update(status='UNCONFIRMED', output='not known')
if case == 'wrong_target': record['database'] = str(tmp_path/'foreign')
if case == 'no_identity': record.pop('native_identity')
if case == 'changed_address': address.write_bytes(b'new address')
if case == 'changed_project': project.write_text('KEY iCDBDir "another-db"\n')
return record
monkeypatch.setattr(native_repair, 'run_native_repair', inspect)
job = dict(root=str(request.evidence_dir), lmc_file=str(request.lmc_file),
block_name=request.block_name, project=str(project), pcb=str(pcb), operation_id='current-open')
held = None
if case == 'exclusive':
import win32con, win32file
held = win32file.CreateFile(str(target.database/'icdb.dat'), win32con.GENERIC_READ,
0, None, win32con.OPEN_EXISTING, 0, None)
try:
if case in ('not_in_use', 'repair_required'):
with consent.resource_guard(job, {os.getpid()}): pass
assert calls == [target]
evidence = json.loads((request.evidence_dir/'pcb-open-resource-proof.json').read_text())
assert evidence['operation_id'] == 'current-open'
assert evidence['repair_confirmed'] is False
else:
with pytest.raises((ValueError, OSError)):
with consent.resource_guard(job, {os.getpid()}): pass
finally:
if held is not None: held.Close()
if case not in ('shared_address', 'exclusive'):
assert calls == [target]
if case != 'changed_address': assert address.read_bytes() == b'unchanged address'
+9 -3
View File
@@ -559,20 +559,26 @@ def test_queue_close_has_one_deadline_and_reports_pending_scope(tmp_path):
thread.join(2)
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup):
def test_real_management_transaction_does_not_handoff_between_remove_and_add(management_setup, monkeypatch):
from dataclasses import replace
from types import SimpleNamespace
from test_management import execute
from cbb_refresh_verify import operation_queue
from cbb_refresh_verify.operation_queue import BatchTaskContext, NativeQueue
request, adapter, source = management_setup
offset = [0.0]
real_clock = time.monotonic
monkeypatch.setattr(operation_queue, 'time', SimpleNamespace(monotonic=lambda: real_clock()+offset[0]))
original = adapter.remove
def slow_remove(*args):
result = original(*args)
time.sleep(.4)
offset[0] += 100 # Native time exceeds admission budget without timing filesystem preparation.
time.sleep(.08) # Let the real admission monitor observe the active native phase.
return result
adapter.remove = slow_remove # Native external boundary; transaction stays real.
context = BatchTaskContext(library=str(request.library), targets=('Block',))
result = NativeQueue().run(lambda: execute(replace(request, operation='update',
source=source, auto_verify=False), adapter), context=context, timeout_seconds=.2)
source=source, auto_verify=False), adapter), context=context, timeout_seconds=10)
assert result.status == 'UPLOADED'
assert adapter.calls[0] == 'remove' and adapter.calls[1][0] == 'add'
+13 -2
View File
@@ -8,6 +8,11 @@ import win32con
import win32file
@pytest.fixture(autouse=True)
def no_real_owner_probe(monkeypatch):
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: [])
def project_fixture(tmp_path):
from cbb_refresh_verify.verify_runner import VerifyRequest
library = tmp_path/'library/library.lmc'; library.parent.mkdir(); library.touch()
@@ -21,6 +26,9 @@ def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch
request, project = project_fixture(tmp_path)
executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch()
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable)
from cbb_refresh_verify import session_record
monkeypatch.setattr(session_record, 'process_identity', lambda pid:
dict(pid=pid, created='fixture', executable='python.exe', owner='fixture'))
calls = []
monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw:
(calls.append(command) or SimpleNamespace(pid=123, poll=lambda: None)))
@@ -32,7 +40,8 @@ def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch
assert session.library == request.lmc_file and session.block == 'Block'
def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeypatch):
@pytest.mark.parametrize('mode', ['versions', 'repair'])
def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeypatch, mode):
from cbb_refresh_verify import native_backup as native
from cbb_refresh_verify.library_lock import LibraryLock
request, project = project_fixture(tmp_path)
@@ -40,7 +49,7 @@ def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeyp
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable)
job = request.evidence_dir/'request.json'; job.parent.mkdir()
job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block',
'project': str(project), 'mentor_bin': ''}))
'project': str(project), 'mentor_bin': '', 'mode': mode}))
calls = []
class NativeProcess:
pid = 456
@@ -58,6 +67,8 @@ def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeyp
result = json.loads(job.with_name('result.json').read_text('utf-8'))
assert result['status'] == 'CLOSED' and result['restore_confirmed'] is False
assert result['native_exit_code'] == 0 and result['safe_to_continue'] is True
assert result['mode'] == mode and result['repair_confirmed'] is False
assert result['validation_passed'] is False
with LibraryLock(request.lmc_file): pass
+1 -1
View File
@@ -11,7 +11,7 @@ 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()
pcb.write_text('KEY ProjectFile "../CBB.prj"\n')
project = work/'CBB.prj'
project.touch()
evidence = tmp_path/'evidence'