import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; let server,list,select,running; before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});({speedtestServers:list,selectedSpeedServer:select}=await server.ssrLoadModule('./src/core/system/speedtestServers.ts'));({speedtestRunning:running}=await server.ssrLoadModule('./src/core/system/networkSpeedtest.ts'));}); after(async()=>{await server?.close();}); const service=(id,kind='librespeed',state='connected')=>({id,kind,state}); const hosts=[{id:'host-a',name:'A',services:[service('wire','wireguard'),service('speed-a')]},{id:'host-b',name:'B',services:[service('speed-b'),service('pending','librespeed','pending')]}]; const health={'host-a':[{id:'speed-a',kind:'librespeed',state:'online',checked_at:Date.now()/1000}]}; test('picker discovers admitted LibreSpeed services across all VPS and checks freshness',()=>{ const items=list(hosts,health);assert.deepEqual(items.map(x=>x.service.id),['speed-a','speed-b']); assert.equal(items[0].online,true);assert.equal(items[1].online,false); assert.equal(list(hosts,{'host-a':[{...health['host-a'][0],checked_at:Date.now()/1000-50}]})[0].online,false); assert.deepEqual(list(null,{}),[]); }); test('sole available server is automatic; explicit and active selections never silently move',()=>{ const items=list(hosts,health);assert.equal(select(items,'').service.id,'speed-a'); assert.equal(select(items,'speed-b').service.id,'speed-b'); assert.equal(select(items,'speed-a','speed-b').service.id,'speed-b'); assert.equal(select(items,'removed'),undefined); assert.equal(select(items,'','removed'),undefined); assert.equal(select(items.map(x=>({...x,online:true})),''),undefined); assert.equal(select([items[1]],'').service.id,'speed-b'); }); test('stopping remains active and terminal cancellation permits a new measurement',()=>{ for(const state of ['queued','applying','cancelling'])assert.equal(running({state}),true); for(const state of ['complete','error'])assert.equal(running({state}),false); assert.equal(running(null),false); });