import fs from 'node:fs';import assert from 'node:assert/strict';import crypto from 'node:crypto';import{session}from'./sgf-single-http.mjs';import{checkedWorkshopReport,workshopPageCsv}from'../resources/js/workshop-report-view.js'; const checks=[],ok=(v,n)=>{assert(v,n);checks.push(n);},s=await session('sgf:workshop_manage','workshop'),path='/api/workshop/orders?',get=async(q,who=s)=>{const r=await who.get(path+new URLSearchParams(q));assert.equal(r.status,200);return checkedWorkshopReport(await r.json(),q);}; const all=await get({page:1,per_page:100}),chosen=all.orders.data.find(o=>o.equipment_id);assert(chosen);const q={page:1,per_page:25,equipment_id:chosen.equipment_id},data=await get(q);ok(data.equipment.id===chosen.equipment_id&&data.equipment.identifier===chosen.equipment,'current equipment metadata exact');ok(data.orders.data.length>0&&data.orders.data.every(o=>o.equipment_id===chosen.equipment_id),'all actual rows use chosen equipment');ok(data.orders.data.every(o=>JSON.stringify(o)===JSON.stringify(all.orders.data.find(a=>a.id===o.id))),'source order fields unchanged');const large=await get({...q,per_page:100});ok(JSON.stringify(large.orders.data)===JSON.stringify(data.orders.data),'25 and100 actual rows equivalent');const narrow=await get({...q,state:chosen.state,search:chosen.reference});ok(narrow.orders.data.some(o=>o.id===chosen.id),'equipment combines state and search');const instant=new Date(chosen.created_at.replace(' ','T').replace(/([+-]\d{2})$/,'$1:00')),day=new Intl.DateTimeFormat('en-CA',{timeZone:'Africa/Johannesburg',year:'numeric',month:'2-digit',day:'2-digit'}).format(instant),dated=await get({...q,from:day,to:day});ok(dated.orders.data.some(o=>o.id===chosen.id),'equipment combines explicit local opening period');const far=await get({...q,page:100000});ok(far.orders.data.length===0&&far.equipment.id===chosen.equipment_id,'empty far page retains selected identity');const cat=await s.get('/api/workshop-equipment?per_page=100');assert.equal(cat.status,200);const catalogue=await cat.json(),unused=catalogue.records.data.find(e=>!all.orders.data.some(o=>o.equipment_id===e.id));assert(unused);const empty=await get({...q,equipment_id:unused.id});ok(empty.orders.total===0&&empty.equipment.id===unused.id,'existing independent equipment returns zero no fallback');for(const bad of[{equipment_id:2147483647},{equipment_id:0},{equipment_id:chosen.equipment_id,tenant_id:2}]){const r=await s.get(path+new URLSearchParams(bad));ok(r.status===(bad.equipment_id===2147483647?403:422),'invalid or unavailable equipment context refused '+JSON.stringify(bad));} const tech=await session('sgf:workshop_record','workshop'),assigned=await get(q,tech);ok(assigned.orders.data.every(o=>o.technician_id===tech.identity.user.id),'technician assignment still enforced');const denied=await tech.get(path+new URLSearchParams({...q,equipment_id:unused.id}));ok(denied.status===403,'technician cannot enumerate unassigned equipment');const other=await session('sgf:commercial_prepare','commercial'),r=await other.get(path+new URLSearchParams(q));ok([403,409].includes(r.status),'cross module refused');ok(workshopPageCsv(data,q).includes(data.equipment.identifier),'CSV explicitly identifies chosen equipment'); const root=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')).root+'/private/receipt-composition-cc50a276c10c',manifest=JSON.parse(fs.readFileSync(root+'/app/public/build/manifest.json'));for(const e of Object.values(manifest).filter(v=>v.isEntry||v.file.includes('WorkshopOrderList'))){const r=await fetch('http://127.0.0.1:8085/build/'+e.file),bytes=Buffer.from(await r.arrayBuffer());ok(r.status===200&&/javascript/.test(r.headers.get('content-type'))&&crypto.createHash('sha256').update(bytes).digest('hex')===crypto.createHash('sha256').update(fs.readFileSync(root+'/app/public/build/'+e.file)).digest('hex'),'served JS MIME and SHA '+e.file);}const file=root+'/equipment-orders-http-20261111.json';fs.writeFileSync(file,JSON.stringify({passed:true,checks,business_commands:0,new_rights:0,browser_visual_review:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length,proof:file}));