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

6 lines
4.3 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
$s=json_decode(file_get_contents(getenv('RECEIPT_COMPOSITION_STATE')),true,64,JSON_THROW_ON_ERROR);$out=$s['root'].'/supplier-demo-smoke.json';if(file_exists($out))throw new LogicException('Smoke proof preserved');require $s['app'].'/vendor/autoload.php';$app=require $s['app'].'/bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();$n=json_decode(file_get_contents($s['root'].'/native-fixture.json'),true);config(['database.connections.supplier_smoke'=>$s['nativeConnection']]);$db=DB::connection('supplier_smoke');$checks=[];$ok=function($v,$name)use(&$checks){if(!$v)throw new RuntimeException($name);$checks[]=$name;};$fingerprint=function()use($db){$h=hash_init('sha256');foreach(['suppliers','quotations','quotation_items','proposal_evaluations','purchase_awards','purchase_award_approvals','purchase_orders','goods_receipts','stock_movements','budget_entries','audit_logs']as$t)foreach($db->table($t)->orderBy('id')->get()as$row)hash_update($h,json_encode((array)$row));return hash_final($h);};$before=$fingerprint();$empty=true;foreach(['quotations','proposal_evaluations','purchase_awards','purchase_orders','goods_receipts','stock_movements']as$t)$empty=$empty&&$db->table($t)->count()===0;$ok($empty,'demo has no proposals evaluations awards orders receipts or movements');$client=new GuzzleHttp\Client(['base_uri'=>'http://127.0.0.1:8067','http_errors'=>false,'timeout'=>25]);
foreach(['actor','issuer','validator']as$role){$jar=new GuzzleHttp\Cookie\CookieJar;$r=$client->get('/qa/enter/'.$role,['cookies'=>$jar]);$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/purchasing-suppliers',$options);$d=json_decode((string)$r->getBody(),true);if($role==='validator'){$ok($r->getStatusCode()===403,'warehouse is denied supplier registry');continue;}$ok($r->getStatusCode()===200&&$d['can_prepare']===($role==='actor')&&$d['suppliers']['total']===3,'registry capability matches '.$role);$r=$client->get('/api/purchasing-suppliers/'.$n['supplier_ids'][0],$options);$item=json_decode((string)$r->getBody(),true);$ok($r->getStatusCode()===200&&$item['capabilities']['review']===($role==='issuer'),'review capability matches '.$role);if($role==='actor'){$r=$client->get('/purchasing/suppliers',$options);$ok($r->getStatusCode()===200&&str_contains((string)$r->getBody(),'supplier-workspace.mjs'),'supplier workspace is live');$r=$client->get('/purchasing/sourcing',$options);$ok($r->getStatusCode()===200&&str_contains((string)$r->getBody(),'/purchasing/suppliers'),'sourcing links to supplier review');$ok($client->get('/api/purchasing-suppliers/0',$options)->getStatusCode()===404&&$client->get('/api/purchasing-suppliers/0/history',$options)->getStatusCode()===404,'creation sentinel has no readable supplier record or history');$data=['operation_key'=>(string)Illuminate\Support\Str::uuid(),'revision'=>$d['new_revision'],'action'=>'create','data'=>['name'=>'Prévia QA','nuit'=>'999111222','contact'=>'QA','email'=>'qa@example.invalid','phone'=>'000','address'=>'QA','category'=>'Material','beneficiaries'=>'Sintético','document_references'=>'Evidência de ensaio','diligence_notes'=>'Prévia sintética sem criação de fornecedor.','blocked'=>false,'next_review'=>date('Y-m-d',strtotime('+1 year'))]];$r=$client->post('/api/purchasing-suppliers/0/preview',array_replace_recursive($options,['headers'=>['X-CSRF-TOKEN'=>$v['csrf']],'json'=>$data]));$p=json_decode((string)$r->getBody(),true);$ok($r->getStatusCode()===200&&!isset($p['impact']['after']['id'])&&$p['impact']['before']===null&&$p['impact']['after']['status']==='pending','new supplier preview omits temporary sequence identifier and commits nothing');}}
$r=$client->get('/supplier-workspace.mjs');$ok($r->getStatusCode()===200&&hash('sha256',(string)$r->getBody())===hash_file('sha256',$s['app'].'/public/supplier-workspace.mjs'),'live supplier script matches delivery');$ok($fingerprint()===$before,'read and rollback-only preview preserve demo business facts');file_put_contents($out,json_encode(['passed'=>true,'checks'=>$checks,'before'=>$before,'after'=>$fingerprint(),'visual_review'=>false,'persistent_changes'=>false,'preview_sequence_may_advance'=>true],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo count($checks)." demo checks passed.\n";