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(''),'x.svg'),'text/plain');assert.equal(mimeOf(Buffer.from(''),'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=' 2482 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','N0TEST@WINLINK.ORG']){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 {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=' 2444 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=' 2466 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('');assert.equal(m.subject,'