cronBotDelivery.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { describe, expect, it, vi } from "vitest";
  2. import { watchCronRunBotDelivery } from "../src/host/cronBotDelivery.js";
  3. describe("watchCronRunBotDelivery", () => {
  4. it("等待 Bot continuous watcher 完成订阅后才返回给 prompt 派发方", async () => {
  5. const order: string[] = [];
  6. let releaseWatcher!: () => void;
  7. const watcherReady = new Promise<void>((resolve) => {
  8. releaseWatcher = resolve;
  9. });
  10. const target = {
  11. provider: "weixin" as const,
  12. botId: "weixin-1",
  13. providerUserId: "wx_chat_1",
  14. chatType: "private" as const,
  15. };
  16. const repo = {
  17. getBotDeliveryTarget: vi.fn(async () => target),
  18. };
  19. const botsService = {
  20. watchAutomationRun: vi.fn(async () => {
  21. order.push("watch-start");
  22. await watcherReady;
  23. order.push("watch-ready");
  24. }),
  25. };
  26. const preparing = watchCronRunBotDelivery({
  27. automationId: "automation-1",
  28. runId: "cron-run-1",
  29. workspaceKey: "ssh://host/workspace",
  30. workspacePath: "/workspace",
  31. workspaceIdentity: "ssh://host/workspace",
  32. taskId: "task-1",
  33. repo,
  34. botsService,
  35. }).then(() => order.push("dispatch-ready"));
  36. await Promise.resolve();
  37. expect(order).toEqual(["watch-start"]);
  38. releaseWatcher();
  39. await preparing;
  40. expect(order).toEqual(["watch-start", "watch-ready", "dispatch-ready"]);
  41. expect(repo.getBotDeliveryTarget).toHaveBeenCalledWith("automation-1", "ssh://host/workspace");
  42. expect(botsService.watchAutomationRun).toHaveBeenCalledWith({
  43. target,
  44. runId: "cron-run-1",
  45. taskId: "task-1",
  46. workspacePath: "/workspace",
  47. workspaceIdentity: "ssh://host/workspace",
  48. });
  49. });
  50. it("没有 Bot 来源时不创建 watcher", async () => {
  51. const botsService = { watchAutomationRun: vi.fn() };
  52. await expect(
  53. watchCronRunBotDelivery({
  54. automationId: "automation-1",
  55. workspaceKey: "/workspace",
  56. workspacePath: "/workspace",
  57. taskId: "task-1",
  58. repo: { getBotDeliveryTarget: vi.fn(async () => undefined) },
  59. botsService,
  60. }),
  61. ).resolves.toBe(false);
  62. expect(botsService.watchAutomationRun).not.toHaveBeenCalled();
  63. });
  64. });