diff --git a/src/gateway/gateway-cli-backend.connect.test.ts b/src/gateway/gateway-cli-backend.connect.test.ts index 01b7a9858eb..b40de3de5d9 100644 --- a/src/gateway/gateway-cli-backend.connect.test.ts +++ b/src/gateway/gateway-cli-backend.connect.test.ts @@ -1,72 +1,17 @@ -import fs from "node:fs/promises"; -import os from "node:os"; -import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; -import { clearConfigCache, clearRuntimeConfigSnapshot } from "../config/config.js"; -import { clearSessionStoreCacheForTest } from "../config/sessions/store.js"; -import { captureEnv } from "../test-utils/env.js"; -import { - connectTestGatewayClient, - ensurePairedTestGatewayClientIdentity, - getFreeGatewayPort, -} from "./gateway-cli-backend.live-helpers.js"; -import { startGatewayServer } from "./server.js"; +import { describe, expect, it } from "vitest"; +import { connectTestGatewayClient } from "./gateway-cli-backend.live-helpers.js"; +import { getFreePort, installGatewayTestHooks, startGatewayServer } from "./test-helpers.js"; -const GATEWAY_CONNECT_TIMEOUT_MS = 90_000; +const GATEWAY_CONNECT_TIMEOUT_MS = 10_000; describe("gateway cli backend connect", () => { - afterEach(() => { - clearRuntimeConfigSnapshot(); - clearConfigCache(); - clearSessionStoreCacheForTest(); - }); + installGatewayTestHooks(); it( "connects a same-process test gateway client in minimal mode", async () => { - const envSnapshot = captureEnv([ - "HOME", - "OPENCLAW_STATE_DIR", - "OPENCLAW_CONFIG_PATH", - "OPENCLAW_GATEWAY_TOKEN", - "OPENCLAW_SKIP_CHANNELS", - "OPENCLAW_SKIP_PROVIDERS", - "OPENCLAW_SKIP_GMAIL_WATCHER", - "OPENCLAW_SKIP_CRON", - "OPENCLAW_SKIP_CANVAS_HOST", - "OPENCLAW_SKIP_BROWSER_CONTROL_SERVER", - "OPENCLAW_BUNDLED_PLUGINS_DIR", - "OPENCLAW_TEST_MINIMAL_GATEWAY", - ]); - - const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-connect-home-")); - const configPath = path.join(tempHome, ".openclaw", "openclaw.json"); - const bundledPluginsDir = path.join(tempHome, "openclaw-test-no-bundled-extensions"); const token = `test-${Date.now()}`; - process.env.HOME = tempHome; - process.env.OPENCLAW_STATE_DIR = path.join(tempHome, ".openclaw"); - process.env.OPENCLAW_CONFIG_PATH = configPath; - process.env.OPENCLAW_GATEWAY_TOKEN = token; - process.env.OPENCLAW_SKIP_CHANNELS = "1"; - process.env.OPENCLAW_SKIP_PROVIDERS = "1"; - process.env.OPENCLAW_SKIP_GMAIL_WATCHER = "1"; - process.env.OPENCLAW_SKIP_CRON = "1"; - process.env.OPENCLAW_SKIP_CANVAS_HOST = "1"; - process.env.OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1"; - process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledPluginsDir; - process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = "1"; - await fs.mkdir(path.dirname(configPath), { recursive: true }); - await fs.mkdir(bundledPluginsDir, { recursive: true }); - await fs.writeFile( - configPath, - `${JSON.stringify({ gateway: { auth: { mode: "token", token } } }, null, 2)}\n`, - ); - clearRuntimeConfigSnapshot(); - clearConfigCache(); - clearSessionStoreCacheForTest(); - - const deviceIdentity = await ensurePairedTestGatewayClientIdentity(); - const port = await getFreeGatewayPort(); + const port = await getFreePort(); const server = await startGatewayServer(port, { bind: "loopback", auth: { mode: "token", token }, @@ -78,7 +23,9 @@ describe("gateway cli backend connect", () => { client = await connectTestGatewayClient({ url: `ws://127.0.0.1:${port}`, token, - deviceIdentity, + timeoutMs: 5_000, + maxAttemptTimeoutMs: 5_000, + requestTimeoutMs: 5_000, }); const health = await client.request("health", undefined, { timeoutMs: 5_000, @@ -89,8 +36,6 @@ describe("gateway cli backend connect", () => { } finally { await client?.stopAndWait({ timeoutMs: 1_000 }).catch(() => {}); await server.close({ reason: "gateway connect regression complete" }); - await fs.rm(tempHome, { recursive: true, force: true }); - envSnapshot.restore(); } }, GATEWAY_CONNECT_TIMEOUT_MS,