Files
SGF/scripts/stock-trace-demo.php
2026-11-04 09:37:20 +02:00

21 lines
4.3 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
$s=json_decode(file_get_contents(getenv('RECEIPT_COMPOSITION_STATE')),true);$out=$s['root'].'/stock-trace-demo.json';if(!($s['stockTrace']??false)||is_file($out))throw new LogicException('Fresh trace demonstration required');
require $s['app'].'/vendor/autoload.php';$app=require $s['app'].'/bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$f=json_decode(file_get_contents($s['root'].'/fixture.json'),true);$n=json_decode(file_get_contents($s['root'].'/native-fixture.json'),true);config(['database.connections.trace_demo'=>$s['nativeConnection']]);$db=DB::connection('trace_demo');$checks=[];$ok=function($v,$name)use(&$checks){if(!$v)throw new RuntimeException($name);$checks[]=$name;};
$physical=fn()=>hash('sha256',json_encode([$db->table('goods_receipts')->orderBy('id')->get(),$db->table('stock_movements')->orderBy('id')->get(),$db->table('audit_logs')->orderBy('id')->get(),DB::table('permission_grants')->orderBy('id')->get()]));$before=$physical();
$ok($db->table('requisitions')->where('tenant_id',$n['tenant'])->count()===0&&$db->table('goods_receipts')->count()===0&&$db->table('stock_movements')->count()===0,'fresh trace demonstration begins without main business operations');
$client=new GuzzleHttp\Client(['base_uri'=>'http://127.0.0.1:8084','http_errors'=>false,'timeout'=>25]);$jar=new GuzzleHttp\Cookie\CookieJar;$r=$client->get('/qa/enter/validator',['cookies'=>$jar,'query'=>['module'=>'stock']]);$v=json_decode((string)$r->getBody(),true);$options=['cookies'=>$jar,'headers'=>['Accept'=>'application/json','X-SGF-Context'=>$v['context'],'X-CSRF-TOKEN'=>$v['csrf']]];
$r=$client->get('/api/stock-read?view=movements',$options);$d=json_decode((string)$r->getBody(),true);$ok($r->getStatusCode()===200&&$d['rows']===[]&&$d['capabilities']['receipt_trace']===true,'authorised Stock capability is present without inventing a movement or selected receipt');
$r=$client->get('/',$options);$ok($r->getStatusCode()===200&&str_contains((string)$r->getBody(),'id="app"'),'existing React host is mounted for explicit Stock entry');
$manifest=json_decode(file_get_contents($s['app'].'/public/build/manifest.json'),true);$entry=$manifest['resources/js/app.jsx']['file'];$r=$client->get('/build/'.$entry);$ok($r->getStatusCode()===200&&hash('sha256',(string)$r->getBody())===hash_file('sha256',$s['app'].'/public/build/'.$entry),'served build contains the current trace integration artifact');
$auditBefore=DB::table('identity_audit')->count();
$r=$client->post('/api/context',$options+['json'=>['tenant_id'=>$f['tenant'],'company_id'=>$f['company'],'unit_id'=>$f['unit'],'module_id'=>'purchasing']]);$d=json_decode((string)$r->getBody(),true);
$ok($r->getStatusCode()===200&&$d['context']['module_id']==='purchasing'&&$d['context']['token']!==$v['context']&&$d['context']['company']['id']===$f['company']&&$d['context']['unit']['id']===$f['unit'],'real existing context API changes only the selected module and renews its token');
$url='/api/purchasing-receipts/movements/999999/origin';$r=$client->get($url,$options);$ok($r->getStatusCode()===409,'old Stock token cannot be reused at Purchasing destination');
$options['headers']['X-SGF-Context']=$d['context']['token'];$r=$client->get($url,$options);$ok($r->getStatusCode()===403,'current destination refuses a nonexistent movement rather than selecting another source');
$r=$client->get('/purchasing/receipts?movement=999999',$options);$ok($r->getStatusCode()===200&&str_contains((string)$r->getBody(),'data-focus-enabled="true"'),'new receipt startup is mounted without preparing an operation');
$r=$client->get('/stock-receipt-focus.mjs');$ok($r->getStatusCode()===200&&hash('sha256',(string)$r->getBody())===hash_file('sha256',$s['app'].'/public/stock-receipt-focus.mjs'),'served receipt resolver helper matches the reviewed local artifact');
$ok($physical()===$before&&DB::table('identity_audit')->count()===$auditBefore+1,'module selection adds only its existing context audit while physical evidence and rights stay unchanged');
file_put_contents($out,json_encode(['passed'=>true,'checks'=>$checks,'physical_before'=>$before,'physical_after'=>$physical(),'context_changes'=>1,'business_commands'=>0,'visual_review'=>false],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo count($checks)." stock trace demo checks passed.\n";