Files
SGF/scripts/financial-receipt-proof.php
2026-11-04 09:37:20 +02:00

10 lines
3.9 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
$out=$s['root'].'/financial-receipt-cases.json';if(file_exists($out))throw new LogicException('Preserved proof');$u=$enter('prepare');$row=DB::table('pa_commands')->where($scope)->where('actor_id',$u->id)->first();$key=$row->command_key;DB::table('pa_access')->insert($scope+['user_id'=>$u->id,'person'=>'cost-qa-person-'.$u->id,'action'=>'payments_view']);
// Infrastructure fixture only: this receipt represents no payment or other business effect.
DB::table('ps_commands')->insert($scope+['actor_id'=>$u->id,'command_key'=>$key,'request_hash'=>hash('sha256','QA receipt-only infrastructure fixture'),'result'=>json_encode(['qa_fixture'=>true,'state'=>'no_business_effect']),'created_at'=>now()]);$checks=0;$ok=function($v,$m)use(&$checks){$checks++;if(!$v)throw new LogicException($m);};$refuse=function($fn)use($ok){$caught=false;try{$fn();}catch(DomainException|InvalidArgumentException|Symfony\Component\HttpKernel\Exception\HttpException){$caught=true;}$ok($caught,'Expected domain/access/key refusal');};$before=$sourceFingerprint();$counts=[DB::table('pa_commands')->count(),DB::table('ps_commands')->count(),DB::table('ps_payments')->count()];$locks=0;$mode=null;DB::listen(function($q)use(&$locks,&$mode){if(str_contains(strtolower($q->sql),'for update'))$locks++;if($mode===null&&(str_contains($q->sql,'"pa_commands"')||str_contains($q->sql,'"ps_commands"')))$mode=['isolation'=>$q->connection->getPdo()->query('SHOW transaction_isolation')->fetchColumn(),'read_only'=>$q->connection->getPdo()->query('SHOW transaction_read_only')->fetchColumn()];});
$a=$ledger->financialReceipt($enter('prepare'),'recognition',$key);$b=$ledger->financialReceipt($enter('prepare'),'settlement',$key);$ok($a['result']===json_decode($row->result,true)&&$a['domain']==='recognition','Actual original recognition receipt');$ok($b['result']['qa_fixture']===true&&$b['domain']==='settlement','Same UUID selects only explicit settlement domain');$ok($a['operation_executed']===false&&$b['operation_executed']===false,'No execution in either domain');$ok($ledger->financialReceipt($enter('prepare'),'settlement',$key)===$b,'Repeated read identical');$ok($ledger->financialReceipt($enter('validate'),'recognition',$key)===null,'Other actor cannot read known key');$refuse(fn()=>$ledger->financialReceipt($enter('validate'),'settlement',$key));$refuse(fn()=>$ledger->financialReceipt($enter('prepare'),'unknown',$key));$refuse(fn()=>$ledger->financialReceipt($enter('prepare'),'recognition','invalid'));$ok($ledger->financialReceipt($enter('prepare'),'recognition',(string)Str::uuid())===null,'Unknown is absent, no fallback');
DB::beginTransaction();try{DB::table('pa_access')->where($scope)->where('user_id',$g['actors']['prepare'])->where('action','payments_view')->delete();$refuse(fn()=>$ledger->financialReceipt($enter('prepare'),'settlement',$key));$ok($ledger->financialReceipt($enter('prepare'),'recognition',$key)!==null,'Recognition right does not inherit settlement or vice versa');}finally{DB::rollBack();}
$ok($locks===0&&$mode===['isolation'=>'repeatable read','read_only'=>'on'],'Coherent snapshot without writer locks');$ok($before===$sourceFingerprint()&&$counts===[DB::table('pa_commands')->count(),DB::table('ps_commands')->count(),DB::table('ps_payments')->count()]&&$counts[2]===0,'All reads/probes preserve source, rights, receipts and zero payments');file_put_contents($s['root'].'/financial-receipt-fixture.json',json_encode(['key'=>$key,'result'=>$a['result'],'settlement_infrastructure_only'=>true],JSON_PRETTY_PRINT));file_put_contents($out,json_encode(['passed'=>true,'checks'=>$checks,'same_uuid_domains_separate'=>true,'read_only'=>$mode,'writer_locks'=>$locks,'no_payments_or_effects_created'=>true,'synthetic_receipt_fixture_explicit'=>true,'source_rights_unchanged'=>true],JSON_PRETTY_PRINT));echo "Financial receipts: $checks checks; actual recognition and explicit no-effect settlement fixture isolated.\n";