diff --git a/src/gateway/server.startup-websocket-race.test.ts b/src/gateway/server.startup-websocket-race.test.ts index 7508279605e..8c18adec3f1 100644 --- a/src/gateway/server.startup-websocket-race.test.ts +++ b/src/gateway/server.startup-websocket-race.test.ts @@ -1,5 +1,6 @@ import http from "node:http"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { connectGatewayClient, disconnectGatewayClient } from "./test-helpers.e2e.js"; import { getFreePort, installGatewayTestHooks, startGatewayServer } from "./test-helpers.js"; const machineNameDelay = vi.hoisted(() => { @@ -104,4 +105,39 @@ describe("gateway startup websocket readiness", () => { } } }); + + it("accepts an immediate websocket connection once startup resolves", async () => { + machineNameDelay.reset(); + const previousMinimal = process.env.OPENCLAW_TEST_MINIMAL_GATEWAY; + process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = "0"; + let server: Awaited> | undefined; + let client: Awaited> | undefined; + try { + const port = await getFreePort(); + machineNameDelay.release(); + server = await startGatewayServer(port, { + auth: { mode: "none" }, + }); + + client = await connectGatewayClient({ + url: `ws://127.0.0.1:${port}`, + timeoutMs: 5_000, + timeoutMessage: "expected websocket connect to succeed immediately after startup", + }); + + expect(client).toBeDefined(); + } finally { + if (client) { + await disconnectGatewayClient(client); + } + if (server) { + await server.close(); + } + if (previousMinimal === undefined) { + delete process.env.OPENCLAW_TEST_MINIMAL_GATEWAY; + } else { + process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = previousMinimal; + } + } + }); });