import {test} from 'node:test';
import assert from 'node:assert/strict';
import {SyncOperations,mailStreams} from './sync-operations.mjs';
const turn=()=>new Promise(r=>setImmediate(r));
test('native BBS stream parsing preserves stream identity and excludes idle rows',()=>{
assert.deepEqual(mailStreams(''),[{id:23,text:'N0PEER N0PEER-8 23 0 0 0'}]);
assert.throws(()=>mailStreams('Signon required'),/non verificabile/);
});
test('preparation can be cancelled before dispatch and the original profile is restored',async()=>{
const fwd={logs:async()=>'',bpq:async()=>''},ops=new SyncOperations(fwd);let dispatched=false,restored=false;
const client={job:{restoring:true,startedAt:new Date().toISOString()},restore:async()=>{restored=true;client.job.restoring=false;}};
const started=ops.start({peer:'N0PEER',kind:'forwarding',label:'Test VHF',client,run:signal=>new Promise((resolve,reject)=>{signal.addEventListener('abort',()=>reject(Error('Aborted')),{once:true});if(signal.aborted)reject(Error('Aborted'));}).then(()=>{dispatched=true;})});
assert.equal(started.state,'preparing');assert.throws(()=>ops.start({peer:'OTHER'}),/giĆ in corso/);await turn();const result=await ops.abort(started.id);assert.equal(result.state,'cancelled');assert.equal(dispatched,false);assert.equal(restored,true);assert.equal(result.active,false);assert.equal((await ops.abort(started.id)).state,'cancelled');
});
test('real log transitions end a session and never expose message text',async()=>{
let text='';const ops=new SyncOperations({logs:async()=>text});const job=ops.start({peer:'N0PEER',label:'Peer',kind:'forwarding',run:async()=>{}});await turn();
const at=new Date(job.startedAt),date=at.toISOString().slice(2,10).replaceAll('-',''),clock=at.toISOString().slice(11,19),prefix=date+' '+clock;
text=prefix+' |N0PEER Connecting to BBS N0PEER\n'+prefix+' {
let mine=true;const posts=[];const ops=new SyncOperations({logs:async()=>'',bpq:async(url,options)=>{if(options){posts.push(options.body);mine=false;}return '';}});
const job=ops.start({peer:'N0PEER',kind:'forwarding',run:signal=>new Promise((resolve,reject)=>signal.addEventListener('abort',()=>reject(Error('Aborted')),{once:true}))});await turn();const result=await ops.abort(job.id);assert.equal(result.state,'cancelled');assert.deepEqual(posts,['call=23&Disconnect=Disconnect']);
});
test('an unsuccessful radio start stays visible until its temporary profile has been restored',async()=>{
const client={job:{restoring:true,startedAt:new Date().toISOString()}},ops=new SyncOperations({logs:async()=>''});
const job=ops.start({peer:'N0PEER',client,run:async()=>{throw Error('Modem not ready');}});await turn();assert.equal(ops.get(job.id).state,'restoring');assert.equal(ops.get(job.id).active,true);
client.job.restoring=false;await ops.update(ops.jobs.get(job.id));assert.equal(ops.get(job.id).state,'failed');assert.equal(ops.get(job.id).active,false);
});
test('progress stays bound to its first outgoing session when the partner connects again',async()=>{
let text='';const ops=new SyncOperations({logs:async()=>text}),j=ops.start({peer:'N0PEER',kind:'forwarding',run:async()=>{}});await turn();
const stamp=date=>date.toISOString().slice(2,10).replaceAll('-','')+' '+date.toISOString().slice(11,19),first=stamp(new Date(j.startedAt)),later=stamp(new Date(Date.parse(j.startedAt)+60000));
text=first+' |N0PEER Connecting to BBS N0PEER';const job=ops.jobs.get(j.id);await ops.update(job);const bound=job.sessionStartedAt;assert.ok(bound);
text+='\n'+later+' |N0PEER Connecting to BBS N0PEER\n'+later+'