test(telegram): inject bot deps in harnesses

This commit is contained in:
Ayaan Zaidi
2026-03-18 10:08:52 +05:30
committed by Val Alexander
parent e5fd061470
commit 3bf540170a
6 changed files with 68 additions and 48 deletions

View File

@@ -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,
};

View File

@@ -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<typeof createTelegramBotBase>[0]) =>
createTelegramBotBase({
...opts,
telegramDeps: telegramBotDepsForTest,
});
const loadConfig = getLoadConfigMock();
const loadWebMedia = getLoadWebMediaMock();

View File

@@ -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<typeof createTelegramBotBase>[0]) =>
createTelegramBotBase({
...opts,
telegramDeps: telegramBotDepsForTest,
});
function createWrappedTelegramClientFetch(proxyFetch: typeof fetch) {
const shutdown = new AbortController();

View File

@@ -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<typeof import("openclaw/plugin-sdk/reply-runtime")>();
return {
...actual,
getReplyFromConfig: mediaHarnessReplySpy,
__replySpy: mediaHarnessReplySpy,
};
});

View File

@@ -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<typeof vi.fn> }).__replySpy;
}, TELEGRAM_BOT_IMPORT_TIMEOUT_MS);

View File

@@ -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<typeof createTelegramBotBase>[0]) =>
createTelegramBotBase({
...opts,
telegramDeps: telegramBotDepsForTest,
});
const loadConfig = getLoadConfigMock();
const readChannelAllowFromStore = getReadChannelAllowFromStoreMock();