automationManagementStoreRunNow.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type {
  3. IZCodeAgentService,
  4. ZCodeAgentRunAutomationNowResult,
  5. } from "@zcode/services";
  6. import type { ZCodeAutomation } from "@zcode/shared";
  7. import { useAutomationManagementStore } from "@/store/automationManagementStore.js";
  8. function deferred<T>() {
  9. let resolve!: (value: T | PromiseLike<T>) => void;
  10. let reject!: (reason?: unknown) => void;
  11. const promise = new Promise<T>((promiseResolve, promiseReject) => {
  12. resolve = promiseResolve;
  13. reject = promiseReject;
  14. });
  15. return { promise, resolve, reject };
  16. }
  17. describe("automationManagementStore runAutomationNow", () => {
  18. afterEach(() => {
  19. useAutomationManagementStore.setState(
  20. useAutomationManagementStore.getInitialState(),
  21. true,
  22. );
  23. });
  24. it("连续立即运行同一 automation 时只发送一次 RPC", async () => {
  25. const pendingRun = deferred<ZCodeAgentRunAutomationNowResult>();
  26. const agentService = {
  27. runAutomationNow: vi.fn(() => pendingRun.promise),
  28. listAllAutomations: vi.fn(async () => []),
  29. } as unknown as IZCodeAgentService;
  30. useAutomationManagementStore.setState({
  31. workspacePath: "/tmp/ws",
  32. workspaceIdentity: null,
  33. });
  34. const first = useAutomationManagementStore
  35. .getState()
  36. .runAutomationNow("automation-1", agentService);
  37. const duplicate = await useAutomationManagementStore
  38. .getState()
  39. .runAutomationNow("automation-1", agentService);
  40. expect(duplicate).toBe("duplicate");
  41. expect(agentService.runAutomationNow).toHaveBeenCalledTimes(1);
  42. pendingRun.resolve({ status: "queued" });
  43. await expect(first).resolves.toBe("queued");
  44. expect(agentService.listAllAutomations).toHaveBeenCalledTimes(1);
  45. });
  46. it("host 拒绝重复立即运行时不再误报 queued 或刷新列表", async () => {
  47. const agentService = {
  48. runAutomationNow: vi.fn(async () => ({ status: "duplicate" as const })),
  49. listAllAutomations: vi.fn(async () => []),
  50. } as unknown as IZCodeAgentService;
  51. useAutomationManagementStore.setState({
  52. workspacePath: "/tmp/ws",
  53. workspaceIdentity: null,
  54. });
  55. await expect(
  56. useAutomationManagementStore
  57. .getState()
  58. .runAutomationNow("automation-1", agentService),
  59. ).resolves.toBe("duplicate");
  60. expect(agentService.runAutomationNow).toHaveBeenCalledTimes(1);
  61. expect(agentService.listAllAutomations).not.toHaveBeenCalled();
  62. });
  63. it("RPC 返回后恢复入口,活动 run 的重复触发交由 host 判断", async () => {
  64. const agentService = {
  65. runAutomationNow: vi
  66. .fn()
  67. .mockResolvedValueOnce({ status: "queued" as const })
  68. .mockResolvedValueOnce({ status: "duplicate" as const }),
  69. listAllAutomations: vi.fn(async () => []),
  70. } as unknown as IZCodeAgentService;
  71. useAutomationManagementStore.setState({
  72. workspacePath: "/tmp/ws",
  73. workspaceIdentity: null,
  74. });
  75. await expect(
  76. useAutomationManagementStore
  77. .getState()
  78. .runAutomationNow("automation-1", agentService),
  79. ).resolves.toBe("queued");
  80. await expect(
  81. useAutomationManagementStore
  82. .getState()
  83. .runAutomationNow("automation-1", agentService),
  84. ).resolves.toBe("duplicate");
  85. expect(agentService.runAutomationNow).toHaveBeenCalledTimes(2);
  86. });
  87. it("启停跨项目 automation 时使用 automation 自己的 workspace,并本地翻转状态", async () => {
  88. const automation = {
  89. automationId: "automation-remote",
  90. title: "remote task",
  91. cronExpr: "*/5 * * * *",
  92. prompt: "ping",
  93. workspacePath: "/tmp/remote",
  94. workspaceIdentity: "ssh://remote/tmp/remote",
  95. enabled: true,
  96. lifecycleStatus: "active",
  97. nextRunAt: Date.now() + 60_000,
  98. runCount: 0,
  99. recurring: true,
  100. createdAt: Date.now(),
  101. updatedAt: Date.now(),
  102. } satisfies ZCodeAutomation;
  103. const agentService = {
  104. setAutomationEnabled: vi.fn(async () => undefined),
  105. listAllAutomations: vi.fn(async () => [
  106. { ...automation, enabled: false, lifecycleStatus: "paused" as const },
  107. ]),
  108. } as unknown as IZCodeAgentService;
  109. useAutomationManagementStore.setState({
  110. workspacePath: "/tmp/current",
  111. workspaceIdentity: null,
  112. automations: [automation],
  113. });
  114. await useAutomationManagementStore
  115. .getState()
  116. .setEnabled("automation-remote", false, agentService);
  117. expect(agentService.setAutomationEnabled).toHaveBeenCalledWith({
  118. workspacePath: "/tmp/remote",
  119. workspaceIdentity: "ssh://remote/tmp/remote",
  120. automationId: "automation-remote",
  121. enabled: false,
  122. });
  123. expect(useAutomationManagementStore.getState().automations[0]).toEqual(
  124. expect.objectContaining({
  125. enabled: false,
  126. lifecycleStatus: "paused",
  127. }),
  128. );
  129. });
  130. });