import assert from "node:assert/strict" import { randomUUID } from "node:crypto" import { test } from "node:test" import { createSqlServerProbeSourceReaders } from "./server-probe-storage-source.js" import { createProductionServerProbeExecutor } from "./server-probe-execution.js" const source = { id: 1, enabled: true, host: "192.0.2.1", port: 443, useSsl: true, verifySsl: true, username: "synthetic-user", password: "synthetic-secret", routingEndpointEpoch: randomUUID() } function fixture() { let rows: Record[] = [source] const queries: { sql: string; values?: unknown[] }[] = [] const scope = [1, 2] const readers = createSqlServerProbeSourceReaders({ async query(sql, values) { queries.push({ sql, values }); return { rows } } }, scope) return { readers, queries, scope, rows(value: Record[]) { rows = value } } } test("endpoint reads use exact bounded projection and host-owned scope; credentials only for the exact source", async () => { const f = fixture(); f.scope.push(3) assert.equal(await f.readers.getServer(3), undefined); assert.equal(f.queries.length, 0) assert.deepEqual(await f.readers.getServer(1), source) assert.deepEqual(f.queries[0].values, [[1, 2], 1]) assert.match(f.queries[0].sql, /routing_endpoint_epoch AS "routingEndpointEpoch"/) assert(!f.queries[0].sql.includes("SELECT *")) f.rows([{ ...source, id: 2 }]); await assert.rejects(f.readers.getServer(1)) f.rows([source, source]); await assert.rejects(f.readers.getServer(1)) f.rows([{ ...source, routingEndpointEpoch: "not-an-epoch" }]); await assert.rejects(f.readers.getServer(1)) }) test("bandwidth metadata is restricted before query and cannot return credentials or a foreign endpoint", async () => { const f = fixture(); f.rows([{ id: 2, host: "192.0.2.2", enabled: true }]) assert.deepEqual(await f.readers.findDestination("192.0.2.2", 2, [2, 9]), { id: 2 }) assert.deepEqual(f.queries[0].values, [[2], 2]) assert(!/username|password/.test(f.queries[0].sql)) f.rows([{ id: 2, host: "192.0.2.2", enabled: true, password: "must-not-appear" }]) await assert.rejects(f.readers.findDestination("192.0.2.2", 2, [2])) f.rows([{ id: 9, host: "192.0.2.2", enabled: true }]); await assert.rejects(f.readers.findDestination("192.0.2.2", undefined, [2, 9])) }) test("peer resolution preserves disabled explicit refusal, absent-ID host fallback and result bounds", async () => { const f = fixture(); f.rows([{ id: 2, host: "192.0.2.2", enabled: false }]) await assert.rejects(f.readers.findDestination("192.0.2.2", 2, [2]), { code: "DESTINATION_DISABLED" }) f.rows([{ id: 2, host: "192.0.2.2", enabled: true }]) assert.deepEqual(await f.readers.findDestination(" 192.0.2.2 ", 9, [2]), { id: 2 }) assert.match(f.queries.at(-1)!.sql, /probe-storage-source:peer-host/) f.rows(Array.from({ length: 1001 }, () => ({ id: 2, host: "192.0.2.2", enabled: true }))) await assert.rejects(f.readers.findDestination("192.0.2.2", undefined, [2])) }) test("source construction rejects ambiguous scope and incomplete production hooks never fall back to pool", () => { const sql = { async query() { return { rows: [] } } } assert.throws(() => createSqlServerProbeSourceReaders(sql, [1, 1])) assert.throws(() => createSqlServerProbeSourceReaders(sql, [-1])) const access = { async readAuthority() { throw Error("must not run") }, async assertProtections() {} } assert.throws(() => createProductionServerProbeExecutor(access, { getServer: async () => undefined } as never), { code: "STATE_UNAVAILABLE" }) for (const malformed of [null, false, 0, "", {}, () => {}]) { assert.throws(() => createProductionServerProbeExecutor(access, malformed as never), { code: "STATE_UNAVAILABLE" }) } })