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

30 lines
1.4 KiB
TypeScript

import assert from "node:assert/strict"
import Fastify from "fastify"
import { serializerCompiler, validatorCompiler } from "@fastify/type-provider-zod"
import trafficFlowRoutes, { resetFlowLiveSlotsForTests, tryAcquireFlowLiveSlot, releaseFlowLiveSlot } from "./traffic-flow.js"
import { closePool } from "../db/index.js"
const app = Fastify({ logger: false })
app.setValidatorCompiler(validatorCompiler)
app.setSerializerCompiler(serializerCompiler)
await app.register(trafficFlowRoutes, { prefix: "/api" })
resetFlowLiveSlotsForTests()
try {
for (const id of ["srv1", "1suffix", "-1", "0", "1.5", "1e3", "9007199254740992"]) {
for (const endpoint of ["analytics", "monthly", "live"]) {
const response = await app.inject({ method: "GET", url: `/api/traffic/flow/${endpoint}?serverId=${encodeURIComponent(id)}&month=2026-10` })
assert.equal(response.statusCode, 400, `${endpoint}/${id}`)
assert.match(response.body, /Некорректный идентификатор сервера/)
}
}
// Failed query validation must not consume the bounded live subscription quota.
for (let i = 0; i < 4; i++) assert.equal(tryAcquireFlowLiveSlot(), true)
assert.equal(tryAcquireFlowLiveSlot(), false)
for (let i = 0; i < 4; i++) releaseFlowLiveSlot()
console.log("traffic-flow-query.test.ts: 21 invalid scoped requests rejected; SSE slots retained")
} finally {
resetFlowLiveSlotsForTests()
await app.close()
await closePool()
}