mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-25 02:03:34 +00:00
This commit is contained in:
@@ -18,6 +18,16 @@ const sendStickerTelegram = vi.fn(async () => ({
|
||||
chatId: "123",
|
||||
}));
|
||||
const deleteMessageTelegram = vi.fn(async () => ({ ok: true }));
|
||||
const editMessageTelegram = vi.fn(async () => ({
|
||||
ok: true,
|
||||
messageId: "456",
|
||||
chatId: "123",
|
||||
}));
|
||||
const createForumTopicTelegram = vi.fn(async () => ({
|
||||
topicId: 99,
|
||||
name: "Topic",
|
||||
chatId: "123",
|
||||
}));
|
||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||
|
||||
vi.mock("../../telegram/send.js", () => ({
|
||||
@@ -30,6 +40,10 @@ vi.mock("../../telegram/send.js", () => ({
|
||||
sendStickerTelegram(...args),
|
||||
deleteMessageTelegram: (...args: Parameters<typeof deleteMessageTelegram>) =>
|
||||
deleteMessageTelegram(...args),
|
||||
editMessageTelegram: (...args: Parameters<typeof editMessageTelegram>) =>
|
||||
editMessageTelegram(...args),
|
||||
createForumTopicTelegram: (...args: Parameters<typeof createForumTopicTelegram>) =>
|
||||
createForumTopicTelegram(...args),
|
||||
}));
|
||||
|
||||
describe("handleTelegramAction", () => {
|
||||
@@ -90,6 +104,8 @@ describe("handleTelegramAction", () => {
|
||||
sendPollTelegram.mockClear();
|
||||
sendStickerTelegram.mockClear();
|
||||
deleteMessageTelegram.mockClear();
|
||||
editMessageTelegram.mockClear();
|
||||
createForumTopicTelegram.mockClear();
|
||||
process.env.TELEGRAM_BOT_TOKEN = "tok";
|
||||
});
|
||||
|
||||
@@ -379,6 +395,85 @@ describe("handleTelegramAction", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "react",
|
||||
params: { action: "react", chatId: "123", messageId: 456, emoji: "✅" },
|
||||
cfg: reactionConfig("minimal"),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(reactMessageTelegram.mock.calls as unknown[][], 3),
|
||||
},
|
||||
{
|
||||
name: "sendMessage",
|
||||
params: { action: "sendMessage", to: "123", content: "hello" },
|
||||
cfg: telegramConfig(),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(sendMessageTelegram.mock.calls as unknown[][], 2),
|
||||
},
|
||||
{
|
||||
name: "poll",
|
||||
params: {
|
||||
action: "poll",
|
||||
to: "123",
|
||||
question: "Q?",
|
||||
answers: ["A", "B"],
|
||||
},
|
||||
cfg: telegramConfig(),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(sendPollTelegram.mock.calls as unknown[][], 2),
|
||||
},
|
||||
{
|
||||
name: "deleteMessage",
|
||||
params: { action: "deleteMessage", chatId: "123", messageId: 1 },
|
||||
cfg: telegramConfig(),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(deleteMessageTelegram.mock.calls as unknown[][], 2),
|
||||
},
|
||||
{
|
||||
name: "editMessage",
|
||||
params: { action: "editMessage", chatId: "123", messageId: 1, content: "updated" },
|
||||
cfg: telegramConfig(),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(editMessageTelegram.mock.calls as unknown[][], 3),
|
||||
},
|
||||
{
|
||||
name: "sendSticker",
|
||||
params: { action: "sendSticker", to: "123", fileId: "sticker-1" },
|
||||
cfg: telegramConfig({ actions: { sticker: true } }),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(sendStickerTelegram.mock.calls as unknown[][], 2),
|
||||
},
|
||||
{
|
||||
name: "createForumTopic",
|
||||
params: { action: "createForumTopic", chatId: "123", name: "Topic" },
|
||||
cfg: telegramConfig({ actions: { createForumTopic: true } }),
|
||||
assertCall: (
|
||||
readCallOpts: (calls: unknown[][], argIndex: number) => Record<string, unknown>,
|
||||
) => readCallOpts(createForumTopicTelegram.mock.calls as unknown[][], 2),
|
||||
},
|
||||
])("forwards resolved cfg for $name action", async ({ params, cfg, assertCall }) => {
|
||||
const readCallOpts = (calls: unknown[][], argIndex: number): Record<string, unknown> => {
|
||||
const args = calls[0];
|
||||
if (!Array.isArray(args)) {
|
||||
throw new Error("Expected Telegram action call args");
|
||||
}
|
||||
const opts = args[argIndex];
|
||||
if (!opts || typeof opts !== "object") {
|
||||
throw new Error("Expected Telegram action options object");
|
||||
}
|
||||
return opts as Record<string, unknown>;
|
||||
};
|
||||
await handleTelegramAction(params as Record<string, unknown>, cfg);
|
||||
const opts = assertCall(readCallOpts);
|
||||
expect(opts.cfg).toBe(cfg);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "media",
|
||||
|
||||
@@ -154,6 +154,7 @@ export async function handleTelegramAction(
|
||||
let reactionResult: Awaited<ReturnType<typeof reactMessageTelegram>>;
|
||||
try {
|
||||
reactionResult = await reactMessageTelegram(chatId ?? "", messageId ?? 0, emoji ?? "", {
|
||||
cfg,
|
||||
token,
|
||||
remove,
|
||||
accountId: accountId ?? undefined,
|
||||
@@ -237,6 +238,7 @@ export async function handleTelegramAction(
|
||||
);
|
||||
}
|
||||
const result = await sendMessageTelegram(to, content, {
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
mediaUrl: mediaUrl || undefined,
|
||||
@@ -293,6 +295,7 @@ export async function handleTelegramAction(
|
||||
durationHours: durationHours ?? undefined,
|
||||
},
|
||||
{
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
replyToMessageId: replyToMessageId ?? undefined,
|
||||
@@ -327,6 +330,7 @@ export async function handleTelegramAction(
|
||||
);
|
||||
}
|
||||
await deleteMessageTelegram(chatId ?? "", messageId ?? 0, {
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
});
|
||||
@@ -367,6 +371,7 @@ export async function handleTelegramAction(
|
||||
);
|
||||
}
|
||||
const result = await editMessageTelegram(chatId ?? "", messageId ?? 0, content, {
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
buttons,
|
||||
@@ -399,6 +404,7 @@ export async function handleTelegramAction(
|
||||
);
|
||||
}
|
||||
const result = await sendStickerTelegram(to, fileId, {
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
replyToMessageId: replyToMessageId ?? undefined,
|
||||
@@ -454,6 +460,7 @@ export async function handleTelegramAction(
|
||||
);
|
||||
}
|
||||
const result = await createForumTopicTelegram(chatId ?? "", name, {
|
||||
cfg,
|
||||
token,
|
||||
accountId: accountId ?? undefined,
|
||||
iconColor: iconColor ?? undefined,
|
||||
|
||||
Reference in New Issue
Block a user