fix(telegram): add DM allowFrom regression tests

This commit is contained in:
Ayaan Zaidi
2026-02-09 22:51:40 +05:30
committed by Ayaan Zaidi
parent 29425e27e5
commit a77afe618d
2 changed files with 57 additions and 1 deletions

View File

@@ -266,7 +266,8 @@ export const buildTelegramMessageContext = async ({
if (created) {
logger.info(
{
chatId: candidate,
chatId: String(chatId),
senderUserId: senderUserId ?? undefined,
username: from?.username,
firstName: from?.first_name,
lastName: from?.last_name,

View File

@@ -274,6 +274,61 @@ describe("createTelegramBot", () => {
expect(replySpy).toHaveBeenCalledTimes(1);
});
it("matches direct message allowFrom against sender user id when chat id differs", async () => {
onSpy.mockReset();
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
replySpy.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: {
allowFrom: ["123456789"],
},
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
chat: { id: 777777777, type: "private" },
from: { id: 123456789, username: "testuser" },
text: "hello",
date: 1736380800,
},
me: { username: "openclaw_bot" },
getFile: async () => ({ download: async () => new Uint8Array() }),
});
expect(replySpy).toHaveBeenCalledTimes(1);
});
it("falls back to direct message chat id when sender user id is missing", async () => {
onSpy.mockReset();
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;
replySpy.mockReset();
loadConfig.mockReturnValue({
channels: {
telegram: {
allowFrom: ["123456789"],
},
},
});
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
chat: { id: 123456789, type: "private" },
text: "hello",
date: 1736380800,
},
me: { username: "openclaw_bot" },
getFile: async () => ({ download: async () => new Uint8Array() }),
});
expect(replySpy).toHaveBeenCalledTimes(1);
});
it("allows group messages with wildcard in allowFrom when groupPolicy is 'allowlist'", async () => {
onSpy.mockReset();
const replySpy = replyModule.__replySpy as unknown as ReturnType<typeof vi.fn>;