Skip to content

Commit 44e31f7

Browse files
committed
test(gateway): stabilize live helper shard
1 parent 63a06e3 commit 44e31f7

3 files changed

Lines changed: 27 additions & 49 deletions

File tree

src/gateway/gateway-cli-backend.connect.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { afterEach, describe, expect, it } from "vitest";
77
import { type RawData, WebSocketServer } from "ws";
88
import { PROTOCOL_VERSION } from "../../packages/gateway-protocol/src/index.js";
99
import { loadOrCreateDeviceIdentity } from "../infra/device-identity.js";
10+
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
1011
import { connectTestGatewayClient } from "./gateway-cli-backend.live-helpers.js";
1112

1213
const GATEWAY_CONNECT_TIMEOUT_MS = 5_000;
@@ -22,6 +23,7 @@ async function startMinimalGatewayServer(params: { token: string }) {
2223
const httpServer = createServer();
2324
const wss = new WebSocketServer({ server: httpServer });
2425
const requests: string[] = [];
26+
let connectParams: Record<string, unknown> | undefined;
2527

2628
wss.on("connection", (ws) => {
2729
ws.send(
@@ -43,6 +45,7 @@ async function startMinimalGatewayServer(params: { token: string }) {
4345
}
4446
requests.push(frame.method ?? "");
4547
if (frame.method === "connect") {
48+
connectParams = frame.params as Record<string, unknown> | undefined;
4649
expect(frame.params?.auth?.token).toBe(params.token);
4750
expect(frame.params?.device?.nonce).toBe("test-nonce");
4851
ws.send(
@@ -81,6 +84,9 @@ async function startMinimalGatewayServer(params: { token: string }) {
8184
const address = httpServer.address() as AddressInfo;
8285
return {
8386
requests,
87+
get connectParams() {
88+
return connectParams;
89+
},
8490
url: `ws://127.0.0.1:${address.port}`,
8591
close: async () => {
8692
for (const client of wss.clients) {
@@ -136,7 +142,12 @@ describe("gateway cli backend connect", () => {
136142
const health = await client.request("health", undefined, {
137143
timeoutMs: 1_000,
138144
});
145+
const connectClient = server.connectParams?.client as Record<string, unknown> | undefined;
139146
expect(health.ok).toBe(true);
147+
expect(connectClient?.id).toBe(GATEWAY_CLIENT_NAMES.TEST);
148+
expect(connectClient?.displayName).toBe("vitest-live");
149+
expect(connectClient?.version).toBe("dev");
150+
expect(connectClient?.mode).toBe(GATEWAY_CLIENT_MODES.TEST);
140151
expect(server.requests).toEqual(["connect", "health"]);
141152
} finally {
142153
await client?.stopAndWait({ timeoutMs: 1_000 }).catch(() => {});

src/gateway/gateway-cli-backend.live-helpers.test.ts

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
1-
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
22
import { testing as cliBackendsTesting } from "../agents/cli-backends.js";
3-
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
4-
5-
const gatewayClientState = vi.hoisted(() => ({
6-
lastOptions: undefined as Record<string, unknown> | undefined,
7-
}));
8-
9-
vi.mock("./client.js", () => ({
10-
GatewayClient: class MockGatewayClient {
11-
constructor(options: Record<string, unknown>) {
12-
gatewayClientState.lastOptions = options;
13-
}
14-
15-
start() {
16-
const options = gatewayClientState.lastOptions as
17-
| { onHelloOk?: (hello: { type: "hello-ok" }) => void }
18-
| undefined;
19-
queueMicrotask(() => options?.onHelloOk?.({ type: "hello-ok" }));
20-
}
21-
22-
async stopAndWait() {}
23-
},
24-
}));
253

264
vi.mock("./client-start-readiness.js", () => ({
275
startGatewayClientWhenEventLoopReady: async (client: { start: () => void }) => {
@@ -37,9 +15,13 @@ describe("gateway cli backend live helpers", () => {
3715
liveHelpers = await import("./gateway-cli-backend.live-helpers.js");
3816
});
3917

18+
beforeEach(() => {
19+
vi.useRealTimers();
20+
});
21+
4022
afterEach(() => {
23+
vi.useRealTimers();
4124
cliBackendsTesting.resetDepsForTest();
42-
gatewayClientState.lastOptions = undefined;
4325
delete process.env.OPENCLAW_SKIP_CHANNELS;
4426
delete process.env.OPENCLAW_SKIP_PROVIDERS;
4527
delete process.env.OPENCLAW_SKIP_GMAIL_WATCHER;
@@ -95,26 +77,6 @@ describe("gateway cli backend live helpers", () => {
9577
expect(process.env.ANTHROPIC_API_KEY_OLD).toBe("old-anthropic-old");
9678
});
9779

98-
it("builds the live gateway client with test identity defaults", async () => {
99-
const { connectTestGatewayClient } = await import("./gateway-cli-backend.live-helpers.js");
100-
101-
const client = await connectTestGatewayClient({
102-
url: "ws://127.0.0.1:18789",
103-
token: "gateway-token",
104-
});
105-
106-
expect(client.start).toBeTypeOf("function");
107-
expect(client.stopAndWait).toBeTypeOf("function");
108-
expect(gatewayClientState.lastOptions?.url).toBe("ws://127.0.0.1:18789");
109-
expect(gatewayClientState.lastOptions?.token).toBe("gateway-token");
110-
expect(gatewayClientState.lastOptions?.clientName).toBe(GATEWAY_CLIENT_NAMES.TEST);
111-
expect(gatewayClientState.lastOptions?.clientDisplayName).toBe("vitest-live");
112-
expect(gatewayClientState.lastOptions?.clientVersion).toBe("dev");
113-
expect(gatewayClientState.lastOptions?.mode).toBe(GATEWAY_CLIENT_MODES.TEST);
114-
expect(gatewayClientState.lastOptions?.connectChallengeTimeoutMs).toBe(45_000);
115-
expect(gatewayClientState.lastOptions).not.toHaveProperty("requestTimeoutMs");
116-
});
117-
11880
it("defaults the model switch probe to Claude Sonnet -> Opus", async () => {
11981
const { resolveCliModelSwitchProbeTarget, shouldRunCliModelSwitchProbe } =
12082
await import("./gateway-cli-backend.live-helpers.js");

src/gateway/gateway-cli-backend.live-probe-helpers.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ServerResponse,
55
} from "node:http";
66
import { createServer as createTcpServer, type Server, type Socket } from "node:net";
7-
import { afterEach, describe, expect, it } from "vitest";
7+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
88
import { verifyCliCronMcpLoopbackPreflight } from "./gateway-cli-backend.live-probe-helpers.js";
99
import {
1010
clearActiveMcpLoopbackRuntimeByOwnerToken,
@@ -58,11 +58,16 @@ function preflightParams(env: NodeJS.ProcessEnv = {}) {
5858
};
5959
}
6060

61-
afterEach(() => {
62-
clearActiveMcpLoopbackRuntimeByOwnerToken(ownerToken);
63-
});
64-
6561
describe("gateway CLI backend live probe helpers", () => {
62+
beforeEach(() => {
63+
vi.useRealTimers();
64+
});
65+
66+
afterEach(() => {
67+
vi.useRealTimers();
68+
clearActiveMcpLoopbackRuntimeByOwnerToken(ownerToken);
69+
});
70+
6671
it("reads loopback JSON-RPC responses without invoking cron verification when cron is absent", async () => {
6772
const methods: string[] = [];
6873
const server = createHttpServer((request, response) => {

0 commit comments

Comments
 (0)