diff --git a/src/commands/channels.add.test.ts b/src/commands/channels.add.test.ts index 2e6bfea3bc1..d6d139e968d 100644 --- a/src/commands/channels.add.test.ts +++ b/src/commands/channels.add.test.ts @@ -7,13 +7,13 @@ import { ensureChannelSetupPluginInstalled, loadChannelSetupPluginRegistrySnapshotForChannel, } from "./channel-setup/plugin-install.js"; -import { setDefaultChannelPluginRegistryForTests } from "./channel-test-helpers.js"; import { configMocks, offsetMocks } from "./channels.mock-harness.js"; import { createMSTeamsCatalogEntry, createMSTeamsSetupPlugin, } from "./channels.plugin-install.test-helpers.js"; import { baseConfigSnapshot, createTestRuntime } from "./test-runtime-config-helpers.js"; +import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js"; const catalogMocks = vi.hoisted(() => ({ listChannelPluginCatalogEntries: vi.fn((): ChannelPluginCatalogEntry[] => []), @@ -49,6 +49,124 @@ vi.mock("./channel-setup/plugin-install.js", async (importOriginal) => { const runtime = createTestRuntime(); let channelsAddCommand: typeof import("./channels.js").channelsAddCommand; +function listConfiguredAccountIds( + channelConfig: { accounts?: Record; botToken?: string } | undefined, +): string[] { + const accountIds = Object.keys(channelConfig?.accounts ?? {}); + if (accountIds.length > 0) { + return accountIds; + } + if (channelConfig?.botToken) { + return [DEFAULT_ACCOUNT_ID]; + } + return []; +} + +function createTelegramAddTestPlugin(): ChannelPlugin { + const resolveTelegramAccount = ( + cfg: Parameters>[0], + accountId: string, + ) => { + const telegram = cfg.channels?.telegram as + | { + botToken?: string; + enabled?: boolean; + accounts?: Record; + } + | undefined; + const resolvedAccountId = accountId || DEFAULT_ACCOUNT_ID; + const scoped = telegram?.accounts?.[resolvedAccountId]; + return { + token: String(scoped?.botToken ?? telegram?.botToken ?? ""), + enabled: + typeof scoped?.enabled === "boolean" + ? scoped.enabled + : typeof telegram?.enabled === "boolean" + ? telegram.enabled + : true, + }; + }; + + return { + ...createChannelTestPluginBase({ + id: "telegram", + label: "Telegram", + docsPath: "/channels/telegram", + }), + config: { + listAccountIds: (cfg) => + listConfiguredAccountIds( + cfg.channels?.telegram as { accounts?: Record; botToken?: string } | undefined, + ), + resolveAccount: resolveTelegramAccount, + }, + setup: { + resolveAccountId: ({ accountId }) => accountId || DEFAULT_ACCOUNT_ID, + applyAccountConfig: ({ cfg, accountId, input }) => { + const telegram = (cfg.channels?.telegram as + | { + enabled?: boolean; + botToken?: string; + accounts?: Record; + } + | undefined) ?? { enabled: true }; + const resolvedAccountId = accountId || DEFAULT_ACCOUNT_ID; + if (resolvedAccountId === DEFAULT_ACCOUNT_ID) { + return { + ...cfg, + channels: { + ...cfg.channels, + telegram: { + ...telegram, + enabled: true, + ...(input.token ? { botToken: input.token } : {}), + }, + }, + }; + } + return { + ...cfg, + channels: { + ...cfg.channels, + telegram: { + ...telegram, + enabled: true, + accounts: { + ...(telegram.accounts ?? {}), + [resolvedAccountId]: { + ...(telegram.accounts?.[resolvedAccountId] ?? {}), + ...(input.token ? { botToken: input.token } : {}), + }, + }, + }, + }, + }; + }, + }, + lifecycle: { + onAccountConfigChanged: async ({ prevCfg, nextCfg, accountId }) => { + const prevTelegram = resolveTelegramAccount(prevCfg, accountId) as { token?: string }; + const nextTelegram = resolveTelegramAccount(nextCfg, accountId) as { token?: string }; + if ((prevTelegram.token ?? "").trim() !== (nextTelegram.token ?? "").trim()) { + await offsetMocks.deleteTelegramUpdateOffset({ accountId }); + } + }, + }, + } as ChannelPlugin; +} + +function setMinimalChannelsAddRegistryForTests(): void { + setActivePluginRegistry( + createTestRegistry([ + { + pluginId: "telegram", + plugin: createTelegramAddTestPlugin(), + source: "test", + }, + ]), + ); +} + function registerMSTeamsSetupPlugin(pluginId = "@openclaw/msteams-plugin"): void { vi.mocked(loadChannelSetupPluginRegistrySnapshotForChannel).mockReturnValue( createTestRegistry([{ pluginId, plugin: createMSTeamsSetupPlugin(), source: "test" }]), @@ -126,7 +244,7 @@ describe("channelsAddCommand", () => { vi.mocked(loadChannelSetupPluginRegistrySnapshotForChannel).mockReturnValue( createTestRegistry(), ); - setDefaultChannelPluginRegistryForTests(); + setMinimalChannelsAddRegistryForTests(); }); it("clears telegram update offsets when the token changes", async () => {