Files
MikrotikManager/backend/src/services/traffic-live.test.ts
T
Denozordec 589fc782f9
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m29s
Docker images / frontend-image (push) Successful in 2m13s
Docker images / updater-image (push) Successful in 40s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m59s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
fix(map): исправить трафик и карточки карты сети
2026-11-26 10:34:20 +07:00

52 lines
4.1 KiB
TypeScript

import assert from "node:assert/strict"
import { parseMonitorTraffic, rateBpsFromDelta, bpsToMbps } from "./traffic-rate.js"
import { readTrafficLiveInterfaces, 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)
const batch = (testClient = cast) =>
readTrafficLiveInterfaces(testClient, ["ether1", "ether2"], new AbortController().signal, async () => {})
const joined = { ...cast, post: async () => [point("ether2", "0", "80000000"), point("ether1", "1300000", "0")] } as typeof cast
const measurements = await batch(joined)
assert.equal(measurements.ether1.rxMbps, 1.3)
assert.equal(measurements.ether2.txMbps, 80)
assert.equal(measurements.ether1.at, measurements.ether2.at)
mode = "partial"
assert.deepEqual(Object.keys(await batch()), ["ether1"])
mode = "fallback"
assert.deepEqual(Object.keys(await batch()), ["ether1", "ether2"])
mode = "auth"
await assert.rejects(batch(), /401/)
console.log("traffic live completeness/fallback/auth/source/abort/positive-small-rate tests ok")