import assert from 'node:assert/strict'; import {before,after,test} from 'node:test'; import {createServer} from './support/vite-server.mjs'; import {createElement} from 'react'; import {renderToStaticMarkup} from 'react-dom/server'; let server,routerMode,RouterOverview,ClientRouterControls; before(async()=>{ server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}}); ({routerMode}=await server.ssrLoadModule('../../packages/router-ui/src/mode.ts')); ({RouterOverview}=await server.ssrLoadModule('../../packages/router-ui/src/RouterOverview.tsx')); ({ClientRouterControls}=await server.ssrLoadModule('../../packages/router-ui/src/ClientRouterControls.tsx')); }); after(async()=>{await server?.close();}); function device(){return { id:'router-test',state:'ready',selected_mode:'aggregation',persisted_mode:'aggregation',active_mode:'direct',active_wan:'M1',active_slots:[1],internet:'unknown', slot_map:[{sim:1,slot:2,position:'right'},{sim:2,slot:3,position:'center'},{sim:3,slot:1,position:'left'}], modems:[1,2,3].map(slot=>({slot,connectivity:'online',radio:'connected',operator:`Carrier ${slot}`,down_kbps:10,up_kbps:5})), client_api:{boot_id:'boot',revision:'revision',rights:['policy'],policy:{mode:'aggregation',fail_seconds:60,recover_seconds:60,min_vpn_seconds:120},details:{pending:false,fallback:true},checks:[]} };} // The UI fixture deliberately omits optional VPN details: that panel is unrelated // to the mode projection and expects its complete transport contract. function forRender(d){return {...d,client_api:{...d.client_api,details:{...d.client_api.details,vpn:{},reserve:{pairs:[]}}}};} function overview(d,ready=true){return renderToStaticMarkup(createElement(RouterOverview,{device:forRender(d),ready,points:[]}));} function controls(d,ready=true){return renderToStaticMarkup(createElement(ClientRouterControls,{device:forRender(d),ready,value:{control_version:1,runtime_id:'runtime',results:[]},transport:{}}));} test('configured aggregation with three online SIMs remains a direct fallback observation',()=>{ const d=device(),m=routerMode(d,true); assert.equal(m.configured,'Агрегация');assert.equal(m.current,'Прямой выход · SIM3'); assert.equal(m.activeSlot,1);assert.equal(m.fallback,true);assert.equal(m.mismatch,true); assert.match(m.message,/Агрегация сейчас не используется/); const html=overview(d),form=controls(d); assert.match(html,/Сейчас: Прямой выход · SIM3/);assert.match(html,/Задано: Агрегация/); assert.match(form,/Сейчас: Прямой выход · SIM3/);assert.match(form,/Сохранённый режим/); assert.ok(form.includes(m.message));assert.ok(html.includes(m.message)); assert.equal((html.match(/текущий выход/g)||[]).length,1); assert.doesNotMatch(html,/· выбран|Одна SIM-карта/); assert.match(html,/Интернет не подтверждён/); }); test('effective SIM follows physical slot mapping, not preference, operator or online count',()=>{ const d=device(); d.selected_mode='direct';d.client_api.policy={...d.client_api.policy,mode:'direct',preferred_sim:'M2'}; assert.equal(routerMode(d,true).activeSlot,1); assert.match(routerMode(d,true).message,/Предпочтительная SIM — SIM1/); for(const [slot,sim] of [[1,3],[2,1],[3,2]])assert.equal(routerMode({...d,active_slots:[slot]},true).current,`Прямой выход · SIM${sim}`); assert.equal(routerMode({...d,active_slots:[]},true).activeSlot,null); assert.equal(routerMode({...d,active_slots:[1,2,3]},true).activeSlot,null); }); test('mode changes update the observation without replacing persisted policy or guessing a fallback',()=>{ const d=device();d.client_api.details.fallback=false; const aggregation=routerMode({...d,active_mode:'aggregation',active_slots:[]},true); assert.equal(aggregation.current,'Агрегация');assert.equal(aggregation.mismatch,false);assert.equal(aggregation.message,''); const mismatch=routerMode(d,true); assert.equal(mismatch.fallback,false);assert.match(mismatch.message,/Причина не подтверждена/); assert.doesNotMatch(mismatch.message,/резерв/); const unknown=routerMode({...d,active_mode:'',active_slots:[]},true); assert.equal(unknown.current,'Текущий режим не определён');assert.equal(unknown.activeSlot,null); assert.match(unknown.message,/не сообщил/); }); test('pending, stale and strategy states do not masquerade as failed or active aggregation',()=>{ const d=device();d.client_api.details.pending=true; assert.match(routerMode(d,true).message,/применяет настройки/); assert.doesNotMatch(routerMode(d,true).message,/Причина не подтверждена/); const stale=routerMode(d,false); assert.equal(stale.current,'Нет свежих данных');assert.equal(stale.activeSlot,null); assert.equal(stale.fallback,false);assert.equal(stale.pending,false); const html=overview(d,false),form=controls(d,false); for(const x of [html,form])assert.match(x,/Сейчас: Нет свежих данных/); assert.doesNotMatch(html,/текущий выход|Сейчас: Прямой выход/); for(const configured of ['whitelist','auto']){ const m=routerMode({...d,selected_mode:configured,client_api:{...d.client_api,details:{pending:false,fallback:false}}},true); assert.equal(m.mismatch,false);assert.equal(m.message,''); } }); test('legacy adapters preserve explicit current WAN without claiming fallback',()=>{ const d={...device(),client_api:null,active_wan:'wan2',active_slots:[]}; assert.equal(routerMode(d,true).current,'Прямой выход · SIM1'); assert.equal(routerMode(d,true).fallback,false); assert.equal(routerMode({...d,active_wan:''},true).activeSlot,null); });