Files
MikrotikManager/backend/src/services/server-probe-storage-history.test.ts
T

34 lines
2.6 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { test } from "node:test"
import { createServerProbeStorageHistoryReader } from "./server-probe-storage-history.js"
import { ServerProbeHistoryUnavailable, type ServerProbeHistoryBinding } from "./server-probe-journal-history.js"
const at = 1800000000000
const binding: ServerProbeHistoryBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), context: {
identity: { kind: "mcp", ownerId: "owner", executionId: randomUUID(), connectionId: randomUUID(), credentialId: randomUUID(), issuedAt: at - 2000, expiresAt: at - 1000, operationName: "servers.probes.run" }, sourceServerId: 1, tool: "ping" } }
test("fixed history reader refuses invalid or extended absolute budgets before I/O", () => {
for (const deadlineAt of [NaN, Infinity, at, at - 1, at + 120001]) assert.throws(() => createServerProbeStorageHistoryReader({ options: { deadlineAt }, now: () => at, assertReadAuthority: async () => { assert.fail("No authority or SQL") } }), ServerProbeHistoryUnavailable)
})
test("aborted read never reaches current authority or SQL", async () => {
const controller = new AbortController(); controller.abort()
const reader = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 120000, signal: controller.signal }, now: () => at, assertReadAuthority: async () => { assert.fail("No authority or SQL") } })
await assert.rejects(reader.read(binding), { name: "AbortError" })
})
test("copied deadline cannot be extended after construction", async () => {
let time = at
const options = { deadlineAt: at + 1000 }
const reader = createServerProbeStorageHistoryReader({ options, now: () => time, assertReadAuthority: async () => { assert.fail("No authority or SQL") } })
options.deadlineAt = at + 120000; time = at + 1000
await assert.rejects(reader.read(binding), ServerProbeHistoryUnavailable)
})
test("authority refusal and time spent in authority both stop before SQL", async () => {
const denial = Error("current authority revoked")
const refused = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 1000 }, now: () => at, assertReadAuthority: async () => { throw denial } })
await assert.rejects(refused.read(binding), error => error === denial)
let time = at, calls = 0
const expired = createServerProbeStorageHistoryReader({ options: { deadlineAt: at + 1000 }, now: () => time, assertReadAuthority: async original => { assert.deepEqual(original, binding); calls++; time = at + 1000 } })
await assert.rejects(expired.read(binding), ServerProbeHistoryUnavailable); assert.equal(calls, 1)
})