channelMentionResolver.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { describe, expect, it } from "vitest";
  2. import { resolveChannelMentionNames } from "../src/bots/channelMentionResolver.js";
  3. import { channelReplyResultSchema } from "@zcode/shared";
  4. const resolve = (names: string[], members: Record<string, string> | undefined) =>
  5. resolveChannelMentionNames(
  6. names.map((name) => ({ type: "mentionName", name })),
  7. [],
  8. members,
  9. "scope",
  10. "feishu",
  11. );
  12. describe("channel recipient name matching", () => {
  13. it("sends two unique partial Chinese names as their full identities", () => {
  14. expect(resolve(["小明", "小红"], { ou_a: "王小明", ou_b: "李小红" })).toMatchObject({
  15. parts: [
  16. { name: "王小明", targetId: "ou_a" },
  17. { name: "李小红", targetId: "ou_b" },
  18. ],
  19. });
  20. });
  21. it("prefers the unique exact name among containing names", () => {
  22. expect(resolve([" Alex "], { ou_a: "Alex", ou_b: "Alex Smith" })).toMatchObject({
  23. parts: [{ targetId: "ou_a" }],
  24. });
  25. });
  26. it("preserves a multiword name and normalizes Unicode and case", () => {
  27. expect(
  28. resolve([" ALEX Smith "], { ou_a: "Alex Smith", ou_b: "Alex", ou_c: "Smith" }),
  29. ).toMatchObject({ parts: [{ targetId: "ou_a" }] });
  30. });
  31. it("reports every unresolved recipient after the first failure without returning partial delivery", () => {
  32. const result = resolve(["Missing", "Alex", "Nobody", "Unique"], {
  33. ou_a: "Alex Smith",
  34. ou_b: "Alex Jones",
  35. ou_c: "Unique",
  36. });
  37. expect(result).toMatchObject({
  38. status: "needs_clarification",
  39. unresolved: [
  40. { name: "Missing", reason: "not_found" },
  41. {
  42. name: "Alex",
  43. reason: "ambiguous",
  44. candidates: [{ name: "Alex Smith" }, { name: "Alex Jones" }],
  45. },
  46. { name: "Nobody", reason: "not_found" },
  47. ],
  48. });
  49. expect(result).not.toHaveProperty("parts");
  50. expect(channelReplyResultSchema.safeParse(result).success).toBe(true);
  51. });
  52. it("does not choose between duplicate exact names", () => {
  53. expect(resolve(["Alex"], { ou_a: "Alex", ou_b: "Alex", ou_c: "Alex Smith" })).toMatchObject({
  54. status: "needs_clarification",
  55. });
  56. });
  57. it("reports lookup failure for all requested names", () => {
  58. expect(resolve(["Alex", "Robin"], undefined)).toMatchObject({
  59. unresolved: [
  60. { name: "Alex", reason: "unavailable" },
  61. { name: "Robin", reason: "unavailable" },
  62. ],
  63. });
  64. });
  65. });