Files
MikrotikManager/backend/src/services/traffic-live.test.ts
T

38 lines
3.4 KiB
TypeScript

import assert from "node:assert/strict"
import { parseMonitorTraffic, rateBpsFromDelta, bpsToMbps } from "./traffic-rate.js"
import { readTrafficLiveSample, TrafficLiveSourceChanged } from "./traffic-live.js"
import { MikrotikError, type MikrotikClient } from "./mikrotik.js"
const point=(name="ether1",rx:unknown="0",tx:unknown="0")=>({name,"rx-bits-per-second":rx,"tx-bits-per-second":tx})
for(const raw of [[],{},null,[null],[{...point(),"rx-bits-per-second":undefined}], [point("ether1","12bad")],[point("ether1",-1)], [point("ether1"),point("ether1")]]) {
assert.throws(()=>parseMonitorTraffic(raw))
}
assert.throws(()=>parseMonitorTraffic([point()],{expectedInterfaces:["ether1","ether2"]}))
assert.throws(()=>parseMonitorTraffic([point("other")],{onlyInterface:"ether1"}))
assert.equal(parseMonitorTraffic([point()]).rxMbps,0)
assert.equal(parseMonitorTraffic([point("ether1","1")]).rxMbps,0.000001)
assert(rateBpsFromDelta(1000,1001,0,30_000)!>0);assert(bpsToMbps(rateBpsFromDelta(1000,1001,0,30_000)!)>0)
assert.equal(parseMonitorTraffic([point()]).timestampKind,"backend-receipt")
let mode="fallback",postNames:string[]=[],guards=0
const client={get:async()=>[{name:"ether1",running:"true",disabled:"false"},{name:"ether2",running:true,disabled:false}],
post:async(_path:string,body:{interface:string})=>{postNames.push(body.interface);if(body.interface.includes(",")){
if(mode==="auth")throw new MikrotikError(401,"/interface/monitor-traffic","Unauthorized")
if(mode==="partial")return[point()]
throw new MikrotikError(400,"/interface/monitor-traffic","Unsupported joined interface")
}if(mode==="deny"&&body.interface==="ether2")throw new MikrotikError(401,"/interface/monitor-traffic","Unauthorized");return[point(body.interface,"1")]}}
const cast=client as unknown as Pick<MikrotikClient,"get"|"post">
const run=()=>readTrafficLiveSample(cast,undefined,{names:[],joinedFailed:false},new AbortController().signal,async()=>{guards++})
const complete=await run();assert.equal(complete.rxMbps,0.000002);assert(guards>=4)
mode="deny";await assert.rejects(run(),/401/)
mode="auth";postNames=[];await assert.rejects(run(),/401/);assert.equal(postNames.length,1)
mode="partial";postNames=[];await assert.rejects(run(),/все live/);assert.equal(postNames.length,1)
await assert.rejects(readTrafficLiveSample(cast,undefined,{names:[],joinedFailed:false},new AbortController().signal,async()=>{throw new TrafficLiveSourceChanged("disabled")}),/disabled/)
const abort=new AbortController();abort.abort();postNames=[];await assert.rejects(readTrafficLiveSample(cast,"ether1",{names:[],joinedFailed:false},abort.signal,async()=>{}));assert.equal(postNames.length,0)
let active=0,maximum=0
const many={get:async()=>Array.from({length:10},(_,i)=>({name:`ether${i}`,running:true,disabled:false})),post:async(_path:string,body:{interface:string})=>{
if(body.interface.includes(","))throw new MikrotikError(400,"/interface/monitor-traffic","Joined unsupported")
active++;maximum=Math.max(maximum,active);await new Promise(resolve=>setTimeout(resolve,5));active--;return[point(body.interface)]
}} as unknown as Pick<MikrotikClient,"get"|"post">
const bounded=await readTrafficLiveSample(many,undefined,{names:[],joinedFailed:false},new AbortController().signal,async()=>{})
assert.equal(bounded.rxMbps,0);assert.equal(maximum,4)
console.log("traffic live completeness/fallback/auth/source/abort/positive-small-rate tests ok")