true]);$commercial=json_decode(file_get_contents($s['root'].'/single-commercial.json'),true); $select=function($role)use($profiles,$f,$app){Auth::forgetGuards();Auth::shouldUse('web');$u=App\Models\User::findOrFail($profiles[$role]['user_id']);Auth::guard('web')->setUser($u);$r=Illuminate\Http\Request::create('/api/workshop-equipment','GET');$r->attributes->set('sgf_module','workshop');$r->setUserResolver(fn()=>$u);$session=app('session')->driver();$session->start();$session->put('company_context',$f['scope']+['module_id'=>'workshop','token'=>'equipment-'.$role]);$r->setLaravelSession($session);$app->instance('request',$r);app(CompanyContext::class)->selectFor($u->id,1,2,2);return $u->id;}; $reject=function(Closure $fn,int $status,string $label)use($ok){$bad=false;try{$fn();}catch(Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e){$bad=$e->getStatusCode()===$status;}catch(Illuminate\Validation\ValidationException){$bad=$status===422;}$ok($bad,$label);};$key=fn()=>(string)Str::uuid();set_exception_handler(function($e){echo json_encode(['passed'=>false,'type'=>get_class($e),'message'=>$e->getMessage(),'frames'=>array_map(fn($f)=>[basename($f['file']??''),$f['line']??0],array_slice($e->getTrace(),0,10))]).PHP_EOL;exit(1);}); $host->beginTransaction();$before=$host->table('wo_equipment')->orderBy('id')->get()->toJson();$beforeIds=$host->table('wo_equipment')->pluck('id')->all();$sourceOrders=$host->table('wo_orders')->orderBy('id')->get()->toJson();$nativeBefore=$native->table('stock_movements')->orderBy('id')->get()->toJson(); try{ $manager=$select('workshop_manage');$w=app(WorkshopEquipment::class);$data=['client_id'=>$commercial['client'],'identifier'=>'QA-EQUIPMENT-'.Str::uuid(),'description'=>'Equipamento fictício de catálogo independente','serial'=>'QA-SERIE-001','reason'=>'Registo individual fundamentado do equipamento de teste.'];$command=$key();$p=$w->preview($manager,$data,$command);$ok($before===$host->table('wo_equipment')->orderBy('id')->get()->toJson(),'preview never registers equipment');$ok(!$p['impact']['creates_order']&&!$p['impact']['financial_effect'],'registration preview declares no order or financial effect'); $created=$w->confirm($manager,$p['preview']);$retry=$w->confirm($manager,$p['preview']);$ok($retry['replayed']&&$retry['id']===$created['id'],'confirmation replay returns original equipment');$ok($sourceOrders===$host->table('wo_orders')->orderBy('id')->get()->toJson(),'independent registration creates no order automatically'); $reject(fn()=>$w->preview($manager,$data,$key()),409,'duplicate individual identifier refused');$different=$w->preview($manager,array_replace($data,['identifier'=>'QA-OTHER-'.Str::uuid()]),$command);$reject(fn()=>$w->confirm($manager,$different['preview']),409,'same command key cannot register another equipment'); $reject(fn()=>$w->preview($manager,array_replace($data,['client_id'=>999999,'identifier'=>'QA-X']),$key()),403,'foreign or unassociated client refused');$reject(fn()=>$w->preview($manager,array_replace($data,['identifier'=>' ']),$key()),422,'blank identifier refused after trimming'); $list=$w->listing($manager,['search'=>$data['identifier'],'per_page'=>25]);$ok($list['records']->total()===1&&$list['records']->items()[0]->id===$created['id'],'literal bounded search finds one registered equipment');$list=$w->listing($manager,['search'=>'%','per_page'=>100]);$ok($list['records']->total()===0,'wildcard remains a literal in page100 search'); $tech=$select('workshop_record');$reject(fn()=>$w->preview($tech,$data,$key()),403,'technician cannot register equipment using read authority');$list=$w->listing($tech,['search'=>$data['identifier']]);$ok($list['records']->total()===0,'unassigned equipment hidden from technician');$manager=$select('workshop_manage'); $order=app(WorkshopWorkflow::class)->open($manager,['client_id'=>$commercial['client'],'equipment_id'=>$created['id'],'reference'=>'OT-EQUIP-'.Str::uuid(),'request'=>'Reutilizar identificação previamente registada.','location'=>'Unidade QA','evidence'=>'EQUIPMENT-REUSE-QA'],$key());$ok($order['equipment_id']===$created['id'],'original order writer reuses the same equipment without another identification'); $ok($before===$host->table('wo_equipment')->whereIn('id',$beforeIds)->orderBy('id')->get()->toJson(),'every pre-existing equipment record is unchanged');$ok($nativeBefore===$native->table('stock_movements')->orderBy('id')->get()->toJson(),'native stock book unchanged'); }finally{while($host->transactionLevel()>0)$host->rollBack();} file_put_contents($work.'/workshop-equipment-result.json',json_encode(['passed'=>true,'checks'=>$checks,'proof_changes_rolled_back'=>true,'new_rights'=>0,'browser_visual_review'=>false],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'checks'=>count($checks),'proof'=>$work.'/workshop-equipment-result.json']).PHP_EOL;