57 lines
3.3 KiB
TypeScript
57 lines
3.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { test } from "node:test"
|
|
import { pool } from "../../db/index.js"
|
|
import { withVerifiedConfigurationChange } from "../../services/config-mutation-policy.js"
|
|
import { createProductionServerPollMcpAdapter, queryServerPollMcpMetadata } from "./server-poll-production.js"
|
|
|
|
test("fixed production construction is inert and direct execution needs the real inherited verified guard", async () => {
|
|
const priorQuery = pool.query, priorConnect = pool.connect
|
|
let io = 0, inheritedChecks = 0
|
|
pool.query = (() => { io++; throw Error("Unexpected production I/O") }) as never
|
|
pool.connect = (() => { io++; throw Error("Unexpected production checkout") }) as never
|
|
try {
|
|
const adapter = createProductionServerPollMcpAdapter(), context = { operationId: randomUUID(), phase: "forward" as const, stepIndex: 0 }
|
|
assert.equal(adapter.service.cancellableDiagnostic, undefined)
|
|
await assert.rejects(adapter.service.execute({}, context), { code: "PERMISSION_DENIED" })
|
|
await withVerifiedConfigurationChange({ actorId: "owner", connectionId: randomUUID(), approved: true, serverIds: [1], objects: [], serverWide: true,
|
|
authorize: async () => { inheritedChecks++ } }, async () => { await assert.rejects(adapter.service.execute({}, context)) })
|
|
assert.equal(inheritedChecks, 1); assert.equal(io, 0)
|
|
} finally { pool.query = priorQuery; pool.connect = priorConnect }
|
|
})
|
|
|
|
test("metadata keeps its read-only transaction, bounded local settings and cleanup in one setup query", async () => {
|
|
const prior = pool.connect, commands: string[] = [], releases: boolean[] = []
|
|
pool.connect = (async () => ({
|
|
async query(config: { text: string; values?: unknown[]; query_timeout: number }) {
|
|
commands.push(config.text)
|
|
assert(config.query_timeout > 0 && config.query_timeout <= 10000)
|
|
return { rows: config.text === "SELECT $1::int AS value" ? [{ value: config.values?.[0] }] : [], command: "SELECT" }
|
|
},
|
|
release(unsafe: boolean) { releases.push(unsafe) },
|
|
})) as never
|
|
try {
|
|
const rows = await queryServerPollMcpMetadata("SELECT $1::int AS value", [7], { deadlineAt: Date.now() + 5000 })
|
|
assert.deepEqual(rows, [{ value: 7 }])
|
|
assert.deepEqual(commands, ["BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY", "SELECT set_config('statement_timeout','10s',true),set_config('TimeZone','UTC',true)", "SELECT $1::int AS value", "ROLLBACK"])
|
|
assert.deepEqual(releases, [false])
|
|
} finally { pool.connect = prior }
|
|
})
|
|
|
|
test("abort during local setup never reaches metadata or a cleanup command on the destroyed lease", async () => {
|
|
const prior = pool.connect, controller = new AbortController(), commands: string[] = [], releases: boolean[] = []
|
|
pool.connect = (async () => ({
|
|
async query(config: { text: string }) {
|
|
commands.push(config.text)
|
|
if (config.text.startsWith("SELECT set_config")) controller.abort()
|
|
return { rows: [], command: "SELECT" }
|
|
},
|
|
release(unsafe: boolean) { releases.push(unsafe) },
|
|
})) as never
|
|
try {
|
|
await assert.rejects(queryServerPollMcpMetadata("SELECT forbidden_after_abort", [], { deadlineAt: Date.now() + 5000, signal: controller.signal }), { code: "STATE_UNAVAILABLE" })
|
|
assert.equal(commands.length, 2)
|
|
assert.deepEqual(releases, [true])
|
|
} finally { pool.connect = prior }
|
|
})
|