52 lines
3.5 KiB
TypeScript
52 lines
3.5 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { basename, dirname, resolve } from "node:path"
|
|
|
|
/** Catalog observation on two disposable databases, not source enrollment. */
|
|
export async function acceptDatabaseSourceObservationSql() {
|
|
assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true")
|
|
const url = new URL(process.env.DATABASE_URL ?? "")
|
|
assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres")
|
|
assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-"))
|
|
const { pool } = await import("./index.js")
|
|
const { Pool } = await import("pg")
|
|
const { captureDatabaseSourceObservation, assertDatabaseSourceObservationCurrent } = await import("./database-source-observation.js")
|
|
const name = "owned_probe_source_" + randomUUID().replaceAll("-", "")
|
|
await pool.query(`CREATE DATABASE "${name}" TEMPLATE template0`)
|
|
const otherUrl = new URL(url); otherUrl.pathname = "/" + name
|
|
const other = new Pool({ connectionString: otherUrl.toString(), max: 1, connectionTimeoutMillis: 3000, statement_timeout: 10000 })
|
|
const observe = async (selected: typeof pool) => {
|
|
const client = await selected.connect()
|
|
const sql = { query: async (text: string, values?: unknown[], options?: { signal?: AbortSignal; timeoutMs: number }) => {
|
|
const config = { text, values, query_timeout: options?.timeoutMs ?? 10000 }
|
|
return client.query<Record<string, unknown>>(config)
|
|
} }
|
|
try {
|
|
await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY")
|
|
await client.query("SET LOCAL statement_timeout='10s'")
|
|
await client.query("SET LOCAL search_path=public,pg_catalog")
|
|
const options = { deadlineAt: Date.now() + 10000 }
|
|
const source = await captureDatabaseSourceObservation(sql, options)
|
|
await assertDatabaseSourceObservationCurrent(sql, source, options)
|
|
assert.equal(source.ready, false); assert.equal(source.generationAuthenticated, false)
|
|
assert(Object.isFrozen(source) && Object.isFrozen(source.source) && Object.isFrozen(source.snapshot))
|
|
return source
|
|
} finally { try { await client.query("ROLLBACK") } finally { client.release(true) } }
|
|
}
|
|
try {
|
|
// Deliberately shadow names only inside the second disposable database.
|
|
// Qualified source SQL must not resolve this rejecting type/operator.
|
|
await other.query("CREATE DOMAIN public.text AS pg_catalog.text CHECK(false)")
|
|
await other.query("CREATE FUNCTION public.source_wrong_eq(pg_catalog.name,pg_catalog.name) RETURNS pg_catalog.bool LANGUAGE sql IMMUTABLE AS 'SELECT false'")
|
|
await other.query("CREATE OPERATOR public.= (LEFTARG=pg_catalog.name,RIGHTARG=pg_catalog.name,FUNCTION=public.source_wrong_eq)")
|
|
const original = await observe(pool), another = await observe(other)
|
|
assert.equal(original.source.systemIdentifier, another.source.systemIdentifier)
|
|
assert.notEqual(original.source.databaseOid, another.source.databaseOid)
|
|
assert.notEqual(original.source.databaseName, another.source.databaseName)
|
|
assert.notEqual(original.snapshot.backendPid, another.snapshot.backendPid)
|
|
return { actualSql: true, sameClusterTwoDatabases: true, distinctDatabaseOidAndName: true,
|
|
actualRepeatableReadSourcePin: true, reorderedSearchPathAndShadowTypeOperatorSafe: true, noGenerationAuthorityClaimed: true,
|
|
applicationDataWrites: 0, productionDatabaseTouched: false }
|
|
} finally { await other.end(); await pool.query(`DROP DATABASE "${name}"`) }
|
|
}
|