table('fx_versions')->count();$commandsBefore=$host->table('fx_commands')->count();$journalsBefore=$native->table('gl_journals')->orderBy('id')->get()->toJson();$domainBefore=[];foreach(['si_invoices','sd_outputs','sr_records','sr_effects','cash_events']as$t)$domainBefore[$t]=$host->table($t)->orderBy('id')->get()->toJson(); $host->beginTransaction();try{ $a0=(int)$host->table('fx_versions')->where('kind','assessment')->max('version');$f0=(int)$host->table('fx_versions')->where('kind','form')->max('version');$selectFiscal(13);$source=$w->listing(13,[])['source'];$ok($source['base']==='862.07'&&$source['vat']==='137.93'&&$source['total']==='1000.00','same frozen FR QA1000 exact base/VAT/total'); $p=$preview(13,$input('assessment','prepare',$a0));$ok($host->table('fx_versions')->count()===$before,'preview rolls back whole fiscal write');$ok($p['impact']['deductible_vat']===null&&$p['impact']['tax_payable']===null&&!$p['impact']['company_assessment_complete'],'unknown deduction/payable never zero or complete company assessment'); $reject(fn()=>$preview(17,$input('assessment','prepare',$a0)),403,'read-only profile cannot prepare');$reject(fn()=>$preview(13,$input('assessment','prepare',$a0,['source_output_id'=>$b['source_output_id']+9999])),403,'foreign/unbound output refused');$reject(fn()=>$preview(13,$input('assessment','prepare',$a0,['period'=>'2026-10'])),403,'foreign period refused');$reject(fn()=>$preview(13,$input('assessment','prepare',$a0,['rate'=>'0'])),422,'client cannot inject rate'); $selectFiscal(13);$first=$w->confirm(13,$p['preview']);$ok($first['version']===$a0+1&&$first['state']==='prepared','explicit output book prepared');$again=$w->confirm(13,$p['preview']);$ok($again['replayed']&&$again['id']===$first['id']&&$host->table('fx_versions')->count()===$before+1,'same command recovered without duplicate'); $reject(fn()=>$preview(14,$input('form','prepare',$f0)),403,'reviewer cannot prepare form');$reject(fn()=>$preview(13,$input('form','prepare',$f0)),409,'unreviewed book blocks form');$reject(fn()=>$preview(13,$input('assessment','review',$a0+1)),403,'preparer cannot review'); $review1=$preview(14,$input('assessment','review',$a0+1));$review2=$preview(14,$input('assessment','review',$a0+1));$selectFiscal(14);$reviewed=$w->confirm(14,$review1['preview']);$ok($reviewed['state']==='reviewed','distinct reviewer confirms exact book version');$reject(fn()=>$w->confirm(14,$review2['preview']),409,'second prior version refused after first commit'); [$form]=$command(13,$input('form','prepare',$f0));$ok($form['state']==='prepared'&&strlen($form['impact']['artifact_sha256'])===64,'map prepared with frozen checksum and reviewed book');$selectFiscal(13);$reject(fn()=>$w->export(13,$form['id']),409,'unreviewed map not exported');[$formReviewed]=$command(14,$input('form','review',$f0+1));$selectFiscal(17);$csv=$w->export(17,$formReviewed['id']);$ok(str_contains($csv['bytes'],'SEM VALIDADE FISCAL')&&str_contains($csv['bytes'],'INDETERMINADO')&&str_contains($csv['bytes'],'862.07;137.93;1000.00'),'reviewed internal map exact, marked synthetic, no invented payable');$ok(hash('sha256',$csv['bytes'])===$csv['sha256'],'frozen export bytes and checksum exact'); $own=$selectFiscal(13);$oldKey=$host->table('fx_commands')->where('actor_id',13)->whereRaw("(result->>'id')::bigint=?",[$first['id']])->value('command_key');$ok($w->receipt($own,$oldKey)['result']['id']===$first['id'],'historical own receipt does not execute again');$selectFiscal(14);$reject(fn()=>$w->receipt(14,$oldKey),404,'another actor cannot recover own-key receipt'); $selectFiscal(17);$page=$w->listing(17,['per_page'=>25]);$ok($page['form_current_review']&&$page['assessment_current_review']&&count($page['events']['data'])>=4,'current book/map and paged history visible');$ok(!isset($page['form']['data']['artifact_csv']),'raw artifact not loaded in listing');$reject(fn()=>$w->listing(17,['per_page'=>500]),422,'unbounded page refused'); // A new book preparation makes the reviewed map stale, without changing the old artifact. [$new]=$command(13,$input('assessment','prepare',$a0+2));$selectFiscal(17);$ok(!$w->listing(17,[])['form_current_review'],'successor makes prior map visibly stale');$reject(fn()=>$w->export(17,$formReviewed['id']),409,'stale map export requires new conference'); // A revoked preparer cannot be approved, even if the proposal/version remains the same. $cut=$preview(14,$input('assessment','review',$new['version']));$host->beginTransaction();try{$host->table('permission_grants')->where(['user_id'=>13,'permission'=>'fiscal_assessments.prepare','unit_id'=>2])->delete();$selectFiscal(14);$reject(fn()=>$w->confirm(14,$cut['preview']),403,'preparer authority revalidated at confirmation');}finally{$host->rollBack();} $selectFiscal(14,'changed');$reject(fn()=>$w->confirm(14,$cut['preview']),409,'context generation change requires conference');$selectFiscal(14);$c=json_decode(Crypt::decryptString($cut['preview']),true);$c['observed']=time()-601;$reject(fn()=>$w->confirm(14,Crypt::encryptString(json_encode($c))),409,'expired preview refused'); // Failure after event insert must roll back event and audit, then recover same command. $host->beginTransaction();try{$host->statement("CREATE FUNCTION fx_fail_test() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected FX failure'; END; $$");$host->statement('CREATE TRIGGER fx_fail_test BEFORE INSERT ON fx_commands FOR EACH ROW EXECUTE FUNCTION fx_fail_test()');$n=$host->table('fx_versions')->count();$failed=false;try{$w->confirm(14,$cut['preview']);}catch(Illuminate\Database\QueryException){$failed=true;}$ok($failed&&$host->table('fx_versions')->count()===$n,'injected receipt failure leaves no event');}finally{$host->rollBack();}$selectFiscal(14);$recovered=$w->confirm(14,$cut['preview']);$ok($recovered['state']==='reviewed','same command recovers after injected failure'); $host->beginTransaction();try{$host->statement('ALTER TABLE sd_outputs DISABLE TRIGGER USER');$host->table('sd_outputs')->where('id',$b['source_output_id'])->update(['pdf_sha256'=>str_repeat('0',64)]);$selectFiscal(17);$blocked=$w->listing(17,[]);$ok($blocked['blocked']!==null&&$blocked['source']===null&&!$blocked['form_current_review'],'changed origin blocks current reading without zero');}finally{$host->rollBack();} }finally{$host->rollBack();} $ok($host->table('fx_versions')->count()===$before&&$host->table('fx_commands')->count()===$commandsBefore,'regression business transaction fully reverted');foreach($domainBefore as$t=>$hash)$ok($host->table($t)->orderBy('id')->get()->toJson()===$hash,'unchanged original '.$t);$ok($native->table('gl_journals')->orderBy('id')->get()->toJson()===$journalsBefore,'original ledger unchanged');file_put_contents($work.'/iva-result.json',json_encode(['passed'=>true,'checks'=>$checks,'regression_only'=>true,'test_writes_rolled_back'=>true,'new_journals'=>0,'new_demo'=>false],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/iva-result.json']).PHP_EOL;