From 9801a0bda85b2d4142c305d2653a8235da37c101 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Mon, 31 Aug 2026 09:22:04 -0400 Subject: [PATCH 1/2] test(session): characterize lifecycle interleavings --- .changeset/quiet-lifecycles-characterize.md | 2 + .../session-broker/src/connection.test.ts | 151 +++++++++ src/session/broker/brokerClient.test.ts | 297 ++++++++++++++++++ 3 files changed, 450 insertions(+) create mode 100644 .changeset/quiet-lifecycles-characterize.md diff --git a/.changeset/quiet-lifecycles-characterize.md b/.changeset/quiet-lifecycles-characterize.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/quiet-lifecycles-characterize.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/session-broker/src/connection.test.ts b/packages/session-broker/src/connection.test.ts index 2eed8032c..a33596c89 100644 --- a/packages/session-broker/src/connection.test.ts +++ b/packages/session-broker/src/connection.test.ts @@ -76,6 +76,36 @@ function createSnapshot(): SessionSnapshot { }; } +/** Await one lifecycle signal while turning a lost wakeup into a bounded test failure. */ +async function settleWithinTestTimeout(promise: PromiseLike | T, timeoutMs = 500) { + let timeout: ReturnType | undefined; + try { + return await Promise.race([ + Promise.resolve(promise), + new Promise((_resolve, reject) => { + timeout = setTimeout( + () => reject(new Error(`Promise did not settle within ${timeoutMs}ms.`)), + timeoutMs, + ); + timeout.unref?.(); + }), + ]); + } finally { + if (timeout) clearTimeout(timeout); + } +} + +/** Create a manually released promise for reconnect race characterization. */ +function createDeferredTest() { + let resolve!: (value: T | PromiseLike) => void; + let reject!: (reason?: unknown) => void; + const promise = new Promise((resolvePromise, rejectPromise) => { + resolve = resolvePromise; + reject = rejectPromise; + }); + return { promise, resolve, reject }; +} + const protocolParsers = createSessionBrokerProtocolParsers< TestSessionInfo, TestSessionState, @@ -153,6 +183,41 @@ describe("session broker connection", () => { }); }); + test("preserves a synchronous socket factory throw and permits a later direct start", () => { + const sockets: TestSocket[] = []; + let attempts = 0; + const connection = createSessionBrokerConnection< + TestSessionInfo, + TestSessionState, + TestSocket, + TestServerMessage, + { ok: true } + >({ + url: "ws://broker.test/session", + createSocket: () => { + attempts += 1; + if (attempts === 1) throw new Error("socket factory exploded"); + const socket = new TestSocket(); + sockets.push(socket); + return socket; + }, + registration: createRegistration(), + snapshot: createSnapshot(), + protocolParsers, + }); + + expect(() => connection.start()).toThrow("socket factory exploded"); + expect(attempts).toBe(1); + expect(sockets).toHaveLength(0); + + connection.start(); + sockets[0]!.emitOpen(); + expect(attempts).toBe(2); + expect(sockets).toHaveLength(1); + expect(JSON.parse(sockets[0]!.sent[0]!)).toMatchObject({ type: "register" }); + connection.stop(); + }); + test("withholds registration and replacement updates until producer authentication completes", async () => { const socket = new TestSocket(); const pair = (await crypto.subtle.generateKey("Ed25519", false, [ @@ -1010,6 +1075,92 @@ describe("session broker connection", () => { expect(sockets).toHaveLength(2); }); + test("explicitly starts a fresh generation after a natural no-reconnect close", async () => { + const sockets: TestSocket[] = []; + const connection = createSessionBrokerConnection< + TestSessionInfo, + TestSessionState, + TestSocket, + TestServerMessage, + { ok: true } + >({ + url: "ws://broker.test/session", + createSocket: () => { + const socket = new TestSocket(); + sockets.push(socket); + return socket; + }, + registration: createRegistration(), + snapshot: createSnapshot(), + protocolParsers, + resolveClose: () => ({ reconnect: false }), + }); + + connection.start(); + sockets[0]!.emitOpen(); + sockets[0]!.emitClose(1000, "complete"); + await Bun.sleep(0); + expect(sockets).toHaveLength(1); + + connection.start(); + sockets[1]!.emitOpen(); + expect(sockets).toHaveLength(2); + expect(sockets.map((socket) => (JSON.parse(socket.sent[0]!) as { type: string }).type)).toEqual( + ["register", "register"], + ); + connection.stop(); + }); + + for (const outcome of ["resolve", "reject"] as const) { + test(`stop fences late reconnect preparation ${outcome} without warning or another socket`, async () => { + const sockets: TestSocket[] = []; + const warnings: string[] = []; + const preparationStarted = createDeferredTest(); + const preparationGate = createDeferredTest(); + const preparationSettled = createDeferredTest(); + const connection = createSessionBrokerConnection< + TestSessionInfo, + TestSessionState, + TestSocket, + TestServerMessage, + { ok: true } + >({ + url: "ws://broker.test/session", + createSocket: () => { + const socket = new TestSocket(); + sockets.push(socket); + return socket; + }, + registration: createRegistration(), + snapshot: createSnapshot(), + protocolParsers, + reconnectDelayMs: 1, + prepareReconnect: async () => { + preparationStarted.resolve(); + try { + await preparationGate.promise; + if (outcome === "reject") throw new Error("late preparation failure"); + } finally { + preparationSettled.resolve(); + } + }, + onWarning: (message) => warnings.push(message), + }); + + connection.start(); + sockets[0]!.emitOpen(); + sockets[0]!.emitClose(); + await settleWithinTestTimeout(preparationStarted.promise); + connection.stop(); + preparationGate.resolve(); + await settleWithinTestTimeout(preparationSettled.promise); + await Bun.sleep(5); + + expect(warnings).toEqual([]); + expect(sockets).toHaveLength(1); + }); + } + test("reconnects after socket close unless a close directive disables it", async () => { const sockets: TestSocket[] = []; const warnings: string[] = []; diff --git a/src/session/broker/brokerClient.test.ts b/src/session/broker/brokerClient.test.ts index ee213dc38..1fe0a114f 100644 --- a/src/session/broker/brokerClient.test.ts +++ b/src/session/broker/brokerClient.test.ts @@ -24,6 +24,8 @@ const originalDisable = process.env.HUNK_MCP_DISABLE; const originalUnsafeRemote = process.env.HUNK_MCP_UNSAFE_ALLOW_REMOTE; const originalRuntimeDir = process.env.XDG_RUNTIME_DIR; const originalConsoleError = console.error; +const nativeSetTimeoutTest = globalThis.setTimeout.bind(globalThis); +const nativeClearTimeoutTest = globalThis.clearTimeout.bind(globalThis); function createRegistration() { return createTestSessionRegistration({ @@ -63,6 +65,152 @@ async function waitUntil( throw new Error(`Timed out waiting for ${label}.`); } +/** Await one lifecycle promise while turning a lost wakeup into a bounded test failure. */ +async function settleWithinTestTimeout(promise: PromiseLike | T, timeoutMs = 500) { + let timeout: ReturnType | undefined; + try { + return await Promise.race([ + Promise.resolve(promise), + new Promise((_resolve, reject) => { + timeout = nativeSetTimeoutTest( + () => reject(new Error(`Promise did not settle within ${timeoutMs}ms.`)), + timeoutMs, + ); + }), + ]); + } finally { + if (timeout) nativeClearTimeoutTest(timeout); + } +} + +/** Create a manually released promise for startup race characterization. */ +function createDeferredTest() { + let resolve!: (value: T | PromiseLike) => void; + let reject!: (reason?: unknown) => void; + const promise = new Promise((resolvePromise, rejectPromise) => { + resolve = resolvePromise; + reject = rejectPromise; + }); + return { promise, resolve, reject }; +} + +/** Replace only timeout scheduling with a deterministic, test-local clock. */ +function installDeterministicClockTest() { + const originalSetTimeout = globalThis.setTimeout; + const originalClearTimeout = globalThis.clearTimeout; + let now = 0; + let nextId = 1; + const scheduled = new Map< + number, + { + due: number; + callback: () => void; + handle: { id: number; unref: () => unknown }; + } + >(); + + globalThis.setTimeout = ((callback: TimerHandler, delay = 0, ...args: unknown[]) => { + if (typeof callback !== "function") throw new Error("Test clock requires function callbacks."); + const id = nextId++; + const handle = { + id, + unref() { + return handle; + }, + }; + scheduled.set(id, { + due: now + Number(delay), + callback: () => callback(...args), + handle, + }); + return handle; + }) as unknown as typeof setTimeout; + globalThis.clearTimeout = ((handle: { id?: number } | number | undefined) => { + const id = typeof handle === "number" ? handle : handle?.id; + if (id !== undefined) scheduled.delete(id); + }) as typeof clearTimeout; + + return { + /** Advance through every callback whose deadline falls in the requested interval. */ + advanceByTest(ms: number) { + const target = now + ms; + for (;;) { + const next = [...scheduled.entries()] + .filter(([, timer]) => timer.due <= target) + .sort((left, right) => left[1].due - right[1].due || left[0] - right[0])[0]; + if (!next) break; + const [id, timer] = next; + scheduled.delete(id); + now = timer.due; + timer.callback(); + } + now = target; + }, + pendingCountTest() { + return scheduled.size; + }, + restoreTest() { + globalThis.setTimeout = originalSetTimeout; + globalThis.clearTimeout = originalClearTimeout; + }, + }; +} + +/** Observe WebSocket construction without opening a network connection. */ +function installWebSocketObserverTest(throwOnAttempt?: number) { + const OriginalWebSocket = globalThis.WebSocket; + const sockets: Array<{ sent: string[] }> = []; + let constructionAttempts = 0; + + class ObservedWebSocketTest { + readyState = 0; + sent: string[] = []; + onopen: (() => void) | null = null; + onmessage: ((event: { data: unknown }) => void) | null = null; + onclose: ((event: { code: number; reason: string }) => void) | null = null; + onerror: (() => void) | null = null; + + constructor(_url: string) { + constructionAttempts += 1; + if (constructionAttempts === throwOnAttempt) throw new Error("socket factory exploded"); + sockets.push(this); + } + + send(data: string) { + this.sent.push(data); + } + + close(code = 1000, reason = "") { + this.readyState = 3; + this.onclose?.({ code, reason }); + } + } + + globalThis.WebSocket = ObservedWebSocketTest as unknown as typeof WebSocket; + return { + sockets, + constructionAttemptsTest: () => constructionAttempts, + restoreTest() { + globalThis.WebSocket = OriginalWebSocket; + }, + }; +} + +/** Supply inert credentials so startup tests can exercise the real connection ownership path. */ +function prepareDirectConnectTest(client: SessionBrokerClient) { + (client as any).credentials = { + producer: {}, + daemonIdentity: { keyId: "daemon-key-test" }, + daemonPublicKey: {}, + }; + return { + host: "127.0.0.1", + port: 47657, + httpOrigin: "http://127.0.0.1:47657", + wsOrigin: "ws://127.0.0.1:47657", + }; +} + afterEach(() => { if (originalHost === undefined) { delete process.env.HUNK_MCP_HOST; @@ -515,6 +663,155 @@ describe("Hunk session daemon client", () => { } }, 10_000); + test("repeated start after success settles while retaining one socket generation", async () => { + delete process.env.HUNK_MCP_DISABLE; + const webSockets = installWebSocketObserverTest(); + const client = new SessionBrokerClient(createRegistration(), createSnapshot()); + const config = prepareDirectConnectTest(client); + (client as any).ensureDaemonAndConnect = async () => { + (client as any).connect(config); + }; + + try { + await settleWithinTestTimeout(client.start()); + expect(webSockets.sockets).toHaveLength(1); + + await settleWithinTestTimeout(client.start()); + expect(webSockets.sockets).toHaveLength(1); + expect(webSockets.constructionAttemptsTest()).toBe(1); + } finally { + client.stop(); + webSockets.restoreTest(); + } + }); + + test("concurrent starts share one settlement and perform one startup attempt", async () => { + delete process.env.HUNK_MCP_DISABLE; + const webSockets = installWebSocketObserverTest(); + const client = new SessionBrokerClient(createRegistration(), createSnapshot()); + const config = prepareDirectConnectTest(client); + const gate = createDeferredTest(); + let attempts = 0; + (client as any).ensureDaemonAndConnect = async () => { + attempts += 1; + await gate.promise; + (client as any).connect(config); + }; + + try { + const first = client.start(); + const second = client.start(); + expect(attempts).toBe(1); + expect(webSockets.sockets).toHaveLength(0); + + gate.resolve(); + await settleWithinTestTimeout(Promise.all([first, second])); + expect(attempts).toBe(1); + expect(webSockets.sockets).toHaveLength(1); + } finally { + client.stop(); + webSockets.restoreTest(); + } + }); + + test("manual start during automatic retry runs now without moving the retry deadline", async () => { + delete process.env.HUNK_MCP_DISABLE; + const clock = installDeterministicClockTest(); + const client = new SessionBrokerClient(createRegistration(), createSnapshot(), { + reconnectDelayMs: 30, + }); + const messages: string[] = []; + console.error = (...args: unknown[]) => { + messages.push(args.map((value) => String(value)).join(" ")); + }; + let attempts = 0; + (client as any).ensureDaemonAndConnect = async () => { + attempts += 1; + if (attempts === 1) throw new Error("startup unavailable"); + }; + + try { + await settleWithinTestTimeout(client.start()); + expect(attempts).toBe(1); + expect(messages).toEqual(["[session:broker] startup unavailable"]); + expect(clock.pendingCountTest()).toBe(1); + + await settleWithinTestTimeout(client.start()); + expect(attempts).toBe(2); + expect(clock.pendingCountTest()).toBe(1); + + clock.advanceByTest(29); + await Promise.resolve(); + expect(attempts).toBe(2); + + clock.advanceByTest(1); + await Promise.resolve(); + expect(attempts).toBe(3); + expect(clock.pendingCountTest()).toBe(0); + } finally { + client.stop(); + clock.restoreTest(); + } + }); + + test.todo("known regression: client retained after synchronous socket factory failure prevents a later fresh start", async () => { + delete process.env.HUNK_MCP_DISABLE; + const webSockets = installWebSocketObserverTest(1); + const client = new SessionBrokerClient(createRegistration(), createSnapshot(), { + reconnectDelayMs: 10_000, + }); + const config = prepareDirectConnectTest(client); + (client as any).ensureDaemonAndConnect = async () => { + (client as any).connect(config); + }; + + try { + await settleWithinTestTimeout(client.start()); + await settleWithinTestTimeout(client.start()); + expect(webSockets.constructionAttemptsTest()).toBe(2); + expect(webSockets.sockets).toHaveLength(1); + } finally { + client.stop(); + webSockets.restoreTest(); + } + }); + + for (const outcome of ["resolve", "reject"] as const) { + test(`stop fences a late startup ${outcome} without warning, retry, or socket mutation`, async () => { + delete process.env.HUNK_MCP_DISABLE; + const clock = installDeterministicClockTest(); + const webSockets = installWebSocketObserverTest(); + const messages: string[] = []; + console.error = (...args: unknown[]) => { + messages.push(args.map((value) => String(value)).join(" ")); + }; + const client = new SessionBrokerClient(createRegistration(), createSnapshot(), { + reconnectDelayMs: 30, + }); + const config = prepareDirectConnectTest(client); + const gate = createDeferredTest(); + (client as any).ensureDaemonAndConnect = async () => { + await gate.promise; + if (outcome === "reject") throw new Error("late startup failure"); + (client as any).connect(config); + }; + + try { + const startup = client.start(); + client.stop(); + gate.resolve(); + await settleWithinTestTimeout(startup); + expect(messages).toEqual([]); + expect(webSockets.constructionAttemptsTest()).toBe(0); + expect(clock.pendingCountTest()).toBe(0); + } finally { + client.stop(); + webSockets.restoreTest(); + clock.restoreTest(); + } + }); + } + test("retries the complete startup cycle and recovers without restarting the client", async () => { const messages: string[] = []; console.error = (...args: unknown[]) => { From af8c19ca9a772e27d17254332e982db4679f9aff Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Mon, 31 Aug 2026 15:43:53 -0400 Subject: [PATCH 2/2] test(session): harden lifecycle fixtures --- src/session/broker/brokerClient.test.ts | 138 +++++++++++++++++------- 1 file changed, 100 insertions(+), 38 deletions(-) diff --git a/src/session/broker/brokerClient.test.ts b/src/session/broker/brokerClient.test.ts index 1fe0a114f..217200f19 100644 --- a/src/session/broker/brokerClient.test.ts +++ b/src/session/broker/brokerClient.test.ts @@ -9,11 +9,19 @@ import { createTestSessionSnapshot, } from "../../../test/helpers/session-daemon-fixtures"; import { HUNK_SESSION_API_VERSION, HUNK_SESSION_DAEMON_VERSION } from "../protocol"; -import { SessionBroker, createSessionBrokerDaemon } from "@hunk/session-broker"; +import { + SessionBroker, + createSessionBrokerDaemon, + type SessionBrokerSocketLike, +} from "@hunk/session-broker"; import { serveSessionBrokerDaemon as serveBunSessionBrokerDaemon } from "@hunk/session-broker-bun"; import { hunkSessionProtocolParsers } from "./protocolParsers"; import { isQuiescentUpgradeRefusal, SessionBrokerClient } from "./brokerClient"; -import { loadOrCreateHunkSessionBrokerCredentials } from "./credentials"; +import type { ResolvedSessionBrokerConfig } from "./brokerConfig"; +import { + loadOrCreateHunkSessionBrokerCredentials, + type HunkSessionBrokerCredentials, +} from "./credentials"; import { serveSessionBrokerDaemon as serveHunkSessionBrokerDaemon } from "./brokerServer"; import { createHttpHunkSessionCliClient } from "../agent/cliClient"; import { resolveSessionBrokerRuntimePaths } from "./brokerLauncher"; @@ -26,6 +34,36 @@ const originalRuntimeDir = process.env.XDG_RUNTIME_DIR; const originalConsoleError = console.error; const nativeSetTimeoutTest = globalThis.setTimeout.bind(globalThis); const nativeClearTimeoutTest = globalThis.clearTimeout.bind(globalThis); +let restoreDeterministicClockTest: (() => void) | null = null; +let restoreWebSocketObserverTest: (() => void) | null = null; + +type DeepPartialTest = T extends object ? { [Key in keyof T]?: DeepPartialTest } : T; + +interface SessionBrokerConnectionTestDouble { + start(): void; + stop(): void; + updateSnapshot(snapshot: ReturnType): void; + replaceSession( + registration: ReturnType, + snapshot: ReturnType, + ): void; +} + +interface SessionBrokerClientTestAccess { + connection: Partial | null; + credentials: DeepPartialTest | null; + waitingForIncumbentExit: boolean; + restartIncompatibleDaemon?: unknown; + connect(config: ResolvedSessionBrokerConfig): void; + ensureDaemonAndConnect(): Promise; + ensureDaemonAvailable(config: ResolvedSessionBrokerConfig): Promise; + scheduleReconnect(delayMs?: number): void; +} + +/** Access lifecycle internals through one explicit, structurally checked test seam. */ +function clientTestAccess(client: SessionBrokerClient) { + return client as unknown as SessionBrokerClientTestAccess; +} function createRegistration() { return createTestSessionRegistration({ @@ -96,6 +134,9 @@ function createDeferredTest() { /** Replace only timeout scheduling with a deterministic, test-local clock. */ function installDeterministicClockTest() { + if (restoreDeterministicClockTest) { + throw new Error("A deterministic test clock is already installed."); + } const originalSetTimeout = globalThis.setTimeout; const originalClearTimeout = globalThis.clearTimeout; let now = 0; @@ -109,6 +150,17 @@ function installDeterministicClockTest() { } >(); + let restored = false; + const restoreTest = () => { + if (restored) return; + restored = true; + globalThis.setTimeout = originalSetTimeout; + globalThis.clearTimeout = originalClearTimeout; + if (restoreDeterministicClockTest === restoreTest) restoreDeterministicClockTest = null; + }; + // Register cleanup before the first process-wide mutation so setup failures remain recoverable. + restoreDeterministicClockTest = restoreTest; + globalThis.setTimeout = ((callback: TimerHandler, delay = 0, ...args: unknown[]) => { if (typeof callback !== "function") throw new Error("Test clock requires function callbacks."); const id = nextId++; @@ -149,20 +201,20 @@ function installDeterministicClockTest() { pendingCountTest() { return scheduled.size; }, - restoreTest() { - globalThis.setTimeout = originalSetTimeout; - globalThis.clearTimeout = originalClearTimeout; - }, + restoreTest, }; } /** Observe WebSocket construction without opening a network connection. */ function installWebSocketObserverTest(throwOnAttempt?: number) { + if (restoreWebSocketObserverTest) { + throw new Error("A WebSocket observer is already installed."); + } const OriginalWebSocket = globalThis.WebSocket; const sockets: Array<{ sent: string[] }> = []; let constructionAttempts = 0; - class ObservedWebSocketTest { + class ObservedWebSocketTest implements SessionBrokerSocketLike { readyState = 0; sent: string[] = []; onopen: (() => void) | null = null; @@ -186,19 +238,26 @@ function installWebSocketObserverTest(throwOnAttempt?: number) { } } + let restored = false; + const restoreTest = () => { + if (restored) return; + restored = true; + globalThis.WebSocket = OriginalWebSocket; + if (restoreWebSocketObserverTest === restoreTest) restoreWebSocketObserverTest = null; + }; + // Register cleanup before replacing the host constructor so later setup failures cannot leak it. + restoreWebSocketObserverTest = restoreTest; globalThis.WebSocket = ObservedWebSocketTest as unknown as typeof WebSocket; return { sockets, constructionAttemptsTest: () => constructionAttempts, - restoreTest() { - globalThis.WebSocket = OriginalWebSocket; - }, + restoreTest, }; } /** Supply inert credentials so startup tests can exercise the real connection ownership path. */ function prepareDirectConnectTest(client: SessionBrokerClient) { - (client as any).credentials = { + clientTestAccess(client).credentials = { producer: {}, daemonIdentity: { keyId: "daemon-key-test" }, daemonPublicKey: {}, @@ -212,6 +271,9 @@ function prepareDirectConnectTest(client: SessionBrokerClient) { } afterEach(() => { + restoreWebSocketObserverTest?.(); + restoreDeterministicClockTest?.(); + if (originalHost === undefined) { delete process.env.HUNK_MCP_HOST; } else { @@ -270,7 +332,7 @@ describe("Hunk session daemon client", () => { test("keeps its previous registration when the live connection rejects replacement", () => { const registration = createRegistration(); const client = new SessionBrokerClient(registration, createSnapshot()); - (client as any).connection = { + clientTestAccess(client).connection = { replaceSession() { throw new Error("connection exploded"); }, @@ -313,7 +375,7 @@ describe("Hunk session daemon client", () => { test("does not retain the legacy PID-based incompatible-daemon replacement path", () => { const client = new SessionBrokerClient(createRegistration(), createSnapshot()); - expect((client as any).restartIncompatibleDaemon).toBeUndefined(); + expect(clientTestAccess(client).restartIncompatibleDaemon).toBeUndefined(); client.stop(); }); @@ -417,17 +479,17 @@ describe("Hunk session daemon client", () => { httpOrigin: `http://127.0.0.1:${port}`, wsOrigin: `ws://127.0.0.1:${port}`, }; - (client as any).credentials = credentials; - (client as any).connect(config); + clientTestAccess(client).credentials = credentials; + clientTestAccess(client).connect(config); await Bun.sleep(7); - (skewedClient as any).credentials = credentials; - (skewedClient as any).connect(config); + clientTestAccess(skewedClient).credentials = credentials; + clientTestAccess(skewedClient).connect(config); await waitUntil("both incompatible session warnings", () => messages.length === 2); expect(messages.every((message) => message.includes("Close older Hunk windows"))).toBe(true); await Bun.sleep(60); expect(websocketOpens).toBe(2); - expect((client as any).waitingForIncumbentExit).toBe(true); - expect((skewedClient as any).waitingForIncumbentExit).toBe(true); + expect(clientTestAccess(client).waitingForIncumbentExit).toBe(true); + expect(clientTestAccess(skewedClient).waitingForIncumbentExit).toBe(true); } finally { client.stop(); skewedClient.stop(); @@ -501,7 +563,7 @@ describe("Hunk session daemon client", () => { try { await client.start(); - const retainedConnection = (client as any).connection; + const retainedConnection = clientTestAccess(client).connection; await waitUntil("incompatible signed hello", () => helloAttempts === 1); incumbentDaemon.shutdown(); @@ -533,7 +595,7 @@ describe("Hunk session daemon client", () => { 25, ); expect(helloAttempts).toBe(1); - expect((client as any).connection).toBe(retainedConnection); + expect(clientTestAccess(client).connection).toBe(retainedConnection); } finally { client.stop(); incumbentDaemon.shutdown(); @@ -590,7 +652,7 @@ describe("Hunk session daemon client", () => { const client = new SessionBrokerClient(createRegistration(), createSnapshot(), { reconnectDelayMs: 10, }); - (client as any).ensureDaemonAvailable = async () => { + clientTestAccess(client).ensureDaemonAvailable = async () => { try { if ((await fetch(`http://127.0.0.1:${port}/health`)).ok) return; } catch { @@ -601,7 +663,7 @@ describe("Hunk session daemon client", () => { try { await client.start(); - const retainedConnection = (client as any).connection; + const retainedConnection = clientTestAccess(client).connection; await waitUntil("first incompatible websocket", () => helloAttempts === 1); await Bun.sleep(35); expect(helloAttempts).toBe(1); @@ -624,7 +686,7 @@ describe("Hunk session daemon client", () => { 5_000, 50, ); - expect((client as any).connection).toBe(retainedConnection); + expect(clientTestAccess(client).connection).toBe(retainedConnection); const firstSuccessor = successor as unknown as Awaited< ReturnType @@ -650,7 +712,7 @@ describe("Hunk session daemon client", () => { 5_000, 50, ); - expect((client as any).connection).toBe(retainedConnection); + expect(clientTestAccess(client).connection).toBe(retainedConnection); } finally { client.stop(); incumbent.stop(true); @@ -668,8 +730,8 @@ describe("Hunk session daemon client", () => { const webSockets = installWebSocketObserverTest(); const client = new SessionBrokerClient(createRegistration(), createSnapshot()); const config = prepareDirectConnectTest(client); - (client as any).ensureDaemonAndConnect = async () => { - (client as any).connect(config); + clientTestAccess(client).ensureDaemonAndConnect = async () => { + clientTestAccess(client).connect(config); }; try { @@ -692,10 +754,10 @@ describe("Hunk session daemon client", () => { const config = prepareDirectConnectTest(client); const gate = createDeferredTest(); let attempts = 0; - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { attempts += 1; await gate.promise; - (client as any).connect(config); + clientTestAccess(client).connect(config); }; try { @@ -725,7 +787,7 @@ describe("Hunk session daemon client", () => { messages.push(args.map((value) => String(value)).join(" ")); }; let attempts = 0; - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { attempts += 1; if (attempts === 1) throw new Error("startup unavailable"); }; @@ -761,8 +823,8 @@ describe("Hunk session daemon client", () => { reconnectDelayMs: 10_000, }); const config = prepareDirectConnectTest(client); - (client as any).ensureDaemonAndConnect = async () => { - (client as any).connect(config); + clientTestAccess(client).ensureDaemonAndConnect = async () => { + clientTestAccess(client).connect(config); }; try { @@ -790,10 +852,10 @@ describe("Hunk session daemon client", () => { }); const config = prepareDirectConnectTest(client); const gate = createDeferredTest(); - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { await gate.promise; if (outcome === "reject") throw new Error("late startup failure"); - (client as any).connect(config); + clientTestAccess(client).connect(config); }; try { @@ -821,7 +883,7 @@ describe("Hunk session daemon client", () => { reconnectDelayMs: 10, }); let attempts = 0; - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { attempts += 1; if (attempts === 1) throw new Error("incumbent incompatible"); }; @@ -838,7 +900,7 @@ describe("Hunk session daemon client", () => { test("does no daemon work when start follows terminal stop", async () => { const client = new SessionBrokerClient(createRegistration(), createSnapshot()); let attempts = 0; - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { attempts += 1; }; @@ -854,11 +916,11 @@ describe("Hunk session daemon client", () => { }); let reconnectScheduled = false; const client = new SessionBrokerClient(createRegistration(), createSnapshot()); - (client as any).ensureDaemonAndConnect = async () => { + clientTestAccess(client).ensureDaemonAndConnect = async () => { await gate; throw new Error("late startup failure"); }; - (client as any).scheduleReconnect = () => { + clientTestAccess(client).scheduleReconnect = () => { reconnectScheduled = true; };