mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
fix(e2e): fail gateway frame waits on socket close
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readGatewayNetworkClientConnectTimeoutMs } from "../../scripts/e2e/lib/gateway-network/limits.mjs";
|
||||
import { onceFrame } from "../../scripts/e2e/lib/gateway-network/ws-frames.mjs";
|
||||
|
||||
describe("gateway network WebSocket open guard", () => {
|
||||
it("rejects loose client timeout env values instead of parsing prefixes", () => {
|
||||
@@ -35,6 +37,54 @@ describe("gateway network WebSocket open guard", () => {
|
||||
).toBe(3000);
|
||||
});
|
||||
|
||||
it("resolves matching frames and ignores unrelated frames", async () => {
|
||||
const ws = new EventEmitter();
|
||||
const frame = onceFrame(ws, (message) => message?.id === "target", 1000);
|
||||
|
||||
ws.emit("message", JSON.stringify({ id: "noise" }));
|
||||
ws.emit("message", JSON.stringify({ id: "target", ok: true }));
|
||||
|
||||
await expect(frame).resolves.toEqual({ id: "target", ok: true });
|
||||
});
|
||||
|
||||
it("times out when no matching frame arrives", async () => {
|
||||
const ws = new EventEmitter();
|
||||
const frame = onceFrame(ws, () => false, 10);
|
||||
|
||||
ws.emit("message", JSON.stringify({ id: "noise" }));
|
||||
|
||||
await expect(frame).rejects.toThrow("timeout");
|
||||
});
|
||||
|
||||
it("rejects frame waits immediately when the socket closes", async () => {
|
||||
const ws = new EventEmitter();
|
||||
const startedAt = Date.now();
|
||||
const frame = onceFrame(ws, () => false, 1000);
|
||||
|
||||
ws.emit("close", 1006, Buffer.from("bye"));
|
||||
|
||||
await expect(frame).rejects.toThrow("closed before frame: 1006 bye");
|
||||
expect(Date.now() - startedAt).toBeLessThan(250);
|
||||
});
|
||||
|
||||
it("rejects frame waits immediately on socket errors", async () => {
|
||||
const ws = new EventEmitter();
|
||||
const frame = onceFrame(ws, () => false, 1000);
|
||||
|
||||
ws.emit("error", new Error("socket exploded"));
|
||||
|
||||
await expect(frame).rejects.toThrow("socket exploded");
|
||||
});
|
||||
|
||||
it("rejects invalid JSON frames instead of crashing the process", async () => {
|
||||
const ws = new EventEmitter();
|
||||
const frame = onceFrame(ws, () => false, 1000);
|
||||
|
||||
ws.emit("message", "{nope");
|
||||
|
||||
await expect(frame).rejects.toThrow();
|
||||
});
|
||||
|
||||
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"');
|
||||
@@ -44,5 +94,6 @@ describe("gateway network WebSocket open guard", () => {
|
||||
expect(healthIndex).toBeGreaterThan(connectIndex);
|
||||
expect(client).toContain('responseError("health", healthRes)');
|
||||
expect(client).toContain('message.includes("closed before open")');
|
||||
expect(client).toContain('message.includes("closed before frame")');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user