ZZZ Digital Lab

Back to n8n workflow hardening

demo.cjs

'use strict';
const fs=require('node:fs'),path=require('node:path');
const {lintWorkflow}=require('./lint.cjs');const {loadExports,FakeReceiver,replay}=require('./lib/replay.cjs');
const [main,error]=loadExports();const before=JSON.parse(fs.readFileSync(path.join(__dirname,'before/broken.json'),'utf8'));
const fixture=JSON.parse(fs.readFileSync(path.join(__dirname,'fixtures/events.json'),'utf8')),one={events:[fixture.events[0]]};
const receiver=new FakeReceiver(),success=replay(fixture,receiver),duplicate=replay(fixture,receiver);
const transient=replay(one,new FakeReceiver({transient:2}));const dlq=new Map(),failedReceiver=new FakeReceiver({permanent:true}),permanent=replay(one,failedReceiver,{dlq});failedReceiver.permanent=false;const recovery=replay(one,failedReceiver,{dlq});
const evidence=path.join(__dirname,'evidence');fs.mkdirSync(evidence,{recursive:true});
const lint={synthetic:true,before:lintWorkflow(before,[error]),after:[...lintWorkflow(main,[error]),...lintWorkflow(error,[main])]};
const result={synthetic:true,scope:'Offline contract replay; not n8n runtime',success,duplicate,transient,permanent,recovery,retainedDeadLetters:[...dlq.values()]};
fs.writeFileSync(path.join(evidence,'lint.json'),JSON.stringify(lint,null,2)+'\n');fs.writeFileSync(path.join(evidence,'replay.json'),JSON.stringify(result,null,2)+'\n');
console.log(JSON.stringify({beforeFindings:lint.before.length,afterFindings:lint.after.length,success:success.accepted,replay:duplicate.accepted,transientAttempts:transient.attempts,deadLetters:permanent.deadLetters,recovery:recovery.accepted},null,2));