125 lines
10 KiB
TypeScript
125 lines
10 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { readServerPollCapacity, type ServerPollCapacitySql } from "./server-poll-capacity.js"
|
|
import { SERVER_POLL_STORAGE_POLICY as policy } from "./server-poll-storage-model.js"
|
|
import { mcpPollStorageCapacitySchema } from "@mmapp/contracts/mcp"
|
|
|
|
const row = (owner: number | null = 2, global = 10) => ({ retention_days: 30,
|
|
global_compacted_admissions:"0",global_archive_bytes:"0",owner_compacted_admissions:owner===null?null:"0",owner_archive_bytes:owner===null?null:"0",
|
|
global_admissions: String(global), global_reserved_rows: String(global * policy.attemptRows), global_reserved_bytes: String(global * (policy.attemptBytes + policy.admissionBytes)),
|
|
owner_admissions: owner === null ? null : String(owner), owner_reserved_rows: owner === null ? null : String(owner * policy.attemptRows), owner_reserved_bytes: owner === null ? null : String(owner * (policy.attemptBytes + policy.admissionBytes)) })
|
|
function fixture(rows: Record<string, unknown>[] = [row()]) {
|
|
const calls: Array<{ text: string; values: unknown[] }> = []
|
|
const sql: ServerPollCapacitySql = { async query(text, values) { calls.push({ text, values }); return { rows } } }
|
|
return { sql, calls }
|
|
}
|
|
|
|
test("one parameterized bounded snapshot reads only settings and exact owner/global usage without maintenance or locks", async () => {
|
|
const f = fixture(), owner = "operator' OR true--"
|
|
const result = await readServerPollCapacity(f.sql, owner, true)
|
|
assert.deepEqual(result, { status: "observed", owner: { used: 2, limit: 8, remaining: 6 }, global: { exhausted: false, usage: { used: 10, limit: 128, remaining: 118 } }, retentionDays: 30 })
|
|
assert.equal(f.calls.length, 1); const { text, values } = f.calls[0]
|
|
assert.deepEqual(values, ["owner:" + owner]); assert(!text.includes(owner)); assert.match(text, /^SELECT\b/)
|
|
assert.match(text, /FROM public\.mcp_settings\b/); assert.equal((text.match(/LEFT JOIN public\.server_poll_storage_usage\b/g) ?? []).length, 2)
|
|
assert.match(text, /o\.scope=\$1/); assert.match(text, /LIMIT 2$/)
|
|
assert(!/\b(?:UPDATE|INSERT|DELETE|TRUNCATE|LOCK|FOR|CALL|gc|admissions_list)\b|;/i.test(text))
|
|
assert(!text.includes("server_poll_storage_admissions"))
|
|
})
|
|
|
|
test("non-admin hides all global counts while reporting exhaustion; admin alone receives numbers", async () => {
|
|
for (const used of [0, 127, 128]) {
|
|
const f = fixture([row(0, used)]), result = await readServerPollCapacity(f.sql, "one-owner", false)
|
|
assert.deepEqual(result, { status: "observed", owner: { used: 0, limit: 8, remaining: 8 }, global: { exhausted: used === 128, usage: null }, retentionDays: 30 })
|
|
assert(!JSON.stringify(result).includes("one-owner")); assert(!JSON.stringify(result).includes('"ready"'))
|
|
}
|
|
})
|
|
|
|
test("owner eight/full global128 expose zero remaining and only entirely null missing owner is zero", async () => {
|
|
const full = await readServerPollCapacity(fixture([row(8, 128)]).sql, "owner", true)
|
|
assert.deepEqual(full, { status: "observed", owner: { used: 8, limit: 8, remaining: 0 }, global: { exhausted: true, usage: { used: 128, limit: 128, remaining: 0 } }, retentionDays: 30 })
|
|
assert.deepEqual(await readServerPollCapacity(fixture([row(null, 0)]).sql, "new-owner", false), { status: "observed", owner: { used: 0, limit: 8, remaining: 8 }, global: { exhausted: false, usage: null }, retentionDays: 30 })
|
|
})
|
|
|
|
test("SQL failure/missing relation or settings/global accounting is unavailable and never zero", async () => {
|
|
for (const code of ["42P01", "08006", "57014"]) {
|
|
let calls = 0
|
|
assert.deepEqual(await readServerPollCapacity({ async query() { calls++; throw Object.assign(Error("private SQL failure"), { code }) } }, "owner", true), { status: "unavailable" })
|
|
assert.equal(calls, 1)
|
|
}
|
|
for (const rows of [[], [row(), row()], [{}], [{ ...row(), global_admissions: null, global_reserved_rows: null, global_reserved_bytes: null }], [{ ...row(), retention_days: undefined }]])
|
|
assert.deepEqual(await readServerPollCapacity(fixture(rows).sql, "owner", true), { status: "unavailable" })
|
|
})
|
|
|
|
test("invalid owner/admin values refuse before query; trimmed Unicode identity remains exact", async () => {
|
|
for (const owner of ["", " ", " owner", "owner ", "a\nb", "a\0b", "a\x7fb", "a\x85b", "a".repeat(201), undefined, 42]) {
|
|
const f = fixture(); assert.deepEqual(await readServerPollCapacity(f.sql, owner as string, false), { status: "unavailable" }); assert.equal(f.calls.length, 0)
|
|
}
|
|
const invalid = fixture(); assert.deepEqual(await readServerPollCapacity(invalid.sql, "owner", "true" as unknown as boolean), { status: "unavailable" }); assert.equal(invalid.calls.length, 0)
|
|
const unicode = fixture(); assert.equal((await readServerPollCapacity(unicode.sql, "Оператор😀", false)).status, "observed"); assert.deepEqual(unicode.calls[0].values, ["owner:Оператор😀"])
|
|
})
|
|
|
|
test("counter triples reject malformed canonical bigint values, partial NULL, mismatch, excessive limits and owner greater than global", async () => {
|
|
const invalid = [undefined, null, true, false, {}, [], 1n, NaN, Infinity, -1, -0, 1.5, Number.MAX_SAFE_INTEGER + 1, "", " 0", "0 ", "00", "01", "+1", "-0", "1.0", "1e0", "0x1", "9007199254740992"]
|
|
for (const field of ["global_admissions", "global_reserved_rows", "global_reserved_bytes", "owner_admissions", "owner_reserved_rows", "owner_reserved_bytes","global_compacted_admissions","global_archive_bytes","owner_compacted_admissions","owner_archive_bytes"])
|
|
for (const value of invalid) assert.deepEqual(await readServerPollCapacity(fixture([{ ...row(), [field]: value }]).sql, "owner", true), { status: "unavailable" }, field + ":" + String(value))
|
|
for (const candidate of [row(9, 10), row(8, 7), row(0, 129), { ...row(), owner_reserved_rows: "0" }, { ...row(), global_reserved_bytes: "0" }, { ...row(null), owner_reserved_bytes: "0" }])
|
|
assert.deepEqual(await readServerPollCapacity(fixture([candidate]).sql, "owner", true), { status: "unavailable" })
|
|
})
|
|
|
|
function compactRow(ownerHeavy:number,ownerCompact:number,globalHeavy=ownerHeavy,globalCompact=ownerCompact,encoded=59075){
|
|
return {...row(ownerHeavy,globalHeavy),owner_compacted_admissions:String(ownerCompact),owner_archive_bytes:String(ownerCompact*encoded),
|
|
owner_reserved_rows:String(ownerHeavy*4019+ownerCompact*2),owner_reserved_bytes:String(ownerHeavy*8421376+ownerCompact*(32768+encoded)),
|
|
global_compacted_admissions:String(globalCompact),global_archive_bytes:String(globalCompact*encoded),
|
|
global_reserved_rows:String(globalHeavy*4019+globalCompact*2),global_reserved_bytes:String(globalHeavy*8421376+globalCompact*(32768+encoded))}
|
|
}
|
|
|
|
test("336 retained histories remain paid while DTO used counts heavy slots and remaining obeys all caps",async()=>{
|
|
const result=await readServerPollCapacity(fixture([compactRow(1,336)]).sql,"owner",true)
|
|
assert.deepEqual(result,{status:"observed",owner:{used:1,limit:8,remaining:3},global:{exhausted:false,usage:{used:1,limit:128,remaining:123}},retentionDays:30})
|
|
})
|
|
|
|
test("largest permitted archives still allow one new poll after336 histories; shared contract accepts the byte-limited remainder",async()=>{
|
|
const before=await readServerPollCapacity(fixture([compactRow(0,336,0,336,131072)]).sql,"owner",true)
|
|
assert(before.status==="observed");assert.equal(before.owner.remaining,1)
|
|
assert.deepEqual(mcpPollStorageCapacitySchema.parse(before),before)
|
|
const occupied=await readServerPollCapacity(fixture([compactRow(1,336,1,336,131072)]).sql,"owner",true)
|
|
assert(occupied.status==="observed");assert.equal(occupied.owner.used,1);assert.equal(occupied.owner.remaining,0)
|
|
assert.deepEqual(mcpPollStorageCapacitySchema.parse(occupied),occupied)
|
|
assert.equal(mcpPollStorageCapacitySchema.safeParse({...occupied,owner:{used:8,limit:8,remaining:1}}).success,false)
|
|
})
|
|
|
|
test("byte-full retained history cannot report available admission despite unused heavy slots",async()=>{
|
|
const ownerFull=await readServerPollCapacity(fixture([compactRow(0,2000,0,2000,40)]).sql,"owner",false)
|
|
assert(ownerFull.status==="observed");assert.equal(ownerFull.owner.used,0);assert.equal(ownerFull.owner.remaining,0)
|
|
assert.equal(ownerFull.global.exhausted,false);assert.equal(ownerFull.global.usage,null)
|
|
const globalFull=await readServerPollCapacity(fixture([compactRow(0,0,0,32600,40)]).sql,"owner",false)
|
|
assert(globalFull.status==="observed");assert.equal(globalFull.global.exhausted,true);assert.equal(globalFull.global.usage,null)
|
|
})
|
|
|
|
test("missing 0041 fields, malformed archive accounting and owner/global mismatch fail unavailable",async()=>{
|
|
const legacy={...row()};delete (legacy as Partial<typeof legacy>).global_compacted_admissions
|
|
for(const value of [legacy,{...row(),owner_compacted_admissions:null},{...row(),global_archive_bytes:"1"},
|
|
compactRow(0,1,0,1,39),compactRow(0,1,0,1,131073),compactRow(0,2,0,1),
|
|
{...compactRow(0,1),owner_reserved_rows:"0"},{...compactRow(0,1),global_reserved_bytes:"0"}])
|
|
assert.deepEqual(await readServerPollCapacity(fixture([value]).sql,"owner",true),{status:"unavailable"})
|
|
})
|
|
|
|
test("canonical PostgreSQL bigint strings and safe integer numbers are accepted; retention current1..365 only", async () => {
|
|
const numeric = Object.fromEntries(Object.entries(row()).map(([key, value]) => [key, Number(value)]))
|
|
assert.equal((await readServerPollCapacity(fixture([numeric]).sql, "owner", true)).status, "observed")
|
|
for (const days of [1, "1", 365, "365"]) {
|
|
const result = await readServerPollCapacity(fixture([{ ...row(), retention_days: days }]).sql, "owner", false)
|
|
assert(result.status === "observed"); assert.equal(result.retentionDays, Number(days))
|
|
}
|
|
for (const days of [0, 366, -1, 1.5, "01", null, undefined]) assert.deepEqual(await readServerPollCapacity(fixture([{ ...row(), retention_days: days }]).sql, "owner", false), { status: "unavailable" })
|
|
})
|
|
|
|
test("returned DTO is deeply frozen and independent of later SQL row/result mutations", async () => {
|
|
const original = row(), rows = [original], f = fixture(rows), result = await readServerPollCapacity(f.sql, "owner", true)
|
|
assert(result.status === "observed"); const expected = structuredClone(result)
|
|
Object.assign(original, row(8, 128), { retention_days: 365 }); rows.splice(0)
|
|
assert.deepEqual(result, expected); assert(Object.isFrozen(result)); assert(Object.isFrozen(result.owner)); assert(Object.isFrozen(result.global)); assert(Object.isFrozen(result.global.usage))
|
|
assert.throws(() => { (result.owner as { used: number }).used = 7 })
|
|
assert(Object.isFrozen(await readServerPollCapacity(fixture([]).sql, "owner", false)))
|
|
})
|