Files
openclaw/extensions/telegram/src/bot-native-commands.fixture-test-support.ts
Peter Steinberger 8616c0c374 refactor: finish shared test helper migrations (#120996)
* test: finish shared helper migrations

* test: fix helper migration CI

* style: fix test import ordering

* test(acpx): restore deferred void types

* test: fix helper migrations after rebase
2026-08-09 06:00:06 -07:00

133 lines
4.2 KiB
TypeScript

// Telegram plugin module implements bot native commands.fixture test support behavior.
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
import { vi } from "vitest";
import type { OpenClawConfig, TelegramAccountConfig } from "../runtime-api.js";
import type { registerTelegramNativeCommands } from "./bot-native-commands.js";
type RegisterTelegramNativeCommandsParams = Parameters<typeof registerTelegramNativeCommands>[0];
export type NativeCommandTestParams = RegisterTelegramNativeCommandsParams & {
allowFrom?: RegisterTelegramNativeCommandsParams["opts"]["allowFrom"];
groupAllowFrom?: RegisterTelegramNativeCommandsParams["opts"]["groupAllowFrom"];
replyToMode?: RegisterTelegramNativeCommandsParams["opts"]["replyToMode"];
};
export function createNativeCommandTestParams(
params: Partial<NativeCommandTestParams> = {},
): RegisterTelegramNativeCommandsParams {
const log = vi.fn();
return {
bot:
params.bot ??
({
api: {
setMyCommands: vi.fn().mockResolvedValue(undefined),
sendMessage: vi.fn().mockResolvedValue(undefined),
},
command: vi.fn(),
} as unknown as NativeCommandTestParams["bot"]),
cfg: params.cfg ?? ({} as OpenClawConfig),
runtime:
params.runtime ??
({
log,
error: vi.fn(),
exit: vi.fn(),
} as unknown as RuntimeEnv),
accountId: params.accountId ?? "default",
telegramCfg: params.telegramCfg ?? ({} as TelegramAccountConfig),
nativeEnabled: params.nativeEnabled ?? true,
nativeSkillsEnabled: params.nativeSkillsEnabled ?? false,
resolveGroupPolicy:
params.resolveGroupPolicy ??
(() =>
({
allowlistEnabled: false,
allowed: true,
}) as ReturnType<NativeCommandTestParams["resolveGroupPolicy"]>),
resolveTelegramGroupConfig:
params.resolveTelegramGroupConfig ??
((_chatId, _messageThreadId) => ({ groupConfig: undefined, topicConfig: undefined })),
shouldSkipUpdate: params.shouldSkipUpdate ?? (() => false),
telegramDeps: params.telegramDeps,
opts: {
...(params.opts ?? { token: "token" }),
allowFrom: params.allowFrom ?? params.opts?.allowFrom ?? [],
groupAllowFrom: params.groupAllowFrom ?? params.opts?.groupAllowFrom ?? [],
replyToMode: params.replyToMode ?? params.opts?.replyToMode ?? "off",
},
};
}
export function createTelegramPrivateCommandContext(params?: {
match?: string;
messageId?: number;
date?: number;
chatId?: number;
userId?: number;
username?: string;
threadId?: number;
}) {
return {
match: params?.match ?? "",
message: {
message_id: params?.messageId ?? 1,
date: params?.date ?? Math.floor(Date.now() / 1000),
chat: { id: params?.chatId ?? 100, type: "private" as const },
...(params?.threadId != null ? { message_thread_id: params.threadId } : {}),
from: { id: params?.userId ?? 200, username: params?.username ?? "bob" },
},
};
}
export function createTelegramGroupCommandContext(params?: {
match?: string;
messageId?: number;
date?: number;
chatId?: number;
title?: string;
userId?: number;
username?: string;
}) {
return {
match: params?.match ?? "",
message: {
message_id: params?.messageId ?? 2,
date: params?.date ?? Math.floor(Date.now() / 1000),
chat: {
id: params?.chatId ?? -1001234567890,
type: "supergroup" as const,
title: params?.title ?? "OpenClaw",
},
from: { id: params?.userId ?? 200, username: params?.username ?? "bob" },
},
};
}
export function createTelegramTopicCommandContext(params?: {
match?: string;
messageId?: number;
date?: number;
chatId?: number;
title?: string;
threadId?: number;
userId?: number;
username?: string;
}) {
return {
match: params?.match ?? "",
message: {
message_id: params?.messageId ?? 2,
date: params?.date ?? Math.floor(Date.now() / 1000),
chat: {
id: params?.chatId ?? -1001234567890,
type: "supergroup" as const,
title: params?.title ?? "OpenClaw",
is_forum: true,
},
message_thread_id: params?.threadId ?? 42,
from: { id: params?.userId ?? 200, username: params?.username ?? "bob" },
},
};
}