providerProvisioningEnvironmentCoordinator.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, it, vi } from "vitest";
  2. import { ProviderProvisioningEnvironmentCoordinator } from "../src/main/providerProvisioningEnvironmentCoordinator.js";
  3. describe("Provider Provisioning Environment Coordinator", () => {
  4. it("coalesces multiple registrations of one Environment into one initial sync", async () => {
  5. const executeA = vi.fn(async () => undefined);
  6. const executeB = vi.fn(async () => undefined);
  7. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  8. const first = coordinator.register("ssh:one", "host-a", executeA);
  9. const second = coordinator.register("ssh:one", "host-b", executeB);
  10. await first.initialSync;
  11. await second.initialSync;
  12. expect(executeA.mock.calls.length + executeB.mock.calls.length).toBe(1);
  13. });
  14. it("syncs different Environments independently", async () => {
  15. const executeA = vi.fn(async () => undefined);
  16. const executeB = vi.fn(async () => undefined);
  17. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  18. await Promise.all([
  19. coordinator.register("ssh:one", "host-a", executeA).initialSync,
  20. coordinator.register("docker:two", "host-b", executeB).initialSync,
  21. ]);
  22. expect(executeA).toHaveBeenCalledOnce();
  23. expect(executeB).toHaveBeenCalledOnce();
  24. });
  25. it("runs one trailing refresh with the latest generation", async () => {
  26. let finishFirst: (() => void) | undefined;
  27. const execute = vi
  28. .fn<() => Promise<void>>()
  29. .mockImplementationOnce(() => new Promise<void>((resolve) => (finishFirst = resolve)))
  30. .mockResolvedValue(undefined);
  31. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  32. const registration = coordinator.register("ssh:one", "host-a", execute);
  33. await vi.waitFor(() => expect(execute).toHaveBeenCalledOnce());
  34. const saveA = coordinator.requestAll("personal-config");
  35. const saveB = coordinator.requestAll("credential");
  36. finishFirst?.();
  37. await Promise.all([registration.initialSync, saveA, saveB]);
  38. expect(execute).toHaveBeenCalledTimes(2);
  39. });
  40. it("keeps one pending intent while offline and drains it when the Environment returns", async () => {
  41. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  42. const first = coordinator.register(
  43. "ssh:one",
  44. "host-a",
  45. vi.fn(async () => undefined),
  46. );
  47. await first.initialSync;
  48. first.dispose();
  49. void coordinator.requestAll("personal-config");
  50. const execute = vi.fn(async () => undefined);
  51. const second = coordinator.register("ssh:one", "host-b", execute);
  52. await second.initialSync;
  53. expect(execute).toHaveBeenCalledOnce();
  54. });
  55. it("hands an interrupted generation to another online Host", async () => {
  56. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  57. let rejectFirst: ((error: Error) => void) | undefined;
  58. const first = coordinator.register(
  59. "ssh:one",
  60. "host-a",
  61. () => new Promise<void>((_resolve, reject) => (rejectFirst = reject)),
  62. );
  63. const executeB = vi.fn(async () => undefined);
  64. const second = coordinator.register("ssh:one", "host-b", executeB);
  65. await vi.waitFor(() => expect(rejectFirst).toBeDefined());
  66. first.dispose();
  67. rejectFirst?.(new Error("host exited"));
  68. await second.initialSync;
  69. expect(executeB).toHaveBeenCalledOnce();
  70. });
  71. it("does not spin on an ordinary failure and retries on the next real trigger", async () => {
  72. const execute = vi
  73. .fn<() => Promise<void>>()
  74. .mockRejectedValueOnce(new Error("remote unavailable"))
  75. .mockResolvedValue(undefined);
  76. const coordinator = new ProviderProvisioningEnvironmentCoordinator();
  77. const registration = coordinator.register("ssh:one", "host-a", execute);
  78. await expect(registration.initialSync).rejects.toThrow("remote unavailable");
  79. expect(execute).toHaveBeenCalledOnce();
  80. await coordinator.requestAll("personal-config");
  81. expect(execute).toHaveBeenCalledTimes(2);
  82. });
  83. });