import test from 'node:test'; import assert from 'node:assert/strict'; import vm from 'node:vm'; import {readFileSync} from 'node:fs'; import {outgoingForm} from './mail-upload.mjs'; import {parseMessage} from './bpq.mjs'; const source=readFileSync(new URL('./public/app.js',import.meta.url),'utf8'); const bulletin={id:10,type:'B',from:'N1AUTHOR',to:'NEWS',via:'WW',subject:'Radio news',text:'First line\nSecond line'}; function client(message){ const elements=new Map(),field=()=>({value:'',focus(){}}),form={elements:{to:field(),type:field(),subject:field(),text:field()},reset(){for(const input of Object.values(this.elements))input.value='';this.elements.type.value='P';}}; const $=selector=>{if(selector==='#compose-form')return form;if(!elements.has(selector))elements.set(selector,{innerHTML:'',textContent:'',showModal(){this.open=true;}});return elements.get(selector);}; const context=vm.createContext({$,loadGeneration:0,selected:null,renderMessages(){},esc:v=>String(v??''),readableBody:v=>v,mailTimeMeta:()=>'',markMessageRead:async()=>{},toast(){},api:async()=>structuredClone(message)}); for(const name of ['replyDraft','readMessage','openCompose'])vm.runInContext(source.split('\n').find(line=>line.startsWith('function '+name+'(')||line.startsWith('async function '+name+'(')),context); return {context,$,form}; } test('Comment keeps bulletin type, category and distribution; Reply privately targets the author',async()=>{ const view=client(bulletin);await view.context.readMessage(10); assert.match(view.$('#reader').innerHTML,/id="comment"/);assert.match(view.$('#reader').innerHTML,/id="reply"/); view.$('#comment').onclick();assert.equal(view.form.elements.type.value,'B');assert.equal(view.form.elements.to.value,'NEWS@WW');assert.equal(view.form.elements.subject.value,'Re: Radio news');assert.match(view.form.elements.text.value,/> First line\n> Second line/); assert.equal(view.$('#compose').open,true); view.$('#reply').onclick();assert.equal(view.form.elements.type.value,'P');assert.equal(view.form.elements.to.value,'N1AUTHOR'); for(const kind of [false,true]){ const draft=view.context.replyDraft(bulletin,kind),native=outgoingForm(draft); assert.equal(native.get('Type'),kind?'B':'P');assert.equal(native.get('To'),kind?'NEWS@WW':'N1AUTHOR');assert.equal(native.get('BID'),''); } }); test('personal messages only offer Reply, and replying explicitly clears a previously selected bulletin type',async()=>{ const view=client({...bulletin,type:'P',to:'N0LOCAL'});await view.context.readMessage(10); assert.doesNotMatch(view.$('#reader').innerHTML,/id="comment"/); view.form.elements.type.value='B';view.$('#reply').onclick();assert.equal(view.form.elements.type.value,'P');assert.equal(view.form.elements.to.value,'N1AUTHOR'); }); test('comments preserve embedded routes, do not invent a distribution, and avoid duplicate Re prefixes',()=>{ const {context}=client(bulletin); assert.equal(context.replyDraft({...bulletin,to:'NEWS @ EU',via:'EU'},true).to,'NEWS@EU'); assert.equal(context.replyDraft({...bulletin,via:''},true).to,'NEWS'); assert.equal(context.replyDraft({...bulletin,subject:'Re: Radio news'},true).subject,'Re: Radio news'); assert.equal(context.replyDraft({...bulletin,subject:'x'.repeat(80)},true).subject.length,80); assert.equal(context.replyDraft(bulletin).to,'N1AUTHOR'); }); test('native bulletin header preserves the distribution for reply drafts',()=>{ const message=parseMessage(''); assert.equal(message.type,'B');assert.equal(message.via,'EU');const {context}=client(message);assert.equal(context.replyDraft(message,true).to,'NEWS@EU'); });