mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:31:24 +00:00
refactor(test): dedupe telegram bot mention scaffolding
This commit is contained in:
@@ -15,6 +15,31 @@ const loadConfig = getLoadConfigMock();
|
||||
|
||||
const ORIGINAL_TZ = process.env.TZ;
|
||||
describe("createTelegramBot", () => {
|
||||
function resetHarnessSpies() {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
sendMessageSpy.mockReset();
|
||||
setMessageReactionSpy.mockReset();
|
||||
setMyCommandsSpy.mockReset();
|
||||
}
|
||||
|
||||
function getMessageHandler() {
|
||||
createTelegramBot({ token: "tok" });
|
||||
return getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
}
|
||||
|
||||
async function dispatchMessage(params: {
|
||||
message: Record<string, unknown>;
|
||||
me?: Record<string, unknown>;
|
||||
}) {
|
||||
const handler = getMessageHandler();
|
||||
await handler({
|
||||
message: params.message,
|
||||
me: params.me ?? { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.TZ = "UTC";
|
||||
});
|
||||
@@ -25,8 +50,7 @@ describe("createTelegramBot", () => {
|
||||
// groupPolicy tests
|
||||
|
||||
it("accepts group messages when mentionPatterns match (without @botUsername)", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
agents: {
|
||||
@@ -44,10 +68,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "group", title: "Test Group" },
|
||||
text: "bert: introduce yourself",
|
||||
@@ -55,8 +76,6 @@ describe("createTelegramBot", () => {
|
||||
message_id: 1,
|
||||
from: { id: 9, first_name: "Ada" },
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
@@ -72,8 +91,7 @@ describe("createTelegramBot", () => {
|
||||
});
|
||||
|
||||
it("accepts group messages when mentionPatterns match even if another user is mentioned", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
agents: {
|
||||
@@ -91,10 +109,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "group", title: "Test Group" },
|
||||
text: "bert: hello @alice",
|
||||
@@ -103,8 +118,6 @@ describe("createTelegramBot", () => {
|
||||
message_id: 3,
|
||||
from: { id: 9, first_name: "Ada" },
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
@@ -112,8 +125,7 @@ describe("createTelegramBot", () => {
|
||||
});
|
||||
|
||||
it("keeps group envelope headers stable (sender identity is separate)", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
agents: {
|
||||
@@ -129,10 +141,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 42, type: "group", title: "Ops" },
|
||||
text: "hello",
|
||||
@@ -145,8 +154,6 @@ describe("createTelegramBot", () => {
|
||||
username: "ada",
|
||||
},
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
@@ -161,9 +168,7 @@ describe("createTelegramBot", () => {
|
||||
);
|
||||
});
|
||||
it("reacts to mention-gated group messages when ackReaction is enabled", async () => {
|
||||
onSpy.mockReset();
|
||||
setMessageReactionSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
messages: {
|
||||
@@ -179,10 +184,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "group", title: "Test Group" },
|
||||
text: "bert hello",
|
||||
@@ -190,13 +192,12 @@ describe("createTelegramBot", () => {
|
||||
message_id: 123,
|
||||
from: { id: 9, first_name: "Ada" },
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(setMessageReactionSpy).toHaveBeenCalledWith(7, 123, [{ type: "emoji", emoji: "👀" }]);
|
||||
});
|
||||
it("clears native commands when disabled", () => {
|
||||
resetHarnessSpies();
|
||||
loadConfig.mockReturnValue({
|
||||
commands: { native: false },
|
||||
});
|
||||
@@ -206,8 +207,7 @@ describe("createTelegramBot", () => {
|
||||
expect(setMyCommandsSpy).toHaveBeenCalledWith([]);
|
||||
});
|
||||
it("skips group messages when requireMention is enabled and no mention matches", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
messages: { groupChat: { mentionPatterns: ["\\bbert\\b"] } },
|
||||
@@ -219,10 +219,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "group", title: "Test Group" },
|
||||
text: "hello everyone",
|
||||
@@ -230,15 +227,12 @@ describe("createTelegramBot", () => {
|
||||
message_id: 2,
|
||||
from: { id: 9, first_name: "Ada" },
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).not.toHaveBeenCalled();
|
||||
});
|
||||
it("allows group messages when requireMention is enabled but mentions cannot be detected", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
loadConfig.mockReturnValue({
|
||||
messages: { groupChat: { mentionPatterns: [] } },
|
||||
@@ -250,10 +244,7 @@ describe("createTelegramBot", () => {
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "group", title: "Test Group" },
|
||||
text: "hello everyone",
|
||||
@@ -262,7 +253,6 @@ describe("createTelegramBot", () => {
|
||||
from: { id: 9, first_name: "Ada" },
|
||||
},
|
||||
me: {},
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
@@ -270,14 +260,9 @@ describe("createTelegramBot", () => {
|
||||
expect(payload.WasMentioned).toBe(false);
|
||||
});
|
||||
it("includes reply-to context when a Telegram reply is received", async () => {
|
||||
onSpy.mockReset();
|
||||
sendMessageSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
resetHarnessSpies();
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
await dispatchMessage({
|
||||
message: {
|
||||
chat: { id: 7, type: "private" },
|
||||
text: "Sure, see below",
|
||||
@@ -288,8 +273,6 @@ describe("createTelegramBot", () => {
|
||||
from: { first_name: "Ada" },
|
||||
},
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||
|
||||
Reference in New Issue
Block a user