automationManagementStoreCreateLimit.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { IZCodeAgentService } from "@zcode/services";
  3. import {
  4. AUTOMATION_CREATE_LIMIT,
  5. AUTOMATION_CREATE_LIMIT_ERROR_CODE,
  6. type ZCodeAutomation,
  7. } from "@zcode/shared";
  8. import { useAutomationManagementStore } from "@/store/automationManagementStore.js";
  9. function makeAutomation(index: number): ZCodeAutomation {
  10. const statuses = ["active", "paused", "completed", "failed"] as const;
  11. return {
  12. automationId: `automation-${index}`,
  13. title: `task-${index}`,
  14. cronExpr: "0 9 * * *",
  15. prompt: "check repository",
  16. workspaceKey: "/tmp/ws",
  17. workspacePath: "/tmp/ws",
  18. locationKind: "local",
  19. recurring: true,
  20. runCount: 0,
  21. enabled: statuses[index % statuses.length] === "active",
  22. lifecycleStatus: statuses[index % statuses.length]!,
  23. dispatchStatus: "idle",
  24. dispatchAttempts: 0,
  25. createdAt: index,
  26. updatedAt: index,
  27. };
  28. }
  29. describe("automationManagementStore create limit", () => {
  30. afterEach(() => {
  31. useAutomationManagementStore.setState(
  32. useAutomationManagementStore.getInitialState(),
  33. true,
  34. );
  35. });
  36. it("全状态任务合计达到 20 条后不发送创建 RPC", async () => {
  37. const agentService = {
  38. createAutomation: vi.fn(),
  39. } as unknown as IZCodeAgentService;
  40. useAutomationManagementStore.setState({
  41. workspacePath: "/tmp/ws",
  42. workspaceIdentity: null,
  43. automations: Array.from({ length: AUTOMATION_CREATE_LIMIT }, (_, index) =>
  44. makeAutomation(index),
  45. ),
  46. });
  47. await expect(
  48. useAutomationManagementStore.getState().createAutomation(
  49. {
  50. title: "task-over-limit",
  51. cronExpr: "0 10 * * *",
  52. prompt: "check limits",
  53. },
  54. agentService,
  55. ),
  56. ).resolves.toBeNull();
  57. expect(agentService.createAutomation).not.toHaveBeenCalled();
  58. expect(useAutomationManagementStore.getState().error).toContain(
  59. AUTOMATION_CREATE_LIMIT_ERROR_CODE,
  60. );
  61. });
  62. });