Files
NODEDC_MISSION_CORE/apps/control-station/test/netBirdConnection.test.mjs

59 lines
4.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {build} from 'esbuild';
const bundle=await build({entryPoints:[new URL('../src/core/fleet/useNetBird.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm',plugins:[{name:'fixture',setup(b){
b.onResolve({filter:/^(react|\.\/useFleet)$/},a=>({path:a.path,namespace:'fixture'}));
b.onLoad({filter:/.*/,namespace:'fixture'},a=>({contents:a.path==='react'?
'export const useState=v=>globalThis.netBirdUI.state(v),useRef=v=>globalThis.netBirdUI.ref(v),useEffect=(f,d)=>globalThis.netBirdUI.effect(f,d),useCallback=f=>f;':
'export const fleetRequest=(...args)=>globalThis.netBirdUI.request(...args);'}));
}}]});
const {useNetBird}=await import('data:text/javascript;base64,'+Buffer.from(bundle.outputFiles[0].contents).toString('base64'));
const status=(pending='',state='disconnected')=>({pending,state,server_url:'https://network.example',error:'',auth_url:'',client_version:'0.82.0'});
function deferred(){let resolve,reject;const promise=new Promise((a,b)=>{resolve=a;reject=b;});return{promise,resolve,reject};}
async function fixture(run){
const old={setTimeout:globalThis.setTimeout,clearTimeout:globalThis.clearTimeout,netBirdUI:globalThis.netBirdUI};
const values=[],effects=[],tasks=[],timers=new Map(),requests=[];let cursor=0,dirty=true,id=0,closed=false;
const h={requests,timers,view:null,
state(v){const i=cursor++;if(!(i in values))values[i]=v;return[values[i],next=>{values[i]=typeof next==='function'?next(values[i]):next;dirty=true;}];},
ref(v){return values[cursor++]??={current:v};},
effect(f,d){const i=cursor++;if(!effects[i]){effects[i]={};tasks.push(()=>{effects[i].cleanup=f();});}},
request(...args){const response=deferred();requests.push({args,...response});return response.promise;},
async settle(){for(let n=0;n<20;n++){await Promise.resolve();if(dirty&&!closed){dirty=false;cursor=0;h.view=useNetBird();tasks.splice(0).forEach(f=>f());}}},
async tick(){for(const [id,fn] of [...timers]){timers.delete(id);fn();}await h.settle();},
close(){closed=true;effects.forEach(e=>e?.cleanup?.());},
};
globalThis.netBirdUI=h;globalThis.setTimeout=f=>{timers.set(++id,f);return id;};globalThis.clearTimeout=id=>timers.delete(id);
try{await h.settle();await run(h);}finally{h.close();Object.assign(globalThis,old);}
}
test('a late poll cannot overwrite accepted connection and double click submits once',()=>fixture(async h=>{
const poll=h.requests[0];h.requests[1].resolve({items:[]});
const first=h.view.action('connect','https://network.example');
const second=h.view.action('connect','https://network.example');
assert.equal(h.requests.filter(x=>x.args[1]==='POST').length,1);
h.requests[2].resolve(status('authentication'));await first;await second;
poll.resolve(status());await h.settle();
assert.equal(h.view.status.pending,'authentication');assert.equal(h.view.busy,true);
}));
test('successful status polling does not erase action or installer failures',()=>fixture(async h=>{
h.requests[0].resolve(status());h.requests[1].reject(Error('download unavailable'));await h.settle();
const action=h.view.action('connect','https://network.example');
h.requests[2].reject(Error('Server unavailable'));await action;await h.settle();
await h.tick();h.requests[3].resolve(status());await h.settle();
assert.equal(h.view.error,'Server unavailable');assert.match(h.view.clientError,/Установщики недоступны/);
}));
test('cancel stays available while waiting for browser authentication',()=>fixture(async h=>{
h.requests[0].resolve(status('authentication'));h.requests[1].resolve({items:[]});await h.settle();
assert.equal(h.view.busy,true);assert.equal(h.view.requestBusy,false);
const cancel=h.view.action('cancel');assert.equal(h.requests[2].args[0],'/netbird/cancel');
h.requests[2].resolve(status());await cancel;await h.settle();assert.equal(h.view.busy,false);
}));
test('closing the window cancels polling without cancelling the system connection',()=>fixture(async h=>{
h.close();h.requests[0].resolve(status('connecting'));h.requests[1].resolve({items:[]});await h.settle();
assert.equal(h.timers.size,0);assert.equal(h.requests.length,2);
assert.equal(h.requests[0].args[4].aborted,true);
}));