49 lines
4.3 KiB
TypeScript
49 lines
4.3 KiB
TypeScript
import {test} from "node:test"
|
|
import assert from "node:assert/strict"
|
|
import {validateNetworkMapSnapshot} from "./network-map-response"
|
|
const server={id:1,name:"Router",host:"192.0.2.1",type:"home-router",country:"RU",enabled:true,status:null,latency:0}
|
|
test("network map retains jump and exit nodes with PostgreSQL timestamps",()=>{
|
|
const timestamps=["2026-10-15 13:46:40.855+00","2026-10-15 20:46:40.855+07","2026-10-15T13:46:40.855Z",null]
|
|
const rows=timestamps.map((polledAt,index)=>({...server,id:index+1,type:index===0?"exit-node":"jump-host",polledAt}))
|
|
const snapshot=validateNetworkMapSnapshot(rows,{tunnels:[],failures:[]},{probes:[]})
|
|
assert.equal(snapshot.servers.length,4)
|
|
assert.equal(snapshot.servers[0].type,"exit-node")
|
|
assert.equal(Date.parse(snapshot.servers[0].polledAt!),Date.parse(snapshot.servers[1].polledAt!))
|
|
assert.equal(snapshot.servers[3].polledAt,null)
|
|
for(const polledAt of ["2026-10-15 13:46:40.855","2026-02-30 13:46:40+00","not-a-date",42]) {
|
|
assert.throws(()=>validateNetworkMapSnapshot([{...server,polledAt}],{tunnels:[],failures:[]},{probes:[]}))
|
|
}
|
|
})
|
|
test("network map retains configured GRE IPsec without key material",()=>{
|
|
const tunnel={id:"*1",serverId:"1",name:"own210",localAddress:"192.0.2.1",remoteAddress:"192.0.2.2",localInnerIp:"10.0.0.1/30",remoteInnerIp:"10.0.0.2/30",poolId:"",ipsec:{secret:""},mtu:1476,keepaliveInterval:60,keepaliveRetries:3,dscp:"inherit",clampTcpMss:true,allowFastPath:false,comment:"",enabled:true,status:"up"}
|
|
const result=validateNetworkMapSnapshot([server],{tunnels:[tunnel],failures:[]},{probes:[]})
|
|
assert.deepEqual(result.tunnels[0].ipsec,{secret:""})
|
|
})
|
|
test("network map requires complete envelopes and preserves zero/null",()=>{
|
|
const result=validateNetworkMapSnapshot([server],{tunnels:[],failures:[]},{probes:[]})
|
|
assert.equal(result.servers[0].latency,0);assert.equal(result.servers[0].status,null)
|
|
for(const args of [[null,{tunnels:[]},{probes:[]}],[[server],{}, {probes:[]}],[[server],{tunnels:[]},{}],[[server,server],{tunnels:[]},{probes:[]}],[[{...server,freeMemory:20,totalMemory:10}],{tunnels:[]},{probes:[]}],[[{...server,status:undefined}],{tunnels:[]},{probes:[]}]])assert.throws(()=>validateNetworkMapSnapshot(...args as [unknown,unknown,unknown]))
|
|
})
|
|
test("network map refuses partial GRE and foreign failure/probe contexts",()=>{
|
|
assert.throws(()=>validateNetworkMapSnapshot([server],{tunnels:[],failures:[{serverId:"1",error:"offline"}]},{probes:[]}),/Неполный/)
|
|
assert.throws(()=>validateNetworkMapSnapshot([server],{tunnels:[],failures:[{serverId:"2",error:"offline"}]},{probes:[]}),/неизвестному/)
|
|
const probe={id:"p",srcServerId:"1",dstServerId:"2",srcInterface:"",dstInterface:"",enabled:true,lastTxAvgMbps:null,lastRxAvgMbps:null,lastPingRttMs:null,lastPingLossPct:null,lastPingAt:null}
|
|
assert.throws(()=>validateNetworkMapSnapshot([server],{tunnels:[]},{probes:[probe]}),/неизвестному/)
|
|
})
|
|
|
|
import {networkMapFlowSchema,validateNetworkMapDns} from "./network-map-response"
|
|
test("map Flow envelope retains zeros and refuses missing metadata and contradictory totals",()=>{
|
|
const empty={"hops":[],"live":false,"rangeMinutes":5,"windowSec":300,"totalBytes":0,"namedBytes":0,"unclassifiedBytes":0,"asnLoaded":false,"countryLoaded":false,"services":[],"serviceEdges":[],"servicePaths":[],"countries":[],"countryEdges":[],"countryPaths":[],"countryServiceGroups":[],"mapServiceMinSharePct":5,"dedupApplied":false,"excludeMeshApplied":false,"excludeOverlayApplied":false}
|
|
assert.equal(networkMapFlowSchema.parse(empty).totalBytes,0)
|
|
assert.equal(networkMapFlowSchema.parse(empty).live,false)
|
|
assert.throws(()=>networkMapFlowSchema.parse({...empty,totalBytes:undefined}))
|
|
assert.throws(()=>networkMapFlowSchema.parse({...empty,namedBytes:1}))
|
|
assert.throws(()=>networkMapFlowSchema.parse({...empty,rangeMinutes:10}))
|
|
})
|
|
test("map DNS matches exactly requested names and accepts explicit unresolved null",()=>{
|
|
assert.deepEqual(validateNetworkMapDns({results:{"router.example":null}},["router.example"]),{"router.example":null})
|
|
assert.throws(()=>validateNetworkMapDns({results:{}},["router.example"]))
|
|
assert.throws(()=>validateNetworkMapDns({results:{"foreign.example":"192.0.2.1"}},["router.example"]))
|
|
assert.throws(()=>validateNetworkMapDns({results:{"router.example":"not-ip"}},["router.example"]))
|
|
})
|