import assert from "node:assert/strict" import {test} from "node:test" import {parseDashboardPath,parseDashboardSpeedProbes,parseDashboardWanRuntime,parseDashboardFilters} from "./dashboard-path-response" import {resolveDefaultRouteLookup,buildDashboardInternetPath,dashboardPathMetrics} from "./dashboard-internet-path" import {servers} from "./data" const snapshot={collectionComplete:true,sampledAt:"2026-10-03T00:00:00Z",servers:[],greTunnels:[],filtersRulesets:[],speedProbes:[],routeLookupByServerId:{},wanRuntimeByHomeId:{}} test("path validates explicit empty snapshot and rejects missing fields, foreign IDs and mismatched time",()=>{ assert.deepEqual(parseDashboardPath({snapshot:null}),{snapshot:null});assert.ok(parseDashboardPath({snapshot})) for(const raw of [{snapshot:{...snapshot,collectionComplete:undefined}},{snapshot:{}},{snapshot:null,sampledAt:snapshot.sampledAt},{snapshot:{...snapshot,speedProbes:undefined}},{snapshot:{...snapshot,routeLookupByServerId:{"99":null}}},{snapshot,sampledAt:"2026-10-03T01:00:00Z"}])assert.throws(()=>parseDashboardPath(raw)) }) test("path fallback validates rules, speed probes and WAN rather than silently using empty data",()=>{ assert.ok(parseDashboardSpeedProbes({probes:[]}));assert.ok(parseDashboardFilters({rulesets:[]}));assert.ok(parseDashboardWanRuntime({defaultGateway:null,defaultInterface:null,uplinks:[]})) for(const work of [()=>parseDashboardSpeedProbes({}),()=>parseDashboardFilters({rulesets:[],stale:true}),()=>parseDashboardWanRuntime({}),()=>parseDashboardWanRuntime({defaultGateway:null,defaultInterface:null,uplinks:[{}]})])assert.throws(work) }) test("route diagnostic rejects malformed and error output; no route is a confirmed empty result",async()=>{ for(const raw of [{},{output:"Error: denied"},{output:12}])await assert.rejects(resolveDefaultRouteLookup(async()=>raw as T,"1")) assert.deepEqual(await resolveDefaultRouteLookup(async()=>({output:""}) as T,"1"),{gateway:null,routingMark:null}) }) test("configured WAN alone does not prove an observed current route",()=>{ const mapped=servers.map((s,i)=>({...s,id:String(i+1)}));const result=buildDashboardInternetPath({servers:mapped,greTunnels:[],filtersRulesets:[],probes:[],routeLookupByServerId:{},wanRuntimeByHomeId:{}}) assert.ok(result);assert.equal(result.pathState,"unknown");assert.equal(result.currentPath,null);assert.equal(result.activeWanUplink,null) }) test("path metrics use dated successful hop observations, preserve RX/TX and reject configured fallback",()=>{ const p={id:"p",srcServerId:"1",dstServerId:"2",srcInterface:"ether1",dstInterface:"",enabled:true,lastPingRttMs:0,lastPingLossPct:0,lastTxAvgMbps:0,lastRxAvgMbps:5,lastPingAt:snapshot.sampledAt,lastRunAt:snapshot.sampledAt,lastStatus:"done",lastError:null,lastPingError:null} assert.deepEqual(dashboardPathMetrics(p),{pingMs:0,dlMbps:5,ulMbps:0,fromMonitoring:true});assert.deepEqual(dashboardPathMetrics(p,true),{pingMs:0,dlMbps:0,ulMbps:5,fromMonitoring:true}) assert.deepEqual(dashboardPathMetrics(null),{pingMs:null,dlMbps:null,ulMbps:null,fromMonitoring:false}) assert.deepEqual(dashboardPathMetrics({...p,lastRunAt:null,lastPingAt:null}),{pingMs:null,dlMbps:null,ulMbps:null,fromMonitoring:false}) assert.deepEqual(dashboardPathMetrics({...p,lastStatus:"error",lastPingError:"denied"}),{pingMs:null,dlMbps:null,ulMbps:null,fromMonitoring:false}) assert.equal(dashboardPathMetrics({...p,lastRxAvgMbps:null}).dlMbps,null) assert.deepEqual(dashboardPathMetrics({...p,enabled:false}),{pingMs:null,dlMbps:null,ulMbps:null,fromMonitoring:false}) }) test("observed default route does not select the first WAN or a merely bound DHCP lease",()=>{ const home={...servers.find(s=>s.type==="home-router")!,id:"1"};const runtime={defaultGateway:"203.0.113.254",defaultInterface:"unknown-interface",uplinks:home.wanUplinks!.map(w=>({id:w.id,iface:w.iface,name:w.name,isp:w.isp,configuredIp:w.ip,leasedIp:w.ip,dhcpStatus:"bound",isDefault:false}))} const result=buildDashboardInternetPath({servers:[home,...servers.filter(s=>s.type!=="home-router")],greTunnels:[],filtersRulesets:[],probes:[],routeLookupByServerId:{"1":{gateway:"203.0.113.254",routingMark:null}},wanRuntimeByHomeId:{"1":runtime}}) assert.equal(result?.activeWanUplink,null);assert.equal(result?.currentPath,null);assert.equal(result?.directWan.gateway,"203.0.113.254") })