From 20e4d42db3de93efdbdb6eddca14672f726fcfd9 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 30 Mar 2026 13:35:37 +0900 Subject: [PATCH] fix(test): trim onboarding post-write registry imports --- .../onboard-channels.post-write.test.ts | 112 +++++++++++++++++- 1 file changed, 106 insertions(+), 6 deletions(-) diff --git a/src/commands/onboard-channels.post-write.test.ts b/src/commands/onboard-channels.post-write.test.ts index f96dd276e22..cdfdfe1c692 100644 --- a/src/commands/onboard-channels.post-write.test.ts +++ b/src/commands/onboard-channels.post-write.test.ts @@ -1,10 +1,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; +import { getChannelSetupWizardAdapter } from "./channel-setup/registry.js"; +import type { ChannelSetupWizardAdapter } from "./channel-setup/types.js"; +import { setActivePluginRegistry } from "../plugins/runtime.js"; +import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js"; import type { OpenClawConfig } from "../config/config.js"; import type { WizardPrompter } from "../wizard/prompts.js"; -import { - patchChannelOnboardingAdapter, - setDefaultChannelPluginRegistryForTests, -} from "./channel-test-helpers.js"; import { createChannelOnboardingPostWriteHookCollector, runCollectedChannelOnboardingPostWriteHooks, @@ -12,6 +12,106 @@ import { } from "./onboard-channels.js"; import { createExitThrowingRuntime, createWizardPrompter } from "./test-wizard-helpers.js"; +function setMinimalTelegramOnboardingRegistryForTests(): void { + setActivePluginRegistry( + createTestRegistry([ + { + pluginId: "telegram", + source: "test", + plugin: { + ...createChannelTestPluginBase({ + id: "telegram", + label: "Telegram", + capabilities: { chatTypes: ["direct", "group"] }, + }), + setup: { + applyAccountConfig: ({ cfg }: { cfg: OpenClawConfig }) => cfg, + }, + setupWizard: { + channel: "telegram", + status: { + configuredLabel: "Configured", + unconfiguredLabel: "Not configured", + resolveConfigured: ({ cfg }: { cfg: OpenClawConfig }) => + Boolean(cfg.channels?.telegram?.botToken), + }, + credentials: [], + }, + }, + }, + ]), + ); +} + +type ChannelSetupWizardAdapterPatch = Partial< + Pick< + ChannelSetupWizardAdapter, + | "afterConfigWritten" + | "configure" + | "configureInteractive" + | "configureWhenConfigured" + | "getStatus" + > +>; + +type PatchedSetupAdapterFields = { + afterConfigWritten?: ChannelSetupWizardAdapter["afterConfigWritten"]; + configure?: ChannelSetupWizardAdapter["configure"]; + configureInteractive?: ChannelSetupWizardAdapter["configureInteractive"]; + configureWhenConfigured?: ChannelSetupWizardAdapter["configureWhenConfigured"]; + getStatus?: ChannelSetupWizardAdapter["getStatus"]; +}; + +function patchChannelOnboardingAdapterForTest( + patch: ChannelSetupWizardAdapterPatch, +): () => void { + const adapter = getChannelSetupWizardAdapter("telegram"); + if (!adapter) { + throw new Error("missing setup adapter for telegram"); + } + + const previous: PatchedSetupAdapterFields = {}; + + if (Object.prototype.hasOwnProperty.call(patch, "getStatus")) { + previous.getStatus = adapter.getStatus; + adapter.getStatus = patch.getStatus ?? adapter.getStatus; + } + if (Object.prototype.hasOwnProperty.call(patch, "afterConfigWritten")) { + previous.afterConfigWritten = adapter.afterConfigWritten; + adapter.afterConfigWritten = patch.afterConfigWritten; + } + if (Object.prototype.hasOwnProperty.call(patch, "configure")) { + previous.configure = adapter.configure; + adapter.configure = patch.configure ?? adapter.configure; + } + if (Object.prototype.hasOwnProperty.call(patch, "configureInteractive")) { + previous.configureInteractive = adapter.configureInteractive; + adapter.configureInteractive = patch.configureInteractive; + } + if (Object.prototype.hasOwnProperty.call(patch, "configureWhenConfigured")) { + previous.configureWhenConfigured = adapter.configureWhenConfigured; + adapter.configureWhenConfigured = patch.configureWhenConfigured; + } + + return () => { + if (Object.prototype.hasOwnProperty.call(patch, "getStatus")) { + adapter.getStatus = previous.getStatus!; + } + if (Object.prototype.hasOwnProperty.call(patch, "afterConfigWritten")) { + adapter.afterConfigWritten = previous.afterConfigWritten; + } + if (Object.prototype.hasOwnProperty.call(patch, "configure")) { + adapter.configure = previous.configure!; + } + if (Object.prototype.hasOwnProperty.call(patch, "configureInteractive")) { + adapter.configureInteractive = previous.configureInteractive; + } + if (Object.prototype.hasOwnProperty.call(patch, "configureWhenConfigured")) { + adapter.configureWhenConfigured = previous.configureWhenConfigured; + } + }; +} + function createPrompter(overrides: Partial): WizardPrompter { return createWizardPrompter( { @@ -45,7 +145,7 @@ function createUnexpectedQuickstartPrompter(select: WizardPrompter["select"]) { describe("setupChannels post-write hooks", () => { beforeEach(() => { - setDefaultChannelPluginRegistryForTests(); + setMinimalTelegramOnboardingRegistryForTests(); }); it("collects onboarding post-write hooks and runs them against the final config", async () => { @@ -61,7 +161,7 @@ describe("setupChannels post-write hooks", () => { } as OpenClawConfig, accountId: "acct-1", })); - const restore = patchChannelOnboardingAdapter("telegram", { + const restore = patchChannelOnboardingAdapterForTest({ configureInteractive, afterConfigWritten, getStatus: vi.fn(async ({ cfg }: { cfg: OpenClawConfig }) => ({