





























1+// OpenClaw SDK tests cover transport behavior.
2+import { beforeEach, describe, expect, it, vi } from "vitest";
3+import { GatewayClientTransport } from "./transport.js";
4+5+type MockGatewayClientInstance = {
6+opts: {
7+onConnectError?: (error: Error) => void;
8+onHelloOk?: (hello: unknown) => void;
9+};
10+request: ReturnType<typeof vi.fn>;
11+start: ReturnType<typeof vi.fn>;
12+stopAndWait: ReturnType<typeof vi.fn>;
13+};
14+15+const gatewayClientMocks = vi.hoisted(() => ({
16+instances: [] as MockGatewayClientInstance[],
17+}));
18+19+vi.mock("@openclaw/gateway-client", () => ({
20+GatewayClient: class {
21+readonly opts: MockGatewayClientInstance["opts"];
22+readonly request = vi.fn();
23+readonly start = vi.fn();
24+readonly stopAndWait = vi.fn(async () => {});
25+26+constructor(opts: MockGatewayClientInstance["opts"]) {
27+this.opts = opts;
28+gatewayClientMocks.instances.push(this);
29+}
30+},
31+}));
32+33+describe("GatewayClientTransport", () => {
34+beforeEach(() => {
35+gatewayClientMocks.instances.length = 0;
36+});
37+38+it("rejects a pending connect when the transport closes before hello-ok", async () => {
39+const transport = new GatewayClientTransport();
40+41+const connect = transport.connect();
42+const connectExpectation = expect(connect).rejects.toThrow(
43+"gateway transport closed before connect completed",
44+);
45+const client = gatewayClientMocks.instances[0];
46+expect(client?.start).toHaveBeenCalledTimes(1);
47+48+await transport.close();
49+50+await connectExpectation;
51+expect(client?.stopAndWait).toHaveBeenCalledTimes(1);
52+});
53+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。