import {test} from 'node:test';import assert from 'node:assert/strict'; import {readAprs,writeAprs,validateAprs,AprsSettings,aprsBlocks} from './aprs-settings.mjs'; import {mkdtemp,mkdir,readFile,writeFile,rm} from 'node:fs/promises';import path from 'node:path';import os from 'node:os'; const ports=[{id:3,name:'Packet',driver:'KISS'}]; const original='NODECALL=N0TEST\r\nAPRSDIGI\r\n APRSCALL=N0TEST-11\r\n***\r\nAPRSDIGI\r\n APRSCALL=N0TEST\r\n LAT=4534.18N\r\n LON=00907.89E\r\n ISHost=euro.aprs2.net\r\n ISPasscode=12345\r\n SaveAPRSMsgs=1\r\n***\r\nAPPLICATION 1,BBS,,N0TEST-11\r\n'; const settings=()=>({...readAprs(original,ports),ports:[{port:3,name:'Packet',driver:'KISS',enabled:true,frequencyMHz:144.8,radioRepeat:false,radioToInternet:true,internetToRadio:false,beaconRadio:false,path:'APRS,WIDE1-1'}]}); test('APRS settings read native config without exposing the passcode and consolidate duplicate blocks',()=>{ const data=settings();assert.equal(data.call,'N0TEST');assert.equal(data.passcodeConfigured,true);assert.ok(!JSON.stringify(data).includes('12345'));const updated=writeAprs(original,data);assert.equal(aprsBlocks(updated).length,1);assert.match(updated,/SaveAPRSMsgs=1/);assert.match(updated,/ISPasscode=12345/);assert.match(updated,/APRSPATH 3=\r\n/);assert.match(updated,/DIGIMAP 3=IS/);assert.match(updated,/APPLICATION 1,BBS,,N0TEST-11/);assert.ok(!updated.includes('GATEDPATH')); const both=writeAprs(original,{...data,ports:data.ports.map(p=>({...p,radioRepeat:true,internetToRadio:true}))});assert.match(both,/DIGIMAP 3=3,IS/);assert.match(both,/GATEDPATH 3=APRS/); const off=writeAprs(original,{...data,internetEnabled:false});assert.ok(!off.includes('ISHost='));assert.match(off,/ISPasscode=12345/); }); test('APRS validation rejects injected config, invalid ports, unsupported frequency and ambiguous state',()=>{ const data=settings();validateAprs(data,ports);for(const patch of [{call:'N0TEST-99'},{host:'host\nRUN bad'},{passcode:'99999'},{latitude:'bad'},{beaconMinutes:0},{ports:[{...data.ports[0],port:1}]},{ports:[{...data.ports[0],frequencyMHz:7.05}]},{internetEnabled:false}])assert.throws(()=>validateAprs({...data,...patch},ports)); }); test('failed native APRS reconfiguration restores the exact file and policy, successful save backs up and sets the listening frequency',async()=>{ const root=await mkdtemp(path.join(os.tmpdir(),'aprs-'));await mkdir(path.join(root,'Automation'));await writeFile(path.join(root,'BPQ32.cfg'),original);const config={root,radioPorts:ports,radioPortsControl:{ports:[{port:3,kind:'packet',aprsGuard:true}]}};const updates=[];let fail=true; const radio={state:{ports:[{id:3,supported:true,current:{active:false},channels:[{frequencyMHz:144.95,enabled:true}],control:{txSuspended:false}}]},update:async(id,value)=>updates.push({id,value})};const service=new AprsSettings(config,radio,async()=>{if(fail)throw Error('Native rejected')}); try{await assert.rejects(service.save(settings()),/Native rejected/);assert.equal(await readFile(path.join(root,'BPQ32.cfg'),'utf8'),original);assert.equal(updates.length,0);fail=false;const state=await service.save(settings());assert.ok(state.appliedAt);assert.equal(updates[0].value.channels.filter(c=>c.enabled)[0].frequencyMHz,144.8);assert.ok(!JSON.stringify(state).includes('12345'));}finally{await rm(root,{recursive:true,force:true});} });