Skip to content

Commit d9cbb7d

Browse files
authored
test(gateway): own session fixture setup and cleanup (#132574)
1 parent 265d34e commit d9cbb7d

9 files changed

Lines changed: 623 additions & 81 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { afterEach, expect, test, vi } from "vitest";
2+
import { captureEnv } from "../test-utils/env.js";
3+
import { startGatewayServerHarness } from "./server.e2e-ws-harness.js";
4+
5+
const startup = vi.hoisted(() => ({
6+
port: vi.fn<() => Promise<number>>(),
7+
server: vi.fn<() => Promise<never>>(),
8+
}));
9+
vi.mock("./test-helpers.js", () => ({
10+
getGatewayTestPort: startup.port,
11+
startTestGatewayServer: startup.server,
12+
connectOk: vi.fn(),
13+
trackConnectChallengeNonce: vi.fn(),
14+
}));
15+
16+
afterEach(() => {
17+
vi.resetAllMocks();
18+
});
19+
20+
test.each(["port", "server"] as const)(
21+
"restores its token snapshot when %s acquisition fails before publishing a close handle",
22+
async (stage) => {
23+
const env = captureEnv(["OPENCLAW_GATEWAY_TOKEN"]);
24+
const failure = new Error(`injected ${stage} acquisition failure`);
25+
process.env.OPENCLAW_GATEWAY_TOKEN = "fixture-token";
26+
startup.port.mockResolvedValue(12345);
27+
startup.server.mockRejectedValue(failure);
28+
if (stage === "port") {
29+
startup.port.mockRejectedValue(failure);
30+
}
31+
try {
32+
await expect(startGatewayServerHarness()).rejects.toBe(failure);
33+
expect(process.env.OPENCLAW_GATEWAY_TOKEN).toBe("fixture-token");
34+
} finally {
35+
env.restore();
36+
}
37+
},
38+
);

‎src/gateway/server.e2e-ws-harness.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ export type GatewayServerHarness = {
2323

2424
/** Start a loopback Gateway server with a helper for opening authenticated test clients. */
2525
export async function startGatewayServerHarness(): Promise<GatewayServerHarness> {
26+
const port = await getGatewayTestPort();
2627
const envSnapshot = captureEnv(["OPENCLAW_GATEWAY_TOKEN"]);
2728
const clients = new Set<WebSocket>();
2829
delete process.env.OPENCLAW_GATEWAY_TOKEN;
29-
const port = await getGatewayTestPort();
3030
const server = await startTestGatewayServer(port, {
3131
auth: { mode: "none" },
3232
bind: "loopback",
3333
controlUiEnabled: false,
34+
}).catch((error: unknown) => {
35+
envSnapshot.restore();
36+
throw error;
3437
});
3538

3639
const openClient = async (opts?: Parameters<typeof connectOk>[1]): Promise<GatewayWsClient> => {

‎src/gateway/server.sessions.create.test.ts‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "node:fs/promises";
66
import os from "node:os";
77
import path from "node:path";
88
import { promisify } from "node:util";
9-
import { afterAll, afterEach, beforeAll, beforeEach, expect, test, vi } from "vitest";
9+
import { afterEach, beforeEach, expect, test, vi } from "vitest";
1010
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
1111
import { getContextWindowCaches } from "../agents/context-cache.js";
1212
import {
@@ -163,13 +163,14 @@ vi.mock("./session-transcript-readers.js", async (importOriginal) => {
163163
return { ...actual, readSessionMessageCountAsync: sessionTranscriptReaderMocks.readCount };
164164
});
165165

166+
let gitWorkspaceTemplate: string;
166167
const { createSessionStoreDir, createSelectedGlobalSessionStore, openClient } =
167-
setupGatewaySessionsTestHarness();
168+
setupGatewaySessionsTestHarness(async (makeTempDir) => {
169+
gitWorkspaceTemplate = await createGitWorkspace(makeTempDir("openclaw-session-git-template-"));
170+
});
168171
const execFileAsync = promisify(execFile);
169172
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
170173
const directoryLinkType = process.platform === "win32" ? "junction" : "dir";
171-
let gitWorkspaceTemplateRoot: string;
172-
let gitWorkspaceTemplate: string;
173174

174175
async function waitForCreatedSessionRun(
175176
context: { chatAbortControllers: Map<string, ChatAbortControllerEntry> },
@@ -195,17 +196,6 @@ async function waitForCreatedSessionRun(
195196
return removed;
196197
}
197198

198-
beforeAll(async () => {
199-
gitWorkspaceTemplateRoot = await fs.realpath(
200-
await fs.mkdtemp(path.join(await fs.realpath(os.tmpdir()), "openclaw-session-git-template-")),
201-
);
202-
gitWorkspaceTemplate = await createGitWorkspace(gitWorkspaceTemplateRoot);
203-
});
204-
205-
afterAll(async () => {
206-
await fs.rm(gitWorkspaceTemplateRoot, { recursive: true, force: true });
207-
});
208-
209199
// Read the real implementations back here rather than capturing them inside the
210200
// mock factories: Vitest runs a factory on first import of the mocked module, and
211201
// this project is `isolate: false`, so on a warm module graph a factory can still

0 commit comments

Comments
 (0)