From 4e265fe7d66b6b0d3570b538f71f08a44b01f1a0 Mon Sep 17 00:00:00 2001 From: Ayaan Zaidi Date: Wed, 18 Mar 2026 12:43:22 +0530 Subject: [PATCH] test(telegram): fix native command runtime mocks --- .../bot-native-commands.session-meta.test.ts | 12 +++++ ...t-native-commands.skills-allowlist.test.ts | 5 ++ .../src/bot-native-commands.test-helpers.ts | 51 +++++++++++++------ 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/extensions/telegram/src/bot-native-commands.session-meta.test.ts b/extensions/telegram/src/bot-native-commands.session-meta.test.ts index 7540f22b1ac..4ef543becda 100644 --- a/extensions/telegram/src/bot-native-commands.session-meta.test.ts +++ b/extensions/telegram/src/bot-native-commands.session-meta.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../../../src/config/config.js"; import type { ResolvedAgentRoute } from "../../../src/routing/resolve-route.js"; +import type { TelegramBotDeps } from "./bot-deps.js"; import { createDeferred, createNativeCommandTestParams, @@ -189,6 +190,16 @@ function registerAndResolveCommandHandlerBase(params: { } = params; const commandHandlers = new Map(); const sendMessage = vi.fn().mockResolvedValue(undefined); + const telegramDeps: TelegramBotDeps = { + loadConfig: vi.fn(() => cfg), + resolveStorePath: sessionMocks.resolveStorePath as TelegramBotDeps["resolveStorePath"], + readChannelAllowFromStore: vi.fn(async () => []), + enqueueSystemEvent: vi.fn(), + dispatchReplyWithBufferedBlockDispatcher: + replyMocks.dispatchReplyWithBufferedBlockDispatcher as TelegramBotDeps["dispatchReplyWithBufferedBlockDispatcher"], + listSkillCommandsForAgents: vi.fn(() => []), + wasSentByBot: vi.fn(() => false), + }; registerTelegramNativeCommands({ ...createNativeCommandTestParams({ bot: { @@ -206,6 +217,7 @@ function registerAndResolveCommandHandlerBase(params: { useAccessGroups, telegramCfg, resolveTelegramGroupConfig, + telegramDeps, }), }); diff --git a/extensions/telegram/src/bot-native-commands.skills-allowlist.test.ts b/extensions/telegram/src/bot-native-commands.skills-allowlist.test.ts index 5a2b2552739..10f0e95bdb8 100644 --- a/extensions/telegram/src/bot-native-commands.skills-allowlist.test.ts +++ b/extensions/telegram/src/bot-native-commands.skills-allowlist.test.ts @@ -11,6 +11,7 @@ import { import { registerTelegramNativeCommands } from "./bot-native-commands.js"; import { createNativeCommandTestParams, + listSkillCommandsForAgents, resetNativeCommandMenuMocks, waitForRegisteredCommands, } from "./bot-native-commands.menu-test-support.js"; @@ -62,6 +63,10 @@ describe("registerTelegramNativeCommands skill allowlist integration", () => { }, ], }; + const actualSkillCommands = await import("../../../src/auto-reply/skill-commands.js"); + listSkillCommandsForAgents.mockImplementation(({ cfg, agentIds }) => + actualSkillCommands.listSkillCommandsForAgents({ cfg, agentIds }), + ); registerTelegramNativeCommands({ ...createNativeCommandTestParams(cfg, { diff --git a/extensions/telegram/src/bot-native-commands.test-helpers.ts b/extensions/telegram/src/bot-native-commands.test-helpers.ts index 3afeb63fbb2..7a35ec37275 100644 --- a/extensions/telegram/src/bot-native-commands.test-helpers.ts +++ b/extensions/telegram/src/bot-native-commands.test-helpers.ts @@ -65,28 +65,36 @@ const replyPipelineMocks = vi.hoisted(() => { export const dispatchReplyWithBufferedBlockDispatcher = replyPipelineMocks.dispatchReplyWithBufferedBlockDispatcher; -vi.mock("openclaw/plugin-sdk/reply-runtime", () => ({ - finalizeInboundContext: replyPipelineMocks.finalizeInboundContext, -})); -vi.mock("openclaw/plugin-sdk/reply-runtime", () => ({ - dispatchReplyWithBufferedBlockDispatcher: - replyPipelineMocks.dispatchReplyWithBufferedBlockDispatcher, -})); -vi.mock("openclaw/plugin-sdk/channel-runtime", () => ({ - createReplyPrefixOptions: replyPipelineMocks.createReplyPrefixOptions, -})); -vi.mock("openclaw/plugin-sdk/channel-runtime", () => ({ - recordInboundSessionMetaSafe: replyPipelineMocks.recordInboundSessionMetaSafe, -})); +vi.mock("openclaw/plugin-sdk/reply-runtime", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + finalizeInboundContext: replyPipelineMocks.finalizeInboundContext, + dispatchReplyWithBufferedBlockDispatcher: + replyPipelineMocks.dispatchReplyWithBufferedBlockDispatcher, + }; +}); +vi.mock("openclaw/plugin-sdk/channel-runtime", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + createReplyPrefixOptions: replyPipelineMocks.createReplyPrefixOptions, + recordInboundSessionMetaSafe: replyPipelineMocks.recordInboundSessionMetaSafe, + }; +}); const deliveryMocks = vi.hoisted(() => ({ deliverReplies: vi.fn(async () => {}), })); export const deliverReplies = deliveryMocks.deliverReplies; vi.mock("./bot/delivery.js", () => ({ deliverReplies: deliveryMocks.deliverReplies })); -vi.mock("openclaw/plugin-sdk/conversation-runtime", () => ({ - readChannelAllowFromStore: vi.fn(async () => []), -})); +vi.mock("openclaw/plugin-sdk/conversation-runtime", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + readChannelAllowFromStore: vi.fn(async () => []), + }; +}); export { createNativeCommandTestParams }; export function createNativeCommandsHarness(params?: { @@ -104,6 +112,16 @@ export function createNativeCommandsHarness(params?: { const sendMessage: AnyAsyncMock = vi.fn(async () => undefined); const setMyCommands: AnyAsyncMock = vi.fn(async () => undefined); const log: AnyMock = vi.fn(); + const telegramDeps = { + loadConfig: vi.fn(() => params?.cfg ?? ({} as OpenClawConfig)), + resolveStorePath: vi.fn((storePath?: string) => storePath ?? "/tmp/sessions.json"), + readChannelAllowFromStore: vi.fn(async () => []), + enqueueSystemEvent: vi.fn(), + dispatchReplyWithBufferedBlockDispatcher: + replyPipelineMocks.dispatchReplyWithBufferedBlockDispatcher, + listSkillCommandsForAgents: vi.fn(() => []), + wasSentByBot: vi.fn(() => false), + }; const bot = { api: { setMyCommands, @@ -128,6 +146,7 @@ export function createNativeCommandsHarness(params?: { nativeEnabled: params?.nativeEnabled ?? true, nativeSkillsEnabled: false, nativeDisabledExplicit: false, + telegramDeps, resolveGroupPolicy: params?.resolveGroupPolicy ?? (() =>