66 lines
12 KiB
JavaScript
66 lines
12 KiB
JavaScript
import {test} from 'node:test';import assert from 'node:assert/strict';import {parseMessages,parseMessage,parseRoutes,parseRadio} from './bpq.mjs';
|
|
import {Chat,parseUser} from './chat.mjs';
|
|
import {incoming} from './public/notifications.js';
|
|
import {mailDirection,mailFolder,cmsStatus} from './model.mjs';
|
|
import {parseAttachments,mimeOf,safeName,b2Body} from './attachments.mjs';
|
|
test('B2 body excludes attachment bytes and preserves UTF8',()=>{const body=Buffer.from('Caffè');const b=Buffer.concat([Buffer.from(`Mid: example\r\nBody: ${body.length}\r\nFile: 3 a.bin\r\n\r\n`),body,Buffer.from('\r\nxxx')]);assert.equal(b2Body(b),'Caffè')});
|
|
test('B2 attachments preserve binary bytes, byte lengths and multiple files',()=>{const body=Buffer.from('Caffè'),one=Buffer.from([0,255,13,10]),two=Buffer.from('hello');const header=Buffer.from(`Mid: test\r\nBody: ${body.length}\r\nFile: ${one.length} a.bin\r\nFile: ${two.length} notes.txt\r\n\r\n`);const message=Buffer.concat([header,body,Buffer.from('\r\n'),one,Buffer.from('\r\n'),two,Buffer.from('\r\n')]);const files=parseAttachments(message);assert.equal(files.length,2);assert.deepEqual(files[0].data,one);assert.deepEqual(files[1].data,two);assert.equal(files[1].mime,'text/plain');});
|
|
test('untrusted HTML/SVG is text and filename paths are removed',()=>{assert.equal(mimeOf(Buffer.from('<svg onload=alert(1)>'),'x.svg'),'text/plain');assert.equal(mimeOf(Buffer.from('<html>'),'x.jpg'),'application/octet-stream');assert.equal(safeName('../../secret.png'),'secret.png');});
|
|
test('truncated B2 attachment is rejected, ordinary bulletin has no binary attachments',()=>{assert.throws(()=>parseAttachments(Buffer.from('Body: 1\r\nFile: 999 foo.png\r\n\r\na\r\nxx')));assert.deepEqual(parseAttachments(Buffer.from('R:261013\r\n\r\nBulletin')),[]);});
|
|
test('long RMS email route does not drop or shift the message row',()=>{const row='<a href=/WebMail/WM?W123&2482> 2482</a> 13-Oct PN 150 RMS N0TEST@WINLINKN0TEST Queue regression\r\n';const m=parseMessages(row)[0];assert.equal(m.id,2482);assert.equal(m.to,'RMS');assert.equal(m.from,'N0TEST');assert.equal(m.subject,'Queue regression');});
|
|
test('own outgoing emails stay in queue even with an SSID or full email sender',()=>{for(const from of ['N0TEST','N0TEST-11','[email protected]']){const m={from,fromAddress:from,to:'RMS',status:'N'};assert.equal(mailDirection(m,'N0TEST'),'outgoing');assert.equal(mailFolder(m,'N0TEST'),'queue')}assert.equal(mailFolder({from:'N0TEST',status:'F'},'N0TEST'),'sent');assert.equal(mailFolder({from:'K1AJD',status:'N'}),'inbox');});
|
|
test('CMS clean close is recognized for both directions of FQ',()=>{for(const direction of ['<','>'])assert.equal(cmsStatus(`261013 20:52:23 |RMS Connecting to BBS RMS\n261013 20:52:26 ${direction}RMS FQ\n261013 20:52:28 |RMS RMS Disconnected`).state,'complete');});
|
|
test('CMS disconnect alone is neutral, explicit protocol error is an error',()=>{const start='261013 20:52:23 |RMS Connecting to BBS RMS\n';assert.equal(cmsStatus(start+'261013 20:52:28 |RMS RMS Disconnected').state,'ended');assert.equal(cmsStatus(start+'261013 20:52:28 <RMS *** Protocol Error').state,'error');});
|
|
test('arrival alerts ignore first load and previously seen IDs',()=>{const messages=[{id:1,type:'B',from:'N1TEST'},{id:2,type:'P',from:'K1AJD'}];assert.deepEqual(incoming(messages,null),{bulletins:[],mail:[]});assert.deepEqual(incoming(messages,new Set([1,2])),{bulletins:[],mail:[]});});
|
|
test('arrival alerts distinguish downloaded bulletins and incoming mail, exclude own writes',()=>{const got=incoming([{id:1,type:'B',from:'N1TEST'},{id:2,type:'P',from:'K1AJD'},{id:3,type:'P',from:'N0TEST'}],new Set(),'N0TEST');assert.equal(got.bulletins.length,1);assert.equal(got.mail.length,1);assert.equal(got.mail[0].id,2);});
|
|
test('chat roster extracts callsign, name, node, room and idle time',()=>{assert.deepEqual(parseUser('K1AJD at AGCHAT Tony, Augusta, GA [General] Idle for 1279 seconds'),{call:'K1AJD',node:'AGCHAT',name:'Tony',qth:'Augusta, GA',topic:'General',idle:1279});});
|
|
test('chat rejects command injection before writing to socket',()=>{const c=new Chat(async()=>({}));c.state='online';assert.throws(()=>c.send('hello\r/QUIT'));assert.throws(()=>c.send('/F'));assert.throws(()=>c.changeTopic('General\r/QUIT'));assert.throws(()=>c.send('hello','K1AJD\rB'));clearInterval(c.timer)});
|
|
test('polling roster lines do not become conversation messages',()=>{const c=new Chat(async()=>({}));c.line('1 Station(s) connected:');c.line('N0TEST at CHAT N0TEST, Town Italy [General] Idle for 0 seconds');assert.equal(c.events.length,0);assert.equal(c.users.length,1);clearInterval(c.timer)});
|
|
test('fixed-width BPQ list retains empty routing field and decodes subject',()=>{const row='<a href=/WebMail/WM?W123&2444> 2444</a> 13-Oct PF 157 N1TEST N0TEST Test & hello\r\n';assert.deepEqual(parseMessages(row),[{id:2444,date:'13-Oct',type:'P',status:'F',size:157,to:'N1TEST',via:'',from:'N0TEST',subject:'Test & hello'}]);});
|
|
test('bulletin columns and routing',()=>{const row='<a href=/WebMail/WM?W123&2466> 2466</a> 13-Oct BN 1784 DX WW HP2DFA DX bulletin\r\n';const m=parseMessages(row)[0];assert.equal(m.to,'DX');assert.equal(m.from,'HP2DFA');assert.equal(m.subject,'DX bulletin');});
|
|
test('message content is extracted without executing HTML, CRs normalized',()=>{const m=parseMessage('<textarea id=txt>From: N0TEST\nTo: TEST\nTitle: <script>\n\nhello\rworld & radio</textarea>');assert.equal(m.subject,'<script>');assert.equal(m.text,'hello\nworld & radio');});
|
|
test('route status is distinct from last-heard value',()=>{const html='<tr>'+['>4','N1TEST-15','192','6','100','2','0','4','5000','18:10','0','161'].map(c=>'<td>'+c+'</td>').join('')+'</tr>';assert.equal(parseRoutes(html)[0].active,true);assert.equal(parseRoutes(html)[0].nodes,6);});
|
|
test('radio online/free/busy are separate states',()=>{const r=parseRadio('Comms State Connected to VARA TNC TNC State Free Mode 2300 Channel State Busy S/N 15,6 Traffic Sent 59 RXed 5480 FT991A 7.050 USB S');assert.equal(r.state,'Free');assert.equal(r.busy,true);assert.equal(r.frequency,'7.050');assert.equal(r.scanning,true)});
|
|
|
|
|
|
test('BPQ monitor decodes fragmented and joined frames, ports and direction', async()=>{
|
|
const {MonitorDecoder}=await import('./monitor.mjs');const frames=[],ports=[];const d=new MonitorDecoder(f=>frames.push(f),p=>ports.push(...p));
|
|
const definition=Buffer.concat([Buffer.from([255,255]),Buffer.from('3|1 VARA HF|2 Telnet|4 AXIP|')]);
|
|
const rx=Buffer.concat([Buffer.from([255,27,17]),Buffer.from('12:00:00R N1TEST-15>NODES Port=4 <UI>\rhello\r'),Buffer.from([254])]);
|
|
const tx=Buffer.concat([Buffer.from([255,27,91]),Buffer.from('12:00:01T N0TEST>N1TEST Port=1\rtest\r'),Buffer.from([254])]);
|
|
const data=Buffer.concat([Buffer.from('node greeting'),definition,rx,tx]);for(const byte of data)d.push(Buffer.from([byte]));
|
|
assert.deepEqual(ports.map(p=>p.id),[1,2,4]);assert.equal(frames.length,2);assert.equal(frames[0].direction,'RX');assert.equal(frames[0].port,4);assert.match(frames[0].text,/hello/);assert.equal(frames[1].direction,'TX');assert.equal(frames[1].port,1);assert.equal(d.buffer.length,0);
|
|
});
|
|
|
|
|
|
test('native VARA bitrate keeps TX/RX separate and excludes speed-level number', async()=>{
|
|
const {parseVaraRate}=await import('./vara-rate.mjs');assert.deepEqual(parseVaraRate('<134>VARA BITRATE (14) 7050 BPS TX'),{bps:7050,direction:'TX',level:14});assert.deepEqual(parseVaraRate('BITRATE (9) 2682 BPS RX'),{bps:2682,direction:'RX',level:9});assert.deepEqual(parseVaraRate('BITRATE 270 RX'),{bps:270,direction:'RX',level:null});assert.equal(parseVaraRate('BUFFER 7050'),null);assert.equal(parseVaraRate('BITRATE ??? RX'),null);assert.equal(parseVaraRate('BITRATE 999999 BPS TX'),null);
|
|
});
|
|
|
|
|
|
test('BPQ message dates preserve UTC hours and infer previous year across January',async()=>{const {bpqTimestamp}=await import('./bpq.mjs');assert.equal(bpqTimestamp('14-Oct 12:11Z',new Date('2026-10-14T12:12:00Z')),'2026-10-14T12:11:00.000Z');assert.equal(bpqTimestamp('31-Dec 23:58Z',new Date('2026-01-01T01:00:00Z')),'2025-12-31T23:58:00.000Z');assert.equal(bpqTimestamp('31-Feb 12:00Z'),null);assert.equal(bpqTimestamp('14-Oct'),null);});
|
|
|
|
|
|
test('generic PACTOR station runs without VARA, chat, monitor or BBS',async()=>{
|
|
const fs=await import('node:fs/promises'),os=await import('node:os'),path=await import('node:path'),http=await import('node:http'),cp=await import('node:child_process');
|
|
const {parseConfig,publicConfig,loadConfig}=await import('./config.mjs');
|
|
const tmp=await fs.mkdtemp(path.join(os.tmpdir(),'bpq-console-test-'));
|
|
const upstream=http.createServer((req,res)=>{res.end('BPQ Ports Comms State Connected to PACTOR TNC TNC State Free Mode PACTOR Channel State Clear Traffic Sent 12 RXed 34');});
|
|
await new Promise(r=>upstream.listen(0,'127.0.0.1',r));
|
|
const httpPort=upstream.address().port;
|
|
const cfg=['NODECALL=N9DEMO-7','NODEALIAS=DEMO','LOCATOR=AA00AA','PORT','PORTNUM=3','ID=PACTOR modem','DRIVER=SCSPACTOR','ENDPORT','PORT','PORTNUM=5','DRIVER=TELNET','HTTPPORT='+httpPort,'USER=admin,fixture,N9DEMO,sysop','ENDPORT'].join('\n');
|
|
await fs.writeFile(path.join(tmp,'BPQ32.cfg'),cfg);await fs.writeFile(path.join(tmp,'console.config.json'),JSON.stringify({bpqRoot:tmp,httpPort,port:18991}));
|
|
const parsed=parseConfig(cfg);assert.equal(parsed.radioPorts[0].id,3);assert.equal(parsed.call,'N9DEMO');
|
|
const child=cp.spawn(process.execPath,['server.mjs'],{cwd:process.cwd(),env:{...process.env,BPQ_ROOT:tmp,BPQ_CONSOLE_CONFIG:path.join(tmp,'console.config.json'),BPQ_CONSOLE_PORT:'18991'},windowsHide:true,stdio:['ignore','pipe','pipe']});
|
|
try{
|
|
await new Promise((resolve,reject)=>{const timeout=setTimeout(()=>reject(new Error('Server fixture timeout')),8000);child.stdout.once('data',()=>{clearTimeout(timeout);resolve()});child.once('exit',code=>{clearTimeout(timeout);reject(new Error('Server fixture exited '+code))})});
|
|
const base='http://127.0.0.1:18991';const login=await fetch(base+'/api/login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:JSON.stringify({user:'admin',password:'fixture'})});assert.equal(login.status,200);const user=await login.json();assert.equal(user.call,'N9DEMO');assert.equal(user.config.features.vara,false);assert.equal(user.config.features.bbs,false);assert.equal(user.config.radio.id,3);assert.equal(JSON.stringify(user.config).includes('fixture'),false);
|
|
const headers={Cookie:login.headers.get('set-cookie').split(';')[0]};assert.equal((await fetch(base+'/api/chat',{headers})).status,409);assert.equal((await fetch(base+'/api/monitor',{headers})).status,409);assert.equal((await fetch(base+'/api/winlink',{headers})).status,409);
|
|
await new Promise(r=>setTimeout(r,1200));const telemetry=await (await fetch(base+'/api/telemetry',{headers})).json();assert.equal(telemetry.current.available,true);assert.equal(telemetry.current.nativeRate.listening,false);assert.equal(telemetry.current.varaCpu,null);assert.equal(telemetry.current.sent,12);
|
|
const dash=await (await fetch(base+'/api/dashboard',{headers})).json();assert.equal(dash.counts.messages,0);
|
|
}finally{child.kill();await new Promise(r=>child.once('exit',r));await new Promise(r=>upstream.close(r));await fs.rm(tmp,{recursive:true,force:true});}
|
|
});
|
|
test('generic own-call handling accepts SSIDs without station constants',()=>{assert.equal(mailFolder({from:'N9DEMO-11',status:'N'},'N9DEMO'),'queue');assert.equal(mailFolder({from:'[email protected]',status:'F'},'N9DEMO'),'sent');assert.equal(mailFolder({from:'N1OTHER',status:'N'},'N9DEMO'),'inbox');});
|
|
|
|
test('configuration detects a Telnet-only node without inventing a modem',async()=>{const {parseConfig}=await import('./config.mjs');const c=parseConfig('NODECALL=N8TEST-7\nPORT\nDRIVER=Telnet.dll\nPORTNUM=2\nHTTPPORT=8088\nTCPPORT=9000\nENDPORT');assert.equal(c.httpPort,8088);assert.equal(c.telnetPort,9000);assert.deepEqual(c.radioPorts,[]);assert.equal(c.call,'N8TEST');});
|