92 lines
6.6 KiB
TypeScript
92 lines
6.6 KiB
TypeScript
/** Real owned PostgreSQL/kernel/HTTP assembly, explicitly synthetic opaque REST
|
|
* origin ONLY. No fake SQL driver, kernel mock or full REST/MCP provenance claim.
|
|
* The caller provisions a fresh isolated fixture for each pending-state case. */
|
|
import assert from "node:assert/strict"
|
|
import { Client } from "pg"
|
|
import { setTimeout as delay } from "node:timers/promises"
|
|
import type { RouterGuardNativeBinding } from "./router-configuration-guard.js"
|
|
import type { createOwnedRouterGuardFixtureFactory } from "./router-configuration-guard.sql-fixture.js"
|
|
|
|
type Make = ReturnType<typeof createOwnedRouterGuardFixtureFactory>
|
|
type Fixture = Awaited<ReturnType<Make>>
|
|
const scope = () => ({ deadlineAt: Date.now() + 20000 })
|
|
const childNative = (binding: RouterGuardNativeBinding): Omit<RouterGuardNativeBinding, "actorProof"> => ({ kind: binding.kind,
|
|
managedServerId: binding.managedServerId, endpoint: binding.endpoint, command: binding.command, method: binding.method, requestPath: binding.requestPath })
|
|
const head = async (fixture: Fixture) => (await fixture.owner.query("SELECT state,sequence::text FROM mmapp_router_guard.head WHERE id=1")).rows[0]
|
|
async function zeroQueries(work: () => Promise<void>): Promise<void> {
|
|
const query = Client.prototype.query; let calls = 0
|
|
// Observation only: every original query/arguments/result is forwarded. The
|
|
// origin failures under test must happen before ANY actual driver call.
|
|
Client.prototype.query = function(this: Client, ...args: unknown[]) { calls++; return Reflect.apply(query, this, args) } as typeof query
|
|
try { await work(); assert.equal(calls, 0) } finally { Client.prototype.query = query }
|
|
}
|
|
|
|
export async function acceptOwnedRouterConfigurationWriter(make: Make) {
|
|
if (process.env.MMAPP_TEST_ROUTER_GUARD_OWNED !== "true") throw Error("Explicit disposable owned writer acceptance required")
|
|
const checks: string[] = []
|
|
for (const scenario of ["writer-physical", "writer-nested", "writer-caught-drop", "writer-orphan", "writer-first-scope"] as const) {
|
|
const fixture = await make(scenario), writer = fixture.createWriterForFixture()
|
|
try {
|
|
if (scenario === "writer-physical" || scenario === "writer-nested") {
|
|
const token = await writer.captureRest(scope()), pids: number[] = []
|
|
await writer.run(token, scope(), async facade => {
|
|
pids.push(await fixture.sampleGuardPid())
|
|
if (scenario === "writer-nested") {
|
|
await writer.run(token, scope(), async nested => {
|
|
pids.push(await fixture.sampleGuardPid())
|
|
await nested.application(async client => { pids.push(await fixture.sampleGuardPid()); await client.query(fixture.applicationCommitQuery) })
|
|
})
|
|
} else await facade.application(async client => { pids.push(await fixture.sampleGuardPid()); await client.query(fixture.applicationCommitQuery) })
|
|
await facade.native(childNative(fixture.nativeBinding))
|
|
pids.push(await fixture.sampleGuardPid())
|
|
})
|
|
assert.equal((await head(fixture)).state, "idle"); assert.equal(await fixture.applicationRows(), 1); assert.equal(fixture.nativeRequests(), 1)
|
|
assert.equal(new Set([...pids, ...await fixture.observedGuardPids()]).size, 1, "all root/borrow/native children retain ONE physical CONTROL PID; application DB is separate")
|
|
await zeroQueries(async () => {
|
|
for (const copied of [{ ...token }, JSON.parse(JSON.stringify(token))]) await assert.rejects(writer.run(copied, scope(), async () => {}))
|
|
await assert.rejects(writer.run(token, scope(), async () => {}))
|
|
})
|
|
checks.push(scenario + ": real root+application COMMIT+native private HTTP ACK use one control PID; nested root borrows; copied/used origins cause zero SQL")
|
|
} else if (scenario === "writer-caught-drop") {
|
|
fixture.setNativeFault("drop-after-write")
|
|
const token = await writer.captureRest(scope())
|
|
await assert.rejects(writer.run(token, scope(), async facade => {
|
|
await facade.native(childNative(fixture.nativeBinding)).catch(() => ({ claimedAcknowledgement: true }))
|
|
return { acknowledged: true }
|
|
}))
|
|
assert.equal(fixture.nativeRequests(), 1); assert.equal((await head(fixture)).state, "pending"); assert.equal(await fixture.applicationRows(), 0)
|
|
checks.push("real native write/drop caught into JSON cannot mint a child/root terminal; journal remains pending")
|
|
} else if (scenario === "writer-orphan") {
|
|
let enter!: () => void, release!: () => void, child: Promise<unknown> | undefined
|
|
const entered = new Promise<void>(resolve => { enter = resolve }), held = new Promise<void>(resolve => { release = resolve })
|
|
const token = await writer.captureRest(scope())
|
|
try {
|
|
await assert.rejects(writer.run(token, scope(), async facade => {
|
|
child = facade.application(async client => { enter(); await held; await client.query(fixture.applicationCommitQuery) })
|
|
void child.catch(() => {})
|
|
await entered; return { droppedChild: true }
|
|
}))
|
|
} finally { release() }
|
|
await assert.rejects(child!)
|
|
assert.equal((await head(fixture)).state, "pending"); assert.equal(await fixture.applicationRows(), 0); assert.equal(fixture.nativeRequests(), 0)
|
|
checks.push("actual begun application child dropped by parent cannot commit late or clear pending with JSON")
|
|
} else {
|
|
const caller = new AbortController(), token = await writer.captureRest({ deadlineAt: Date.now() + 5000, signal: caller.signal })
|
|
caller.abort()
|
|
await zeroQueries(async () => { await assert.rejects(writer.run(token, { deadlineAt: Date.now() + 60000, signal: new AbortController().signal }, async () => {})) })
|
|
const short = await writer.captureRest({ deadlineAt: Date.now() + 20 }); await delay(30)
|
|
const originalNow = Date.now
|
|
try {
|
|
Date.now = () => originalNow() - 60000
|
|
await zeroQueries(async () => { await assert.rejects(writer.run(short, { deadlineAt: originalNow() + 60000 }, async () => {})) })
|
|
} finally { Date.now = originalNow }
|
|
assert.equal((await head(fixture)).state, "idle"); assert.equal(fixture.nativeRequests(), 0)
|
|
checks.push("first caller abort and original short monotonic capture cannot be revived by larger/new-signal run or wall rollback; zero SQL")
|
|
}
|
|
await fixture.assertJournalMacs()
|
|
} finally { await fixture.close() }
|
|
}
|
|
return { checks, actualPgRun: true, actualKernelAndHttp: true, syntheticOpaqueRestOrigin: true, genuineRestProvenance: false, genuineMcpProvenance: false,
|
|
runtimeActivation: false, allWriterCoverage: false, realChrCalls: 0 }
|
|
}
|