53 lines
2.7 KiB
TypeScript
53 lines
2.7 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import Fastify from "fastify"
|
|
import { validatorCompiler, serializerCompiler } from "@fastify/type-provider-zod"
|
|
import { createBgpRoutes } from "./bgp.js"
|
|
import { pollBgpCatalog } from "../services/bgp-catalog-poll.js"
|
|
import { servers } from "../db/schema.js"
|
|
import type { BgpSessionRead } from "../types/server.js"
|
|
|
|
const devices = [{ id: 1, name: "Первый" }, { id: 2, name: "Второй" }]
|
|
const row = { id: "*1", serverId: 1 }
|
|
const mixed = await pollBgpCatalog(devices, async device => {
|
|
if (device.id === 2) throw new Error("Таймаут BGP")
|
|
return [row]
|
|
})
|
|
assert.deepEqual(mixed, { sessions: [row], failures: [{ serverId: 2, serverName: "Второй", error: "Таймаут BGP" }] })
|
|
assert.deepEqual(await pollBgpCatalog(devices, async () => []), { sessions: [], failures: [] })
|
|
assert.equal((await pollBgpCatalog(devices, async () => { throw new Error("Отказ") })).failures.length, 2)
|
|
for (const rows of [[{ ...row, serverId: 2 }], [row, row], [{ ...row, id: " " }]]) {
|
|
const result = await pollBgpCatalog([devices[0]], async () => rows)
|
|
assert.equal(result.sessions.length, 0)
|
|
assert.equal(result.failures.length, 1)
|
|
}
|
|
assert.deepEqual(await pollBgpCatalog([], async () => { throw new Error("Не вызывать") }), { sessions: [], failures: [] })
|
|
|
|
const app = Fastify()
|
|
app.setValidatorCompiler(validatorCompiler)
|
|
app.setSerializerCompiler(serializerCompiler)
|
|
let reads = 0
|
|
await app.register(createBgpRoutes({
|
|
listEnabledServers: async () => devices as typeof servers.$inferSelect[],
|
|
findServer: async id => devices.find(d => d.id === id) as typeof servers.$inferSelect | undefined,
|
|
readSessions: async device => {
|
|
reads++
|
|
if (device.id === 2) throw new Error("Таймаут BGP")
|
|
return [row as BgpSessionRead]
|
|
},
|
|
}), { prefix: "/api" })
|
|
const catalog = await app.inject({ method: "GET", url: "/api/bgp/catalog" })
|
|
assert.equal(catalog.statusCode, 200)
|
|
assert.deepEqual(catalog.json(), mixed)
|
|
const legacy = await app.inject({ method: "GET", url: "/api/bgp/sessions" })
|
|
assert.deepEqual(legacy.json(), [row])
|
|
const selected = await app.inject({ method: "GET", url: "/api/servers/2/bgp/sessions" })
|
|
assert.equal(selected.statusCode, 503)
|
|
assert.equal(selected.json().error, "Таймаут BGP")
|
|
assert.deepEqual((await app.inject({ method: "GET", url: "/api/servers/1/bgp/sessions" })).json(), [row])
|
|
assert.equal((await app.inject({ method: "GET", url: "/api/servers/99/bgp/sessions" })).statusCode, 404)
|
|
const before = reads
|
|
assert.equal((await app.inject({ method: "GET", url: "/api/servers/0/bgp/sessions" })).statusCode, 400)
|
|
assert.equal(reads, before)
|
|
await app.close()
|
|
console.log("BGP catalog: partial/empty/failed/context/duplicates and HTTP compatibility/503 passed")
|