Files
MikrotikManager/backend/src/modules/mcp/server-probe-storage-host.test.ts
T

65 lines
3.9 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { test } from "node:test"
import type { PoolClient } from "pg"
import { pool } from "../../db/index.js"
import { createStorageServerProbeMcpAdapter, serverProbeStorageJournalDeadline } from "./server-probe-storage-host.js"
import { planServerProbeExecution } from "../../services/server-probe-execution-model.js"
import { PROBE_STORAGE_LIMITS } from "./diagnostic-cancellation-storage.js"
import { findMcpService } from "./service-registry.js"
import type { ActionContext } from "./action-types.js"
const context = (): ActionContext => ({ principal: { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(Date.now() + 60000).toISOString() }, connection: {} } as ActionContext)
const input = { serverId: 1, request: { tool: "ping", target: "127.0.0.1" } }
test("journal lifetime covers every supported executor budget and cannot outlive private plan or authority", () => {
const now = 1800000000000, authority = now + 3600000, prepared = now + 300000
for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) {
const plan = planServerProbeExecution({ tool, target: "127.0.0.1", bwRemoteAddress: "127.0.0.1" })
assert(serverProbeStorageJournalDeadline(authority, prepared, now) - now >= plan.wallBudgetMs, tool + " is cut off before its supported wall budget")
}
assert.equal(serverProbeStorageJournalDeadline(authority, now + 90000, now), now + 90000)
assert.equal(serverProbeStorageJournalDeadline(now + 60000, prepared, now), now + 60000)
assert.equal(serverProbeStorageJournalDeadline(authority, prepared, now + 150000), prepared)
for (const expiry of [NaN, Infinity, now, now - 1]) assert.throws(() => serverProbeStorageJournalDeadline(authority, expiry, now))
})
async function withoutPool(work: () => Promise<void> | void) {
const originalQuery = pool.query, originalConnect = pool.connect
pool.query = (() => { assert.fail("Unexpected pool query") }) as typeof pool.query
pool.connect = (() => { assert.fail("Unexpected checkout") }) as typeof pool.connect
try { await work() } finally { pool.query = originalQuery; pool.connect = originalConnect }
}
test("opt-in host construction is inert, validates trusted quota and registers no service", async () => {
await withoutPool(() => {
assert.equal(findMcpService("servers.probes.run"), undefined)
createStorageServerProbeMcpAdapter()
assert.equal(findMcpService("servers.probes.run"), undefined)
assert.throws(() => createStorageServerProbeMcpAdapter({ ...PROBE_STORAGE_LIMITS, globalFiles: 0 }))
})
})
test("pre-aborted target refuses before pool or borrowed planner SQL", async () => {
await withoutPool(async () => {
const host = createStorageServerProbeMcpAdapter(), controller = new AbortController(); controller.abort()
await assert.rejects(host.target(input, { ...context(), signal: controller.signal }), { code: "READ_CANCELLED" })
const transactionClient = { query: (() => assert.fail("Unexpected borrowed planner SQL")) as PoolClient["query"] }
await assert.rejects(host.target(input, { ...context(), signal: controller.signal, transactionClient }), { code: "READ_CANCELLED" })
})
})
test("detached child of failed planner context cannot fall back to the pool", async () => {
await withoutPool(async () => {
const host = createStorageServerProbeMcpAdapter(), actor = context()
let release!: () => void, child: Promise<unknown> | undefined
const gate = new Promise<void>(resolve => { release = resolve })
const transactionClient = { query: (async () => {
child ??= gate.then(() => host.target(input, actor))
return { rows: [] }
}) as unknown as PoolClient["query"] }
try {
await assert.rejects(host.target(input, { ...actor, transactionClient }))
assert(child)
const rejected = assert.rejects(child, { code: "STATE_CONFLICT" })
release(); await rejected
} finally { release() }
})
})