tests/run.cjs
'use strict';
const assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),cp=require('node:child_process');
const {lintWorkflow}=require('../lint.cjs');const {loadExports,runCode,prepare,FakeReceiver,replay,buildIncident}=require('../lib/replay.cjs');
const root=path.join(__dirname,'..'); const [main,errorWorkflow]=loadExports();
const before=JSON.parse(fs.readFileSync(path.join(root,'before/broken.json'),'utf8'));
const fixture=JSON.parse(fs.readFileSync(path.join(root,'fixtures/events.json'),'utf8'));
const one={events:[fixture.events[0]]};const clone=x=>structuredClone(x),cases=[];
const test=(name,fn)=>cases.push({name,fn}); const codes=w=>lintWorkflow(w,[errorWorkflow]).map(f=>f.code);
function mutateNode(name,fn){const w=clone(main);fn(w.nodes.find(n=>n.name===name),w);return w;}
test('before: linter exposes all five requested defect families',()=>{
const found=codes(before);for(const code of ['RETRY_BOUND','IDEMPOTENCY_HEADER','MISSING_ERROR_WORKFLOW','UNBOUNDED_CYCLE','INLINE_SECRET','DEAD_LETTER_PATH'])assert.ok(found.includes(code),code);
});
test('before: same-batch duplicate event is emitted twice',()=>{
const node=before.nodes.find(n=>n.name==='Pass Events');assert.equal(runCode(node,[{body:fixture}]).length,3);
});
test('after: main and error exports have zero static policy findings',()=>{
assert.deepEqual(lintWorkflow(main,[errorWorkflow]),[]);assert.deepEqual(lintWorkflow(errorWorkflow,[main]),[]);
});
test('after: exports are inactive and all service URLs use .invalid placeholders',()=>{
for(const w of [before,main,errorWorkflow])assert.equal(w.active,false);
for(const w of [main,errorWorkflow])for(const n of w.nodes.filter(n=>n.type.endsWith('.httpRequest')))assert.ok(n.parameters.url.includes('.invalid/'));
});
test('after: embedded Code node sources match reviewed source files',()=>{
for(const [w,name,file] of [[main,'Prepare Events','prepare_events.js'],[main,'Build Dead Letter','build_dead_letter.js'],[errorWorkflow,'Build Incident','build_incident.js']])assert.equal(w.nodes.find(n=>n.name===name).parameters.jsCode,fs.readFileSync(path.join(root,'code',file),'utf8'));
});
test('after: bounded preparation deduplicates within one envelope',()=>{assert.equal(prepare({body:fixture}).length,2);});
test('after: oversized and empty batches fail before downstream delivery',()=>{
assert.throws(()=>prepare({events:Array.from({length:51},()=>fixture.events[0])}),/BATCH_LIMIT/);assert.throws(()=>prepare({events:[]}),/EVENTS_REQUIRED/);
});
test('after: multiple envelopes are refused rather than silently ignored',()=>{
const node=main.nodes.find(n=>n.name==='Prepare Events');assert.throws(()=>runCode(node,[fixture,fixture]),/ONE_ENVELOPE_REQUIRED/);
});
test('after: invalid identifiers, currency and fractional amounts fail schema checks',()=>{
for(const patch of [{event_id:'../../bad'},{currency:'EUR'},{amount_minor:2.5},{amount_minor:-1},{customer_ref:''},{event_id:1},{source:7},{customer_ref:123}])assert.throws(()=>prepare({events:[{...fixture.events[0],...patch}]}),/EVENT_SCHEMA/);
});
test('after: identity is stable across replay and unaffected by field ordering',()=>{
const e=fixture.events[0],other={currency:e.currency,amount_minor:e.amount_minor,customer_ref:e.customer_ref,source:e.source,event_id:e.event_id};
assert.deepEqual(prepare({events:[e]}),prepare({events:[other]}));assert.equal(prepare(one)[0].idempotencyKey,'lead-event:v1:form:EV001');
});
test('after: reused key with conflicting payload fails instead of hiding an update',()=>{
assert.throws(()=>prepare({events:[fixture.events[0],{...fixture.events[0],amount_minor:99}]}),/IDEMPOTENCY_CONFLICT/);
});
test('after: receiver atomically suppresses duplicate processing across executions',()=>{
const receiver=new FakeReceiver();assert.equal(replay(fixture,receiver).accepted,2);const second=replay(fixture,receiver);assert.equal(second.accepted,0);assert.equal(second.duplicates,2);assert.equal(receiver.records.size,2);
});
test('after: transient failure uses export retry cap and fixed backoff',()=>{
const result=replay(one,new FakeReceiver({transient:2}));assert.equal(result.attempts,3);assert.deepEqual(result.waitsMs,[1000,1000]);assert.equal(result.accepted,1);assert.equal(result.deadLetters,0);
});
test('after: lost acknowledgement after receiver commit does not duplicate a record',()=>{
const receiver=new FakeReceiver({lostAck:true});const result=replay(one,receiver);assert.equal(result.attempts,2);assert.equal(receiver.records.size,1);assert.equal(result.duplicates,1);
});
test('after: permanent failure stops at cap and creates a dead-letter plus incident',()=>{
const dlq=new Map(),result=replay(one,new FakeReceiver({permanent:true}),{dlq});assert.equal(result.attempts,3);assert.equal(result.deadLetters,1);assert.equal(dlq.size,1);assert.equal(result.status,'delivery-failed');assert.equal(result.incidents.length,1);
const record=[...dlq.values()][0];assert.equal(record.idempotencyKey,prepare(one)[0].idempotencyKey);assert.equal(record.status,'awaiting_review');
});
test('after: failed dead-letter persistence raises an explicit incident outcome',()=>{
const result=replay(one,new FakeReceiver({permanent:true}),{failDlq:true});assert.equal(result.status,'dead-letter-failed');assert.equal(result.deadLetters,0);assert.equal(result.incidents.length,1);
});
test('after: recovery replay accepts once and preserves the review record',()=>{
const dlq=new Map(),receiver=new FakeReceiver({permanent:true});replay(one,receiver,{dlq});receiver.permanent=false;
assert.equal(replay(one,receiver,{dlq}).accepted,1);assert.equal(replay(one,receiver,{dlq}).accepted,0);assert.equal(dlq.size,1);
});
test('after: conflicting cross-execution payload becomes a visible failure',()=>{
const receiver=new FakeReceiver();replay(one,receiver);const result=replay({events:[{...fixture.events[0],amount_minor:1}]},receiver);assert.equal(receiver.records.size,1);assert.equal(result.status,'delivery-failed');
});
test('after: item linking retains the correct event for multiple dead letters',()=>{
const originals=prepare(fixture),node=main.nodes.find(n=>n.name==='Build Dead Letter');const records=runCode(node,[{error:'x'},{error:'y'}],{originals});
assert.equal(records[0].json.payload.event_id,'EV001');assert.equal(records[1].json.payload.event_id,'EV002');
assert.throws(()=>runCode(node,[{}],{originals:[]}),/DLQ_ORIGIN_MISSING/);
});
test('after: incident summary omits raw payload, stack and error message',()=>{
const incident=buildIncident({execution:{id:'SYN-EXEC-5',error:{message:'SYNTHETIC_PRIVATE_TEXT',stack:'SYNTHETIC_STACK'}},workflow:{id:'SYN-WF'},payload:{unused:'SYNTHETIC_PAYLOAD'}});
assert.equal(incident.incident_key,'incident:SYN-EXEC-5');assert.ok(!JSON.stringify(incident).includes('SYNTHETIC_PRIVATE_TEXT'));assert.ok(!JSON.stringify(incident).includes('SYNTHETIC_PAYLOAD'));
});
test('after: trigger-error incident works without execution ID',()=>{
assert.equal(buildIncident({trigger:{timestamp:123456},workflow:{id:'SYN-WF'}}).incident_key,'incident:123456');assert.throws(()=>buildIncident({workflow:{}}),/INCIDENT_ID_MISSING/);
});
test('linter: removed retry, backoff and timeout controls are detected',()=>{
const w=mutateNode('Deliver Event',n=>{n.retryOnFail=false;n.waitBetweenTries=0;n.parameters.options.timeout=0;});for(const c of ['RETRY_BOUND','RETRY_BACKOFF','HTTP_TIMEOUT'])assert.ok(codes(w).includes(c));
});
test('linter: missing credential reference and inline marker are detected without disclosure',()=>{
const w=mutateNode('Deliver Event',n=>{delete n.credentials;n.parameters.headerParameters.parameters.push({name:'Authorization',value:'Bearer SYNTHETIC_VALUE_REDACT_ME'});});
const findings=lintWorkflow(w,[errorWorkflow]);assert.ok(findings.some(f=>f.code==='CREDENTIAL_REFERENCE'));assert.ok(findings.some(f=>f.code==='INLINE_SECRET'));assert.ok(!JSON.stringify(findings).includes('SYNTHETIC_VALUE_REDACT_ME'));
});
test('linter: removed or unknown error workflow is detected',()=>{
const w=clone(main);delete w.settings.errorWorkflow;assert.ok(codes(w).includes('MISSING_ERROR_WORKFLOW'));w.settings.errorWorkflow='missing';assert.ok(codes(w).includes('UNKNOWN_ERROR_WORKFLOW'));
});
test('linter: a newly introduced graph back-edge is detected',()=>{
const w=clone(main);w.connections.Success={main:[[{node:'Prepare Events',type:'main',index:0}]]};assert.ok(codes(w).includes('UNBOUNDED_CYCLE'));
});
test('linter: disconnected dead-letter branch and lost item-linking are detected',()=>{
const w=clone(main);w.connections['Deliver Event'].main[1]=[];assert.ok(codes(w).includes('DEAD_LETTER_PATH'));
const w2=mutateNode('Build Dead Letter',n=>{n.parameters.jsCode='return [];';});assert.ok(codes(w2).includes('DLQ_ITEM_LINKING'));
});
test('linter: removing idempotency and bounded input guards is detected',()=>{
const w=mutateNode('Deliver Event',n=>{n.parameters.headerParameters.parameters=[];});assert.ok(codes(w).includes('IDEMPOTENCY_HEADER'));
const w2=mutateNode('Prepare Events',n=>{n.parameters.jsCode=n.parameters.jsCode.replace('if (events.length > MAX_BATCH) throw','if (false) throw');});assert.ok(codes(w2).includes('BATCH_BOUND'));
});
test('linter: broken edges, obvious Code loops and unsafe webhook config are detected',()=>{
const w=clone(main);w.connections.Success={main:[[{node:'missing',type:'main',index:0}]]};w.nodes.find(n=>n.name==='Prepare Events').parameters.jsCode+='\nwhile (true) {}';const hook=w.nodes.find(n=>n.name==='Synthetic Webhook');delete hook.credentials;hook.parameters.responseMode='onReceived';
for(const c of ['BROKEN_CONNECTION','CODE_LOOP_REVIEW','WEBHOOK_AUTH','EARLY_ACK'])assert.ok(codes(w).includes(c));
});
test('linter CLI: baseline fails, hardened set passes, invalid JSON returns input error',()=>{
const cli=args=>cp.spawnSync(process.execPath,['lint.cjs',...args],{cwd:root,encoding:'utf8'});
assert.equal(cli(['before/broken.json']).status,1);assert.equal(cli(['after/main.json','after/error-workflow.json']).status,0);
assert.equal(cli(['package.json']).status,1);assert.equal(cli(['README.md']).status,2);
fs.mkdirSync(path.join(root,'tmp'),{recursive:true});const invalid=path.join(root,'tmp/invalid.json');fs.writeFileSync(invalid,'{');assert.equal(cli(['tmp/invalid.json']).status,2);fs.unlinkSync(invalid);
});
let failed=0;const results=[];
for(const {name,fn} of cases){try{fn();results.push({name,status:'PASS'});console.log('PASS '+name);}catch(error){failed++;results.push({name,status:'FAIL',message:error.message});console.log('FAIL '+name+'\n'+error.stack);}}
const report={synthetic:true,scope:'Static export policy plus actual embedded Code execution with an in-memory receiver; no n8n runtime',passed:cases.length-failed,failed,results};
fs.mkdirSync(path.join(root,'evidence'),{recursive:true});fs.writeFileSync(path.join(root,'evidence/tests.json'),JSON.stringify(report,null,2)+'\n');console.log(`RESULT ${report.passed}/${cases.length} passed; ${failed} failed`);process.exitCode=failed?1:0;