feishu-topic-interactions.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import type { BotActor, ZCodeStreamEvent } from "@zcode/shared";
  2. import {
  3. createBotsServiceHarness,
  4. createFeishuConfig,
  5. createRecordingFeishuFetch,
  6. readBotSyntheticFixture,
  7. type RecordedRequest,
  8. } from "#e2e/bots/helpers/bots-service-harness";
  9. describe("BOT-E2E-TP-03 topic interaction routing", () => {
  10. for (const provider of ["feishu", "lark"] as const) {
  11. for (const mentionedBot of [false, true]) {
  12. it(`${provider} mentioned=${mentionedBot}: keeps permission and question cards in their authorized topic`, async () => {
  13. const fixture = await readBotSyntheticFixture("feishu-topic-interactions.json");
  14. expect(fixture.classification).toBe("synthetic");
  15. const requests: RecordedRequest[] = [];
  16. const originalFetch = globalThis.fetch;
  17. const record = createRecordingFeishuFetch(requests);
  18. globalThis.fetch = async (input, init) => {
  19. const url = String(input);
  20. if (url.includes("/im/v1/chats/"))
  21. return Response.json({ code: 0, data: { name: "E2E Topic", chat_mode: "group" } });
  22. if (url.includes("/im/v1/messages?"))
  23. return Response.json({
  24. code: 0,
  25. data: {
  26. items: [
  27. {
  28. message_id: "om_request",
  29. chat_id: "oc_topic",
  30. thread_id: "omt_cards",
  31. msg_type: "text",
  32. body: { content: JSON.stringify({ text: fixture.promptMarker }) },
  33. sender: { id: "ou_e2e_user", sender_type: "user" },
  34. create_time: "2",
  35. },
  36. {
  37. message_id: "om_root",
  38. chat_id: "oc_topic",
  39. thread_id: "omt_cards",
  40. msg_type: "text",
  41. body: { content: JSON.stringify({ text: "Topic root" }) },
  42. sender: { id: "app-e2e", sender_type: "app" },
  43. create_time: "1",
  44. },
  45. ],
  46. has_more: false,
  47. },
  48. });
  49. if (url.endsWith("/im/v1/messages/om_root"))
  50. return Response.json({
  51. code: 0,
  52. data: {
  53. items: [
  54. {
  55. message_id: "om_root",
  56. chat_id: "oc_topic",
  57. thread_id: "omt_cards",
  58. msg_type: "text",
  59. body: { content: JSON.stringify({ text: "Topic root" }) },
  60. sender: { id: "app-e2e", sender_type: "app" },
  61. create_time: "1",
  62. },
  63. ],
  64. },
  65. });
  66. return record(input, init);
  67. };
  68. const botId = `${provider}-topic-card-e2e`;
  69. const h = createBotsServiceHarness({ config: createFeishuConfig(provider, botId) });
  70. h.zcodeTaskService.submitBotGroupInput = async (params: { commandId: string }) => ({
  71. commandId: params.commandId,
  72. status: "accepted",
  73. revisionAtDecision: 1,
  74. result: { type: "inputAccepted", inputId: params.commandId, delivery: "startNow" },
  75. });
  76. const actor: BotActor = {
  77. botId,
  78. provider,
  79. chatType: "group",
  80. chatId: "oc_topic",
  81. providerUserId: "ou_e2e_user",
  82. };
  83. try {
  84. await h.service.handleInboundMessage({ botId, actor, text: "/enable" });
  85. await h.service.handleInboundMessage({
  86. botId,
  87. actor: {
  88. ...actor,
  89. threadId: "omt_cards",
  90. rootMessageId: "om_root",
  91. providerMessageId: "om_request",
  92. mentionedBot,
  93. },
  94. text: fixture.promptMarker!,
  95. mentionedBot,
  96. topicRootIsCurrentBot: true,
  97. });
  98. expect(h.subscriptions).toHaveLength(1);
  99. requests.length = 0;
  100. const listener = h.subscriptions[0]!.listener;
  101. const admitted = (await h.service.getBotStates()).find(
  102. (state) => state.group?.threadId === "omt_cards",
  103. )!;
  104. const inputId = Object.keys(admitted.group!.inputs!)[0]!;
  105. expect(admitted.group!.inputs![inputId]!.source.mentionedBot).toBe(mentionedBot);
  106. await listener({
  107. type: "task_run_started",
  108. taskId: "task-e2e-bot",
  109. traceId: "run-cards",
  110. inputId,
  111. startedAt: Date.now(),
  112. inputOrigin: "desktop",
  113. } as ZCodeStreamEvent);
  114. await listener({
  115. type: "permission_request",
  116. taskId: "task-e2e-bot",
  117. traceId: "run-cards",
  118. requestId: "permission-cards",
  119. kind: "read",
  120. description: "Read file",
  121. raw: {},
  122. options: [
  123. {
  124. optionId: "allow",
  125. kind: "allow_once",
  126. name: "Allow",
  127. response: { decision: "allow" },
  128. },
  129. ],
  130. } as ZCodeStreamEvent);
  131. await listener({
  132. type: "elicitation_request",
  133. taskId: "task-e2e-bot",
  134. traceId: "run-cards",
  135. requestId: "question-cards",
  136. message: "Which option?",
  137. options: [{ value: "first", label: "First" }],
  138. } as ZCodeStreamEvent);
  139. const posts = requests.filter(
  140. (r) =>
  141. r.method === "POST" && (r.url.endsWith("/reply") || r.url.includes("/messages?")),
  142. );
  143. expect(posts).toHaveLength(1);
  144. // 引用本次触发消息;root 仅作为话题锚点,不再冒充被回复者。
  145. expect(posts[0]!.url).toContain("/om_request/reply");
  146. expect(JSON.parse(posts[0]!.body!).reply_in_thread).toBe(true);
  147. const updates = requests.filter((r) => r.method === "PATCH");
  148. expect(updates).toHaveLength(1);
  149. const group = (await h.service.getBotStates()).find(
  150. (s) => s.group?.threadId === "omt_cards",
  151. )!.group!;
  152. for (const request of [posts[0]!, updates[0]!]) {
  153. const card = JSON.stringify(JSON.parse(JSON.parse(request.body!).content));
  154. // 权限审批始终提醒管理员;普通问答只提醒明确 @ 机器人的发送者。
  155. expect(card.includes("<at")).toBe(request === posts[0] || mentionedBot);
  156. expect(card).toContain("omt_cards");
  157. expect(card).toContain("task-e2e-bot");
  158. expect(card).toContain(group.authorizationId!);
  159. }
  160. } finally {
  161. await h.service.disposeAllAndWait();
  162. globalThis.fetch = originalFetch;
  163. }
  164. });
  165. }
  166. }
  167. });