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