conversation-session-bot-channel-worker.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { createBotWorkerIntegration } from "../helpers/bot-worker-integration.js";
  2. import { startConversationModelProviderReplayServer } from "../helpers/model-provider-replay.js";
  3. import {
  4. createRecordingFeishuFetch,
  5. type RecordedRequest,
  6. } from "../bots/helpers/bots-service-harness.js";
  7. import { generateTraceId, type BotInboundMessage } from "@zcode/shared";
  8. describe("BOT-E2E-MN-02/FN-01 actual channel reply tool chain", () => {
  9. it("sends a trusted mention then resolves a new typed name from desktop input", async function () {
  10. this.timeout(180000);
  11. const replay = await startConversationModelProviderReplayServer(
  12. "conversation-session-bot-channel-worker",
  13. );
  14. const requests: RecordedRequest[] = [];
  15. const originalFetch = globalThis.fetch;
  16. const record = createRecordingFeishuFetch(requests);
  17. globalThis.fetch = async (input, init) => {
  18. if (!String(input).startsWith("https://open.feishu.cn/"))
  19. throw new Error("Unexpected non-fixture request");
  20. if (String(input).includes("/members?"))
  21. return Response.json({
  22. code: 0,
  23. data: {
  24. items: [{ member_id: "ou_directory", name: "目录目标" }],
  25. has_more: false,
  26. },
  27. });
  28. if (String(input).includes("/im/v1/chats/"))
  29. return Response.json({ code: 0, data: { name: "Isolated group" } });
  30. return record(input, init);
  31. };
  32. let cleanup: Awaited<ReturnType<typeof createBotWorkerIntegration>> | undefined;
  33. const input: BotInboundMessage = {
  34. botId: "bot-worker",
  35. text: "E2E_BOT_CHANNEL_FIRST 请提醒 @目标",
  36. actor: {
  37. botId: "bot-worker",
  38. provider: "feishu",
  39. chatType: "group",
  40. chatId: "oc_worker",
  41. providerUserId: "ou_e2e_user",
  42. providerMessageId: "om_first",
  43. },
  44. contentParts: [
  45. { type: "text", text: "E2E_BOT_CHANNEL_FIRST 请提醒 " },
  46. {
  47. type: "channelMention",
  48. refId: "om_first:m1",
  49. name: "目标",
  50. targetId: "ou_target",
  51. channel: "feishu",
  52. idType: "open_id",
  53. entityType: "user",
  54. },
  55. ],
  56. };
  57. try {
  58. const host = (cleanup = await createBotWorkerIntegration(
  59. replay.baseUrl,
  60. "E2E_CHANNEL_DONE_first",
  61. ));
  62. await host.bot.service.handleInboundMessage({
  63. ...input,
  64. text: "/enable",
  65. contentParts: undefined,
  66. });
  67. await host.bot.service.handleInboundMessage(input);
  68. const key = JSON.stringify(["bot-worker", "oc_worker"]);
  69. let taskId = "";
  70. await browser.waitUntil(
  71. async () => {
  72. taskId = host.bot.readState().bots[key]?.activeTaskId ?? "";
  73. return !!taskId && host.reverseReplies.length === 1;
  74. },
  75. { timeout: 60000, timeoutMsg: "真实 ReplyToChannel 未到达 Host(权限/协议/执行失败)" },
  76. );
  77. await browser.waitUntil(
  78. async () =>
  79. (await host.agent.readSession({ workspacePath: host.workspacePath, sessionId: taskId }))
  80. .session.status === "idle",
  81. { timeout: 30000 },
  82. );
  83. expect(
  84. Object.values(host.bot.readState().bots[key]!.group!.deliveries!)
  85. .filter((d) => d.contentParts)
  86. .map((d) => d.status),
  87. ).toEqual(["sent"]);
  88. expect(host.droppedTextEvents.length).toBeGreaterThan(0);
  89. await browser.waitUntil(
  90. async () =>
  91. Object.values(host.bot.readState().bots[key]!.group!.deliveries!).some(
  92. (d) => d.text === "E2E_CHANNEL_DONE_first" && d.status === "sent",
  93. ),
  94. { timeout: 15000 },
  95. );
  96. expect(
  97. Object.values(host.bot.readState().bots[key]!.group!.deliveries!).filter(
  98. (d) => d.text === "E2E_CHANNEL_DONE_first",
  99. ),
  100. ).toHaveLength(1);
  101. await host.task.sendPrompt({
  102. taskId,
  103. content: "E2E_BOT_CHANNEL_FOLLOWUP 请 @ 目录目标",
  104. traceId: generateTraceId("e2e-followup"),
  105. modelSelection: host.modelSelection,
  106. });
  107. await browser.waitUntil(
  108. async () =>
  109. host.reverseReplies.length === 2 &&
  110. (await host.agent.readSession({ workspacePath: host.workspacePath, sessionId: taskId }))
  111. .session.status === "idle",
  112. { timeout: 60000 },
  113. );
  114. const deliveries = Object.values(host.bot.readState().bots[key]!.group!.deliveries!).filter(
  115. (d) => d.contentParts,
  116. );
  117. expect(deliveries).toHaveLength(2);
  118. expect(deliveries.map((d) => d.status)).toEqual(["sent", "sent"]);
  119. expect(
  120. deliveries.map((d) => d.contentParts?.find((p) => p.type === "channelMention")?.targetId),
  121. ).toEqual(["ou_target", "ou_directory"]);
  122. const nativePosts = requests.filter(
  123. (r) =>
  124. r.method === "POST" &&
  125. (r.body?.includes("<at id=ou_target>") || r.body?.includes("<at id=ou_directory>")),
  126. );
  127. expect(nativePosts).toHaveLength(2);
  128. expect(nativePosts.every((r) => r.url.endsWith("/om_first/reply"))).toBe(true);
  129. expect(host.permissionRequests).toHaveLength(0);
  130. // 当前产品仍回传模型总结,本 case 不偷偷改变已暂停的重复回执语义。
  131. expect(host.reverseReplies[1]).not.toHaveProperty("authorizationId");
  132. } finally {
  133. await cleanup?.dispose();
  134. globalThis.fetch = originalFetch;
  135. await replay.stop();
  136. }
  137. });
  138. });