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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { generateTraceId, type BotActor } from "@zcode/shared";
  2. import { createBotWorkerIntegration } from "../helpers/bot-worker-integration.js";
  3. import { installBotPlatformFixture } from "../helpers/bot-platform-fixture.js";
  4. import { startConversationModelProviderReplayServer } from "../helpers/model-provider-replay.js";
  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 platform = installBotPlatformFixture();
  19. let cleanup: Awaited<ReturnType<typeof createBotWorkerIntegration>> | undefined;
  20. try {
  21. const host = (cleanup = await createBotWorkerIntegration(replay.baseUrl));
  22. await host.bot.service.handleInboundMessage({
  23. botId: "bot-worker",
  24. actor: groupActor,
  25. text: "/enable",
  26. });
  27. await host.bot.service.handleInboundMessage({
  28. botId: "bot-worker",
  29. actor: { ...groupActor, providerMessageId: "om_running" },
  30. text: "E2E_BOT_GROUP_RUNNING",
  31. });
  32. await browser.waitUntil(
  33. async () => host.observedTextEvents.some((text) => text.includes("E2E_GROUP_RUNNING")),
  34. { timeout: 30000 },
  35. );
  36. const queued = await host.bot.service.handleInboundMessage({
  37. botId: "bot-worker",
  38. actor: { ...groupActor, providerMessageId: "om_queued", providerUserId: "ou_member" },
  39. text: "E2E_BOT_GROUP_QUEUED",
  40. });
  41. expect(queued.map((reply) => reply.text).join("\n")).toMatch(/等待执行|Waiting to run/);
  42. const key = JSON.stringify(["bot-worker", "oc_worker"]);
  43. await browser.waitUntil(
  44. async () =>
  45. Object.values(host.bot.readState().bots[key]?.group?.deliveries ?? {}).some(
  46. (delivery) =>
  47. delivery.status === "sent" && delivery.text.includes("E2E_GROUP_QUEUED_DONE"),
  48. ),
  49. { timeout: 45000 },
  50. );
  51. const deliveries = Object.values(host.bot.readState().bots[key]!.group!.deliveries!);
  52. expect(
  53. deliveries.filter(
  54. (delivery) => delivery.text.includes("E2E_GROUP_FINISHED") && delivery.status === "sent",
  55. ),
  56. ).toHaveLength(1);
  57. expect(
  58. deliveries.filter(
  59. (delivery) =>
  60. delivery.text.includes("E2E_GROUP_QUEUED_DONE") &&
  61. delivery.replyToMessageId === "om_queued",
  62. ),
  63. ).toHaveLength(1);
  64. } finally {
  65. await cleanup?.dispose();
  66. platform.restore();
  67. await replay.stop();
  68. }
  69. });
  70. it("delivers an IM turn and a later desktop turn through the persistent private subscription", async function () {
  71. this.timeout(120000);
  72. const replay = await startConversationModelProviderReplayServer(
  73. "conversation-session-bot-continuous-worker",
  74. );
  75. const platform = installBotPlatformFixture();
  76. let cleanup: Awaited<ReturnType<typeof createBotWorkerIntegration>> | undefined;
  77. try {
  78. const host = (cleanup = await createBotWorkerIntegration(replay.baseUrl));
  79. const actor: BotActor = {
  80. ...groupActor,
  81. chatType: "private",
  82. providerMessageId: "om_private",
  83. };
  84. await host.bot.service.handleInboundMessage({
  85. botId: "bot-worker",
  86. actor,
  87. text: "E2E_BOT_PRIVATE_FIRST",
  88. });
  89. const posts = (marker: string) =>
  90. platform.requests.filter(
  91. (r) =>
  92. r.method === "POST" && r.url.includes("/im/v1/messages?") && r.body?.includes(marker),
  93. );
  94. await browser.waitUntil(async () => posts("E2E_PRIVATE_FIRST_DONE").length === 1, {
  95. timeout: 45000,
  96. });
  97. const taskId = host.bot.readState().bots["bot-worker"]!.activeTaskId!;
  98. await host.task.sendPrompt({
  99. taskId,
  100. content: "E2E_BOT_PRIVATE_NEXT",
  101. traceId: generateTraceId("private-followup"),
  102. modelSelection: host.modelSelection,
  103. });
  104. await browser.waitUntil(async () => posts("E2E_PRIVATE_NEXT_DONE").length === 1, {
  105. timeout: 45000,
  106. });
  107. expect(posts("E2E_PRIVATE_FIRST_DONE")).toHaveLength(1);
  108. expect(posts("E2E_PRIVATE_NEXT_DONE")[0]!.body).not.toContain("E2E_PRIVATE_FIRST_DONE");
  109. // 实际调用桌面 task service;没有把 inputOrigin=mobile 当作手机远控证明。
  110. } finally {
  111. await cleanup?.dispose();
  112. platform.restore();
  113. await replay.stop();
  114. }
  115. });
  116. });