diff --git a/extensions/feishu/src/bot.checkBotMentioned.test.ts b/extensions/feishu/src/bot.checkBotMentioned.test.ts index a7ea6792275..8aa80b31f3f 100644 --- a/extensions/feishu/src/bot.checkBotMentioned.test.ts +++ b/extensions/feishu/src/bot.checkBotMentioned.test.ts @@ -1,12 +1,12 @@ import { describe, it, expect } from "vitest"; -import { parseFeishuMessageEvent } from "./bot.js"; +import { parseFeishuMessageEvent, type FeishuMessageEvent } from "./bot.js"; // Helper to build a minimal FeishuMessageEvent for testing function makeEvent( chatType: "p2p" | "group" | "private", mentions?: Array<{ key: string; name: string; id: { open_id?: string } }>, text = "hello", -) { +): FeishuMessageEvent { return { sender: { sender_id: { user_id: "u1", open_id: "ou_sender" }, @@ -22,7 +22,7 @@ function makeEvent( }; } -function makePostEvent(content: unknown) { +function makePostEvent(content: unknown): FeishuMessageEvent { return { sender: { sender_id: { user_id: "u1", open_id: "ou_sender" } }, message: { @@ -36,7 +36,7 @@ function makePostEvent(content: unknown) { }; } -function makeShareChatEvent(content: unknown) { +function makeShareChatEvent(content: unknown): FeishuMessageEvent { return { sender: { sender_id: { user_id: "u1", open_id: "ou_sender" } }, message: { @@ -55,15 +55,15 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { it("returns mentionedBot=false when there are no mentions", () => { const event = makeEvent("group", []); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.mentionedBot).toBe(false); }); it("falls back to sender user_id when open_id is missing", () => { const event = makeEvent("p2p", []); - (event as any).sender.sender_id = { user_id: "u_mobile_only" }; + event.sender.sender_id = { user_id: "u_mobile_only" }; - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.senderOpenId).toBe("u_mobile_only"); expect(ctx.senderId).toBe("u_mobile_only"); }); @@ -72,7 +72,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "Bot", id: { open_id: BOT_OPEN_ID } }, ]); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.mentionedBot).toBe(true); }); @@ -80,7 +80,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "OpenClaw Bot (Alias)", id: { open_id: BOT_OPEN_ID } }, ]); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID, "OpenClaw Bot"); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID, "OpenClaw Bot"); expect(ctx.mentionedBot).toBe(true); }); @@ -88,7 +88,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } }, ]); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.mentionedBot).toBe(false); }); @@ -96,7 +96,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } }, ]); - const ctx = parseFeishuMessageEvent(event as any, undefined); + const ctx = parseFeishuMessageEvent(event, undefined); expect(ctx.mentionedBot).toBe(false); }); @@ -104,7 +104,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeEvent("group", [ { key: "@_user_1", name: "Alice", id: { open_id: "ou_alice" } }, ]); - const ctx = parseFeishuMessageEvent(event as any, ""); + const ctx = parseFeishuMessageEvent(event, ""); expect(ctx.mentionedBot).toBe(false); }); @@ -114,7 +114,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { [{ key: "@_bot_1", name: ".*", id: { open_id: BOT_OPEN_ID } }], "@NotBot hello", ); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.content).toBe("@NotBot hello"); }); @@ -124,7 +124,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { [{ key: ".*", name: "Bot", id: { open_id: BOT_OPEN_ID } }], "hello world", ); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.content).toBe("hello world"); }); @@ -136,7 +136,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { [{ tag: "text", text: "What does this document say" }], ], }); - const ctx = parseFeishuMessageEvent(event as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(event, BOT_OPEN_ID); expect(ctx.mentionedBot).toBe(true); }); @@ -144,7 +144,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makePostEvent({ content: [[{ tag: "text", text: "hello" }]], }); - const ctx = parseFeishuMessageEvent(event as any, "ou_bot_123"); + const ctx = parseFeishuMessageEvent(event, "ou_bot_123"); expect(ctx.mentionedBot).toBe(false); }); @@ -155,7 +155,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { [{ tag: "text", text: "hello" }], ], }); - const ctx = parseFeishuMessageEvent(event as any, "ou_bot_123"); + const ctx = parseFeishuMessageEvent(event, "ou_bot_123"); expect(ctx.mentionedBot).toBe(false); }); @@ -169,7 +169,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { [{ tag: "code_block", language: "ts", text: "const x = 1;" }], ], }); - const ctx = parseFeishuMessageEvent(event as any, "ou_bot_123"); + const ctx = parseFeishuMessageEvent(event, "ou_bot_123"); expect(ctx.content).toContain("before `inline()`"); expect(ctx.content).toContain("```ts\nconst x = 1;\n```"); }); @@ -179,7 +179,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { body: "Merged and Forwarded Message", share_chat_id: "sc_abc123", }); - const ctx = parseFeishuMessageEvent(event as any, "ou_bot_123"); + const ctx = parseFeishuMessageEvent(event, "ou_bot_123"); expect(ctx.content).toBe("Merged and Forwarded Message"); }); @@ -187,7 +187,7 @@ describe("parseFeishuMessageEvent – mentionedBot", () => { const event = makeShareChatEvent({ share_chat_id: "sc_abc123", }); - const ctx = parseFeishuMessageEvent(event as any, "ou_bot_123"); + const ctx = parseFeishuMessageEvent(event, "ou_bot_123"); expect(ctx.content).toBe("[Forwarded message: sc_abc123]"); }); }); diff --git a/extensions/feishu/src/bot.stripBotMention.test.ts b/extensions/feishu/src/bot.stripBotMention.test.ts index 1c23c8fced9..75947d0c7f6 100644 --- a/extensions/feishu/src/bot.stripBotMention.test.ts +++ b/extensions/feishu/src/bot.stripBotMention.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from "vitest"; -import { parseFeishuMessageEvent } from "./bot.js"; +import { parseFeishuMessageEvent, type FeishuMessageEvent } from "./bot.js"; function makeEvent( text: string, mentions?: Array<{ key: string; name: string; id: { open_id?: string; user_id?: string } }>, chatType: "p2p" | "group" = "p2p", -) { +): FeishuMessageEvent { return { sender: { sender_id: { user_id: "u1", open_id: "ou_sender" } }, message: { @@ -23,7 +23,7 @@ const BOT_OPEN_ID = "ou_bot"; describe("normalizeMentions (via parseFeishuMessageEvent)", () => { it("returns original text when mentions are missing", () => { - const ctx = parseFeishuMessageEvent(makeEvent("hello world", undefined) as any, BOT_OPEN_ID); + const ctx = parseFeishuMessageEvent(makeEvent("hello world", undefined), BOT_OPEN_ID); expect(ctx.content).toBe("hello world"); }); @@ -31,7 +31,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { const ctx = parseFeishuMessageEvent( makeEvent("@_bot_1 hello", [ { key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }, - ]) as any, + ]), BOT_OPEN_ID, ); expect(ctx.content).toBe("hello"); @@ -43,7 +43,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { "@_bot_1 hello", [{ key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }], "group", - ) as any, + ), BOT_OPEN_ID, ); expect(ctx.content).toBe("hello"); @@ -55,7 +55,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { "@_bot_1 /model", [{ key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }], "group", - ) as any, + ), BOT_OPEN_ID, ); expect(ctx.content).toBe("/model"); @@ -66,7 +66,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { makeEvent("@_bot_1 @_user_alice hello", [ { key: "@_bot_1", name: "Bot", id: { open_id: "ou_bot" } }, { key: "@_user_alice", name: "Alice", id: { open_id: "ou_alice" } }, - ]) as any, + ]), BOT_OPEN_ID, ); expect(ctx.content).toBe('Alice hello'); @@ -76,7 +76,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { const ctx = parseFeishuMessageEvent( makeEvent("@_user_1 hi", [ { key: "@_user_1", name: "Alice", id: { user_id: "uid_alice" } }, - ]) as any, + ]), BOT_OPEN_ID, ); expect(ctx.content).toBe("@Alice hi"); @@ -84,7 +84,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { it("falls back to plain @name when no id is present", () => { const ctx = parseFeishuMessageEvent( - makeEvent("@_unknown hey", [{ key: "@_unknown", name: "Nobody", id: {} }]) as any, + makeEvent("@_unknown hey", [{ key: "@_unknown", name: "Nobody", id: {} }]), BOT_OPEN_ID, ); expect(ctx.content).toBe("@Nobody hey"); @@ -92,7 +92,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { it("treats mention key regex metacharacters as literal text", () => { const ctx = parseFeishuMessageEvent( - makeEvent("hello world", [{ key: ".*", name: "Bot", id: { open_id: "ou_bot" } }]) as any, + makeEvent("hello world", [{ key: ".*", name: "Bot", id: { open_id: "ou_bot" } }]), BOT_OPEN_ID, ); expect(ctx.content).toBe("hello world"); @@ -103,7 +103,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { makeEvent("@_bot_1 hi @_user_2", [ { key: "@_bot_1", name: "Bot One", id: { open_id: "ou_bot_1" } }, { key: "@_user_2", name: "User Two", id: { open_id: "ou_user_2" } }, - ]) as any, + ]), BOT_OPEN_ID, ); expect(ctx.content).toBe( @@ -115,7 +115,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { const ctx = parseFeishuMessageEvent( makeEvent("@_user_1 hi", [ { key: "@_user_1", name: "$& the user", id: { open_id: "ou_x" } }, - ]) as any, + ]), BOT_OPEN_ID, ); // $ is preserved literally (no $& pattern substitution); & is not escaped in tag body @@ -126,7 +126,7 @@ describe("normalizeMentions (via parseFeishuMessageEvent)", () => { const ctx = parseFeishuMessageEvent( makeEvent("@_user_1 test", [ { key: "@_user_1", name: "