fix: prevent iCDB disconnects and recover failed projects
This commit is contained in:
1 parent
8a517d6f38
commit
2490067b2d
19 files changed
+1117
-60
No files matched your search
@@ -0,0 +1,142 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def target_fixture(tmp_path):
|
||||
from test_verify_runner import fixture, request
|
||||
from cbb_refresh_verify.native_repair import select_repair_target
|
||||
work = fixture(tmp_path)
|
||||
database = work/'database'
|
||||
database.mkdir()
|
||||
(database/'icdb.dat').write_bytes(b'original damaged database')
|
||||
diagnosis = {'error_code': 'ICDB_REPAIR_REQUIRED', 'machine': 'OLD-PC',
|
||||
'database_path': str(database), 'session': '2026-10-24 08.22.29-10308', 'scope': 'project'}
|
||||
return request(tmp_path), diagnosis, select_repair_target(request(tmp_path), diagnosis)
|
||||
|
||||
|
||||
def test_repair_target_matches_actual_database_not_guessed_cbb_name(tmp_path):
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
assert target.project.name == 'CBB1.prj'
|
||||
assert target.database == target.project.parent/'database'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['foreign', 'shared', 'missing_session'])
|
||||
def test_repair_target_rejects_unresolved_or_shared_database(tmp_path, change):
|
||||
from cbb_refresh_verify.native_repair import select_repair_target
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
if change == 'foreign': record['database_path'] = str(tmp_path/'foreign/database')
|
||||
if change == 'shared':
|
||||
record.update(scope='central_library', database_path=str(tmp_path/'SymbolLibs/__ReusableBlocksSymb'))
|
||||
if change == 'missing_session': record['session'] = ''
|
||||
with pytest.raises(ValueError):
|
||||
select_repair_target(request, record)
|
||||
|
||||
|
||||
def test_repair_backup_contains_database_locks_backups_and_original_files(tmp_path):
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
for relative in ('database/cdbsvr/sAddress.adr', 'ProjectBackup/backups/old.zip',
|
||||
'PCB/copy.lock', 'Log Files/ProjectBackupLog.txt'):
|
||||
path = target.project.parent/relative
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(b'original '+relative.encode())
|
||||
destination = tmp_path/'repair-backup'
|
||||
manifest = backup_project(target, destination)
|
||||
assert manifest['complete'] is True
|
||||
assert (destination/'database/icdb.dat').read_bytes() == b'original damaged database'
|
||||
assert (destination/'database/cdbsvr/sAddress.adr').read_bytes().startswith(b'original')
|
||||
assert (destination/'ProjectBackup/backups/old.zip').is_file()
|
||||
assert (destination/'PCB/copy.lock').is_file()
|
||||
|
||||
|
||||
def test_repair_backup_refuses_database_open_by_another_client(tmp_path):
|
||||
import win32file, win32con
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
handle = win32file.CreateFile(str(target.database/'icdb.dat'), win32con.GENERIC_READ,
|
||||
win32con.FILE_SHARE_READ, None, win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(OSError):
|
||||
backup_project(target, tmp_path/'backup')
|
||||
finally:
|
||||
handle.Close()
|
||||
assert not (tmp_path/'backup/backup-manifest.json').exists()
|
||||
|
||||
|
||||
def test_repair_backup_rejects_reparse_directory(tmp_path):
|
||||
import _winapi
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
outside = tmp_path/'outside'
|
||||
outside.mkdir()
|
||||
_winapi.CreateJunction(str(outside), str(target.project.parent/'linked'))
|
||||
with pytest.raises(ValueError):
|
||||
backup_project(target, tmp_path/'backup')
|
||||
|
||||
|
||||
def test_native_confirmation_requires_exact_target_and_known_repair_prompt():
|
||||
from cbb_refresh_verify.native_repair_ui import confirmation_allowed
|
||||
assert confirmation_allowed('iCDB Project Backup - C:\\exact.prj', r'C:\exact.prj',
|
||||
'The project content will be validated and repaired if necessary.\n'
|
||||
'A project backup will be created before any action take place.\n'
|
||||
'You will be able to revert all changes using that backup.')
|
||||
assert not confirmation_allowed('iCDB Project Backup - C:\\OTHER.prj', r'C:\exact.prj',
|
||||
'The project content will be validated and repaired if necessary.')
|
||||
assert not confirmation_allowed('iCDB Project Backup - C:\\exact.prj', r'C:\exact.prj',
|
||||
'Confirm no other clients are connected. Continue anyway?')
|
||||
|
||||
|
||||
def test_native_diagnostics_finish_alone_does_not_prove_repair_success():
|
||||
from cbb_refresh_verify.native_repair_ui import repair_completed
|
||||
assert not repair_completed('Diagnostic: FINISH', 'IC: Closing project', native_backup=False)
|
||||
assert not repair_completed('ERROR: invalid database\nDiagnostic: FINISH',
|
||||
'iCDBProjectBackupRepair\nIC: Closing project', native_backup=True)
|
||||
assert repair_completed('Diagnostic: START\nDiagnostic: FINISH',
|
||||
'iCDBProjectBackupRepair\nIC: Closing project', native_backup=True)
|
||||
|
||||
|
||||
def test_automatic_repair_retries_only_once_and_preserves_original_failure(tmp_path, monkeypatch):
|
||||
from dataclasses import replace
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
original = runner.VerifyResult('FAIL', stderr='native lost session',
|
||||
safe_to_continue=False, error_category='ICDB_REPAIR_REQUIRED', evidence_path=evidence)
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
return original
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status':'REPAIRED'})
|
||||
outcome = runner.run_verify(request)
|
||||
assert len(calls) == 2
|
||||
assert outcome.status == 'FAIL'
|
||||
assert outcome.stderr == 'native lost session'
|
||||
assert outcome.recovery_attempts[0]['original_error'] == 'native lost session'
|
||||
|
||||
|
||||
def test_automatic_repair_does_not_retry_busy_or_unsupported_target(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
return runner.VerifyResult('FAIL', stderr='native lost session',
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=False, evidence_path=evidence)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status':'BLOCKED'})
|
||||
outcome = runner.run_verify(request)
|
||||
assert len(calls) == 1
|
||||
assert outcome.safe_to_continue is False
|
||||
assert outcome.recovery_attempts[0]['status'] == 'BLOCKED'
|
||||
Reference in new issue
Block a user