23 lines
2.0 KiB
JavaScript
23 lines
2.0 KiB
JavaScript
const fs=require('node:fs');
|
|
const state=JSON.parse(fs.readFileSync('deliverables/demo-api-progress.json'));
|
|
const secret=JSON.parse(fs.readFileSync('.demo-api-private.json'));
|
|
const base=state.base;
|
|
async function api(path,method='GET',body,token){const r=await fetch(base+path,{method,redirect:'error',signal:AbortSignal.timeout(25000),headers:{'Content-Type':'application/json',...(token?{Authorization:`Bearer ${token}`}:{})},body:body?JSON.stringify(body):undefined});const d=await r.json();if(!r.ok)throw new Error(`HTTP ${r.status} ${path}`);return d.data&&d.meta?d.data:d;}
|
|
(async()=>{
|
|
const results={};
|
|
for(let i=0;i<4;i++){
|
|
const login=await api('/auth/login','POST',{email:`oudelaa-api-demo-${i+5}@example.invalid`,password:secret.password});
|
|
try{
|
|
const key=`cart-${i}`;
|
|
if(!state.done[key]){state.done[key]=await api('/marketplace/cart/items','POST',{listingId:state.done[`product-${i+8}`]._id,quantity:1},login.accessToken);fs.writeFileSync('deliverables/demo-api-progress.json',JSON.stringify(state,null,2));}
|
|
if(i===0){results.notifications=await api('/notifications/unread-count','GET',undefined,login.accessToken);}
|
|
}finally{await api('/auth/logout','POST',{refreshToken:login.refreshToken},login.accessToken);}
|
|
}
|
|
const home=await api('/marketplace/home');results.marketplace=home.summary;
|
|
const image=home.sections.listings.items[0].imageUrls[0];
|
|
const url=new URL(image);if(url.hostname!=='s3.cumin.dev')throw new Error('Unexpected media origin');
|
|
const response=await fetch(image,{redirect:'error',signal:AbortSignal.timeout(20000)});results.image={status:response.status,contentType:response.headers.get('content-type')};await response.arrayBuffer();
|
|
results.operations=Object.keys(state.done).length;results.pending=state.pending;
|
|
fs.writeFileSync('deliverables/demo-api-verification.json',JSON.stringify(results,null,2));console.log(JSON.stringify(results));
|
|
})().catch(e=>{console.error(e.message.startsWith('HTTP')?e.message:'Verification failed; no credentials logged.');process.exitCode=1;});
|