gitAutoRefresh.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildGitAutoRefreshWatchPaths,
  4. parseGitAutoRefreshWatchPaths,
  5. shouldEnableGitAutoRefreshForWorkspace,
  6. stringifyGitAutoRefreshWatchPaths,
  7. } from "@/lib/gitAutoRefresh.js";
  8. describe("buildGitAutoRefreshWatchPaths", () => {
  9. it("does not watch stale Git state after switching workspace identity", () => {
  10. expect(
  11. shouldEnableGitAutoRefreshForWorkspace({
  12. enabled: true,
  13. currentWorkspaceKey: "remote:ssh:root@server-b:/root",
  14. summaryWorkspaceKey: "remote:ssh:root@server-a:/root",
  15. }),
  16. ).toBe(false);
  17. expect(
  18. shouldEnableGitAutoRefreshForWorkspace({
  19. enabled: true,
  20. currentWorkspaceKey: "remote:ssh:root@server-b:/root",
  21. summaryWorkspaceKey: "remote:ssh:root@server-b:/root",
  22. }),
  23. ).toBe(true);
  24. });
  25. it("keeps the recursive workspace watcher on non-Linux platforms", () => {
  26. expect(
  27. buildGitAutoRefreshWatchPaths(
  28. {
  29. workspacePath: "/repo",
  30. repoRoot: "/repo",
  31. isGitAvailable: true,
  32. isRepository: true,
  33. autoRefreshWatchPaths: [{ path: "/repo/.git", recursive: true }],
  34. },
  35. { platform: "darwin" },
  36. ),
  37. ).toEqual([
  38. {
  39. path: "/repo",
  40. recursive: true,
  41. },
  42. {
  43. path: "/repo/.git",
  44. recursive: true,
  45. },
  46. ]);
  47. });
  48. it("watches only Git metadata paths on Linux", () => {
  49. expect(
  50. buildGitAutoRefreshWatchPaths(
  51. {
  52. workspacePath: "/home/user",
  53. repoRoot: "/home/user",
  54. isGitAvailable: true,
  55. isRepository: true,
  56. autoRefreshWatchPaths: [
  57. { path: "/home/user", recursive: true },
  58. { path: "/home/user/.git", recursive: true },
  59. ],
  60. },
  61. { platform: "linux" },
  62. ),
  63. ).toEqual([
  64. {
  65. path: "/home/user/.git",
  66. recursive: true,
  67. },
  68. ]);
  69. });
  70. it("keeps the conservative metadata-only policy before the workspace Host platform is known", () => {
  71. expect(
  72. buildGitAutoRefreshWatchPaths({
  73. workspacePath: "/repo",
  74. repoRoot: "/repo",
  75. isGitAvailable: true,
  76. isRepository: true,
  77. autoRefreshWatchPaths: [
  78. { path: "/repo", recursive: true },
  79. { path: "/repo/.git", recursive: true },
  80. ],
  81. }),
  82. ).toEqual([
  83. {
  84. path: "/repo/.git",
  85. recursive: true,
  86. },
  87. ]);
  88. });
  89. it("does not reintroduce a legacy workspace path from an old server summary on Linux", () => {
  90. expect(
  91. buildGitAutoRefreshWatchPaths(
  92. {
  93. workspacePath: "/repo",
  94. repoRoot: "/repo",
  95. isGitAvailable: true,
  96. isRepository: true,
  97. autoRefreshWatchPaths: [
  98. { path: "/repo/", recursive: true },
  99. { path: "/repo/.git", recursive: true },
  100. ],
  101. },
  102. { platform: "linux" },
  103. ),
  104. ).toEqual([
  105. {
  106. path: "/repo/.git",
  107. recursive: true,
  108. },
  109. ]);
  110. });
  111. it("also watches Git metadata paths returned by the repository summary", () => {
  112. expect(
  113. buildGitAutoRefreshWatchPaths(
  114. {
  115. workspacePath: "/worktree",
  116. repoRoot: "/worktree",
  117. isGitAvailable: true,
  118. isRepository: true,
  119. autoRefreshWatchPaths: [
  120. { path: "/main/.git/worktrees/worktree", recursive: true },
  121. { path: "/main/.git", recursive: true },
  122. ],
  123. },
  124. { platform: "win32" },
  125. ),
  126. ).toEqual([
  127. {
  128. path: "/worktree",
  129. recursive: true,
  130. },
  131. {
  132. path: "/main/.git/worktrees/worktree",
  133. recursive: true,
  134. },
  135. {
  136. path: "/main/.git",
  137. recursive: true,
  138. },
  139. ]);
  140. });
  141. it("does not watch when git is unavailable or the workspace is not a repository", () => {
  142. expect(
  143. buildGitAutoRefreshWatchPaths({
  144. workspacePath: "/repo",
  145. repoRoot: "/repo",
  146. isGitAvailable: false,
  147. isRepository: true,
  148. }),
  149. ).toEqual([]);
  150. expect(
  151. buildGitAutoRefreshWatchPaths({
  152. workspacePath: "/repo",
  153. repoRoot: "/repo",
  154. isGitAvailable: true,
  155. isRepository: false,
  156. }),
  157. ).toEqual([]);
  158. });
  159. it("keeps the same signature for equivalent watch paths", () => {
  160. const first = buildGitAutoRefreshWatchPaths(
  161. {
  162. workspacePath: "/repo",
  163. repoRoot: "/repo",
  164. isGitAvailable: true,
  165. isRepository: true,
  166. autoRefreshWatchPaths: [{ path: "/repo/.git", recursive: true }],
  167. },
  168. { platform: "linux" },
  169. );
  170. const second = buildGitAutoRefreshWatchPaths(
  171. {
  172. workspacePath: "/repo",
  173. repoRoot: "/repo",
  174. isGitAvailable: true,
  175. isRepository: true,
  176. autoRefreshWatchPaths: [{ path: "/repo/.git/", recursive: true }],
  177. },
  178. { platform: "linux" },
  179. );
  180. const signature = stringifyGitAutoRefreshWatchPaths(first);
  181. expect(stringifyGitAutoRefreshWatchPaths(second)).toBe(signature);
  182. expect(parseGitAutoRefreshWatchPaths(signature)).toEqual(first);
  183. });
  184. });