conversation-session-bot-continuous-worker.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { generateTraceId, type BotActor } from "@zcode/shared";
  2. import { createBotWorkerIntegration } from "#e2e/helpers/bot-worker-integration";
  3. import { installBotPlatformFixture } from "#e2e/helpers/bot-platform-fixture";
  4. import { startConversationModelProviderReplayServer } from "#e2e/helpers/model-provider-replay";
  5. const groupActor: BotActor = {
  6. botId: "bot-worker",
  7. provider: "feishu",
  8. chatType: "group",
  9. chatId: "oc_worker",
  10. providerUserId: "ou_e2e_user",
  11. };
  12. describe("BOT-E2E-GR-02/PS-01 actual worker lifecycle", () => {
  13. it("promotes a group queue item after the first execution finishes normally", async function () {
  14. this.timeout(120000);
  15. const replay = await startConversationModelProviderReplayServer(
  16. "conversation-session-bot-continuous-worker",
  17. );
  18. const replayFetch = globalThis.fetch;
  19. const platform = installBotPlatformFixture();
  20. let cleanup: Awaited<ReturnType<typeof createBotWorkerIntegration>> | undefined;
  21. try {
  22. const host = (cleanup = await createBotWorkerIntegration(replay.baseUrl));
  23. await host.bot.service.handleInboundMessage({
  24. botId: "bot-worker",
  25. actor: groupActor,
  26. text: "/enable",
  27. });
  28. await host.bot.service.handleInboundMessage({
  29. botId: "bot-worker",
  30. actor: { ...groupActor, providerMessageId: "om_running" },
  31. text: "E2E_BOT_GROUP_RUNNING",
  32. });
  33. await browser.waitUntil(
  34. async () => host.observedTextEvents.some((text) => text.includes("E2E_GROUP_RUNNING")),
  35. { timeout: 30000 },
  36. );
  37. const queued = await host.bot.service.handleInboundMessage({
  38. botId: "bot-worker",
  39. actor: { ...groupActor, providerMessageId: "om_queued", providerUserId: "ou_member" },
  40. text: "E2E_BOT_GROUP_QUEUED",
  41. });
  42. expect(queued.map((reply) => reply.text).join("\n")).toMatch(/等待执行|Waiting to run/);
  43. // 固定延迟会在慢机器上提前结束首轮;确认入队后才释放模型响应。
  44. const released = await replayFetch(`${replay.baseUrl}/__e2e__/release/queue-running`, {
  45. method: "POST",
  46. signal: AbortSignal.timeout(5000),
  47. });
  48. expect(released.status).toBe(204);
  49. const key = JSON.stringify(["bot-worker", "oc_worker"]);
  50. await browser.waitUntil(
  51. async () =>
  52. Object.values(host.bot.readState().bots[key]?.group?.deliveries ?? {}).some(
  53. (delivery) =>
  54. delivery.status === "sent" && delivery.text.includes("E2E_GROUP_QUEUED_DONE"),
  55. ),
  56. { timeout: 45000 },
  57. );
  58. const deliveries = Object.values(host.bot.readState().bots[key]!.group!.deliveries!);
  59. expect(
  60. deliveries.filter(
  61. (delivery) => delivery.text.includes("E2E_GROUP_FINISHED") && delivery.status === "sent",
  62. ),
  63. ).toHaveLength(1);
  64. expect(
  65. deliveries.filter(
  66. (delivery) =>
  67. delivery.text.includes("E2E_GROUP_QUEUED_DONE") &&
  68. delivery.replyToMessageId === "om_queued",
  69. ),
  70. ).toHaveLength(1);
  71. } finally {
  72. await cleanup?.dispose();
  73. platform.restore();
  74. await replay.stop();
  75. }
  76. });
  77. it("delivers an IM turn and a later desktop turn through the persistent private subscription", async function () {
  78. this.timeout(120000);
  79. const replay = await startConversationModelProviderReplayServer(
  80. "conversation-session-bot-continuous-worker",
  81. );
  82. const platform = installBotPlatformFixture();
  83. let cleanup: Awaited<ReturnType<typeof createBotWorkerIntegration>> | undefined;
  84. try {
  85. const host = (cleanup = await createBotWorkerIntegration(replay.baseUrl));
  86. const actor: BotActor = {
  87. ...groupActor,
  88. chatType: "private",
  89. providerMessageId: "om_private",
  90. };
  91. await host.bot.service.handleInboundMessage({
  92. botId: "bot-worker",
  93. actor,
  94. text: "E2E_BOT_PRIVATE_FIRST",
  95. });
  96. const posts = (marker: string) =>
  97. platform.requests.filter(
  98. (r) =>
  99. r.method === "POST" && r.url.includes("/im/v1/messages?") && r.body?.includes(marker),
  100. );
  101. await browser.waitUntil(async () => posts("E2E_PRIVATE_FIRST_DONE").length === 1, {
  102. timeout: 45000,
  103. });
  104. const taskId = host.bot.readState().bots["bot-worker"]!.activeTaskId!;
  105. await host.task.sendPrompt({
  106. taskId,
  107. content: "E2E_BOT_PRIVATE_NEXT",
  108. traceId: generateTraceId("private-followup"),
  109. modelSelection: host.modelSelection,
  110. });
  111. await browser.waitUntil(async () => posts("E2E_PRIVATE_NEXT_DONE").length === 1, {
  112. timeout: 45000,
  113. });
  114. expect(posts("E2E_PRIVATE_FIRST_DONE")).toHaveLength(1);
  115. expect(posts("E2E_PRIVATE_NEXT_DONE")[0]!.body).not.toContain("E2E_PRIVATE_FIRST_DONE");
  116. // 实际调用桌面 task service;没有把 inputOrigin=mobile 当作手机远控证明。
  117. } finally {
  118. await cleanup?.dispose();
  119. platform.restore();
  120. await replay.stop();
  121. }
  122. });
  123. });