import {test} from 'node:test'; import assert from 'node:assert/strict'; import {parseForwarding,safeScript,nextForwarding,forwardingHistory,Forwarding} from './forwarding.mjs'; import {mkdtemp,mkdir,writeFile,rm} from 'node:fs/promises'; import os from 'node:os';import path from 'node:path'; const fixture=`BBSForwarding : { N0TEST : { ConnectScript = "ATTACH 3|RADIO 14.100 USB|C N0TEST-8"; Enabled = 1; HRoutes = "WW"; HRoutesP = "WW"; FwdInterval = 3600; RequestReverse = 1; RevFWDInterval = 7200; FWDTimes = "0500-2159"; BBSHA = "N0TEST.TEST.USA.NOAM"; } RMS : { ConnectScript = "C 5 CMS"; Enabled = 1; FwdInterval = 1800; } LOCAL : { Enabled = 0; } } BBSUsers : { SECRET = "private credentials"; }`; test('forwarding profiles detect generic modem, Internet and redact secret commands',()=>{ const p=parseForwarding(fixture,[{id:3,driver:'SCSPACTOR'},{id:5,driver:'TELNET'}]);assert.equal(p.length,3);assert.equal(parseForwarding('Localtime = 1;\n'+fixture)[0].timesLocal,true);assert.equal(p[0].transport,'Radio');assert.equal(p[0].driver,'SCSPACTOR');assert.deepEqual(p[0].personal,['WW']);assert.equal(p[1].transport,'Internet');assert.equal(p[2].configured,false);assert.ok(!JSON.stringify(p).includes('private credentials'));assert.ok(!safeScript('PASSWORD secret|RRADIO AUTH key|C 5 host user password').join().includes('secret'));assert.ok(!safeScript('PASSWORD secret|RRADIO AUTH key|C 5 host user password').join().includes('key')); }); test('next forwarding estimate respects UTC windows and never invents a timer without history',()=>{ const p=parseForwarding(fixture)[0];const last={startedAt:'2026-10-16T21:30:00Z'};assert.equal(nextForwarding(p,last,Date.parse('2026-10-16T21:40:00Z')),'2026-10-17T05:00:00.000Z');assert.equal(nextForwarding(p,null),null);assert.equal(nextForwarding({...p,enabled:false},last),null);assert.equal(nextForwarding({...p,reverse:false},last,Date.now(),0),null);assert.equal(nextForwarding({...p,times:['2500-2600']},last),null); }); test('forwarding history isolates peers and distinguishes clean completion from failed calls',()=>{ const log=['261016 10:00:00 |N0TEST Connecting to BBS N0TEST','261016 10:00:05 N0TEST FQ','261016 10:00:10 |N0TEST N0TEST Disconnected','261016 11:00:00 |RMS Connecting to BBS RMS','261016 11:00:01 { const root=await mkdtemp(path.join(os.tmpdir(),'bpq-forward-'));await mkdir(path.join(root,'BPQMailChat'));await mkdir(path.join(root,'Logs'));await writeFile(path.join(root,'BPQMailChat','BPQMail.cfg'),fixture);const calls=[];let busy=false; const f=new Forwarding({root,ports:[{id:3,driver:'SCSPACTOR'},{id:5,driver:'TELNET'}]},async(url,opts)=>{calls.push([url,opts?.body]);if(url==='/Mail/Status')return 'href=/Mail/FWD?fixture';if(url==='/Mail/FWD?fixture')return 'var Key = "fixture"';if(url.startsWith('/Mail/FwdDetails'))return 'Forwarding Config - 2 Messages Queued';return 'ok';},()=>({active:busy})); try{await assert.rejects(f.start('UNKNOWN'));await assert.rejects(f.start('LOCAL'));busy=true;await assert.rejects(f.start('N0TEST'),/Modem occupato/);busy=false;await Promise.all([f.start('N0TEST'),f.start('RMS')]);assert.deepEqual(calls.filter(c=>c[0].includes('FwdDetails')||c[0].includes('FWDSave')).map(c=>c[1]),['N0TEST','StartForward','RMS','StartForward']);await assert.rejects(f.start('N0TEST'),/giĆ  richiesta/);const snapshot=await f.snapshot();assert.equal(snapshot.profiles[0].queued,2);assert.equal(snapshot.scheduleTimezone,'UTC');}finally{await rm(root,{recursive:true,force:true});} }); test('outgoing FBB count requires remote turn acknowledgement after acceptance',()=>{const prefix='261016 10:00:00 |N0TEST Connecting to BBS N0TEST\n261016 10:00:01 >N0TEST FA B N9DEMO EU BBS 1_N9DEMO 100\n261016 10:00:02