Files
MikrotikManager/backend/src/routes/traffic-flow-settings-shared.test.ts
T

32 lines
2.4 KiB
TypeScript

import assert from "node:assert/strict"
import Fastify from "fastify"
import { createTrafficFlowSettingsRoutes } from "./traffic-flow-settings.js"
import { createTrafficFlowSettingsService } from "../services/traffic-flow-settings-service.js"
import type { FlowSettingsConfiguration } from "../services/traffic-flow-settings-execution.js"
import type { TrafficFlowSettingsDto } from "@mmapp/contracts/traffic-flow"
let current: FlowSettingsConfiguration = {id:1,enabled:false,topN:200,hostPublicKey:"public",hostPrivateKey:"PRIVATE",future:"preserved"}
let starts=0,writes=0,fail=false,legacy=0
const phases:string[]=[]
const execute = createTrafficFlowSettingsService({
exclusive:async work=>work(),guard:async work=>work(),agentPinned:()=>false,proof:async()=>"a".repeat(64),
store:()=>({read:async()=>structuredClone(current),claim:async(_id,phase)=>{phases.push(phase)},compareAndSet:async(_id,_proof,_before,desired)=>{writes++;current=structuredClone(desired);return true},complete:async()=>{}}),
startListener:async fence=>{await fence?.();starts++;if(fail)throw Error("PRIVATE unexpected raw error")},
})
const app=Fastify()
await app.register(createTrafficFlowSettingsRoutes({
read:async()=>({enabled:current.enabled,topN:current.topN} as TrafficFlowSettingsDto),updateWithListener:execute,
update:async()=>{legacy++},startListener:async()=>{legacy++},ensureKeys:async()=>({publicKey:"public",created:false}),hasPrivateKey:async()=>true,hostFiles:async()=>[],
}))
for(const payload of [{enabled:false},{topN:200},{mapServiceMinSharePct:0}]){
const response=await app.inject({method:"PUT",url:"/traffic/flow/settings",payload})
assert.equal(response.statusCode,200);assert.equal(response.json().ok,true)
}
fail=true
const partial=await app.inject({method:"PUT",url:"/traffic/flow/settings",payload:{topN:100}})
assert.equal(partial.statusCode,200);assert.equal(partial.json().settings.topN,100);assert.ok(partial.json().listenerError)
assert.ok(!partial.body.includes("PRIVATE"));assert.equal(current.future,"preserved")
assert.equal(starts,4);assert.equal(writes,4);assert.equal(legacy,0)
assert.deepEqual(phases,["configuration","listener","configuration","listener","configuration","listener","configuration","listener"])
await app.close()
console.log("NetFlow PUT uses shared full settings executor once, no-op/display restart and saved configuration HTTP200 sanitized listenerError; injected no worker/SQL")