feishuGroupMentions.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. readGroupMessage,
  4. readGroupMentionText,
  5. } from "../src/bots/providers/feishuGroupMessages.js";
  6. describe("group mention gate", () => {
  7. const mentions = [
  8. { key: "@_user_1", id: { open_id: "ou_bot" }, name: "Bot" },
  9. { key: "@_user_2", id: { open_id: "ou_member" }, name: "Member" },
  10. ];
  11. it("preserves the current bot mention in canonical text", () => {
  12. expect(readGroupMentionText("@_user_1 /new", mentions, "ou_bot", "user")).toBe("@Bot /new");
  13. expect(readGroupMentionText("@_user_1 ask @_user_2", mentions, "ou_bot", "user")).toBe(
  14. "@Bot ask @Member",
  15. );
  16. });
  17. it("rejects other bots, missing identity and text that merely looks like an at", () => {
  18. expect(readGroupMentionText("@Bot hello", [], "ou_bot", "user")).toBeNull();
  19. expect(readGroupMentionText("@_user_1 hello", mentions, "other", "user")).toBeNull();
  20. expect(readGroupMentionText("@_user_1 hello", mentions, "", "user")).toBeNull();
  21. expect(readGroupMentionText("@_user_1 hello", mentions, "ou_bot", "app")).toBeNull();
  22. });
  23. });
  24. describe("native mention assignment", () => {
  25. const mentions = [
  26. { key: "@_self", id: { open_id: "ou_self" }, name: "Outline Bot" },
  27. { key: "@_other", id: { open_id: "ou_other" }, name: "Travel Bot" },
  28. ];
  29. it("retains every recipient and their position in a multi-bot assignment", () => {
  30. const parsed = readGroupMessage(
  31. "Trip\n@_self handles outline\n@_other handles travel",
  32. mentions,
  33. "ou_self",
  34. "user",
  35. )!;
  36. expect(parsed.text).toBe("Trip\n@Outline Bot handles outline\n@Travel Bot handles travel");
  37. expect(
  38. parsed.contentParts?.filter((p) => p.type === "channelMention").map((p) => p.targetId),
  39. ).toEqual(["ou_self", "ou_other"]);
  40. expect(parsed.commandText).toBe(parsed.text);
  41. });
  42. it("strips only leading self mentions from the command copy", () => {
  43. const parsed = readGroupMessage("@_self /workspace", mentions, "ou_self", "user")!;
  44. expect(parsed.text).toBe("@Outline Bot /workspace");
  45. expect(parsed.commandText).toBe("/workspace");
  46. expect(readGroupMessage("@_other /stop @_self", mentions, "ou_self", "user")?.commandText).toBe(
  47. "@Travel Bot /stop @Outline Bot",
  48. );
  49. expect(readGroupMessage("@Outline Bot /stop", [], "ou_self", "user")?.commandText).toBe(
  50. "@Outline Bot /stop",
  51. );
  52. expect(readGroupMessage("@_self", mentions, "ou_self", "user")?.commandText).toBe("");
  53. });
  54. });