import json from pathlib import Path import pytest from dx_productivity.trial_task_center.errors import TaskCenterError from dx_productivity.trial_task_center.native_helper import read_snapshot, transaction_binding from dx_productivity.trial_task_center.quick_run import _encode_vbs_field @pytest.fixture def proof_case(tmp_path): project=tmp_path/'board.prj';project.touch() root=tmp_path/'data'/'quick-run';root.mkdir(parents=True) row=dict(task_id='task',job_id='job',project_path=str(project),schematic_name='MAIN',sheet_number='1', old_part='OLD',new_part='NEW',value_mode='preserve',target_scope='all') snapshot=root/'snapshot-job.tsv' snapshot.write_text(f'M\t{project}\tOLD\tNEW\tMAIN:1\tpreserve\nC\tuid\tR1\t0\t0\t0\t1\tpins\n',encoding='utf8') meta,components=read_snapshot(snapshot) report={'ok':True,'stage':'native_replace_verified','transaction':transaction_binding( snapshot,meta,components,['lib','NEW','sym','','name','label'],'sym'), 'batch_attempt':dict(task_id='task',job_id='job',project_path=str(project), session_id='a'*32,attempt_id='b'*32,generation=1)} Path(str(snapshot)+'.native.json').write_text(json.dumps(report),encoding='utf8') (root/'post-state-job.tsv').write_text('job_id\tproject_path\tschematic_name\tsheet_number\tsemantic_state\n'+ '\t'.join(_encode_vbs_field(row[key]) for key in ('job_id','project_path','schematic_name','sheet_number'))+ '\t'+_encode_vbs_field('S\tfull_sheet_v1\r\nR1=NEW\r\n')+'\n',encoding='utf8') return tmp_path/'data',root,row def test_receipt_proof_binds_native_snapshot_attempt_and_semantic_state(proof_case): from dx_productivity.trial_task_center.execution_proof import capture_result, proof_intact data,root,row=proof_case proof=capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True) assert proof['baseline']['state']=='S\tfull_sheet_v1\r\nR1=NEW\r\n' assert proof['native_attempt']['attempt_id']=='b'*32 assert proof_intact(proof) # Active handoff can be archived without breaking committed proof identity. root.rename(root.with_name('archived')) assert proof_intact(proof) Path(proof['files'][0]['path']).write_text('changed',encoding='utf8') assert not proof_intact(proof) @pytest.mark.parametrize('damage',['missing_report','foreign_attempt','snapshot_changed','missing_baseline']) def test_unbound_result_cannot_be_imported_as_a_completion_receipt(proof_case,damage): from dx_productivity.trial_task_center.execution_proof import capture_result data,root,row=proof_case report=root/'snapshot-job.tsv.native.json' if damage=='missing_report':report.unlink() elif damage=='foreign_attempt': record=json.loads(report.read_text());record['batch_attempt']['job_id']='other';report.write_text(json.dumps(record)) elif damage=='snapshot_changed': with (root/'snapshot-job.tsv').open('a') as stream:stream.write('changed\n') else:(root/'post-state-job.tsv').unlink() with pytest.raises(TaskCenterError,match='quick_run_uncertain'): capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True) def test_proof_changed_during_binding_is_never_committed(proof_case,monkeypatch): from dx_productivity.trial_task_center.execution_proof import capture_result from dx_productivity.trial_task_center import native_helper data,root,row=proof_case original=native_helper.require_resumable_transaction def changed(snapshot,*args): result=original(snapshot,*args) with Path(snapshot).open('a') as stream:stream.write('changed after validation\n') return result monkeypatch.setattr(native_helper,'require_resumable_transaction',changed) with pytest.raises(TaskCenterError):capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True) def test_success_without_save_close_code_is_not_a_verified_native_receipt(proof_case): from dx_productivity.trial_task_center.execution_proof import capture_result data,root,row=proof_case with pytest.raises(TaskCenterError):capture_result(data,root,row,('saved_ok','','unverified close'),strict=True)