wslDetect.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import {
  3. WSL_DISCOVERY_CACHE_TTL_MS,
  4. invalidateWSLDiscoveryCache,
  5. isWSLAvailable,
  6. listWSLDistros,
  7. } from "../src/remote/wsl-detect.js";
  8. describe("WSL discovery cache", () => {
  9. beforeEach(() => {
  10. vi.useFakeTimers();
  11. vi.spyOn(process, "platform", "get").mockReturnValue("win32");
  12. invalidateWSLDiscoveryCache();
  13. });
  14. afterEach(() => {
  15. invalidateWSLDiscoveryCache();
  16. vi.useRealTimers();
  17. vi.restoreAllMocks();
  18. });
  19. it("single-flights status discovery and refreshes after five seconds", async () => {
  20. const executor = vi.fn(async () => Buffer.from("ok"));
  21. const first = isWSLAvailable(executor);
  22. const second = isWSLAvailable(executor);
  23. await expect(Promise.all([first, second])).resolves.toEqual([true, true]);
  24. expect(executor).toHaveBeenCalledOnce();
  25. vi.advanceTimersByTime(WSL_DISCOVERY_CACHE_TTL_MS - 1);
  26. await expect(isWSLAvailable(executor)).resolves.toBe(true);
  27. expect(executor).toHaveBeenCalledOnce();
  28. vi.advanceTimersByTime(1);
  29. await expect(isWSLAvailable(executor)).resolves.toBe(true);
  30. expect(executor).toHaveBeenCalledTimes(2);
  31. });
  32. it("does not retain a rejected distro-list promise", async () => {
  33. const executor = vi
  34. .fn<() => Promise<Buffer>>()
  35. .mockRejectedValueOnce(new Error("temporary failure"))
  36. .mockResolvedValueOnce(Buffer.from(" NAME STATE VERSION\n* Ubuntu Running 2\n"));
  37. await expect(listWSLDistros(executor)).rejects.toThrow("temporary failure");
  38. await expect(listWSLDistros(executor)).resolves.toEqual([
  39. { name: "Ubuntu", isDefault: true, state: "Running", version: 2 },
  40. ]);
  41. expect(executor).toHaveBeenCalledTimes(2);
  42. });
  43. });