'use strict'; const test=require('node:test');const assert=require('node:assert/strict');const {orderWorkflowApi}=require('../src/order-workflow-api.cjs'); const user={username:'admin',role:'admin'}; async function request(route,body,{role='admin',pipeline,workflow}={}){let result;await orderWorkflowApi({user:{...user,role},sessionId:'s',route,method:body===undefined?'GET':'POST',body,pipeline,workflow:workflow||{},reply:(status,value)=>result={status,value}});return result;} test('HTTP cannot forge BPM provenance, PDF success, SMB success, or caller-owned batches',async()=>{ for(const [route,body] of [['/api/order-workflow/identify',{provenance:'bpm-readonly',detail:{}}],['/api/order-workflow/orders/o/pdf-success',{jobId:'forged'}],['/api/order-workflow/orders/o/archive-success',{archiveId:'forged'}],['/api/order-workflow/batches/b/freeze',{groups:{},templates:{}}]])await assert.rejects(()=>request(route,body),error=>[403,404,409].includes(error.status)); }); test('sample check only accepts a persisted arrival reference and verifies configuration admin role',async()=>{ const pipeline={checkRule:async(id,input,principal)=>({valid:true,checkId:'c',id,input,actor:principal.user.username})}; const ok=await request('/api/order-workflow/rules/r/check',{connectionId:'c',source:'v4',taskId:'t'},{pipeline});assert.equal(ok.value.checkId,'c'); await assert.rejects(()=>request('/api/order-workflow/rules/r/check',{connectionId:'c',source:'v4',taskId:'t',detail:{forged:true}},{pipeline}),error=>error.status===400); await assert.rejects(()=>request('/api/order-workflow/rules/r/check',{connectionId:'c',source:'v4',taskId:'t'},{role:'operator',pipeline}),error=>error.status===403); }); test('viewer can inspect workflow and cannot retry or resolve and unknown requires admin evidence',async()=>{ const workflow={listOrders:()=>[],listBatches:()=>[],resolveBatch:()=>({status:'sent'})},pipeline={arrivals:()=>[],config:()=>({enabled:false}),retry:async()=>({status:'archived'})}; assert.equal((await request('/api/order-workflow',undefined,{role:'viewer',workflow,pipeline})).status,200); await assert.rejects(()=>request('/api/order-workflow/orders/o/retry',{},{role:'viewer',workflow,pipeline}),error=>error.status===403); await assert.rejects(()=>request('/api/order-workflow/batches/b/resolve',{resolution:'retry',note:'已核验邮件记录'},{role:'operator',workflow,pipeline}),error=>error.status===403); assert.equal((await request('/api/order-workflow/batches/b/resolve',{resolution:'sent',note:'已核验邮件记录'},{workflow,pipeline})).value.status,'sent'); });