channelMention.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. channelContentPartsSchema,
  4. channelContentText,
  5. resolveChannelReplyParts,
  6. } from "@zcode/shared";
  7. import { readGroupMessage } from "../src/bots/providers/feishuGroupMessages.js";
  8. describe("trusted channel mention nodes", () => {
  9. const mentions = [
  10. { key: "@_user_1", id: { open_id: "ou_bot" }, name: "ZBot" },
  11. { key: "@_user_2", id: { open_id: "ou_target" }, name: "Ryan Bot" },
  12. ];
  13. it("keeps identity, order and text including the bot wakeup", () => {
  14. const result = readGroupMessage("@_user_1 帮我 @_user_2 确认", mentions, "ou_bot", "user");
  15. expect(result?.text).toBe("@ZBot 帮我 @Ryan Bot 确认");
  16. expect(result?.contentParts).toContainEqual(
  17. expect.objectContaining({ type: "channelMention", targetId: "ou_target", name: "Ryan Bot" }),
  18. );
  19. expect(channelContentText(result!.contentParts!)).toBe(result!.text);
  20. });
  21. it("does not remove a longer target key sharing the bot key prefix", () => {
  22. const result = readGroupMessage(
  23. "@_user_1 帮我 @_user_10",
  24. [mentions[0], { ...mentions[1], key: "@_user_10" }],
  25. "ou_bot",
  26. "user",
  27. );
  28. expect(result?.text).toBe("@ZBot 帮我 @Ryan Bot");
  29. expect(result?.contentParts).toContainEqual(expect.objectContaining({ targetId: "ou_target" }));
  30. });
  31. it("does not promote typed names into identities and rejects unknown refs", () => {
  32. const result = readGroupMessage("帮我 @Ryan Bot", [], "ou_bot", "user");
  33. expect(result?.contentParts?.some((p) => p.type === "channelMention")).not.toBe(true);
  34. expect(() => resolveChannelReplyParts([{ type: "mention", refId: "invented" }], [])).toThrow();
  35. });
  36. it("rejects conflicting identity references", () => {
  37. const parts = readGroupMessage("@_user_2", mentions, "ou_bot", "user")!.contentParts!;
  38. const node = parts.find((part) => part.type === "channelMention")!;
  39. expect(() =>
  40. resolveChannelReplyParts(
  41. [{ type: "mention", refId: node.refId }],
  42. [node, { ...node, targetId: "ou_other" }],
  43. ),
  44. ).toThrow("Conflicting");
  45. });
  46. it("rejects broadcast identities and malformed nodes", () => {
  47. expect(
  48. channelContentPartsSchema.safeParse([
  49. {
  50. type: "channelMention",
  51. refId: "m1",
  52. channel: "feishu",
  53. targetId: "all",
  54. name: "all",
  55. idType: "open_id",
  56. entityType: "unknown",
  57. },
  58. ]).success,
  59. ).toBe(false);
  60. });
  61. });