Files
openclaw/test/scripts/gateway-network-client.test.ts
Peter Steinberger fe261b0f59 chore(tooling): typecheck root test/** with a dedicated tsgo lane (#104475)
* chore(types): add declaration files for scripts/lib and scripts/e2e modules

* chore(types): add declaration files for top-level script modules (a-m)

* chore(types): add declaration files for top-level script modules (n-z)

* test: use a non-secret-shaped gateway token fixture

* test: type ci workflow guard helpers for the root test lane

* chore(tooling): typecheck root test/** with a dedicated tsgo lane

- test/tsconfig/tsconfig.test.root.json: root-test program (strict unused checks,
  fixtures excluded; two Docker E2E clients that import built dist/** stay out,
  same rationale as the scripts/e2e exclusion in tsconfig.scripts.json)
- tsgo:test:root wired into tsgo:test, check:test-types, scripts/check.mjs, and
  the ci.yml test-types shard, mirroring the tsgo:scripts lane (#104348)
- changed-lane routing: test/**/*.ts (excluding fixtures) and the lane tsconfig
  now trigger 'typecheck test root' in check:changed; previously test/ paths ran
  lint only, so harness type errors surfaced first in CI (#104287 envDir case)
- burn down all 1071 latent type errors in the program: precise param/local
  types across test/scripts, test/vitest, test/e2e, and transitive scripts/e2e
  program members; 205 sibling .d.mts declaration files for imported .mjs
  modules (committed separately); zero any, zero ts-expect-error
- resolve the pre-existing testing star-export ambiguity in
  scripts/e2e/parallels/common.ts with an explicit re-export

Closes #104388

* chore(types): correct declaration fidelity per structured review

- re-derive 51 .d.mts files from implementation data flow instead of
  initializers: fix a wrong never return (runTestProjectsDelegation returns
  the child), add encoding-sensitive exec/spawn overloads (plain-gh), restore
  the full release profile union, make parsed paths string | null, add missing
  parseArgs fields via help/non-help unions, add a missing sibling declaration
  (budget-number-args), drop 15 unused lint directives
- precise install-record/tuple typing removes the type-aware oxlint
  regressions the first declarations caused in scripts/e2e implementations
- route .mts declaration edits under test/ to the testRoot lane and reference
  the test-root project from tsconfig.projects.json so tsgo:all covers it
  (closes both review findings against the lane wiring)

* chore(scripts): keep telegram runner dist typing structural for the boundary guard

* chore(types): declare runtime pack and gateway readiness exports added on main

* test: pin the importTargetPlan form of the plugin-contract plan import

The guard expectation still referenced the raw await import( form that
7ae5996bb3 (#103975) replaced with the importTargetPlan fallback helper;
the assertion fails on current main.
2026-07-11 06:15:41 -07:00

310 lines
9.9 KiB
TypeScript

// Gateway Network Client tests cover gateway network client script behavior.
import { EventEmitter } from "node:events";
import { describe, expect, it, vi } from "vitest";
import {
type GatewayFrame,
assertGatewaySuspendingError,
assertReadySuspensionResponse,
assertSuspendedProbes,
runGatewayNetworkClient,
} from "../../scripts/e2e/lib/gateway-network/client.mjs";
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", () => {
function healthResponse() {
return {
ok: true,
payload: {
agents: [],
channelOrder: [],
channels: {},
defaultAgentId: "codex",
durationMs: 3,
ok: true,
sessions: { count: 0, path: "/state/sessions", recent: [] },
ts: Date.now(),
},
};
}
it("rejects loose client timeout env values instead of parsing prefixes", () => {
expect(() =>
readGatewayNetworkClientConnectTimeoutMs({
OPENCLAW_GATEWAY_NETWORK_CLIENT_CONNECT_TIMEOUT_MS: "100ms",
}),
).toThrow("invalid OPENCLAW_GATEWAY_NETWORK_CLIENT_CONNECT_TIMEOUT_MS: 100ms");
expect(() =>
readGatewayNetworkClientConnectTimeoutMs({
OPENCLAW_GATEWAY_NETWORK_CONNECT_READY_TIMEOUT_MS: "1e3",
}),
).toThrow("invalid OPENCLAW_GATEWAY_NETWORK_CONNECT_READY_TIMEOUT_MS: 1e3");
expect(() =>
readGatewayNetworkClientConnectTimeoutMs({
OPENCLAW_GATEWAY_NETWORK_CLIENT_CONNECT_TIMEOUT_MS: "0",
}),
).toThrow("invalid OPENCLAW_GATEWAY_NETWORK_CLIENT_CONNECT_TIMEOUT_MS: 0");
});
it("prefers the explicit client timeout over the connect-ready fallback", () => {
expect(
readGatewayNetworkClientConnectTimeoutMs({
OPENCLAW_GATEWAY_NETWORK_CLIENT_CONNECT_TIMEOUT_MS: "5000",
OPENCLAW_GATEWAY_NETWORK_CONNECT_READY_TIMEOUT_MS: "1000",
}),
).toBe(5000);
expect(
readGatewayNetworkClientConnectTimeoutMs({
OPENCLAW_GATEWAY_NETWORK_CONNECT_READY_TIMEOUT_MS: "3000",
}),
).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();
});
function createNetworkClientHarness(
responses: Array<{ error?: { message?: string }; ok: boolean }>,
) {
const frames = [...responses];
const sentMethods: string[] = [];
const stdout: string[] = [];
let closeCount = 0;
const socket = {
close: () => {
closeCount += 1;
},
send: (payload: string) => {
sentMethods.push(JSON.parse(payload).method);
},
};
return {
get closeCount() {
return closeCount;
},
sentMethods,
stdout,
deps: {
delay: async () => {},
onceFrame: async (
_ws: unknown,
predicate: (frame: GatewayFrame) => boolean,
_timeoutMs?: number,
) => {
const frame = {
type: "res",
id: sentMethods.at(-1) === "connect" ? "c1" : "h1",
...frames.shift(),
};
expect(predicate(frame)).toBe(true);
return frame;
},
openSocket: async () => socket,
protocolVersion: 1,
stdout: (message: string) => {
stdout.push(message);
},
},
};
}
it("proves health after the authenticated connect handshake", async () => {
const harness = createNetworkClientHarness([{ ok: true }, healthResponse()]);
await runGatewayNetworkClient(
{ token: "test-token", url: "ws://127.0.0.1:12345", timeoutMs: 1000 },
harness.deps,
);
expect(harness.sentMethods).toEqual(["connect", "health"]);
expect(harness.stdout).toEqual(["ok"]);
expect(harness.closeCount).toBe(1);
});
it("bounds socket and frame waits by the client deadline", async () => {
const harness = createNetworkClientHarness([{ ok: true }, healthResponse()]);
const openSocket = vi.fn(harness.deps.openSocket);
const onceFrameMock = vi.fn(harness.deps.onceFrame);
await runGatewayNetworkClient(
{ token: "test-token", url: "ws://127.0.0.1:12345", timeoutMs: 250 },
{
...harness.deps,
onceFrame: onceFrameMock,
openSocket,
},
);
const openSocketCalls = openSocket.mock.calls as unknown as Array<[unknown, number]>;
const onceFrameCalls = onceFrameMock.mock.calls as unknown as Array<[unknown, unknown, number]>;
expect(openSocketCalls[0]?.[1]).toBeGreaterThan(0);
expect(openSocketCalls[0]?.[1]).toBeLessThanOrEqual(250);
expect(onceFrameCalls.map((call) => call[2])).toHaveLength(2);
for (const frameTimeoutMs of onceFrameCalls.map((call) => call[2])) {
expect(frameTimeoutMs).toBeGreaterThan(0);
expect(frameTimeoutMs).toBeLessThanOrEqual(250);
}
});
it("does not sleep past the remaining client deadline between retries", async () => {
const delays: number[] = [];
let now = 1_000;
const dateSpy = vi.spyOn(Date, "now").mockImplementation(() => now);
try {
await expect(
runGatewayNetworkClient(
{ token: "test-token", url: "ws://127.0.0.1:12345", timeoutMs: 250 },
{
delay: async (ms: number) => {
delays.push(ms);
now += ms;
},
openSocket: async () => {
now += 200;
throw new Error("ECONNREFUSED");
},
protocolVersion: 1,
stdout: () => {},
},
),
).rejects.toThrow("ECONNREFUSED");
} finally {
dateSpy.mockRestore();
}
expect(delays).toEqual([50]);
});
it("fails a connected socket whose health success lacks summary evidence", async () => {
const harness = createNetworkClientHarness([{ ok: true }, { ok: true }]);
await expect(
runGatewayNetworkClient(
{ token: "test-token", url: "ws://127.0.0.1:12345", timeoutMs: 1000 },
harness.deps,
),
).rejects.toThrow("health failed: missing health summary payload");
expect(harness.sentMethods).toEqual(["connect", "health"]);
expect(harness.stdout).toEqual([]);
expect(harness.closeCount).toBe(1);
});
it("fails a connected socket whose health probe fails", async () => {
const harness = createNetworkClientHarness([
{ ok: true },
{ ok: false, error: { message: "health unavailable" } },
]);
await expect(
runGatewayNetworkClient(
{ token: "test-token", url: "ws://127.0.0.1:12345", timeoutMs: 1000 },
harness.deps,
),
).rejects.toThrow("health failed: health unavailable");
expect(harness.sentMethods).toEqual(["connect", "health"]);
expect(harness.closeCount).toBe(1);
});
it("accepts only an idle future suspension lease", () => {
const payload = {
status: "ready",
suspensionId: "lease-1",
expiresAtMs: 2_000,
activeCount: 0,
blockers: [],
};
const response = (overrides = {}) => ({
status: 200,
body: { ok: true, payload: { ...payload, ...overrides } },
});
expect(assertReadySuspensionResponse(response(), 1_000)).toEqual(payload);
expect(() => assertReadySuspensionResponse(response({ expiresAtMs: 999 }), 1_000)).toThrow(
"expire in the future",
);
expect(() => assertReadySuspensionResponse(response({ activeCount: 1 }), 1_000)).toThrow(
"no active work",
);
});
it("requires canonical prepared-suspension RPC and probe evidence", () => {
const suspendedError = {
ok: false,
error: {
code: "UNAVAILABLE",
retryable: true,
details: { reason: "gateway-suspending", phase: "prepared" },
},
};
const health = { status: 200, body: { ok: true, status: "live" } };
const readiness = { status: 503, body: { ready: false, failing: ["gateway-draining"] } };
expect(() => assertGatewaySuspendingError(suspendedError)).not.toThrow();
expect(() => assertSuspendedProbes(health, readiness)).not.toThrow();
expect(() =>
assertGatewaySuspendingError({
...suspendedError,
error: {
...suspendedError.error,
details: { reason: "gateway-restarting", phase: "prepared" },
},
}),
).toThrow("identify gateway suspension");
expect(() =>
assertSuspendedProbes(health, {
status: 503,
body: { ready: false, failing: ["channels"] },
}),
).toThrow("identify gateway-draining");
});
});