From 1f256306c96393342b4e95668d748bf9d0252949 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 27 Apr 2026 20:58:40 +0100 Subject: [PATCH] test: align gateway tests with config io split --- src/gateway/server-startup-plugins.ts | 24 +++-- ...artup-matrix-migration.integration.test.ts | 91 +++++++++---------- ...essage-handler.post-connect-health.test.ts | 1 - src/gateway/startup-auth.test.ts | 10 +- 4 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/gateway/server-startup-plugins.ts b/src/gateway/server-startup-plugins.ts index 64d62f02ea8..70af38709e5 100644 --- a/src/gateway/server-startup-plugins.ts +++ b/src/gateway/server-startup-plugins.ts @@ -25,6 +25,19 @@ type GatewayPluginBootstrapLog = { debug: (message: string) => void; }; +export function resolveGatewayStartupMaintenanceConfig(params: { + cfgAtStart: OpenClawConfig; + startupRuntimeConfig: OpenClawConfig; +}): OpenClawConfig { + return params.cfgAtStart.channels === undefined && + params.startupRuntimeConfig.channels !== undefined + ? { + ...params.cfgAtStart, + channels: params.startupRuntimeConfig.channels, + } + : params.cfgAtStart; +} + async function prestageGatewayBundledRuntimeDeps(params: { cfg: OpenClawConfig; pluginIds: readonly string[]; @@ -101,13 +114,10 @@ export async function prepareGatewayPluginBootstrap(params: { log: GatewayPluginBootstrapLog; }) { const activationSourceConfig = params.activationSourceConfig ?? params.cfgAtStart; - const startupMaintenanceConfig = - params.cfgAtStart.channels === undefined && params.startupRuntimeConfig.channels !== undefined - ? { - ...params.cfgAtStart, - channels: params.startupRuntimeConfig.channels, - } - : params.cfgAtStart; + const startupMaintenanceConfig = resolveGatewayStartupMaintenanceConfig({ + cfgAtStart: params.cfgAtStart, + startupRuntimeConfig: params.startupRuntimeConfig, + }); const shouldRunStartupMaintenance = !params.minimalTestGateway || startupMaintenanceConfig.channels !== undefined; diff --git a/src/gateway/server.startup-matrix-migration.integration.test.ts b/src/gateway/server.startup-matrix-migration.integration.test.ts index ba79c7ef1d7..f77af5008fa 100644 --- a/src/gateway/server.startup-matrix-migration.integration.test.ts +++ b/src/gateway/server.startup-matrix-migration.integration.test.ts @@ -1,33 +1,9 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; - -const runChannelPluginStartupMaintenanceMock = vi.hoisted(() => - vi.fn().mockResolvedValue(undefined), -); - -vi.mock("../channels/plugins/lifecycle-startup.js", () => ({ - runChannelPluginStartupMaintenance: (params: unknown) => - runChannelPluginStartupMaintenanceMock(params), -})); - -vi.mock("../agents/agent-scope.js", () => ({ - resolveAgentWorkspaceDir: () => "/workspace", - resolveDefaultAgentId: () => "default", -})); - -vi.mock("../agents/subagent-registry.js", () => ({ - initSubagentRegistry: vi.fn(), -})); +import { describe, expect, it } from "vitest"; +import { resolveGatewayStartupMaintenanceConfig } from "./server-startup-plugins.js"; describe("gateway startup channel maintenance wiring", () => { - beforeEach(() => { - vi.resetModules(); - runChannelPluginStartupMaintenanceMock.mockClear(); - }); - - it("runs startup channel maintenance with the resolved startup config", async () => { - const { prepareGatewayPluginBootstrap } = await import("./server-startup-plugins.js"); - - await prepareGatewayPluginBootstrap({ + it("uses channels from the resolved startup config when startup config repaired them", () => { + const resolved = resolveGatewayStartupMaintenanceConfig({ cfgAtStart: { plugins: { enabled: true }, }, @@ -41,30 +17,45 @@ describe("gateway startup channel maintenance wiring", () => { }, }, }, - minimalTestGateway: true, - log: { - info: vi.fn(), - warn: vi.fn(), - error: vi.fn(), - debug: vi.fn(), + }); + + expect(resolved.channels).toEqual({ + matrix: { + homeserver: "https://matrix.example.org", + userId: "@bot:example.org", + accessToken: "tok-123", + }, + }); + }); + + it("preserves explicit startup channel config", () => { + const resolved = resolveGatewayStartupMaintenanceConfig({ + cfgAtStart: { + plugins: { enabled: true }, + channels: { + matrix: { + homeserver: "https://matrix.original.example", + userId: "@original:example.org", + accessToken: "original-token", + }, + }, + }, + startupRuntimeConfig: { + plugins: { enabled: true }, + channels: { + matrix: { + homeserver: "https://matrix.repaired.example", + userId: "@repaired:example.org", + accessToken: "repaired-token", + }, + }, }, }); - expect(runChannelPluginStartupMaintenanceMock).toHaveBeenCalledTimes(1); - expect(runChannelPluginStartupMaintenanceMock).toHaveBeenCalledWith( - expect.objectContaining({ - cfg: expect.objectContaining({ - channels: expect.objectContaining({ - matrix: expect.objectContaining({ - homeserver: "https://matrix.example.org", - userId: "@bot:example.org", - accessToken: "tok-123", - }), - }), - }), - env: process.env, - log: expect.anything(), - }), - ); + expect(resolved.channels?.matrix).toEqual({ + homeserver: "https://matrix.original.example", + userId: "@original:example.org", + accessToken: "original-token", + }); }); }); diff --git a/src/gateway/server/ws-connection/message-handler.post-connect-health.test.ts b/src/gateway/server/ws-connection/message-handler.post-connect-health.test.ts index 7f153467675..bf74c29a4cf 100644 --- a/src/gateway/server/ws-connection/message-handler.post-connect-health.test.ts +++ b/src/gateway/server/ws-connection/message-handler.post-connect-health.test.ts @@ -48,7 +48,6 @@ vi.mock("../../../config/config.js", () => ({ vi.mock("../../../config/io.js", () => ({ getRuntimeConfig: loadConfigMock, })); - vi.mock("../../../infra/system-presence.js", () => ({ upsertPresence: upsertPresenceMock, })); diff --git a/src/gateway/startup-auth.test.ts b/src/gateway/startup-auth.test.ts index f5dd6cdb039..0432e6a773b 100644 --- a/src/gateway/startup-auth.test.ts +++ b/src/gateway/startup-auth.test.ts @@ -12,13 +12,9 @@ const mocks = vi.hoisted(() => ({ replaceConfigFile: vi.fn(async (_params: { nextConfig: OpenClawConfig }) => {}), })); -vi.mock("../config/config.js", async () => { - const actual = await vi.importActual("../config/config.js"); - return { - ...actual, - replaceConfigFile: mocks.replaceConfigFile, - }; -}); +vi.mock("../config/mutate.js", () => ({ + replaceConfigFile: mocks.replaceConfigFile, +})); vi.mock("../config/mutate.js", async () => { const actual = await vi.importActual("../config/mutate.js");