// Telegram plugin module implements bot native commands.menu test support behavior. import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env"; import { expect, vi, type Mock } from "vitest"; import type { OpenClawConfig } from "../runtime-api.js"; import type { TelegramNativeCommandDeps } from "./bot-native-command-deps.runtime.js"; import { createNativeCommandTestParams as createBaseNativeCommandTestParams, createTelegramPrivateCommandContext, type NativeCommandTestParams as RegisterTelegramNativeCommandsParams, } from "./bot-native-commands.fixture-test-support.js"; type RegisteredCommand = { command: string; description: string; }; type UnknownMock = Mock<(...args: unknown[]) => unknown>; type CreateCommandBotResult = { bot: RegisterTelegramNativeCommandsParams["bot"]; commandHandlers: Map Promise>; sendMessage: ReturnType; deleteMessage: ReturnType; setMyCommands: ReturnType; }; type CreateCommandBotParams = { api?: Record; }; const skillCommandMocks = vi.hoisted(() => ({ listSkillCommandsForAgents: vi.fn( () => [], ), })); const deliveryMocks = vi.hoisted(() => ({ deliverReplies: vi.fn(async () => ({ delivered: true })), editMessageTelegram: vi.fn(async () => ({ ok: true as const, messageId: "999", chatId: "100" })), emitTelegramMessageSentHooks: vi.fn(), })); export const listSkillCommandsForAgents = skillCommandMocks.listSkillCommandsForAgents; export const deliverReplies = deliveryMocks.deliverReplies; export const editMessageTelegram = deliveryMocks.editMessageTelegram; export const emitTelegramMessageSentHooks: UnknownMock = deliveryMocks.emitTelegramMessageSentHooks; vi.mock("./bot/delivery.js", () => ({ deliverReplies, emitTelegramMessageSentHooks, })); vi.mock("./bot/delivery.replies.js", () => ({ deliverReplies, })); export async function waitForRegisteredCommands( setMyCommands: ReturnType, ): Promise { await vi.waitFor(() => { expect(setMyCommands).toHaveBeenCalled(); }); return setMyCommands.mock.calls.at(0)?.[0] as RegisteredCommand[]; } export function resetNativeCommandMenuMocks() { listSkillCommandsForAgents.mockClear(); listSkillCommandsForAgents.mockReturnValue([]); deliverReplies.mockClear(); deliverReplies.mockResolvedValue({ delivered: true }); editMessageTelegram.mockClear(); editMessageTelegram.mockResolvedValue({ ok: true as const, messageId: "999", chatId: "100" }); emitTelegramMessageSentHooks.mockClear(); } export function createCommandBot(params: CreateCommandBotParams = {}): CreateCommandBotResult { const commandHandlers = new Map Promise>(); const sendMessage = vi.fn().mockResolvedValue({ message_id: 999 }); const deleteMessage = vi.fn().mockResolvedValue(true); const setMyCommands = vi.fn().mockResolvedValue(undefined); const bot = { api: { setMyCommands, sendMessage, deleteMessage, ...params.api, }, command: vi.fn((name: string, cb: (ctx: unknown) => Promise) => { commandHandlers.set(name, cb); }), } as unknown as RegisterTelegramNativeCommandsParams["bot"]; return { bot, commandHandlers, sendMessage, deleteMessage, setMyCommands }; } export function createNativeCommandTestParams( cfg: OpenClawConfig, params: Partial = {}, ): RegisterTelegramNativeCommandsParams { const telegramDeps: TelegramNativeCommandDeps = { getRuntimeConfig: vi.fn(() => cfg) as TelegramNativeCommandDeps["getRuntimeConfig"], readChannelAllowFromStore: vi.fn( async () => [], ) as TelegramNativeCommandDeps["readChannelAllowFromStore"], dispatchChannelInboundTurn: vi.fn(async (plan) => ({ admission: { kind: "dispatch" }, dispatched: true, ctxPayload: plan.ctxPayload, routeSessionKey: plan.route.sessionKey, dispatchResult: { queuedFinal: false, counts: { block: 0, final: 0, tool: 0 }, }, })) as TelegramNativeCommandDeps["dispatchChannelInboundTurn"], listSkillCommandsForAgents, syncTelegramMenuCommands: vi.fn(({ bot, commandsToRegister }) => { if (commandsToRegister.length === 0) { return undefined; } return bot.api.setMyCommands(commandsToRegister); }) as TelegramNativeCommandDeps["syncTelegramMenuCommands"], editMessageTelegram, sendMessageTelegram: vi.fn(async () => ({ messageId: "999", chatId: "100" })), }; return createBaseNativeCommandTestParams({ cfg, runtime: params.runtime ?? ({} as RuntimeEnv), nativeSkillsEnabled: true, telegramDeps, ...params, }); } export function createPrivateCommandContext( params?: Parameters[0], ) { return createTelegramPrivateCommandContext(params); }