import test from 'node:test';import assert from 'node:assert/strict';import {deleteNativeMessage} from './delete-message.mjs'; const list=(status='N')=>'Message List\r\n 7 17-Oct B'+status+' 100 '+['ALL','WW','N1TEST'].map(s=>s.padEnd(7)).join(' ')+' Test bulletin\r\n'; test('delete uses a fresh private WebMail session and confirms the message is gone',async()=>{ const calls=[];let killed=false; const bpq=async url=>{calls.push(url);if(url==='/Webmail')return 'Bulls';if(url==='/WebMail/WMALL?fresh')return killed?'Message List':list();if(url==='/WebMail/WMDel/7?fresh'){killed=true;return 'Message #7 Killed';}throw Error('Unexpected or stale WebMail key');}; assert.deepEqual(await deleteNativeMessage(7,bpq),{ok:true});assert.deepEqual(calls,['/Webmail','/WebMail/WMALL?fresh','/WebMail/WMDel/7?fresh','/WebMail/WMALL?fresh']); }); test('a BPQ permission denial is reported instead of pretending the message was deleted',async()=>{ await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'Mail':url.includes('WMDel')?'Not your message':list()),/permessi sysop/); }); test('a native HTTP 200 or session-redirect page is insufficient if the message remains visible',async()=>{ await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'Mail':list()),/non ha confermato/); }); test('already absent or killed messages are idempotent and never issue another delete',async()=>{ for(const html of ['Message List',list('K')]){const calls=[];const result=await deleteNativeMessage(7,async url=>{calls.push(url);return url==='/Webmail'?'Mail':html;});assert.equal(result.alreadyDeleted,true);assert.ok(!calls.some(url=>url.includes('WMDel')));} }); test('BPQ may retain the killed row for sysop inspection; status K confirms deletion',async()=>{ let killed=false;assert.deepEqual(await deleteNativeMessage(7,async url=>{if(url==='/Webmail')return 'Mail';if(url.includes('WMDel')){killed=true;return 'Killed';}return list(killed?'K':'N');}),{ok:true}); }); test('unverifiable sessions and invalid message IDs cannot issue a deletion',async()=>{ await assert.rejects(deleteNativeMessage(0,async()=>{throw Error('Must not contact BPQ');}),/non valido/); await assert.rejects(deleteNativeMessage(7,async()=>'

Sign in

'),/WebMail/); await assert.rejects(deleteNativeMessage(7,async url=>url==='/Webmail'?'Mail':'Sign in'),/Lista messaggi/); }); test('sysop Console can delete another author bulletin through the native admin action, preserving metadata',async()=>{ let killed=false,save=null;const fields={From:'N1TEST',To:'ALL',BID:'BID_123',EFROM:'',VIA:'WW',Title:'Test & bulletin'}; const bpq=async(url,options)=>{if(url==='/Webmail')return 'Mail';if(url.includes('WMALL'))return killed?'Message List':list();if(url.includes('WMDel'))return 'Not your message';if(url==='/Mail/Status')return 'Messages';if(url.startsWith('/Mail/MsgDetails'))return '

Message 7

'+Object.entries(fields).map(([k,v])=>'').join('')+'';if(url.startsWith('/Mail/MsgSave')){save=options.body;killed=true;return 'Saved';}throw Error(url);}; assert.deepEqual(await deleteNativeMessage(7,bpq,{sysop:true}),{ok:true});assert.equal(save,'N1TEST|ALL|BID_123||WW|Test & bulletin|B|K|'); }); test('admin fallback refuses to modify the wrong message or incomplete metadata',async()=>{ let saves=0;const bpq=async url=>{if(url==='/Webmail')return 'Mail';if(url.includes('WMALL'))return list();if(url.includes('WMDel'))return 'Not your message';if(url==='/Mail/Status')return 'Messages';if(url.includes('MsgDetails'))return '

Message 8

';if(url.includes('MsgSave'))saves++;return '';}; await assert.rejects(deleteNativeMessage(7,bpq,{sysop:true}),/non ha confermato il messaggio/);assert.equal(saves,0); });