Files
order-notification-automation/tests/bpm/scan-delivery.test.cjs
T

17 lines
2.3 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
const {BpmScanStore}=require('../../src/modules/bpm/scan-store.cjs');
test('committed delivery survives restart encrypted and acknowledgement releases only transport payload',t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'scan-delivery-'));let store=new BpmScanStore(dir);t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});
const runId=store.begin('c','synthetic','todo'),payload={runId,connectionId:'c',rows:[{title:'synthetic-private-title'}],startedAt:'2026-10-21T01:00:00Z',finishedAt:'2026-10-21T01:00:01Z'};
store.complete(runId,'c',[{arrivalDay:'2026-10-21',taskId:'t',source:'todo:fixture'}],{id:'c',owner:'synthetic'},{delivery:payload});
const bytes=store.db.prepare('SELECT payload FROM execution_deliveries WHERE run_id=?').get(runId).payload;assert.equal(Buffer.from(bytes).includes(Buffer.from('synthetic-private-title')),false);
store.close();store=new BpmScanStore(dir);assert.deepEqual(store.deliveries.pending('c',Date.now())[0].payload,payload);store.deliveries.acknowledge(runId);assert.equal(store.deliveries.summary('c').pending,0);
assert.equal(store.db.prepare('SELECT payload FROM execution_deliveries WHERE run_id=?').get(runId).payload,null);assert.equal(store.status().runs.find(run=>run.id===runId).status,'success');assert.equal(store.list({kind:'todo'}).total,1);
});
test('a damaged transport payload remains visible as delivery failure without falsifying its successful read',t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'scan-delivery-damaged-')),store=new BpmScanStore(dir);t.after(()=>{store.close();fs.rmSync(dir,{recursive:true,force:true});});
const id=store.begin('c','synthetic','todo');store.complete(id,'c',[],{id:'c'},{delivery:{runId:id,connectionId:'c',rows:[],startedAt:'2026-10-21T01:00:00Z',finishedAt:'2026-10-21T01:00:01Z'}});
store.db.prepare('UPDATE execution_deliveries SET payload=? WHERE run_id=?').run(Buffer.from('damaged'),id);assert.deepEqual(store.deliveries.pending('c',Date.now()),[]);assert.equal(store.deliveries.summary('c').status,'error');assert.equal(store.deliveries.summary('c').pending,1);assert.equal(store.status().runs.find(run=>run.id===id).status,'success');
});