|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { clearConfigCache, clearRuntimeConfigSnapshot } from "../config/config.js"; |
| 6 | +import { clearSessionStoreCacheForTest } from "../config/sessions/store.js"; |
| 7 | +import { captureEnv } from "../test-utils/env.js"; |
| 8 | +import { |
| 9 | + connectTestGatewayClient, |
| 10 | + ensurePairedTestGatewayClientIdentity, |
| 11 | + getFreeGatewayPort, |
| 12 | +} from "./gateway-cli-backend.live-helpers.js"; |
| 13 | +import { startGatewayServer } from "./server.js"; |
| 14 | + |
| 15 | +const GATEWAY_CONNECT_TIMEOUT_MS = 90_000; |
| 16 | + |
| 17 | +describe("gateway cli backend connect", () => { |
| 18 | + afterEach(() => { |
| 19 | + clearRuntimeConfigSnapshot(); |
| 20 | + clearConfigCache(); |
| 21 | + clearSessionStoreCacheForTest(); |
| 22 | + }); |
| 23 | + |
| 24 | + it( |
| 25 | + "connects a same-process test gateway client in minimal mode", |
| 26 | + async () => { |
| 27 | + const envSnapshot = captureEnv([ |
| 28 | + "HOME", |
| 29 | + "OPENCLAW_STATE_DIR", |
| 30 | + "OPENCLAW_CONFIG_PATH", |
| 31 | + "OPENCLAW_GATEWAY_TOKEN", |
| 32 | + "OPENCLAW_SKIP_CHANNELS", |
| 33 | + "OPENCLAW_SKIP_PROVIDERS", |
| 34 | + "OPENCLAW_SKIP_GMAIL_WATCHER", |
| 35 | + "OPENCLAW_SKIP_CRON", |
| 36 | + "OPENCLAW_SKIP_CANVAS_HOST", |
| 37 | + "OPENCLAW_SKIP_BROWSER_CONTROL_SERVER", |
| 38 | + "OPENCLAW_BUNDLED_PLUGINS_DIR", |
| 39 | + "OPENCLAW_TEST_MINIMAL_GATEWAY", |
| 40 | + ]); |
| 41 | + |
| 42 | + const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-connect-home-")); |
| 43 | + const configPath = path.join(tempHome, ".openclaw", "openclaw.json"); |
| 44 | + const bundledPluginsDir = path.join(tempHome, "openclaw-test-no-bundled-extensions"); |
| 45 | + const token = `test-${Date.now()}`; |
| 46 | + process.env.HOME = tempHome; |
| 47 | + process.env.OPENCLAW_STATE_DIR = path.join(tempHome, ".openclaw"); |
| 48 | + process.env.OPENCLAW_CONFIG_PATH = configPath; |
| 49 | + process.env.OPENCLAW_GATEWAY_TOKEN = token; |
| 50 | + process.env.OPENCLAW_SKIP_CHANNELS = "1"; |
| 51 | + process.env.OPENCLAW_SKIP_PROVIDERS = "1"; |
| 52 | + process.env.OPENCLAW_SKIP_GMAIL_WATCHER = "1"; |
| 53 | + process.env.OPENCLAW_SKIP_CRON = "1"; |
| 54 | + process.env.OPENCLAW_SKIP_CANVAS_HOST = "1"; |
| 55 | + process.env.OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1"; |
| 56 | + process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = bundledPluginsDir; |
| 57 | + process.env.OPENCLAW_TEST_MINIMAL_GATEWAY = "1"; |
| 58 | + await fs.mkdir(path.dirname(configPath), { recursive: true }); |
| 59 | + await fs.mkdir(bundledPluginsDir, { recursive: true }); |
| 60 | + await fs.writeFile( |
| 61 | + configPath, |
| 62 | + `${JSON.stringify({ gateway: { auth: { mode: "token", token } } }, null, 2)}\n`, |
| 63 | + ); |
| 64 | + clearRuntimeConfigSnapshot(); |
| 65 | + clearConfigCache(); |
| 66 | + clearSessionStoreCacheForTest(); |
| 67 | + |
| 68 | + const deviceIdentity = await ensurePairedTestGatewayClientIdentity(); |
| 69 | + const port = await getFreeGatewayPort(); |
| 70 | + const server = await startGatewayServer(port, { |
| 71 | + bind: "loopback", |
| 72 | + auth: { mode: "token", token }, |
| 73 | + controlUiEnabled: false, |
| 74 | + }); |
| 75 | + let client: Awaited<ReturnType<typeof connectTestGatewayClient>> | undefined; |
| 76 | + |
| 77 | + try { |
| 78 | + client = await connectTestGatewayClient({ |
| 79 | + url: `ws://127.0.0.1:${port}`, |
| 80 | + token, |
| 81 | + deviceIdentity, |
| 82 | + }); |
| 83 | + const health = await client.request<{ ok?: boolean }>("health", undefined, { |
| 84 | + timeoutMs: 5_000, |
| 85 | + }); |
| 86 | + expect(health).toMatchObject({ |
| 87 | + ok: true, |
| 88 | + }); |
| 89 | + } finally { |
| 90 | + await client?.stopAndWait({ timeoutMs: 1_000 }).catch(() => {}); |
| 91 | + await server.close({ reason: "gateway connect regression complete" }); |
| 92 | + await fs.rm(tempHome, { recursive: true, force: true }); |
| 93 | + envSnapshot.restore(); |
| 94 | + } |
| 95 | + }, |
| 96 | + GATEWAY_CONNECT_TIMEOUT_MS, |
| 97 | + ); |
| 98 | +}); |
0 commit comments