useGitAutoRefresh.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @vitest-environment jsdom
  2. import { renderHook, waitFor } from "@testing-library/react";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. const { fileWatcherService, systemService } = vi.hoisted(() => ({
  5. fileWatcherService: {
  6. watch: vi.fn(async () => ({ id: "watch-1" })),
  7. unwatch: vi.fn(async () => {}),
  8. disposeAll: vi.fn(),
  9. onDynamicChange: vi.fn(() => () => ({ dispose: vi.fn() })),
  10. },
  11. systemService: {
  12. info: vi.fn(async () => ({ homedir: "/home/tester", platform: "linux" })),
  13. },
  14. }));
  15. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  16. useWorkspaceServices: () => ({ fileWatcherService, systemService }),
  17. }));
  18. import { useGitAutoRefresh } from "@/hooks/useGitAutoRefresh.js";
  19. describe("useGitAutoRefresh", () => {
  20. beforeEach(() => {
  21. fileWatcherService.watch.mockClear();
  22. fileWatcherService.unwatch.mockClear();
  23. fileWatcherService.onDynamicChange.mockClear();
  24. systemService.info.mockClear();
  25. });
  26. it("uses the workspace Host platform and never recursively watches a Linux workspace root", async () => {
  27. renderHook(() =>
  28. useGitAutoRefresh({
  29. workspacePath: "/repo",
  30. workspaceIdentity: "remote:ssh:linux:/repo",
  31. remoteSessionId: "remote-session-1",
  32. gitSummary: {
  33. workspacePath: "/repo",
  34. repoRoot: "/repo",
  35. workspaceInRepoPath: ".",
  36. autoRefreshWatchPaths: [
  37. { path: "/repo", recursive: true },
  38. { path: "/repo/.git", recursive: true },
  39. ],
  40. branchName: "main",
  41. trackingBranchName: null,
  42. headRefType: "branch",
  43. ahead: 0,
  44. behind: 0,
  45. isDirty: false,
  46. isGitAvailable: true,
  47. isRepository: true,
  48. },
  49. gitSummaryWorkspaceKey: "remote:ssh:linux:/repo",
  50. enabled: true,
  51. onRefreshGit: vi.fn(),
  52. }),
  53. );
  54. await waitFor(() => expect(fileWatcherService.watch).toHaveBeenCalledTimes(1));
  55. expect(fileWatcherService.watch).toHaveBeenCalledWith({
  56. path: "/repo/.git",
  57. recursive: true,
  58. });
  59. expect(systemService.info).toHaveBeenCalledTimes(1);
  60. });
  61. });