Files
beads-group/scripts/integration-smoke.mjs
T

31 lines
3.0 KiB
JavaScript

import assert from 'node:assert/strict'
import {randomUUID} from 'node:crypto'
let base=process.env.PERLER_TEST_API_URL
if(!base||!process.env.PERLER_TEST_ADMIN_PASSWORD)throw new Error('必须显式指定隔离测试服务 PERLER_TEST_API_URL 与 PERLER_TEST_ADMIN_PASSWORD')
let checks=0
async function call(path,method='GET',data,token='',expected=200){let r=await fetch(base+'/api'+path,{method,headers:{'Content-Type':'application/json',...(token?{Authorization:'Bearer '+token}:{})},body:data===undefined?undefined:JSON.stringify(data)});assert.equal(r.status,200);let b=await r.json();assert.equal(b.code,expected,`${path}: ${b.msg}`);checks++;return b.data}
let health=await call('/health');assert.equal(health.service,'perler-beads')
let guest=await call('/auth/login','POST',{code:'dev-guest'})
let admin=await call('/auth/admin','POST',{username:process.env.PERLER_TEST_ADMIN_USERNAME||'admin',password:process.env.PERLER_TEST_ADMIN_PASSWORD})
let colors=await call('/palettes?storeId=store-001')
let capabilities=await call('/capabilities');assert.equal(capabilities.payment.enabled,false);assert.equal(capabilities.storedValue.enabled,false)
await call('/admin/patterns','GET',undefined,guest.token,401)
let p=await call('/patterns','POST',{title:'自动联调临时作品',category:'测试',width:2,height:2,cells:[colors[0].code,null,colors[1].code,colors[0].code],colors:colors.slice(0,2),visibility:'private',storeId:'store-001'},guest.token)
await call('/patterns/'+p.id,'GET',undefined,'',403)
await call('/favorites/'+p.id,'PUT',{active:true},guest.token)
assert.ok((await call('/favorites','GET',undefined,guest.token)).includes(p.id))
await call('/visits','POST',{storeId:'store-001',tableCode:'T01'},guest.token)
let body={kind:'print',note:'自动联调:幂等与状态变更',patternId:p.id,requestId:randomUUID()}
let tasks=await Promise.all(Array.from({length:4},()=>call('/service-requests','POST',body,guest.token)))
assert.equal(new Set(tasks.map(t=>t.id)).size,1)
await call('/service-requests','POST',{...body,note:'不同内容'},guest.token,409)
let task=tasks[0]
await call('/service-requests/'+task.id,'PATCH',{status:'completed'},guest.token,401)
await call('/service-requests/'+task.id,'PATCH',{status:'accepted'},admin.token)
await call('/service-requests/'+task.id,'PATCH',{status:'completed'},admin.token)
let saved=(await call('/service-requests','GET',undefined,guest.token)).find(t=>t.id===task.id);assert.equal(saved.status,'completed')
let print=await fetch(base+'/api/patterns/'+p.id+'/print?pitchMm=5',{headers:{Authorization:'Bearer '+admin.token}});assert.ok(print.headers.get('content-type').includes('image/svg'));let svg=await print.text();assert.match(svg,/50 mm calibration/);assert.match(svg,/width="5.00" height="5.00"/);checks++
await call('/favorites/'+p.id,'PUT',{active:false},guest.token)
await call('/patterns/'+p.id,'DELETE',undefined,guest.token)
console.log(JSON.stringify({checks,concurrentReplayCount:4,createdTaskId:task.id,status:'passed'},null,2))