Files
MikrotikManager/backend/src/modules/mcp/poll-runtime-policy-source.test.ts
T

45 lines
3.2 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import type { PoolClient } from "pg"
import { pollRuntimePolicySql, readPollRuntimePolicySource } from "./poll-runtime-policy-source.js"
const empty = () => ({ protection_count: 0, protection_oversize: false, protections: [], calls: 3, mutations: 4, reads: 1, grant_oversize: false, grants: [] })
const credential = "owned-credential"
const grant = { id: "grant", startsAt: "2026-10-29T00:00:00Z", expiresAt: "2026-10-29T01:00:00Z", scope: { serverIds: [1], operationIds: ["servers.poll"], allowGlobal: false } }
function client(rows: () => unknown[]) {
let queries = 0
return { get queries() { return queries }, query: (async (text: string, values: unknown[]) => {
queries++; assert.equal(text, pollRuntimePolicySql); assert.deepEqual(values, ["connection", credential]); return { rows: rows() }
}) as PoolClient["query"] }
}
test("each invocation executes a fresh bound SELECT, including protection and current grant changes", async () => {
let row: Record<string, unknown> = empty()
const sql = client(() => [row])
assert.deepEqual(await readPollRuntimePolicySource(sql, "connection", credential), { protections: [], usage: { calls: 3, mutations: 4, reads: 1 }, temporaryGrant: undefined })
const object = { kind: "server", id: "1", serverId: 1, protection: "deny" }
row = { ...empty(), protection_count: 1, protections: [object], grants: [{ ...grant, credentialId: credential }] }
assert.deepEqual(await readPollRuntimePolicySource(sql, "connection", credential), { protections: [{ object_ref: object }], usage: { calls: 3, mutations: 4, reads: 1 }, temporaryGrant: grant })
row = empty(); assert.equal((await readPollRuntimePolicySource(sql, "connection", credential)).temporaryGrant, undefined)
assert.equal(sql.queries, 3)
})
test("missing, duplicated, malformed, truncated and oversized source refuses", async () => {
for (const rows of [[], [empty(), empty()], [{ ...empty(), calls: -1 }], [{ ...empty(), reads: "1" }], [{ ...empty(), extra: true }],
[{ ...empty(), protection_count: 1 }], [{ ...empty(), protection_count: 1001, protections: Array(1001).fill({}) }],
[{ ...empty(), protection_oversize: true }]]) {
await assert.rejects(readPollRuntimePolicySource(client(() => rows), "connection", credential), { code: "STATE_UNAVAILABLE" })
}
})
test("ambiguous, oversized, malformed and different-credential grants refuse", async () => {
for (const patch of [{ grants: [{}, {}] }, { grant_oversize: true, grants: [null] },
{ grants: [{ ...grant, credentialId: "other" }] }]) {
await assert.rejects(readPollRuntimePolicySource(client(() => [{ ...empty(), ...patch }]), "connection", credential), { code: "GRANT_INACTIVE" })
}
await assert.rejects(readPollRuntimePolicySource(client(() => [{ ...empty(), grants: [{ credentialId: credential }] }]), "connection", credential))
})
test("source errors propagate without retry or fallback", async () => {
const failure = Error("owned SQL failure"); let queries = 0
const sql = { query: (async () => { queries++; throw failure }) as PoolClient["query"] }
await assert.rejects(readPollRuntimePolicySource(sql, "connection", credential), error => error === failure)
assert.equal(queries, 1)
})