fix(e2e): prove gateway health after websocket connect

This commit is contained in:
Vincent Koc
2026-05-31 01:39:19 +02:00
parent 6561bdc41d
commit 6270d5326f
8 changed files with 130 additions and 23 deletions

View File

@@ -57,6 +57,7 @@ Docs: https://docs.openclaw.ai
- CI/tooling: route script edits through conventional owner tests when matching `test/scripts` or `src/scripts` coverage already exists.
- CI/tooling: honor option terminators in the memory FD repro script so follow-on arguments are not reparsed.
- Release/CI/E2E: assert plugin lifecycle runtime inspect output instead of only capturing it.
- Release/CI/E2E: make gateway-network prove the advertised health RPC and retry early WebSocket closes without burning full open timeouts.
- Release/CI/E2E: honor option terminators across release, Parallels smoke, plugin gauntlet, and extension-memory scripts.
- Release/CI/E2E: fail plugin gateway gauntlet QA chunks when the requested suite summary is missing or invalid.
- Performance: prebuild QA runtime probes with generated plugin assets but without CLI startup metadata.

View File

@@ -40,6 +40,22 @@ function onceFrame(ws, filter, timeoutMs = 10_000) {
});
}
function responseError(method, response) {
const message = response.error?.message ?? "unknown";
return new Error(`${method} failed: ${message}`);
}
function isRetryableStartupError(message) {
return (
message.includes("gateway starting") ||
message.includes("closed before open") ||
message.includes("ws open timeout") ||
message.includes("ECONNREFUSED") ||
message.includes("ECONNRESET") ||
message.includes("timeout")
);
}
let lastError;
while (Date.now() < deadline) {
let ws;
@@ -67,33 +83,28 @@ while (Date.now() < deadline) {
);
const connectRes = await onceFrame(ws, (frame) => frame?.type === "res" && frame?.id === "c1");
if (connectRes.ok) {
ws.close();
console.log("ok");
process.exit(0);
}
if (!connectRes.ok) {
lastError = responseError("connect", connectRes);
if (!isRetryableStartupError(lastError.message)) {
throw lastError;
}
} else {
ws.send(JSON.stringify({ type: "req", id: "h1", method: "health" }));
const healthRes = await onceFrame(
ws,
(frame) => frame?.type === "res" && frame?.id === "h1",
);
if (healthRes.ok) {
ws.close();
console.log("ok");
process.exit(0);
}
const message = connectRes.error?.message ?? "unknown";
lastError = new Error(`connect failed: ${message}`);
if (
!message.includes("gateway starting") &&
!message.includes("ws open timeout") &&
!message.includes("ECONNREFUSED") &&
!message.includes("ECONNRESET") &&
!message.includes("timeout")
) {
throw lastError;
throw responseError("health", healthRes);
}
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
const message = lastError.message;
if (
!message.includes("gateway starting") &&
!message.includes("ws open timeout") &&
!message.includes("ECONNREFUSED") &&
!message.includes("ECONNRESET") &&
!message.includes("timeout")
) {
if (!isRetryableStartupError(lastError.message)) {
throw lastError;
}
} finally {

View File

@@ -1,3 +1,19 @@
function formatCloseValue(value) {
if (value === undefined || value === null) {
return "";
}
if (typeof value === "string") {
return value;
}
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
return value.toString();
}
if (value instanceof Uint8Array) {
return Buffer.from(value).toString();
}
return JSON.stringify(value) ?? "";
}
export function waitForWebSocketOpen(ws, timeoutMs, message = "ws open timeout") {
return new Promise((resolve, reject) => {
let settled = false;
@@ -10,11 +26,19 @@ export function waitForWebSocketOpen(ws, timeoutMs, message = "ws open timeout")
clearTimeout(timer);
ws.off?.("open", onOpen);
ws.off?.("error", onError);
ws.off?.("close", onClose);
fn(value);
};
const onOpen = () => settle(resolve);
const onError = (error) =>
settle(reject, error instanceof Error ? error : new Error(String(error)));
const onClose = (code, reason) => {
const closeDetails = [formatCloseValue(code), formatCloseValue(reason)]
.filter(Boolean)
.join(" ");
const suffix = closeDetails ? `: ${closeDetails}` : "";
settle(reject, new Error(`closed before open${suffix}`));
};
const timer = setTimeout(() => {
const consumeAbortError = () => {};
const removeAbortErrorConsumer = () => {
@@ -23,6 +47,7 @@ export function waitForWebSocketOpen(ws, timeoutMs, message = "ws open timeout")
};
try {
ws.off?.("error", onError);
ws.off?.("close", onClose);
ws.on?.("error", consumeAbortError);
ws.once?.("close", removeAbortErrorConsumer);
ws.terminate?.();
@@ -37,5 +62,6 @@ export function waitForWebSocketOpen(ws, timeoutMs, message = "ws open timeout")
timer.unref?.();
ws.once("open", onOpen);
ws.once("error", onError);
ws.once("close", onClose);
});
}

View File

@@ -300,6 +300,7 @@ function isRetryableGatewayConnectError(error: Error): boolean {
return (
message.includes("gateway ws open timeout") ||
message.includes("gateway connect timeout") ||
message.includes("closed before open") ||
message.includes("gateway closed") ||
message.includes("econnrefused") ||
message.includes("socket hang up")

View File

@@ -6,6 +6,22 @@ type WebSocketOpenHandle = {
terminate?: () => void;
};
function formatCloseValue(value: unknown): string {
if (value === undefined || value === null) {
return "";
}
if (typeof value === "string") {
return value;
}
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
return value.toString();
}
if (value instanceof Uint8Array) {
return Buffer.from(value).toString();
}
return JSON.stringify(value) ?? "";
}
export function waitForWebSocketOpen(
ws: WebSocketOpenHandle,
timeoutMs: number,
@@ -19,6 +35,7 @@ export function waitForWebSocketOpen(
clearTimeout(timer);
ws.off?.("open", onOpen);
ws.off?.("error", onError);
ws.off?.("close", onClose);
};
const resolveOpen = () => {
if (settled) {
@@ -38,6 +55,13 @@ export function waitForWebSocketOpen(
};
const onOpen = () => resolveOpen();
const onError = (error: unknown) => rejectOpen(error);
const onClose = (code?: unknown, reason?: unknown) => {
const closeDetails = [formatCloseValue(code), formatCloseValue(reason)]
.filter(Boolean)
.join(" ");
const suffix = closeDetails ? `: ${closeDetails}` : "";
rejectOpen(new Error(`closed before open${suffix}`));
};
timer = setTimeout(() => {
const consumeAbortError = () => {};
const removeAbortErrorConsumer = () => {
@@ -46,6 +70,7 @@ export function waitForWebSocketOpen(
};
try {
ws.off?.("error", onError);
ws.off?.("close", onClose);
ws.on?.("error", consumeAbortError);
ws.once?.("close", removeAbortErrorConsumer);
ws.terminate?.();
@@ -60,5 +85,6 @@ export function waitForWebSocketOpen(
timer.unref?.();
ws.once("open", onOpen);
ws.once("error", onError);
ws.once("close", onClose);
});
}

View File

@@ -33,6 +33,7 @@ describe("E2E WebSocket open guard", () => {
expect(ws.terminated).toBe(true);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
it("uses caller-specific timeout messages", async () => {
@@ -58,5 +59,19 @@ describe("E2E WebSocket open guard", () => {
expect(ws.terminated).toBe(false);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
it("rejects immediately when the socket closes before opening", async () => {
const ws = new FakeWebSocket();
const opened = waitForWebSocketOpen(ws, 1000);
ws.emit("close", 1006, Buffer.from("bye"));
await expect(opened).rejects.toThrow("closed before open: 1006 bye");
expect(ws.terminated).toBe(false);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
});

View File

@@ -1,3 +1,4 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { readGatewayNetworkClientConnectTimeoutMs } from "../../scripts/e2e/lib/gateway-network/limits.mjs";
@@ -33,4 +34,15 @@ describe("gateway network WebSocket open guard", () => {
}),
).toBe(3000);
});
it("proves health after the authenticated connect handshake", () => {
const client = readFileSync("scripts/e2e/lib/gateway-network/client.mjs", "utf8");
const connectIndex = client.indexOf('method: "connect"');
const healthIndex = client.indexOf('method: "health"');
expect(connectIndex).toBeGreaterThanOrEqual(0);
expect(healthIndex).toBeGreaterThan(connectIndex);
expect(client).toContain('responseError("health", healthRes)');
expect(client).toContain('message.includes("closed before open")');
});
});

View File

@@ -38,6 +38,7 @@ describe("mcp channel WebSocket open guard", () => {
expect(ws.terminated).toBe(true);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
it("cleans listeners after successful opens", async () => {
@@ -50,5 +51,19 @@ describe("mcp channel WebSocket open guard", () => {
expect(ws.terminated).toBe(false);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
it("rejects immediately when the socket closes before opening", async () => {
const ws = new FakeWebSocket();
const opened = waitForWebSocketOpen(ws, 1000);
ws.emit("close", 1006, Buffer.from("bye"));
await expect(opened).rejects.toThrow("closed before open: 1006 bye");
expect(ws.terminated).toBe(false);
expect(ws.listenerCount("open")).toBe(0);
expect(ws.listenerCount("error")).toBe(0);
expect(ws.listenerCount("close")).toBe(0);
});
});