bot-private-task-sync.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import type { BotProvider, 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-PS-01 private task output subscription", () => {
  10. const originalFetch = globalThis.fetch;
  11. let harness: ReturnType<typeof createBotsServiceHarness> | undefined;
  12. afterEach(async () => {
  13. await harness?.service.disposeAllAndWait();
  14. globalThis.fetch = originalFetch;
  15. });
  16. for (const provider of ["feishu", "telegram", "weixin"] satisfies BotProvider[]) {
  17. it(`keeps ${provider} subscribed across IM, desktop and mobile turns`, async () => {
  18. const fixture = await readBotSyntheticFixture("private-task-sync.json");
  19. expect(fixture.caseId).toBe("BOT-E2E-PS-01");
  20. const requests: RecordedRequest[] = [];
  21. const feishuFetch = createRecordingFeishuFetch(requests);
  22. globalThis.fetch = async (input, init) => {
  23. if (provider === "feishu") return feishuFetch(input, init);
  24. requests.push({
  25. url: String(input),
  26. method: init?.method ?? "GET",
  27. body: String(init?.body ?? ""),
  28. });
  29. return new Response(JSON.stringify({ ok: true, ret: 0 }));
  30. };
  31. const config = createFeishuConfig();
  32. const bot = {
  33. ...config.bots[0]!,
  34. provider,
  35. replyMode:
  36. provider === "feishu" ? ("streaming_card" as const) : ("assistant_changes" as const),
  37. };
  38. config.bots = [bot];
  39. harness = createBotsServiceHarness({ config });
  40. await harness.service.handleInboundMessage({
  41. botId: bot.id,
  42. actor: {
  43. botId: bot.id,
  44. provider,
  45. providerUserId: bot.providerUserId!,
  46. chatType: "private",
  47. providerContextToken: "e2e-context",
  48. },
  49. text: `${fixture.promptMarker} run`,
  50. });
  51. // 微信首次入站只激活,随后才形成 task。
  52. if (harness.subscriptions.length === 0)
  53. await harness.service.handleInboundMessage({
  54. botId: bot.id,
  55. actor: {
  56. botId: bot.id,
  57. provider,
  58. providerUserId: bot.providerUserId!,
  59. chatType: "private",
  60. providerContextToken: "e2e-context",
  61. },
  62. text: `${fixture.promptMarker} run again`,
  63. });
  64. expect(harness.subscriptions).toHaveLength(1);
  65. // Bot 输入的命令信封 clientId 带 `bot:` 前缀(所有渠道,含没有回推目标的 Telegram):CLI 据此
  66. // 不对 IM 用户声明桌面设计模式(Host 的 inputOrigin 对 Bot 也是 desktop)。
  67. await browser.waitUntil(async () => harness!.sendPromptCalls.length > 0, {
  68. timeout: 10_000,
  69. timeoutMsg: `${provider} 私聊输入没有发到 task service`,
  70. });
  71. for (const call of harness.sendPromptCalls) {
  72. expect((call as { clientId?: string }).clientId).toMatch(
  73. new RegExp(`^bot:${bot.id}:[^:]+:${bot.providerUserId}$`, "u"),
  74. );
  75. }
  76. expect(harness.subscriptions[0]!.params.deliveryKind).toBe("bot-channel-continuous");
  77. const emit = harness.subscriptions[0]!.listener;
  78. const run = async (id: string, inputOrigin: "desktop" | "mobile") => {
  79. const base = { taskId: "task-e2e-bot", traceId: id, inputId: id };
  80. await emit({
  81. ...base,
  82. type: "task_run_started",
  83. startedAt: Date.now(),
  84. inputOrigin,
  85. } as ZCodeStreamEvent);
  86. await emit({ ...base, type: "agent_message_chunk", content: id } as ZCodeStreamEvent);
  87. await emit({ ...base, type: "task_complete", stopReason: "end_turn" } as ZCodeStreamEvent);
  88. };
  89. await run("IM_REPLY", "desktop");
  90. await run("DESKTOP_REPLY", "desktop");
  91. await run("MOBILE_REPLY", "mobile");
  92. const creations = requests.filter(
  93. (request) =>
  94. request.method === "POST" &&
  95. /sendMessage|sendmessage|im\/v1\/messages\?/.test(request.url),
  96. );
  97. expect(creations).toHaveLength(3);
  98. expect(creations[1]!.body).toContain("DESKTOP_REPLY");
  99. expect(creations[1]!.body).not.toContain("IM_REPLY");
  100. expect(creations[2]!.body).toContain("MOBILE_REPLY");
  101. await harness.service.saveConfig({ ...config, bots: [{ ...bot, enabled: false }] });
  102. const count = requests.length;
  103. await run("LATE_REPLY", "desktop");
  104. expect(requests).toHaveLength(count);
  105. });
  106. }
  107. });