reply-to-channel.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { describe, expect, it, vi } from "vitest";
  2. import { replyToChannelHandler } from "../src/tool/handlers/reply-to-channel.js";
  3. import type { ToolExecutionContext } from "../src/tool/types.js";
  4. function fixture() {
  5. const node = {
  6. type: "channelMention",
  7. refId: "m1",
  8. name: "Ryan Bot",
  9. targetId: "ou_target",
  10. idType: "open_id",
  11. entityType: "unknown",
  12. channel: "feishu",
  13. };
  14. const source = {
  15. botId: "bot",
  16. appId: "app",
  17. provider: "feishu",
  18. chatId: "oc_group",
  19. messageId: "om_input",
  20. senderId: "ou_sender",
  21. senderName: "Sender",
  22. authorizationId: "auth",
  23. contentParts: [node],
  24. };
  25. const user = {
  26. info: {
  27. id: "user",
  28. role: "user",
  29. metadata: { conversationInputIntent: { sourceCommandId: "input", botGroupSource: source } },
  30. },
  31. parts: [],
  32. };
  33. const assistant = {
  34. info: { id: "assistant", role: "assistant", parentID: "user" },
  35. parts: [{ type: "tool", callID: "call" }],
  36. };
  37. const messages = [
  38. user,
  39. assistant,
  40. { info: { id: "queued", role: "user", metadata: {} }, parts: [] },
  41. ];
  42. const reply = vi.fn(async () => ({
  43. status: "sent",
  44. deliveryId: "delivery",
  45. providerMessageId: "om_sent",
  46. }));
  47. const abort = new AbortController();
  48. const context = {
  49. sessionId: "task",
  50. toolCallId: "call",
  51. abortSignal: abort.signal,
  52. sessionStore: { messages: async () => messages },
  53. topicResourcePort: { reply },
  54. } as unknown as ToolExecutionContext;
  55. return { context, reply, abort, user, assistant };
  56. }
  57. const input = {
  58. parts: [
  59. { type: "mention", refId: "m1" },
  60. { type: "text", text: " 请确认" },
  61. ],
  62. };
  63. describe("ReplyToChannel", () => {
  64. it("forwards typed names and preserves clarification without claiming delivery", async () => {
  65. const f = fixture();
  66. const clarification = {
  67. status: "needs_clarification",
  68. unresolved: [
  69. {
  70. name: "Alex",
  71. reason: "ambiguous",
  72. candidates: [{ ref: "opaque", name: "Alex", label: "Alex (member)", kind: "user" }],
  73. truncated: false,
  74. },
  75. { name: "Missing", reason: "not_found", candidates: [], truncated: false },
  76. ],
  77. };
  78. f.reply.mockResolvedValue(clarification as never);
  79. const parts = [
  80. { type: "mentionName", name: "Alex" },
  81. { type: "mentionName", name: "Missing" },
  82. ];
  83. expect(await replyToChannelHandler({ parts }, f.context)).toEqual(clarification);
  84. expect(f.reply.mock.calls[0]?.[0]).toMatchObject({ parts });
  85. });
  86. it("uses executing assistant parent rather than a newer queued user input", async () => {
  87. const { context, reply } = fixture();
  88. expect(await replyToChannelHandler(input, context)).toMatchObject({
  89. status: "sent",
  90. providerMessageId: "om_sent",
  91. });
  92. expect(reply).toHaveBeenCalledWith(
  93. {
  94. taskId: "task",
  95. inputId: "input",
  96. toolCallId: "call",
  97. parts: input.parts,
  98. },
  99. expect.objectContaining({ signal: context.abortSignal }),
  100. );
  101. });
  102. it.each(["cancelled", "missingCall", "missingSource"])(
  103. "rejects %s before sending",
  104. async (kind) => {
  105. const f = fixture();
  106. if (kind === "cancelled") f.abort.abort();
  107. if (kind === "missingCall") f.assistant.parts = [];
  108. if (kind === "missingSource") f.user.info.metadata = {} as typeof f.user.info.metadata;
  109. await expect(replyToChannelHandler(input, f.context)).rejects.toThrow();
  110. expect(f.reply).not.toHaveBeenCalled();
  111. },
  112. );
  113. it("forwards desktop follow-up without botGroupSource for Host binding resolution", async () => {
  114. const f = fixture();
  115. f.user.info.metadata = {
  116. conversationInputIntent: { sourceCommandId: "desktop-input" },
  117. } as typeof f.user.info.metadata;
  118. await expect(replyToChannelHandler(input, f.context)).resolves.toMatchObject({
  119. status: "sent",
  120. });
  121. expect(f.reply).toHaveBeenCalledWith(
  122. { taskId: "task", inputId: "desktop-input", toolCallId: "call", parts: input.parts },
  123. expect.anything(),
  124. );
  125. });
  126. it("preserves unknown receipt status without claiming success", async () => {
  127. const f = fixture();
  128. f.reply.mockResolvedValue({ status: "unknown", deliveryId: "delivery", providerMessageId: "" });
  129. expect(await replyToChannelHandler(input, f.context)).toMatchObject({ status: "unknown" });
  130. });
  131. });