Files
MikrotikManager/lib/route-optimizer-response.test.ts

28 lines
2.5 KiB
TypeScript

import {test} from "node:test"
import assert from "node:assert/strict"
import {optimizerServersSchema,optimizerRulesSchema,optimizerProbesSchema} from "./route-optimizer-response"
const server={id:1,name:"Router",host:"192.0.2.1",type:"home-router",country:"RU",enabled:true,latency:0,status:null}
const probe={id:"p",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"ether2",enabled:true,lastPingRttMs:0,lastPingLossPct:0,lastTxAvgMbps:0,lastRxAvgMbps:null,lastPingAt:"2026-09-30T00:00:00Z"}
test("optimizer observed zero/null and empty sources are distinct from missing",()=>{
assert.equal(optimizerServersSchema.parse([server])[0].latency,0)
assert.equal(optimizerProbesSchema.parse({probes:[probe]}).probes[0].lastTxAvgMbps,0)
assert.equal(optimizerProbesSchema.parse({probes:[probe]}).probes[0].lastRxAvgMbps,null)
assert.ok(optimizerProbesSchema.safeParse({probes:[]}).success)
assert.ok(!optimizerProbesSchema.safeParse({}).success)
assert.ok(optimizerRulesSchema.safeParse({rulesets:[],live:false,stale:false}).success)
assert.ok(!optimizerRulesSchema.safeParse({rulesets:[],stale:true}).success)
})
test("optimizer refuses unknown types, duplicate inventory/WAN and invalid metrics",()=>{
for(const value of [[{...server,type:undefined}],[server,server],[{...server,latency:-1}],[{...server,wanUplinks:[{id:"wan",name:"WAN",isp:"ISP",iface:"ether1",ip:"",maxDl:0,maxUl:10}]}]]) assert.ok(!optimizerServersSchema.safeParse(value).success)
for(const value of [[probe,probe],[{...probe,lastPingRttMs:-1}],[{...probe,lastPingLossPct:101}],[{...probe,lastPingAt:"yesterday"}],[{...probe,lastTxAvgMbps:undefined}]]) assert.ok(!optimizerProbesSchema.safeParse({probes:value}).success)
})
import {buildLiveOptimizerData,DEFAULT_ROUTE_AI_OPTIMIZER_SETTINGS} from "./route-optimizer-data"
test("optimizer does not recommend an absent chain at a zero threshold",()=>{
const rows=[{...server,type:"home-router" as const,site:"TEST",model:"CHR",wanUplinks:[{id:"wan",name:"WAN",isp:"ISP",iface:"ether1",ip:"198.51.100.1",maxDl:100,maxUl:100}]}]
const rules=optimizerRulesSchema.parse({rulesets:[{serverId:"1",rules:[{id:"r",community:"65001:1",communityName:"Service",gateway:"192.0.2.2",gatewayTunnelId:"",description:""}]}]}).rulesets
const result=buildLiveOptimizerData(rows,rules,{...DEFAULT_ROUTE_AI_OPTIMIZER_SETTINGS,switchThreshold:0})
const recommendation=result.homes[0].commRecs[0]
assert.equal(recommendation.current,null);assert.equal(recommendation.recommended,null);assert.equal(recommendation.shouldSwitch,false)
})