Files
MikrotikManager/backend/src/services/server-probe-storage-fresh.sql.test.ts
T

42 lines
3.9 KiB
TypeScript

import { MIGRATION_MANIFEST } from "../db/migration-manifest.js"
import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { basename, dirname, resolve } from "node:path"
assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_FRESH, "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, closePool } = await import("../db/index.js")
const { Pool } = await import("pg")
const { applySqlMigrations } = await import("../db/migrate.js")
const { applyLegacyProbeFixtureMigrations } = await import("../db/legacy-probe-fixture.js")
const { readFile } = await import("node:fs/promises")
const migration = await readFile(new URL("../../drizzle/0037_server_probe_storage_v2.sql", import.meta.url), "utf8")
try {
await applySqlMigrations(pool); await applySqlMigrations(pool)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations")).rows[0].n, MIGRATION_MANIFEST.length)
assert.deepEqual((await pool.query("SELECT * FROM server_probe_storage_usage")).rows, [{ scope: "global", admissions: 0, reserved_files: 0, reserved_bytes: 0 }])
assert.equal((await pool.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 0)
const name = "owned_probe_fresh_" + 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: 2, connectionTimeoutMillis: 3000 })
try {
await applyLegacyProbeFixtureMigrations(other)
const receiptId = randomUUID()
await other.query("INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,$2,'rest',$3,$2,'JOURNAL_UNKNOWN',0,0)", [receiptId, "a".repeat(64), randomUUID()])
const before = (await other.query("SELECT row_to_json(q) AS value FROM server_probe_execution_quarantines q")).rows
const client = await other.connect()
try { await assert.rejects(client.query(migration)) } finally { client.release(true) }
assert.deepEqual((await other.query("SELECT row_to_json(q) AS value FROM server_probe_execution_quarantines q")).rows, before)
assert.equal((await other.query("SELECT to_regclass('public.server_probe_storage_usage') IS NULL AS absent")).rows[0].absent, true)
assert.equal((await other.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0037_server_probe_storage_v2'")).rows[0].n, 0)
assert.equal((await other.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 1)
} finally { await other.end(); await pool.query(`DROP DATABASE "${name}"`) }
console.log("Owned fresh probe migration:", JSON.stringify({ normalStartupTwice: true, migrationCount: MIGRATION_MANIFEST.length, emptyGlobalSeed: true, nonemptyLegacyRefusedBeforeDdl: true, legacyRowsAndColumnPreserved: true, noForgedMarker: true }))
await (await import("../modules/mcp/security.js")).accessGeneration()
console.log("Owned fresh MCP host:", JSON.stringify(await (await import("../modules/mcp/server-probe-storage-host.sql-helper.js")).acceptServerProbeStorageHostSql()))
console.log("Owned fresh public all tools:", JSON.stringify(await (await import("../modules/mcp/server-probe-all-tools.sql-helper.js")).acceptServerProbeAllToolsSql()))
console.log("Owned fresh REST host:", JSON.stringify(await (await import("./server-probe-storage-rest.sql-helper.js")).acceptServerProbeStorageRestSql()))
} finally { await closePool() }