diff --git a/extensions/telegram/src/bot.create-telegram-bot.test-harness.ts b/extensions/telegram/src/bot.create-telegram-bot.test-harness.ts index d8a742a0340..648638bd23b 100644 --- a/extensions/telegram/src/bot.create-telegram-bot.test-harness.ts +++ b/extensions/telegram/src/bot.create-telegram-bot.test-harness.ts @@ -258,20 +258,13 @@ export const telegramBotRuntimeForTest = { return runnerHoisted.sequentializeSpy(); }, apiThrottler: () => runnerHoisted.throttlerSpy(), - loadConfig, }; -export const telegramBotMessageDispatchRuntimeForTest = { - dispatchReplyWithBufferedBlockDispatcher, -}; -export const telegramBotNativeCommandsRuntimeForTest = { - dispatchReplyWithBufferedBlockDispatcher, - listSkillCommandsForAgents, -}; -export const telegramBotHandlersRuntimeForTest = { +export const telegramBotDepsForTest = { loadConfig, resolveStorePath: resolveStorePathMock, readChannelAllowFromStore, enqueueSystemEvent: enqueueSystemEventSpy, + dispatchReplyWithBufferedBlockDispatcher, listSkillCommandsForAgents, wasSentByBot, }; diff --git a/extensions/telegram/src/bot.create-telegram-bot.test.ts b/extensions/telegram/src/bot.create-telegram-bot.test.ts index c791282b9f9..b9098fc7b37 100644 --- a/extensions/telegram/src/bot.create-telegram-bot.test.ts +++ b/extensions/telegram/src/bot.create-telegram-bot.test.ts @@ -26,9 +26,7 @@ const { sequentializeSpy, setMessageReactionSpy, setMyCommandsSpy, - telegramBotHandlersRuntimeForTest, - telegramBotMessageDispatchRuntimeForTest, - telegramBotNativeCommandsRuntimeForTest, + telegramBotDepsForTest, telegramBotRuntimeForTest, throttlerSpy, useSpy, @@ -36,16 +34,17 @@ const { import { resolveTelegramFetch } from "./fetch.js"; // Import after the harness registers `vi.mock(...)` for grammY and Telegram internals. -const { createTelegramBot, getTelegramSequentialKey, setTelegramBotRuntimeForTest } = - await import("./bot.js"); -const { setBotHandlersRuntimeForTest } = await import("./bot-handlers.runtime.js"); -const { setBotMessageDispatchRuntimeForTest } = await import("./bot-message-dispatch.js"); -const { setBotNativeCommandsRuntimeForTest } = await import("./bot-native-commands.js"); - +const { + createTelegramBot: createTelegramBotBase, + getTelegramSequentialKey, + setTelegramBotRuntimeForTest, +} = await import("./bot.js"); setTelegramBotRuntimeForTest(telegramBotRuntimeForTest); -setBotHandlersRuntimeForTest(telegramBotHandlersRuntimeForTest); -setBotMessageDispatchRuntimeForTest(telegramBotMessageDispatchRuntimeForTest); -setBotNativeCommandsRuntimeForTest(telegramBotNativeCommandsRuntimeForTest); +const createTelegramBot = (opts: Parameters[0]) => + createTelegramBotBase({ + ...opts, + telegramDeps: telegramBotDepsForTest, + }); const loadConfig = getLoadConfigMock(); const loadWebMedia = getLoadWebMediaMock(); diff --git a/extensions/telegram/src/bot.fetch-abort.test.ts b/extensions/telegram/src/bot.fetch-abort.test.ts index 4d1d253107d..7a58aa86e87 100644 --- a/extensions/telegram/src/bot.fetch-abort.test.ts +++ b/extensions/telegram/src/bot.fetch-abort.test.ts @@ -1,17 +1,17 @@ import { describe, expect, it, vi } from "vitest"; import { getTelegramNetworkErrorOrigin } from "./network-errors.js"; -const { botCtorSpy, telegramBotRuntimeForTest } = +const { botCtorSpy, telegramBotDepsForTest } = await import("./bot.create-telegram-bot.test-harness.js"); -const { createTelegramBot, setTelegramBotRuntimeForTest } = await import("./bot.js"); -const { setBotHandlersRuntimeForTest } = await import("./bot-handlers.runtime.js"); -const { setBotMessageDispatchRuntimeForTest } = await import("./bot-message-dispatch.js"); -const { setBotNativeCommandsRuntimeForTest } = await import("./bot-native-commands.js"); - +const { telegramBotRuntimeForTest } = await import("./bot.create-telegram-bot.test-harness.js"); +const { createTelegramBot: createTelegramBotBase, setTelegramBotRuntimeForTest } = + await import("./bot.js"); setTelegramBotRuntimeForTest(telegramBotRuntimeForTest); -setBotHandlersRuntimeForTest(); -setBotMessageDispatchRuntimeForTest(); -setBotNativeCommandsRuntimeForTest(); +const createTelegramBot = (opts: Parameters[0]) => + createTelegramBotBase({ + ...opts, + telegramDeps: telegramBotDepsForTest, + }); function createWrappedTelegramClientFetch(proxyFetch: typeof fetch) { const shutdown = new AbortController(); diff --git a/extensions/telegram/src/bot.media.e2e-harness.ts b/extensions/telegram/src/bot.media.e2e-harness.ts index 5edebbbb648..e21308c7403 100644 --- a/extensions/telegram/src/bot.media.e2e-harness.ts +++ b/extensions/telegram/src/bot.media.e2e-harness.ts @@ -68,9 +68,35 @@ export const telegramBotRuntimeForTest = { }, sequentialize: () => vi.fn(), apiThrottler: () => throttlerSpy(), +}; + +const mediaHarnessReplySpy = vi.hoisted(() => + vi.fn(async (_ctx, opts) => { + await opts?.onReplyStart?.(); + return undefined; + }), +); +const mediaHarnessDispatchReplyWithBufferedBlockDispatcher = vi.hoisted(() => + vi.fn(async (params) => { + await params.dispatcherOptions?.typingCallbacks?.start?.(); + const reply = await mediaHarnessReplySpy(params.ctx, params.replyOptions); + const payloads = reply === undefined ? [] : Array.isArray(reply) ? reply : [reply]; + for (const payload of payloads) { + await params.dispatcherOptions?.deliver?.(payload, { kind: "final" }); + } + return { queuedFinal: false, counts: {} }; + }), +); +export const telegramBotDepsForTest = { loadConfig: () => ({ channels: { telegram: { dmPolicy: "open", allowFrom: ["*"] } }, }), + resolveStorePath: vi.fn((storePath?: string) => storePath ?? "/tmp/telegram-media-sessions.json"), + readChannelAllowFromStore: vi.fn(async () => [] as string[]), + enqueueSystemEvent: vi.fn(), + dispatchReplyWithBufferedBlockDispatcher: mediaHarnessDispatchReplyWithBufferedBlockDispatcher, + listSkillCommandsForAgents: vi.fn(() => []), + wasSentByBot: vi.fn(() => false), }; beforeEach(() => { @@ -131,10 +157,11 @@ vi.doMock("openclaw/plugin-sdk/conversation-runtime", () => ({ })), })); -vi.doMock("openclaw/plugin-sdk/reply-runtime", () => { - const replySpy = vi.fn(async (_ctx, opts) => { - await opts?.onReplyStart?.(); - return undefined; - }); - return { getReplyFromConfig: replySpy, __replySpy: replySpy }; +vi.doMock("openclaw/plugin-sdk/reply-runtime", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + getReplyFromConfig: mediaHarnessReplySpy, + __replySpy: mediaHarnessReplySpy, + }; }); diff --git a/extensions/telegram/src/bot.media.test-utils.ts b/extensions/telegram/src/bot.media.test-utils.ts index 1324980a941..a98afa96b69 100644 --- a/extensions/telegram/src/bot.media.test-utils.ts +++ b/extensions/telegram/src/bot.media.test-utils.ts @@ -108,7 +108,11 @@ beforeAll(async () => { sendChatActionSpyRef = harness.sendChatActionSpy; const botModule = await import("./bot.js"); botModule.setTelegramBotRuntimeForTest(harness.telegramBotRuntimeForTest); - ({ createTelegramBot: createTelegramBotRef } = botModule); + createTelegramBotRef = (opts) => + botModule.createTelegramBot({ + ...opts, + telegramDeps: harness.telegramBotDepsForTest, + }); const replyModule = await import("openclaw/plugin-sdk/reply-runtime"); replySpyRef = (replyModule as unknown as { __replySpy: ReturnType }).__replySpy; }, TELEGRAM_BOT_IMPORT_TIMEOUT_MS); diff --git a/extensions/telegram/src/bot.test.ts b/extensions/telegram/src/bot.test.ts index 36a42a74830..7df6fa8816b 100644 --- a/extensions/telegram/src/bot.test.ts +++ b/extensions/telegram/src/bot.test.ts @@ -22,9 +22,7 @@ const { replySpy, sendMessageSpy, setMyCommandsSpy, - telegramBotHandlersRuntimeForTest, - telegramBotMessageDispatchRuntimeForTest, - telegramBotNativeCommandsRuntimeForTest, + telegramBotDepsForTest, telegramBotRuntimeForTest, wasSentByBot, } = await import("./bot.create-telegram-bot.test-harness.js"); @@ -35,15 +33,14 @@ const { listNativeCommandSpecs, listNativeCommandSpecsForConfig } = const { loadSessionStore } = await import("../../../src/config/sessions.js"); const { normalizeTelegramCommandName } = await import("../../../src/config/telegram-custom-commands.js"); -const { createTelegramBot, setTelegramBotRuntimeForTest } = await import("./bot.js"); -const { setBotHandlersRuntimeForTest } = await import("./bot-handlers.runtime.js"); -const { setBotMessageDispatchRuntimeForTest } = await import("./bot-message-dispatch.js"); -const { setBotNativeCommandsRuntimeForTest } = await import("./bot-native-commands.js"); - +const { createTelegramBot: createTelegramBotBase, setTelegramBotRuntimeForTest } = + await import("./bot.js"); setTelegramBotRuntimeForTest(telegramBotRuntimeForTest); -setBotHandlersRuntimeForTest(telegramBotHandlersRuntimeForTest); -setBotMessageDispatchRuntimeForTest(telegramBotMessageDispatchRuntimeForTest); -setBotNativeCommandsRuntimeForTest(telegramBotNativeCommandsRuntimeForTest); +const createTelegramBot = (opts: Parameters[0]) => + createTelegramBotBase({ + ...opts, + telegramDeps: telegramBotDepsForTest, + }); const loadConfig = getLoadConfigMock(); const readChannelAllowFromStore = getReadChannelAllowFromStoreMock();