diff --git a/tests/boundary/mcp/buildComputerMcp.test.ts b/tests/boundary/mcp/buildComputerMcp.test.ts index 0fe9c3bd02..deed463d7b 100644 --- a/tests/boundary/mcp/buildComputerMcp.test.ts +++ b/tests/boundary/mcp/buildComputerMcp.test.ts @@ -25,6 +25,7 @@ import { } from "../../fixtures/buildComputerMcp.js"; import { COMPUTER_START_TIMING } from "../../../src/desktop/AgentEnvComputer.js"; import { + awaitFakeDeployWait, FAKE_FRAME_PNG, startFakeComputer, type FakeApp, @@ -603,16 +604,15 @@ it("stops the helper when the caller cancels a start", async () => { }, { signal: controller.signal }, ); - const deploying = waitFor(async () => { - const notes = await runner.notes("deploying.jsonl"); - expect(notes).toHaveLength(1); - return notes; - }).finally(() => controller.abort()); - const [, helper] = await Promise.all([ - expect(call).rejects.toThrow(), - deploying, - ]); - await expect.poll(() => runner.notes("interrupted.jsonl")).toEqual(helper); + try { + const helper = await awaitFakeDeployWait(runner, call); + controller.abort(); + await expect(call).rejects.toThrow(); + await expect.poll(() => runner.notes("interrupted.jsonl")).toEqual(helper); + } finally { + controller.abort(); + await Promise.allSettled([call]); + } }); /** An app whose reset keeps the computer unready for `polls` readiness polls, 500 ms apart. */ diff --git a/tests/boundary/providers/buildComputerLifecycle.test.ts b/tests/boundary/providers/buildComputerLifecycle.test.ts index 221fad2193..555dd0e39f 100644 --- a/tests/boundary/providers/buildComputerLifecycle.test.ts +++ b/tests/boundary/providers/buildComputerLifecycle.test.ts @@ -4,9 +4,11 @@ import { setTimeout as delay } from "node:timers/promises"; import { expect, it, onTestFinished } from "vitest"; -import { fakeBuildComputers as setUp } from "../../fixtures/fakeAgentEnvComputer.js"; +import { + awaitFakeDeployWait, + fakeBuildComputers as setUp, +} from "../../fixtures/fakeAgentEnvComputer.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -import { waitFor } from "../../support/testWait.js"; it("joins a start still in progress when the owner closes, and refuses later starts", async () => { const { computers, runner, start } = await setUp( @@ -18,12 +20,16 @@ it("joins a start still in progress when the owner closes, and refuses later sta order.push("start settled"); return result; }); - const helper = await waitFor(async () => { - const notes = await runner.notes("deploying.jsonl"); - expect(notes).toHaveLength(1); - return notes; - }).finally(() => computers.closeAll()); - order.push("closeAll returned"); + const helper = await awaitFakeDeployWait(runner, pending).finally( + async () => { + try { + await computers.closeAll(); + order.push("closeAll returned"); + } finally { + await Promise.allSettled([pending]); + } + }, + ); expect(order).toEqual(["start settled", "closeAll returned"]); const started = await pending; expect(started.ok).toBe(false); diff --git a/tests/fixtures/fakeAgentEnvComputer.ts b/tests/fixtures/fakeAgentEnvComputer.ts index 2c03229acb..8c2f0220df 100644 --- a/tests/fixtures/fakeAgentEnvComputer.ts +++ b/tests/fixtures/fakeAgentEnvComputer.ts @@ -34,7 +34,9 @@ import { onTestFinished } from "vitest"; import { DesktopBuildComputers } from "../../src/application/desktop/DesktopBuildComputers.js"; import type { BuildComputerDefaults } from "../../src/desktop/BuildComputerEnvironment.js"; +import { awaitAcknowledgement } from "./lifecycleAcknowledgement.js"; import { createTestTempDirectory } from "./temporaryDirectory.js"; +import { waitFor } from "../support/testWait.js"; /** A 2x1 PNG of a black and a white pixel, the fake driver's drawn desktop. */ export const FAKE_FRAME_PNG = @@ -733,6 +735,31 @@ export interface FakeAgentEnvRunner { notes(name: string): Promise; } +/** Observe the exact helper waiting at the cancellable deployment phase. */ +export const awaitFakeDeployWait = async ( + runner: FakeAgentEnvRunner, + operation: Promise, +): Promise<{ pid: number }[]> => { + let observing = true; + const acknowledgement = waitFor(async () => { + if (!observing) return []; + return z + .tuple([z.object({ pid: z.int().positive() })]) + .parse(await runner.notes("deploying.jsonl")); + }); + try { + await awaitAcknowledgement( + acknowledgement, + operation, + "fake deployment wait", + ); + return await acknowledgement; + } finally { + observing = false; + await Promise.allSettled([acknowledgement]); + } +}; + /** * JSONL notes from the fake build computer. A trailing fragment without a * newline is still being appended. Every newline-terminated line is a record