2026-02-16 02:19:18 +00:00
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
2026-02-01 10:03:47 +09:00
|
|
|
|
import { escapeRegExp, formatEnvelopeTimestamp } from "../../test/helpers/envelope-timestamp.js";
|
|
|
|
|
|
import { expectInboundContextContract } from "../../test/helpers/inbound-contract.js";
|
2026-01-16 08:20:48 +00:00
|
|
|
|
import {
|
|
|
|
|
|
listNativeCommandSpecs,
|
|
|
|
|
|
listNativeCommandSpecsForConfig,
|
|
|
|
|
|
} from "../auto-reply/commands-registry.js";
|
2026-02-17 23:20:36 +05:30
|
|
|
|
import { normalizeTelegramCommandName } from "../config/telegram-custom-commands.js";
|
2026-02-16 02:19:18 +00:00
|
|
|
|
import {
|
|
|
|
|
|
answerCallbackQuerySpy,
|
|
|
|
|
|
commandSpy,
|
|
|
|
|
|
editMessageTextSpy,
|
|
|
|
|
|
enqueueSystemEventSpy,
|
|
|
|
|
|
getLoadConfigMock,
|
|
|
|
|
|
getReadChannelAllowFromStoreMock,
|
|
|
|
|
|
getOnHandler,
|
2026-01-23 06:21:50 +00:00
|
|
|
|
listSkillCommandsForAgents,
|
2026-02-16 02:19:18 +00:00
|
|
|
|
onSpy,
|
|
|
|
|
|
replySpy,
|
|
|
|
|
|
sendMessageSpy,
|
|
|
|
|
|
setMyCommandsSpy,
|
|
|
|
|
|
wasSentByBot,
|
|
|
|
|
|
} from "./bot.create-telegram-bot.test-harness.js";
|
|
|
|
|
|
import { createTelegramBot } from "./bot.js";
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
const loadConfig = getLoadConfigMock();
|
|
|
|
|
|
const readChannelAllowFromStore = getReadChannelAllowFromStoreMock();
|
2026-01-23 22:51:37 +00:00
|
|
|
|
|
2026-01-16 12:10:20 +00:00
|
|
|
|
function resolveSkillCommands(config: Parameters<typeof listNativeCommandSpecsForConfig>[0]) {
|
2026-02-17 15:46:25 +09:00
|
|
|
|
void config;
|
|
|
|
|
|
return listSkillCommandsForAgents() as NonNullable<
|
|
|
|
|
|
Parameters<typeof listNativeCommandSpecsForConfig>[1]
|
|
|
|
|
|
>["skillCommands"];
|
2026-01-16 12:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 05:32:28 +00:00
|
|
|
|
const ORIGINAL_TZ = process.env.TZ;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
describe("createTelegramBot", () => {
|
2026-02-01 22:21:26 +00:00
|
|
|
|
beforeEach(() => {
|
2026-01-22 05:32:28 +00:00
|
|
|
|
process.env.TZ = "UTC";
|
2026-01-13 21:13:05 +02:00
|
|
|
|
loadConfig.mockReturnValue({
|
2026-01-22 06:27:45 +00:00
|
|
|
|
agents: {
|
|
|
|
|
|
defaults: {
|
|
|
|
|
|
envelopeTimezone: "utc",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", allowFrom: ["*"] },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-01-22 05:32:28 +00:00
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
process.env.TZ = ORIGINAL_TZ;
|
|
|
|
|
|
});
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-01-16 08:20:48 +00:00
|
|
|
|
it("merges custom commands with native commands", () => {
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
customCommands: [
|
|
|
|
|
|
{ command: "custom_backup", description: "Git backup" },
|
|
|
|
|
|
{ command: "/Custom_Generate", description: "Create an image" },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
loadConfig.mockReturnValue(config);
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
|
|
|
|
|
|
const registered = setMyCommandsSpy.mock.calls[0]?.[0] as Array<{
|
|
|
|
|
|
command: string;
|
|
|
|
|
|
description: string;
|
|
|
|
|
|
}>;
|
2026-01-16 12:10:20 +00:00
|
|
|
|
const skillCommands = resolveSkillCommands(config);
|
|
|
|
|
|
const native = listNativeCommandSpecsForConfig(config, { skillCommands }).map((command) => ({
|
2026-02-17 23:20:36 +05:30
|
|
|
|
command: normalizeTelegramCommandName(command.name),
|
2026-01-16 08:20:48 +00:00
|
|
|
|
description: command.description,
|
|
|
|
|
|
}));
|
|
|
|
|
|
expect(registered.slice(0, native.length)).toEqual(native);
|
|
|
|
|
|
expect(registered.slice(native.length)).toEqual([
|
|
|
|
|
|
{ command: "custom_backup", description: "Git backup" },
|
|
|
|
|
|
{ command: "custom_generate", description: "Create an image" },
|
|
|
|
|
|
]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("ignores custom commands that collide with native commands", () => {
|
|
|
|
|
|
const errorSpy = vi.fn();
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
customCommands: [
|
|
|
|
|
|
{ command: "status", description: "Custom status" },
|
|
|
|
|
|
{ command: "custom_backup", description: "Git backup" },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
loadConfig.mockReturnValue(config);
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({
|
|
|
|
|
|
token: "tok",
|
|
|
|
|
|
runtime: {
|
|
|
|
|
|
log: vi.fn(),
|
|
|
|
|
|
error: errorSpy,
|
|
|
|
|
|
exit: ((code: number) => {
|
|
|
|
|
|
throw new Error(`exit ${code}`);
|
|
|
|
|
|
}) as (code: number) => never,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const registered = setMyCommandsSpy.mock.calls[0]?.[0] as Array<{
|
|
|
|
|
|
command: string;
|
|
|
|
|
|
description: string;
|
|
|
|
|
|
}>;
|
2026-01-16 12:10:20 +00:00
|
|
|
|
const skillCommands = resolveSkillCommands(config);
|
|
|
|
|
|
const native = listNativeCommandSpecsForConfig(config, { skillCommands }).map((command) => ({
|
2026-02-17 23:20:36 +05:30
|
|
|
|
command: normalizeTelegramCommandName(command.name),
|
2026-01-16 08:20:48 +00:00
|
|
|
|
description: command.description,
|
|
|
|
|
|
}));
|
|
|
|
|
|
const nativeStatus = native.find((command) => command.command === "status");
|
|
|
|
|
|
expect(nativeStatus).toBeDefined();
|
|
|
|
|
|
expect(registered).toContainEqual({ command: "custom_backup", description: "Git backup" });
|
|
|
|
|
|
expect(registered).not.toContainEqual({ command: "status", description: "Custom status" });
|
2026-01-16 09:18:53 +00:00
|
|
|
|
expect(registered.filter((command) => command.command === "status")).toEqual([nativeStatus]);
|
2026-01-16 08:20:48 +00:00
|
|
|
|
expect(errorSpy).toHaveBeenCalled();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("registers custom commands when native commands are disabled", () => {
|
|
|
|
|
|
const config = {
|
|
|
|
|
|
commands: { native: false },
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
customCommands: [
|
|
|
|
|
|
{ command: "custom_backup", description: "Git backup" },
|
|
|
|
|
|
{ command: "custom_generate", description: "Create an image" },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
loadConfig.mockReturnValue(config);
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
|
|
|
|
|
|
const registered = setMyCommandsSpy.mock.calls[0]?.[0] as Array<{
|
|
|
|
|
|
command: string;
|
|
|
|
|
|
description: string;
|
|
|
|
|
|
}>;
|
|
|
|
|
|
expect(registered).toEqual([
|
|
|
|
|
|
{ command: "custom_backup", description: "Git backup" },
|
|
|
|
|
|
{ command: "custom_generate", description: "Create an image" },
|
|
|
|
|
|
]);
|
2026-01-31 15:58:24 +09:00
|
|
|
|
const reserved = new Set(listNativeCommandSpecs().map((command) => command.name));
|
|
|
|
|
|
expect(registered.some((command) => reserved.has(command.command))).toBe(false);
|
2026-01-16 08:20:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-16 20:16:35 +00:00
|
|
|
|
it("blocks callback_query when inline buttons are allowlist-only and sender not authorized", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({
|
|
|
|
|
|
token: "tok",
|
|
|
|
|
|
config: {
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
dmPolicy: "pairing",
|
|
|
|
|
|
capabilities: { inlineButtons: "allowlist" },
|
|
|
|
|
|
allowFrom: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
const callbackHandler = onSpy.mock.calls.find((call) => call[0] === "callback_query")?.[1] as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
expect(callbackHandler).toBeDefined();
|
|
|
|
|
|
|
|
|
|
|
|
await callbackHandler({
|
|
|
|
|
|
callbackQuery: {
|
|
|
|
|
|
id: "cbq-2",
|
|
|
|
|
|
data: "cmd:option_b",
|
|
|
|
|
|
from: { id: 9, first_name: "Ada", username: "ada_bot" },
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 11,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-16 20:16:35 +00:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).not.toHaveBeenCalled();
|
|
|
|
|
|
expect(answerCallbackQuerySpy).toHaveBeenCalledWith("cbq-2");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-27 02:35:09 -05:00
|
|
|
|
it("edits commands list for pagination callbacks", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
listSkillCommandsForAgents.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const callbackHandler = onSpy.mock.calls.find((call) => call[0] === "callback_query")?.[1] as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
expect(callbackHandler).toBeDefined();
|
|
|
|
|
|
|
|
|
|
|
|
await callbackHandler({
|
|
|
|
|
|
callbackQuery: {
|
|
|
|
|
|
id: "cbq-3",
|
|
|
|
|
|
data: "commands_page_2:main",
|
|
|
|
|
|
from: { id: 9, first_name: "Ada", username: "ada_bot" },
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 12,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-27 02:35:09 -05:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(listSkillCommandsForAgents).toHaveBeenCalledWith({
|
|
|
|
|
|
cfg: expect.any(Object),
|
|
|
|
|
|
agentIds: ["main"],
|
|
|
|
|
|
});
|
|
|
|
|
|
expect(editMessageTextSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const [chatId, messageId, text, params] = editMessageTextSpy.mock.calls[0] ?? [];
|
|
|
|
|
|
expect(chatId).toBe(1234);
|
|
|
|
|
|
expect(messageId).toBe(12);
|
|
|
|
|
|
expect(String(text)).toContain("ℹ️ Commands");
|
|
|
|
|
|
expect(params).toEqual(
|
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
|
reply_markup: expect.any(Object),
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("blocks pagination callbacks when allowlist rejects sender", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
editMessageTextSpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({
|
|
|
|
|
|
token: "tok",
|
|
|
|
|
|
config: {
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
dmPolicy: "pairing",
|
|
|
|
|
|
capabilities: { inlineButtons: "allowlist" },
|
|
|
|
|
|
allowFrom: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
const callbackHandler = onSpy.mock.calls.find((call) => call[0] === "callback_query")?.[1] as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
expect(callbackHandler).toBeDefined();
|
|
|
|
|
|
|
|
|
|
|
|
await callbackHandler({
|
|
|
|
|
|
callbackQuery: {
|
|
|
|
|
|
id: "cbq-4",
|
|
|
|
|
|
data: "commands_page_2",
|
|
|
|
|
|
from: { id: 9, first_name: "Ada", username: "ada_bot" },
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 13,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-27 02:35:09 -05:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(editMessageTextSpy).not.toHaveBeenCalled();
|
|
|
|
|
|
expect(answerCallbackQuerySpy).toHaveBeenCalledWith("cbq-4");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:13:05 +02:00
|
|
|
|
it("includes sender identity in group envelope headers", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
2026-01-22 05:46:04 +00:00
|
|
|
|
loadConfig.mockReturnValue({
|
2026-01-22 06:27:45 +00:00
|
|
|
|
agents: {
|
|
|
|
|
|
defaults: {
|
|
|
|
|
|
envelopeTimezone: "utc",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-01-22 06:27:45 +00:00
|
|
|
|
},
|
2026-01-22 05:46:04 +00:00
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
groupPolicy: "open",
|
|
|
|
|
|
groups: { "*": { requireMention: false } },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-01-22 05:46:04 +00:00
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-01-22 05:46:04 +00:00
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-01-22 05:46:04 +00:00
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 42, type: "group", title: "Ops" },
|
|
|
|
|
|
text: "hello",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 2,
|
|
|
|
|
|
from: {
|
|
|
|
|
|
id: 99,
|
|
|
|
|
|
first_name: "Ada",
|
|
|
|
|
|
last_name: "Lovelace",
|
|
|
|
|
|
username: "ada",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-01-22 05:46:04 +00:00
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-22 05:46:04 +00:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-01-22 05:46:04 +00:00
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
|
|
|
|
|
expectInboundContextContract(payload);
|
2026-01-22 06:27:45 +00:00
|
|
|
|
const expectedTimestamp = formatEnvelopeTimestamp(new Date("2025-01-09T00:00:00Z"));
|
|
|
|
|
|
const timestampPattern = escapeRegExp(expectedTimestamp);
|
2026-01-22 05:46:04 +00:00
|
|
|
|
expect(payload.Body).toMatch(
|
2026-01-22 06:27:45 +00:00
|
|
|
|
new RegExp(`^\\[Telegram Ops id:42 (\\+\\d+[smhd] )?${timestampPattern}\\]`),
|
2026-01-22 05:46:04 +00:00
|
|
|
|
);
|
|
|
|
|
|
expect(payload.SenderName).toBe("Ada Lovelace");
|
|
|
|
|
|
expect(payload.SenderId).toBe("99");
|
|
|
|
|
|
expect(payload.SenderUsername).toBe("ada");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("uses quote text when a Telegram partial reply is received", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-14 02:33:56 +00:00
|
|
|
|
sendMessageSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
chat: { id: 7, type: "private" },
|
|
|
|
|
|
text: "Sure, see below",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
date: 1736380800,
|
2026-02-14 02:33:56 +00:00
|
|
|
|
reply_to_message: {
|
|
|
|
|
|
message_id: 9001,
|
|
|
|
|
|
text: "Can you summarize this?",
|
|
|
|
|
|
from: { first_name: "Ada" },
|
|
|
|
|
|
},
|
|
|
|
|
|
quote: {
|
|
|
|
|
|
text: "summarize this",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
|
|
|
|
|
expect(payload.Body).toContain("[Quoting Ada id:9001]");
|
|
|
|
|
|
expect(payload.Body).toContain('"summarize this"');
|
|
|
|
|
|
expect(payload.ReplyToId).toBe("9001");
|
|
|
|
|
|
expect(payload.ReplyToBody).toBe("summarize this");
|
|
|
|
|
|
expect(payload.ReplyToSender).toBe("Ada");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("handles quote-only replies without reply metadata", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-14 02:33:56 +00:00
|
|
|
|
sendMessageSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
chat: { id: 7, type: "private" },
|
|
|
|
|
|
text: "Sure, see below",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
date: 1736380800,
|
2026-02-14 02:33:56 +00:00
|
|
|
|
quote: {
|
|
|
|
|
|
text: "summarize this",
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-02-14 02:33:56 +00:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(payload.Body).toContain("[Quoting unknown sender]");
|
|
|
|
|
|
expect(payload.Body).toContain('"summarize this"');
|
|
|
|
|
|
expect(payload.ReplyToId).toBeUndefined();
|
|
|
|
|
|
expect(payload.ReplyToBody).toBe("summarize this");
|
|
|
|
|
|
expect(payload.ReplyToSender).toBe("unknown sender");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("uses external_reply quote text for partial replies", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
sendMessageSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 7, type: "private" },
|
|
|
|
|
|
text: "Sure, see below",
|
|
|
|
|
|
date: 1736380800,
|
2026-02-14 02:33:56 +00:00
|
|
|
|
external_reply: {
|
|
|
|
|
|
message_id: 9002,
|
2026-01-13 21:13:05 +02:00
|
|
|
|
text: "Can you summarize this?",
|
|
|
|
|
|
from: { first_name: "Ada" },
|
2026-02-14 02:33:56 +00:00
|
|
|
|
quote: {
|
|
|
|
|
|
text: "summarize this",
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(payload.Body).toContain("[Quoting Ada id:9002]");
|
|
|
|
|
|
expect(payload.Body).toContain('"summarize this"');
|
|
|
|
|
|
expect(payload.ReplyToId).toBe("9002");
|
|
|
|
|
|
expect(payload.ReplyToBody).toBe("summarize this");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
expect(payload.ReplyToSender).toBe("Ada");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("accepts group replies to the bot without explicit mention when requireMention is enabled", async () => {
|
2026-01-28 00:59:24 +04:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
2026-02-14 02:33:56 +00:00
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { groups: { "*": { requireMention: true } } },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-01-17 02:54:45 +05:30
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 456, type: "group", title: "Ops Chat" },
|
|
|
|
|
|
text: "following up",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
reply_to_message: {
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
text: "original reply",
|
2026-02-14 02:33:56 +00:00
|
|
|
|
from: { id: 999, first_name: "OpenClaw" },
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-02-14 02:33:56 +00:00
|
|
|
|
me: { id: 999, username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(payload.WasMentioned).toBe(true);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("inherits group allowlist + requireMention in topics", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
groupPolicy: "allowlist",
|
|
|
|
|
|
groups: {
|
|
|
|
|
|
"-1001234567890": {
|
|
|
|
|
|
requireMention: false,
|
|
|
|
|
|
allowFrom: ["123456789"],
|
|
|
|
|
|
topics: {
|
|
|
|
|
|
"99": {},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: {
|
|
|
|
|
|
id: -1001234567890,
|
|
|
|
|
|
type: "supergroup",
|
|
|
|
|
|
title: "Forum Group",
|
|
|
|
|
|
is_forum: true,
|
|
|
|
|
|
},
|
2026-02-14 02:33:56 +00:00
|
|
|
|
from: { id: 123456789, username: "testuser" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
text: "hello",
|
|
|
|
|
|
date: 1736380800,
|
2026-02-14 02:33:56 +00:00
|
|
|
|
message_thread_id: 99,
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("prefers topic allowFrom over group allowFrom", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
groupPolicy: "allowlist",
|
2026-01-13 21:13:05 +02:00
|
|
|
|
groups: {
|
|
|
|
|
|
"-1001234567890": {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
allowFrom: ["123456789"],
|
2026-01-13 21:13:05 +02:00
|
|
|
|
topics: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
"99": { allowFrom: ["999999999"] },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: {
|
|
|
|
|
|
id: -1001234567890,
|
|
|
|
|
|
type: "supergroup",
|
|
|
|
|
|
title: "Forum Group",
|
|
|
|
|
|
is_forum: true,
|
|
|
|
|
|
},
|
2026-02-14 02:33:56 +00:00
|
|
|
|
from: { id: 123456789, username: "testuser" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
text: "hello",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_thread_id: 99,
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(0);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("allows group messages for per-group groupPolicy open override (global groupPolicy allowlist)", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
groupPolicy: "allowlist",
|
|
|
|
|
|
groups: {
|
|
|
|
|
|
"-100123456789": {
|
|
|
|
|
|
groupPolicy: "open",
|
|
|
|
|
|
requireMention: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-14 02:33:56 +00:00
|
|
|
|
readChannelAllowFromStore.mockResolvedValueOnce(["123456789"]);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
chat: { id: -100123456789, type: "group", title: "Test Group" },
|
|
|
|
|
|
from: { id: 999999, username: "random" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
text: "hello",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
},
|
2026-01-30 03:15:10 +01:00
|
|
|
|
me: { username: "openclaw_bot" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
it("blocks control commands from unauthorized senders in per-group open groups", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
groupPolicy: "allowlist",
|
|
|
|
|
|
groups: {
|
|
|
|
|
|
"-100123456789": {
|
|
|
|
|
|
groupPolicy: "open",
|
|
|
|
|
|
requireMention: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-14 02:33:56 +00:00
|
|
|
|
readChannelAllowFromStore.mockResolvedValueOnce(["123456789"]);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-02-14 02:33:56 +00:00
|
|
|
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
2026-02-14 02:33:56 +00:00
|
|
|
|
chat: { id: -100123456789, type: "group", title: "Test Group" },
|
|
|
|
|
|
from: { id: 999999, username: "random" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
text: "/status",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
},
|
2026-02-14 02:33:56 +00:00
|
|
|
|
me: { username: "openclaw_bot" },
|
|
|
|
|
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-14 02:33:56 +00:00
|
|
|
|
expect(replySpy).not.toHaveBeenCalled();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
2026-01-25 10:18:51 +05:30
|
|
|
|
it("sets command target session key for dm topic commands", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
sendMessageSpy.mockReset();
|
|
|
|
|
|
commandSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
replySpy.mockResolvedValue({ text: "response" });
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
commands: { native: true },
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
dmPolicy: "pairing",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-01 14:55:41 +05:30
|
|
|
|
readChannelAllowFromStore.mockResolvedValueOnce(["12345"]);
|
2026-01-25 10:18:51 +05:30
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
|
|
|
|
|
| ((ctx: Record<string, unknown>) => Promise<void>)
|
|
|
|
|
|
| undefined;
|
2026-01-31 16:19:20 +09:00
|
|
|
|
if (!handler) {
|
|
|
|
|
|
throw new Error("status command handler missing");
|
|
|
|
|
|
}
|
2026-01-25 10:18:51 +05:30
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 12345, type: "private" },
|
|
|
|
|
|
from: { id: 12345, username: "testuser" },
|
|
|
|
|
|
text: "/status",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
message_thread_id: 99,
|
|
|
|
|
|
},
|
|
|
|
|
|
match: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
const payload = replySpy.mock.calls[0][0];
|
|
|
|
|
|
expect(payload.CommandTargetSessionKey).toBe("agent:main:main:thread:99");
|
|
|
|
|
|
});
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
2026-01-18 22:50:57 +00:00
|
|
|
|
it("allows native DM commands for paired users", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
sendMessageSpy.mockReset();
|
|
|
|
|
|
commandSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
replySpy.mockResolvedValue({ text: "response" });
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
commands: { native: true },
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
dmPolicy: "pairing",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-01 14:55:41 +05:30
|
|
|
|
readChannelAllowFromStore.mockResolvedValueOnce(["12345"]);
|
2026-01-18 22:50:57 +00:00
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
|
|
|
|
|
| ((ctx: Record<string, unknown>) => Promise<void>)
|
|
|
|
|
|
| undefined;
|
2026-01-31 16:19:20 +09:00
|
|
|
|
if (!handler) {
|
|
|
|
|
|
throw new Error("status command handler missing");
|
|
|
|
|
|
}
|
2026-01-18 22:50:57 +00:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 12345, type: "private" },
|
|
|
|
|
|
from: { id: 12345, username: "testuser" },
|
|
|
|
|
|
text: "/status",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
},
|
|
|
|
|
|
match: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(
|
|
|
|
|
|
sendMessageSpy.mock.calls.some(
|
|
|
|
|
|
(call) => call[1] === "You are not authorized to use this command.",
|
|
|
|
|
|
),
|
|
|
|
|
|
).toBe(false);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-22 07:43:52 +00:00
|
|
|
|
it("blocks native DM commands for unpaired users", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
sendMessageSpy.mockReset();
|
|
|
|
|
|
commandSpy.mockReset();
|
|
|
|
|
|
replySpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
commands: { native: true },
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: {
|
|
|
|
|
|
dmPolicy: "pairing",
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2026-02-01 14:55:41 +05:30
|
|
|
|
readChannelAllowFromStore.mockResolvedValueOnce([]);
|
2026-01-22 07:43:52 +00:00
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = commandSpy.mock.calls.find((call) => call[0] === "status")?.[1] as
|
|
|
|
|
|
| ((ctx: Record<string, unknown>) => Promise<void>)
|
|
|
|
|
|
| undefined;
|
2026-01-31 16:19:20 +09:00
|
|
|
|
if (!handler) {
|
|
|
|
|
|
throw new Error("status command handler missing");
|
|
|
|
|
|
}
|
2026-01-22 07:43:52 +00:00
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
message: {
|
|
|
|
|
|
chat: { id: 12345, type: "private" },
|
|
|
|
|
|
from: { id: 12345, username: "testuser" },
|
|
|
|
|
|
text: "/status",
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
},
|
|
|
|
|
|
match: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(replySpy).not.toHaveBeenCalled();
|
|
|
|
|
|
expect(sendMessageSpy).toHaveBeenCalledWith(
|
|
|
|
|
|
12345,
|
|
|
|
|
|
"You are not authorized to use this command.",
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:13:05 +02:00
|
|
|
|
it("registers message_reaction handler", () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
2026-01-15 18:13:49 +02:00
|
|
|
|
const reactionHandler = onSpy.mock.calls.find((call) => call[0] === "message_reaction");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
expect(reactionHandler).toBeDefined();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:34:40 +02:00
|
|
|
|
it("enqueues system event for reaction", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
2026-01-13 21:34:40 +02:00
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 500 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada", username: "ada_bot" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "👍" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledWith(
|
2026-01-13 21:13:05 +02:00
|
|
|
|
"Telegram reaction added: 👍 by Ada (@ada_bot) on msg 42",
|
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
|
contextKey: expect.stringContaining("telegram:reaction:add:1234:42:9"),
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("skips reaction when reactionNotifications is off", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
wasSentByBot.mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "off" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 501 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "👍" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-16 20:51:39 +00:00
|
|
|
|
it("defaults reactionNotifications to own", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-16 20:51:39 +00:00
|
|
|
|
wasSentByBot.mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 502 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 43,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "👍" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
2026-01-16 20:51:39 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:34:40 +02:00
|
|
|
|
it("allows reaction in all mode regardless of message sender", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
wasSentByBot.mockReturnValue(false);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
2026-01-13 21:34:40 +02:00
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
2026-01-13 21:34:40 +02:00
|
|
|
|
update: { update_id: 503 },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 99,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
2026-01-13 21:34:40 +02:00
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "🎉" }],
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledWith(
|
2026-01-13 21:34:40 +02:00
|
|
|
|
"Telegram reaction added: 🎉 by Ada on msg 99",
|
|
|
|
|
|
expect.any(Object),
|
|
|
|
|
|
);
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 17:20:17 +00:00
|
|
|
|
it("skips reaction in own mode when message is not sent by bot", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-15 17:20:17 +00:00
|
|
|
|
wasSentByBot.mockReturnValue(false);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "own" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 503 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 99,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "🎉" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
|
2026-01-15 17:20:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("allows reaction in own mode when message is sent by bot", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-15 17:20:17 +00:00
|
|
|
|
wasSentByBot.mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "own" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 503 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 99,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "🎉" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
2026-01-15 17:20:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("skips reaction from bot users", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-15 17:20:17 +00:00
|
|
|
|
wasSentByBot.mockReturnValue(true);
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 503 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 99,
|
|
|
|
|
|
user: { id: 9, first_name: "Bot", is_bot: true },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "🎉" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
|
2026-01-15 17:20:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:34:40 +02:00
|
|
|
|
it("skips reaction removal (only processes added reactions)", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
2026-01-13 21:34:40 +02:00
|
|
|
|
update: { update_id: 504 },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
2026-01-13 21:34:40 +02:00
|
|
|
|
message_id: 42,
|
2026-01-13 21:13:05 +02:00
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
2026-01-13 21:34:40 +02:00
|
|
|
|
old_reaction: [{ type: "emoji", emoji: "👍" }],
|
|
|
|
|
|
new_reaction: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
|
2026-01-13 21:34:40 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-17 09:01:30 -05:00
|
|
|
|
it("enqueues one event per added emoji reaction", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
|
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
|
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 505 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 1234, type: "private" },
|
|
|
|
|
|
message_id: 42,
|
|
|
|
|
|
user: { id: 9, first_name: "Ada" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [{ type: "emoji", emoji: "👍" }],
|
|
|
|
|
|
new_reaction: [
|
|
|
|
|
|
{ type: "emoji", emoji: "👍" },
|
|
|
|
|
|
{ type: "emoji", emoji: "🔥" },
|
|
|
|
|
|
{ type: "emoji", emoji: "🎉" },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
expect(enqueueSystemEventSpy.mock.calls.map((call) => call[0])).toEqual([
|
|
|
|
|
|
"Telegram reaction added: 🔥 by Ada on msg 42",
|
|
|
|
|
|
"Telegram reaction added: 🎉 by Ada on msg 42",
|
|
|
|
|
|
]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-04 22:35:51 +00:00
|
|
|
|
it("routes forum group reactions to the general topic (thread id not available on reactions)", async () => {
|
2026-01-13 21:34:40 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:34:40 +02:00
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
2026-02-04 22:35:51 +00:00
|
|
|
|
// MessageReactionUpdated does not include message_thread_id in the Bot API,
|
|
|
|
|
|
// so forum reactions always route to the general topic (1).
|
2026-01-13 21:34:40 +02:00
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 505 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 5678, type: "supergroup", is_forum: true },
|
|
|
|
|
|
message_id: 100,
|
|
|
|
|
|
user: { id: 10, first_name: "Bob", username: "bob_user" },
|
|
|
|
|
|
date: 1736380800,
|
2026-01-13 21:13:05 +02:00
|
|
|
|
old_reaction: [],
|
2026-01-13 21:34:40 +02:00
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "🔥" }],
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledWith(
|
2026-01-13 21:34:40 +02:00
|
|
|
|
"Telegram reaction added: 🔥 by Bob (@bob_user) on msg 100",
|
|
|
|
|
|
expect.objectContaining({
|
2026-02-04 22:35:51 +00:00
|
|
|
|
sessionKey: expect.stringContaining("telegram:group:5678:topic:1"),
|
2026-01-13 21:34:40 +02:00
|
|
|
|
contextKey: expect.stringContaining("telegram:reaction:add:5678:100:10"),
|
|
|
|
|
|
}),
|
2026-01-13 21:13:05 +02:00
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-13 21:34:40 +02:00
|
|
|
|
it("uses correct session key for forum group reactions in general topic", async () => {
|
2026-01-13 21:13:05 +02:00
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:13:05 +02:00
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
2026-01-13 21:34:40 +02:00
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
2026-01-13 21:34:40 +02:00
|
|
|
|
update: { update_id: 506 },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
messageReaction: {
|
2026-01-13 21:34:40 +02:00
|
|
|
|
chat: { id: 5678, type: "supergroup", is_forum: true },
|
|
|
|
|
|
message_id: 101,
|
|
|
|
|
|
// No message_thread_id - should default to general topic (1)
|
|
|
|
|
|
user: { id: 10, first_name: "Bob" },
|
2026-01-13 21:13:05 +02:00
|
|
|
|
date: 1736380800,
|
2026-01-13 21:34:40 +02:00
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "👀" }],
|
2026-01-13 21:13:05 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledWith(
|
2026-01-13 21:34:40 +02:00
|
|
|
|
"Telegram reaction added: 👀 by Bob on msg 101",
|
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
|
sessionKey: expect.stringContaining("telegram:group:5678:topic:1"),
|
|
|
|
|
|
contextKey: expect.stringContaining("telegram:reaction:add:5678:101:10"),
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("uses correct session key for regular group reactions without topic", async () => {
|
|
|
|
|
|
onSpy.mockReset();
|
2026-02-16 02:19:18 +00:00
|
|
|
|
enqueueSystemEventSpy.mockReset();
|
2026-01-13 21:34:40 +02:00
|
|
|
|
|
|
|
|
|
|
loadConfig.mockReturnValue({
|
|
|
|
|
|
channels: {
|
|
|
|
|
|
telegram: { dmPolicy: "open", reactionNotifications: "all" },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
createTelegramBot({ token: "tok" });
|
|
|
|
|
|
const handler = getOnHandler("message_reaction") as (
|
|
|
|
|
|
ctx: Record<string, unknown>,
|
|
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
|
|
await handler({
|
|
|
|
|
|
update: { update_id: 507 },
|
|
|
|
|
|
messageReaction: {
|
|
|
|
|
|
chat: { id: 9999, type: "group" },
|
|
|
|
|
|
message_id: 200,
|
|
|
|
|
|
user: { id: 11, first_name: "Charlie" },
|
|
|
|
|
|
date: 1736380800,
|
|
|
|
|
|
old_reaction: [],
|
|
|
|
|
|
new_reaction: [{ type: "emoji", emoji: "❤️" }],
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-02-16 02:19:18 +00:00
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
expect(enqueueSystemEventSpy).toHaveBeenCalledWith(
|
2026-01-13 21:34:40 +02:00
|
|
|
|
"Telegram reaction added: ❤️ by Charlie on msg 200",
|
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
|
sessionKey: expect.stringContaining("telegram:group:9999"),
|
|
|
|
|
|
contextKey: expect.stringContaining("telegram:reaction:add:9999:200:11"),
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
// Verify session key does NOT contain :topic:
|
2026-02-17 14:30:36 +09:00
|
|
|
|
const eventOptions = enqueueSystemEventSpy.mock.calls[0]?.[1] as {
|
|
|
|
|
|
sessionKey?: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
const sessionKey = eventOptions.sessionKey ?? "";
|
2026-01-13 21:34:40 +02:00
|
|
|
|
expect(sessionKey).not.toContain(":topic:");
|
2026-01-13 21:13:05 +02:00
|
|
|
|
});
|
|
|
|
|
|
});
|