Files
MikrotikManager/lib/ospf-url-state.test.ts

21 lines
1.4 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { readOspfUrl, patchOspfUrl } from "./ospf-url-state"
test("OSPF device/tab URL preserves unrelated context", () => {
const url=patchOspfUrl(new URLSearchParams("keep=x"),{server:"2",tab:"bfd"})
assert.deepEqual(readOspfUrl(new URL(url,"http://test").searchParams),{server:"2",tab:"bfd",search:"",node:null,error:null})
assert.equal(patchOspfUrl(new URL(url,"http://test").searchParams,{server:null,tab:null}),"/ospf?keep=x")
})
test("invalid or repeated context is explicit instead of widening to all",()=>{
for(const raw of ["server=a&server=b","tab=bfd&tab=neighbors","tab=broken","server=", "server="+"x".repeat(65),"server=%2F"]) assert.ok(readOspfUrl(new URLSearchParams(raw)).error)
assert.equal(readOspfUrl(new URLSearchParams("server=srv1&tab=neighbors")).error,null)
})
test("OSPF search/node roundtrip and invalid bounds",()=>{
const params=new URLSearchParams({server:"1",tab:"neighbors",q:"ether & 1",node:"remote-192.0.2.2",keep:"x"})
const state=readOspfUrl(params)
assert.equal(state.search,"ether & 1");assert.equal(state.node,"remote-192.0.2.2");assert.equal(state.error,null)
for(const raw of ["q=x&q=y","node=1&node=2","node=%2F","q="+"x".repeat(257),"node="+"x".repeat(129)])assert.ok(readOspfUrl(new URLSearchParams(raw)).error)
assert.equal(patchOspfUrl(params,{q:null,node:null}),"/ospf?server=1&tab=neighbors&keep=x")
})