Files
MikrotikManager/tests/unit/flow-live-admission.test.ts
T

55 lines
1.9 KiB
TypeScript

import assert from "node:assert/strict"
import test from "node:test"
import { openFlowLiveResponse } from "../../hooks/use-flow-live"
const url = "http://127.0.0.1/api/traffic/flow/live"
test("opens the NetFlow stream after a transient 429", async () => {
const { signal } = new AbortController()
let calls = 0
let discarded = false
const response = await openFlowLiveResponse(url, signal, async (target, init) => {
calls++
assert.equal(target, url)
assert.equal(init?.signal, signal)
assert.equal(new Headers(init?.headers).get("Accept"), "text/event-stream")
if (calls === 1) {
return new Response(new ReadableStream({ cancel() { discarded = true } }), { status: 429 })
}
return new Response("event: sample\ndata: {}\n\n", { headers: { "Content-Type": "text/event-stream" } })
})
assert.equal(calls, 2)
assert.equal(discarded, true)
assert.equal(response.status, 200)
})
test("stops retrying when the NetFlow live view unmounts", async () => {
const controller = new AbortController()
let calls = 0
const response = openFlowLiveResponse(url, controller.signal, async () => {
calls++
return new Response(new ReadableStream({
cancel() { setTimeout(() => controller.abort(), 20) },
}), { status: 429 })
})
await assert.rejects(response, { name: "AbortError" })
assert.equal(calls, 1)
})
test("keeps the live admission budget bounded and leaves authorization failures terminal", async () => {
const { signal } = new AbortController()
let calls = 0
const limited = await openFlowLiveResponse(url, signal, async () => {
calls++
return new Response(null, { status: 429 })
})
assert.equal(limited.status, 429)
assert.equal(calls, 4)
const unauthorized = await openFlowLiveResponse(url, signal, async () => {
calls++
return new Response(null, { status: 401 })
})
assert.equal(unauthorized.status, 401)
assert.equal(calls, 5)
})