47 lines
4.0 KiB
TypeScript
47 lines
4.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
|
|
import { pool } from "../db/index.js"
|
|
import { hasPermission, type AuthUser } from "../lib/permissions.js"
|
|
import { defaultSidebarCountsDependencies } from "../services/sidebar-counts.js"
|
|
import { sidebarCountsPermissions } from "../modules/mcp/sidebar-counts-actions.js"
|
|
import { authenticateMcpToken, createMcpConnection } from "../modules/mcp/store.js"
|
|
import { invokeMcpTool } from "../modules/mcp/runtime.js"
|
|
|
|
export async function verifyMcpSidebarCounts(actor: AuthUser, nativeReads: () => number) {
|
|
const owner = { ...actor, permissions: [...new Set([...actor.permissions, ...sidebarCountsPermissions])] }
|
|
const issue = async (global = true, permissions: readonly string[] = sidebarCountsPermissions) => {
|
|
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Sidebar counts fixture", mode: "observer", permissions, expiresAt: new Date(Date.now() + 1800000).toISOString(), scope: { serverIds: [], operationIds: ["system.sidebar.counts.read"], allowGlobal: global } }), owner)
|
|
return authenticateMcpToken(access.token)
|
|
}
|
|
const principal = await issue()
|
|
const writesBefore = Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action LIKE 'configuration.%'")).rows[0].n)
|
|
const result = await invokeMcpTool(principal, "mm_sidebar_counts_get", {}) as { counts: Record<string, number | null>; unknownCounters: string[]; incomplete: boolean; atomicSnapshot: boolean; scope: string }
|
|
const core = await defaultSidebarCountsDependencies.core() as Record<string, number>
|
|
for (const [key, value] of Object.entries(core)) assert.equal(result.counts[key], value)
|
|
for (const [key, value] of Object.entries(result.counts)) {
|
|
assert.ok(value === null || Number.isSafeInteger(value) && value >= 0)
|
|
assert.equal(result.unknownCounters.includes(key), value === null)
|
|
}
|
|
assert.equal(result.incomplete, result.unknownCounters.length > 0); assert.equal(result.atomicSnapshot, false); assert.equal(result.scope, "global-all-servers")
|
|
const deniedReads = nativeReads()
|
|
await assert.rejects(invokeMcpTool(await issue(false), "mm_sidebar_counts_get", {})); assert.equal(nativeReads(), deniedReads)
|
|
for (const permission of sidebarCountsPermissions) {
|
|
const granted = sidebarCountsPermissions.filter(item => item !== permission), scoped = await issue(true, granted)
|
|
if (hasPermission(granted, permission)) assert.ok(await invokeMcpTool(scoped, "mm_sidebar_counts_get", {}), "Existing section-admin implications remain valid")
|
|
else { const before = nativeReads(); await assert.rejects(invokeMcpTool(scoped, "mm_sidebar_counts_get", {})); assert.equal(nativeReads(), before) }
|
|
}
|
|
await assert.rejects(invokeMcpTool(principal, "mm_sidebar_counts_get", { serverId: 1 }))
|
|
const ids = Array.from({ length: 101 }, (_, index) => 30000 + index), marker = "sidebar-limit-" + randomUUID()
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=ANY($1::int[])", [ids])).rows[0].n, 0)
|
|
try {
|
|
await pool.query("INSERT INTO servers(id,name,host,enabled,username,password) SELECT n,$1,'127.0.0.1',false,'fixture','NEVER_SIDEBAR_LOGIN' FROM unnest($2::int[]) AS n", [marker, ids])
|
|
const before = nativeReads()
|
|
await assert.rejects(invokeMcpTool(principal, "mm_sidebar_counts_get", {}), (error: { code?: string }) => error.code === "LIMIT_EXCEEDED")
|
|
assert.equal(nativeReads(), before)
|
|
} finally { await pool.query("DELETE FROM servers WHERE id=ANY($1::int[]) AND name=$2", [ids, marker]) }
|
|
assert.equal(Number((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action LIKE 'configuration.%'")).rows[0].n), writesBefore)
|
|
assert.ok(!JSON.stringify(result).includes("NEVER_"))
|
|
console.log("✓ Sidebar counts: real SQL core parity without user/binding hydration, all-source permission conjunction and global scope, null versus zero, bounded global catalog, strict args and no configuration writes")
|
|
}
|