/** Owned synthetic SQL fixture only. No native dispatch or production enrolment. */ import assert from "node:assert/strict" import { randomUUID } from "node:crypto" import { pool } from "../db/index.js" import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js" import { createProductionServerProbeExecutor, type ServerProbeAuthority } from "./server-probe-execution.js" export async function acceptProbeStorageSourceSql(serverId: number) { assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true") const peer = Number((await pool.query(`INSERT INTO servers(name,host,port,enabled,username,password) VALUES('owned-storage-source-peer','192.0.2.2',443,true,'synthetic-peer-user','synthetic-peer-secret') RETURNING id`)).rows[0].id) const client = await pool.connect(), original = pool.query const pids = new Set(), queries: string[] = [] const raw = { async query(text: string, values?: unknown[]) { pids.add(Number((await client.query("SELECT pg_backend_pid() AS pid")).rows[0].pid)) queries.push(text); return client.query(text, values) } } try { pool.query = (() => { throw Error("Owned source fixture forbids pool.query under lease") }) as typeof pool.query const readers = createSqlServerProbeSourceReaders(raw, [serverId, peer]) const authority: ServerProbeAuthority = { transport: "mcp", ownerId: "owned-source-owner", connectionId: randomUUID(), credentialId: randomUUID(), expiresAt: Date.now() + 300000, generation: "owned-synthetic-generation", policyProof: "a".repeat(64), ownerProof: "b".repeat(64), active: true, operationAllowed: true, scopeServerIds: [serverId, peer], ownerPermissions: ["mm:servers:write"], policyPermissions: ["mm:servers:write"] } const executor = createProductionServerProbeExecutor({ async readAuthority() { return structuredClone(authority) }, async assertProtections(ids) { assert(ids.every(id => authority.scopeServerIds.includes(id))) } }, readers) const ping = await executor.prepare(serverId, { tool: "ping", target: "192.0.2.2" }) assert.equal((await executor.preflight(ping)).receiptId, ping.receiptId) const bandwidth = await executor.prepare(serverId, { tool: "bandwidth", bwRemoteAddress: "192.0.2.2", dstServerId: peer }) assert.equal(bandwidth.destinationServerId, peer) await executor.preflight(bandwidth) assert(!JSON.stringify(bandwidth).includes("synthetic-peer-secret")) await raw.query("UPDATE servers SET password='synthetic-changed' WHERE id=$1", [peer]) await assert.rejects(executor.preflight(bandwidth), { code: "STATE_CONFLICT" }) await raw.query("UPDATE servers SET enabled=false WHERE id=$1", [peer]) await assert.rejects(readers.findDestination("192.0.2.2", peer, authority.scopeServerIds), { code: "DESTINATION_DISABLED" }) assert.equal(await readers.getServer(peer + 100000), undefined) assert.equal(pids.size, 1) assert(queries.some(sql => sql.includes("peer-id"))); assert(queries.some(sql => sql.includes("endpoint"))) return { productionExecutorMapping: true, productionCrypto: true, exactEndpointProofDrift: true, disabledPeerRefused: true, explicitSqlProjection: true, poolQueryForbiddenUnderLease: true, physicalBackends: pids.size, sourceSqlQueries: queries.length, syntheticAuthority: true, fullRuntimeAuthorityIntegrated: false, nativeOrDnsCalls: 0, routesRegistered: false } } finally { pool.query = original; client.release(true) } }