"""PCB open errors identify ICDB findings by native content, never by code alone.""" from dataclasses import replace import json import pytest from test_maintenance_entry import case, no_native_launch NATIVE_ERROR = ( 'Database cannot be loaded and the server cannot be started, the project will not be opened. ' "Please use iCDB Project Backup 'Repair project' functionality to fix this issue. " '(Database [C:\\isolated\\Work\\database\\icdb.dat] in old, unsupported format)') @pytest.mark.parametrize('message,expected', [ (NATIVE_ERROR, True), ('Unable to open iCDB connection.', True), ('iCDB database check FAILED', True), ('iCDB database check PASSED', False), ('0x8004021E: Failed to open document.', False), ('ICDB_REPAIR_REQUIRED: Failed to open document.', False), ('Cannot open [C:\\icdb\\icdb.dat].', False), ('PCB file was not found. Evidence: C:\\icdb\\result.json', False), ]) def test_error_content_requires_an_icdb_failure_message(message, expected): from cbb_refresh_verify.icdb_recovery import icdb_error_content assert bool(icdb_error_content(message)) is expected def test_unknown_icdb_description_in_an_actual_open_error_is_retained(): from cbb_refresh_verify.icdb_recovery import icdb_error_content assert icdb_error_content('iCDB connection: E_NEW_NATIVE_CODE', error_message=True) @pytest.mark.parametrize('mode', ['append', 'rewrite', 'retained', 'unchanged']) def test_native_error_content_is_from_this_open_attempt(tmp_path, mode): from cbb_refresh_verify.icdb_recovery import snapshot_log, read_icdb_error path = tmp_path/'PCBLogFile.txt' previous = 'old header\n'+NATIVE_ERROR+'\n' if mode in ('retained','unchanged') else 'old header\n' path.write_text(previous, 'utf-8') baseline = snapshot_log(path) if mode == 'append': path.write_text(previous+NATIVE_ERROR, 'utf-8') elif mode == 'rewrite': path.write_text('new header\n'+NATIVE_ERROR, 'utf-8') elif mode == 'retained': path.write_text('new header\n'+NATIVE_ERROR+'\nNormal startup', 'utf-8') assert bool(read_icdb_error(path, baseline)) is (mode in ('append','rewrite')) @pytest.mark.parametrize('failure,category', [(NATIVE_ERROR,'ICDB_REPAIR_REQUIRED'), ('0x8004021E: Failed to open document.','MENTOR_DATABASE_LOAD_FAILED')]) def test_batch_scan_opens_without_repair_and_classifies_native_content(case, monkeypatch, failure, category): from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner, native_repair request, _, address = case probes=[] monkeypatch.setattr(native_repair,'run_native_repair',lambda *a,**k:pytest.fail('scan dispatched Project Backup')) def diagnose(probe, **kw): probes.append(probe) return runner.VerifyResult('FAIL',stderr=failure,error_category='MENTOR_DATABASE_LOAD_FAILED') monkeypatch.setattr(runner,'run_verify',diagnose) result=entry.run_icdb_check(replace(request,auto_repair=True)) assert len(probes)==1 and probes[0].diagnostic_only and not probes[0].maintenance assert not probes[0].auto_repair and not probes[0].native_lm_verify assert result.status=='FAIL' and result.error_category==category and result.safe_to_continue assert result.library_flag=='UNKNOWN' and result.recovery_attempts==() assert address.read_bytes()==b'old server address - preserved' def test_scan_uses_the_current_open_evidence_even_when_popup_is_generic(case, monkeypatch): from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner request, _, _=case def diagnose(probe, **kw): root=probe.evidence_dir/'verify-current'; (root/'diagnostics').mkdir(parents=True) (root/'diagnostics/icdb-error-content.json').write_text(json.dumps( {'native_message':NATIVE_ERROR,'source':'PCBLogFile.txt'}),'utf-8') result=root/'result.json'; result.write_text('{}','utf-8') return runner.VerifyResult('FAIL',stderr='Problem encountered while opening one of the database files.', evidence_path=result,error_category='MENTOR_DATABASE_LOAD_FAILED') monkeypatch.setattr(runner,'run_verify',diagnose) result=entry.run_icdb_check(request) assert result.error_category=='ICDB_REPAIR_REQUIRED' and NATIVE_ERROR in result.stderr def test_scan_does_not_reuse_an_older_operation_error_evidence(case, monkeypatch): from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner request,_,_=case root=request.evidence_dir/'older-operation';(root/'diagnostics').mkdir(parents=True) (root/'diagnostics/icdb-error-content.json').write_text(json.dumps({'native_message':NATIVE_ERROR}),'utf-8') result=root/'result.json';result.write_text('{}','utf-8') monkeypatch.setattr(runner,'run_verify',lambda *a,**k:runner.VerifyResult('FAIL', stderr='Failed to open document.',error_category='MENTOR_DATABASE_LOAD_FAILED',evidence_path=result)) scanned=entry.run_icdb_check(request) assert scanned.error_category=='MENTOR_DATABASE_LOAD_FAILED' def test_error_capture_retains_the_new_native_corruption_detail(tmp_path): from cbb_refresh_verify.icdb_recovery import snapshot_log from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs pcb=tmp_path/'PCB/CBB.pcb'; log=pcb.parent/'LogFiles/PCBLogFile.txt' log.parent.mkdir(parents=True) evidence=tmp_path/'evidence'; (evidence/'diagnostics').mkdir(parents=True) baseline=snapshot_log(log) (evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps( {'source':str(log),'baseline':baseline}),'utf-8') log.write_text(NATIVE_ERROR,'utf-8') collect_native_failure_logs(pcb,evidence) record=json.loads((evidence/'diagnostics/icdb-error-content.json').read_text('utf-8')) assert record['source']==str(log) and record['native_message']==NATIVE_ERROR @pytest.mark.parametrize('scenario', ['current', 'other_pid', 'old', 'other_database']) def test_repeated_failure_uses_only_this_pcb_process_icdb_log(tmp_path, scenario): import os,time from cbb_refresh_verify.verify_diagnostics import collect_native_client_error root=tmp_path/'evidence'; (root/'diagnostics').mkdir(parents=True) logs=tmp_path/'Client/Logs' pid=123 started=time.time_ns()-1_000_000_000 folder=logs/('2026-11-05 12.00.00-'+str(321 if scenario=='other_pid' else pid)) folder.mkdir(parents=True) source=folder/'2026.11.05.iCDBManager.txt' database=tmp_path/'Work/database' message=f'SingleFile: ERROR: Database [{database}/icdb.dat] in old, unsupported format' if scenario=='other_database': message=message.replace(str(database),'C:/other/database') source.write_text(f'PID [{pid}]\n'+message,'utf-8') if scenario=='old':os.utime(source,ns=(started-1_000_000_000,started-1_000_000_000)) (root/'diagnostics/native-client-context.json').write_text(json.dumps( {'native_pid':pid,'started_ns':started,'client_log_root':str(logs),'database':str(database)}),'utf-8') collect_native_client_error(root) result=root/'diagnostics/icdb-error-content.json' assert result.exists() is (scenario=='current') if result.exists(): record=json.loads(result.read_text('utf-8')) assert record['native_pid']==pid and 'unsupported format' in record['native_message']