Files
MikrotikManager/backend/src/services/firewall-poll.test.ts
T

38 lines
2.3 KiB
TypeScript

import assert from "node:assert/strict"
import { pollFirewallChunks } from "./firewall-poll.js"
import { fetchFirewallState, captureFirewallSnapshot } from "./firewall-live.js"
import { MikrotikClient } from "./mikrotik.js"
const client = {
getFirewallRules: async (family: string, table: string) => {
if (family === "ip6" && table === "nat") throw new Error("slice unavailable")
return [{ ".id": "*1", chain: "forward", action: "accept", packets: "1" }]
},
getFirewallAddressList: async () => [],
}
const partial = await pollFirewallChunks(client)
assert.equal(partial.ruleChunks.length, 8)
assert.equal(partial.listChunks.length, 2)
assert.deepEqual(partial.failures, [{ family: "ip6", table: "nat", error: "slice unavailable" }])
assert.equal(partial.ruleChunks.filter(chunk => chunk.raw.length === 1).length, 7)
const empty = await pollFirewallChunks({ getFirewallRules: async () => [], getFirewallAddressList: async () => [] })
assert.deepEqual(empty.failures, [])
assert.ok(empty.ruleChunks.every(chunk => chunk.raw.length === 0))
const failed = await pollFirewallChunks({ getFirewallRules: async () => { throw new Error("failed") }, getFirewallAddressList: async () => { throw new Error("failed") } })
assert.equal(failed.failures.length, 10)
const malformed = await pollFirewallChunks({ getFirewallRules: async () => null as never, getFirewallAddressList: async () => [] })
assert.equal(malformed.failures.length, 8)
assert.ok(malformed.failures.every(failure => failure.error.includes("некорректный список")))
const fromServer = MikrotikClient.fromServer
try {
MikrotikClient.fromServer = () => client as unknown as ReturnType<typeof fromServer>
const server = { id: 1, name: "Router", host: "192.0.2.1" } as Parameters<typeof fetchFirewallState>[0]
await assert.rejects(fetchFirewallState(server), /Не удалось полностью прочитать Firewall/)
await assert.rejects(captureFirewallSnapshot(server), /Не удалось полностью прочитать Firewall/)
const state = await fetchFirewallState(server, true)
assert.equal(state.rules.length, 7)
assert.deepEqual(state.failures, partial.failures)
} finally { MikrotikClient.fromServer = fromServer }
console.log("Firewall polling: mixed/empty/all-failed/malformed and strict snapshot rejection passed")