From 3830e687dd611f9f212f3438f91eeebf176c212a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 15 Apr 2026 12:30:16 +0100 Subject: [PATCH] test(perf): speed up slow gateway specs --- src/gateway/hooks.test.ts | 11 +++++++-- src/gateway/session-message-events.test.ts | 28 ++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/gateway/hooks.test.ts b/src/gateway/hooks.test.ts index 6e5f136755c..b96c9573417 100644 --- a/src/gateway/hooks.test.ts +++ b/src/gateway/hooks.test.ts @@ -1,6 +1,5 @@ import type { IncomingMessage } from "node:http"; import { afterEach, beforeEach, describe, expect, test } from "vitest"; -import { createIMessageTestPlugin } from "../../test/helpers/channels/imessage-test-plugin.js"; import type { OpenClawConfig } from "../config/config.js"; import { setActivePluginRegistry } from "../plugins/runtime.js"; import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js"; @@ -31,6 +30,14 @@ const createDemoAliasPlugin = () => ({ }, }); +const createIMessageAliasPlugin = () => ({ + ...createChannelTestPluginBase({ + id: "imessage", + label: "iMessage", + docsPath: "/channels/imessage", + }), +}); + describe("gateway hooks helpers", () => { const resolveHooksConfigOrThrow = (cfg: OpenClawConfig) => { const resolved = resolveHooksConfig(cfg); @@ -131,7 +138,7 @@ describe("gateway hooks helpers", () => { { pluginId: "imessage", source: "test", - plugin: createIMessageTestPlugin(), + plugin: createIMessageAliasPlugin(), }, ]), ); diff --git a/src/gateway/session-message-events.test.ts b/src/gateway/session-message-events.test.ts index d9b65f569b1..6d375f7dd2d 100644 --- a/src/gateway/session-message-events.test.ts +++ b/src/gateway/session-message-events.test.ts @@ -20,15 +20,22 @@ installGatewayTestHooks({ scope: "suite" }); const cleanupDirs: string[] = []; let harness: Awaited>; +let subscribedOperatorWs: + | Awaited>["openWs"]>> + | undefined; let previousMinimalGateway: string | undefined; beforeAll(async () => { previousMinimalGateway = process.env.OPENCLAW_TEST_MINIMAL_GATEWAY; delete process.env.OPENCLAW_TEST_MINIMAL_GATEWAY; harness = await createGatewaySuiteHarness(); + subscribedOperatorWs = await harness.openWs(); + await connectOk(subscribedOperatorWs, { scopes: ["operator.read"] }); + await rpcReq(subscribedOperatorWs, "sessions.subscribe"); }); afterAll(async () => { + subscribedOperatorWs?.close(); await harness.close(); if (previousMinimalGateway === undefined) { delete process.env.OPENCLAW_TEST_MINIMAL_GATEWAY; @@ -52,17 +59,12 @@ async function createSessionStoreFile(): Promise { } async function withOperatorSessionSubscriber( - harness: Awaited>, - run: (ws: Awaited>) => Promise, + run: (ws: NonNullable) => Promise, ) { - const ws = await harness.openWs(); - try { - await connectOk(ws, { scopes: ["operator.read"] }); - await rpcReq(ws, "sessions.subscribe"); - return await run(ws); - } finally { - ws.close(); + if (!subscribedOperatorWs) { + throw new Error("subscribed operator websocket is not ready"); } + return await run(subscribedOperatorWs); } function waitForSessionMessageEvent( @@ -157,7 +159,7 @@ describe("session.message websocket events", () => { storePath, }); - await withOperatorSessionSubscriber(harness, async (ws) => { + await withOperatorSessionSubscriber(async (ws) => { const changedEvent = onceMessage( ws, (message) => @@ -327,7 +329,7 @@ describe("session.message websocket events", () => { "utf-8", ); - await withOperatorSessionSubscriber(harness, async (ws) => { + await withOperatorSessionSubscriber(async (ws) => { const { messageEvent, changedEvent } = await emitTranscriptUpdateAndCollectEvents({ ws, sessionKey: "agent:main:main", @@ -487,7 +489,7 @@ describe("session.message websocket events", () => { "utf-8", ); - await withOperatorSessionSubscriber(harness, async (ws) => { + await withOperatorSessionSubscriber(async (ws) => { const { messageEvent, changedEvent } = await emitTranscriptUpdateAndCollectEvents({ ws, sessionKey: "agent:main:main", @@ -636,7 +638,7 @@ describe("session.message websocket events", () => { "utf-8", ); - await withOperatorSessionSubscriber(harness, async (ws) => { + await withOperatorSessionSubscriber(async (ws) => { const messageEventPromise = waitForSessionMessageEvent(ws, "agent:main:newer"); emitSessionTranscriptUpdate({