Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

test(session-host): inject the connect poll pause instead of waiting on the real clock

Brandon Bridgese22a556d7886
diff --git a/src/core/session-host-client-staged.test.ts b/src/core/session-host-client-staged.test.ts
index 9a1c06eee..d44d7ef2d 100644
--- a/src/core/session-host-client-staged.test.ts
+++ b/src/core/session-host-client-staged.test.ts
@@ -23,41 +23,53 @@ beforeEach(() => {
identityMocks.readExistingSessionHostIdentity.mockReturnValue({ kind: 'absent' })
})
+// Each failing connect polls 30 × 150 ms for a host that never appears (the launcher is mocked).
+// On the real clock that was 4.5 s per connect and 22.6 s for this file on CI (audit code-devx-4);
+// the pause is injected instead, so the loop still runs every attempt of its budget.
+const connectPollSleep = vi.fn(async (_ms: number) => {})
+const newClient = (
+ stageRuntime?: (script: string) => Promise<{ exe: string; script: string } | null>
+): SessionHostClient =>
+ new SessionHostClient({ userDataDir: testTmpDir('nt-staged-'), stageRuntime, connectPollSleep })
+
const staged = { exe: 'C:\\L\\nodeterm\\session-host\\v\\nodeterm-sessionhost-v2.exe', script: 'C:\\L\\h.cjs' }
describe('SessionHostClient launches from a staged runtime when one is available', () => {
it('passes the staged runtime to the launcher', async () => {
const stageRuntime = vi.fn(async () => staged)
- const client = new SessionHostClient({ userDataDir: testTmpDir('nt-staged-'), stageRuntime })
+ const client = newClient(stageRuntime)
await expect(client.listSessions()).rejects.toThrow()
expect(stageRuntime).toHaveBeenCalledWith('fake-session-host.cjs')
expect(launcherMocks.spawnSessionHost).toHaveBeenCalledTimes(1)
expect(launcherMocks.spawnSessionHost.mock.calls[0][2]).toEqual(staged)
- }, 30_000)
+ // The ordinary budget is unchanged: 30 polls 150 ms apart (4.5 s) before giving up.
+ expect(connectPollSleep).toHaveBeenCalledTimes(30)
+ expect(connectPollSleep.mock.calls.every(([ms]) => ms === 150)).toBe(true)
+ })
it('falls back to the legacy launch when staging rejects', async () => {
const stageRuntime = vi.fn(async () => {
throw new Error('disk full')
})
- const client = new SessionHostClient({ userDataDir: testTmpDir('nt-staged-'), stageRuntime })
+ const client = newClient(stageRuntime)
await expect(client.listSessions()).rejects.toThrow()
expect(launcherMocks.spawnSessionHost.mock.calls[0][2]).toBeNull()
- }, 30_000)
+ })
it('stops offering the staged runtime after it failed to start once', async () => {
const stageRuntime = vi.fn(async () => staged)
- const client = new SessionHostClient({ userDataDir: testTmpDir('nt-staged-'), stageRuntime })
+ const client = newClient(stageRuntime)
await expect(client.listSessions()).rejects.toThrow()
const onStagedFailure = launcherMocks.spawnSessionHost.mock.calls[0][3] as () => void
onStagedFailure()
await expect(client.listSessions()).rejects.toThrow()
const last = launcherMocks.spawnSessionHost.mock.calls.at(-1)!
expect(last[2]).toBeNull()
- }, 30_000)
+ })
it('without a stager behaves exactly as before (no staged argument)', async () => {
- const client = new SessionHostClient({ userDataDir: testTmpDir('nt-staged-') })
+ const client = newClient()
await expect(client.listSessions()).rejects.toThrow()
expect(launcherMocks.spawnSessionHost.mock.calls[0][2]).toBeNull()
- }, 30_000)
+ })
})
diff --git a/src/core/session-host-client-startup-wait.test.ts b/src/core/session-host-client-startup-wait.test.ts
index 499d702da..ea1bffb34 100644
--- a/src/core/session-host-client-startup-wait.test.ts
+++ b/src/core/session-host-client-startup-wait.test.ts
@@ -27,6 +27,13 @@ const emptyLock = (): never => {
const tempDir = (): string => testTmpDir('nt-host-wait-')
+// The client's poll pause, injected so the 45- and 35-poll waits below cost no wall-clock: on the
+// real 150 ms timer this file took 12 s on CI (audit code-devx-4). Only the pause is replaced, not
+// the loop, so the attempt counts these tests pin are exactly what production runs.
+const connectPollSleep = vi.fn(async (_ms: number) => {})
+const newClient = (): SessionHostClient =>
+ new SessionHostClient({ userDataDir: tempDir(), connectPollSleep })
+
beforeEach(() => {
vi.clearAllMocks()
launcherMocks.resolveSessionHostScript.mockReturnValue('fake-session-host.cjs')
@@ -41,12 +48,11 @@ describe('SessionHostClient waits for a host that is still starting', () => {
identityMocks.readExistingSessionHostIdentity.mock.calls.length < 45 ? 'starting' : 'other'
)
- await expect(new SessionHostClient({ userDataDir: tempDir() }).listSessions()).rejects.toThrow(
- 'file is empty'
- )
+ await expect(newClient().listSessions()).rejects.toThrow('file is empty')
// Proof it did not stop at 30: the ordinary budget would have given up long before this.
expect(identityMocks.readExistingSessionHostIdentity.mock.calls.length).toBeGreaterThan(40)
- }, 30_000)
+ expect(connectPollSleep.mock.calls.filter(([ms]) => ms === 150).length).toBeGreaterThan(30)
+ })
it('does not spawn a second host while one is starting', async () => {
identityMocks.readExistingSessionHostIdentity.mockImplementation(emptyLock)
@@ -54,22 +60,19 @@ describe('SessionHostClient waits for a host that is still starting', () => {
identityMocks.readExistingSessionHostIdentity.mock.calls.length < 35 ? 'starting' : 'other'
)
- await expect(
- new SessionHostClient({ userDataDir: tempDir() }).listSessions()
- ).rejects.toThrow()
+ await expect(newClient().listSessions()).rejects.toThrow()
expect(launcherMocks.spawnSessionHost).not.toHaveBeenCalled()
- }, 30_000)
+ })
it('fails fast, exactly as before, when nothing is starting', async () => {
identityMocks.readExistingSessionHostIdentity.mockImplementation(emptyLock)
identityMocks.startupLockState.mockReturnValue('other')
- await expect(new SessionHostClient({ userDataDir: tempDir() }).listSessions()).rejects.toThrow(
- 'file is empty'
- )
+ await expect(newClient().listSessions()).rejects.toThrow('file is empty')
// Unchanged behaviour: an empty lock nobody is heartbeating is an integrity failure, reported
// after the small pre-launch batch — no host is launched and nothing waits on it.
expect(identityMocks.readExistingSessionHostIdentity.mock.calls.length).toBeLessThanOrEqual(6)
expect(launcherMocks.spawnSessionHost).not.toHaveBeenCalled()
- }, 30_000)
+ expect(connectPollSleep).not.toHaveBeenCalledWith(150)
+ })
})
diff --git a/src/core/session-host-client.ts b/src/core/session-host-client.ts
index 8b79f809e..f9c84a7bf 100644
--- a/src/core/session-host-client.ts
+++ b/src/core/session-host-client.ts
@@ -441,9 +441,18 @@ export class SessionHostClient {
/** Classify the running host's image for prepare-for-update (`resolveHostStaging`). Absent
* (every non-Windows or unpackaged client) ⇒ `unknown`. */
hostStaging?: (host: { exe?: string; pid: number | null }) => Promise<HostImageStaging>
+ /** The pause between connect polls (the pre-launch publication retries and the post-launch
+ * wait for the host). Absent = a real timer. Tests inject an immediate one: each failing
+ * connect waited out 30 × 150 ms = 4.5 s of real time, and the staged/startup-wait suites
+ * spent ~35 s of CI doing nothing else (audit code-devx-4). Not fake timers: 94005975. */
+ connectPollSleep?: (ms: number) => Promise<void>
}
) {}
+ private pollSleep(ms: number): Promise<void> {
+ return (this.deps.connectPollSleep ?? sleep)(ms)
+ }
+
/** A staged host crashed on start this app run: launch from the installed binary from now on. */
private stagedRuntimeDisabled = false
@@ -651,7 +660,7 @@ export class SessionHostClient {
let lastPublicationError: Error | null = null
const waitStartedAt = Date.now()
for (let attempt = 0; ; attempt++) {
- await sleep(150)
+ await this.pollSleep(150)
try {
if (await this.tryConnectOnce()) {
const socket = this.socket
@@ -703,7 +712,7 @@ export class SessionHostClient {
// Still empty after the small bound — but a host that is heartbeating that lock is simply
// still starting, so the launch path's own wait owns it rather than failing this create.
if (this.hostIsStarting()) return false
- await sleep(50)
+ await this.pollSleep(50)
}
}
throw lastError ?? new Error('session-host state publication did not complete')