projectFileTree.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // @vitest-environment jsdom
  2. import { act, renderHook } from "@testing-library/react";
  3. import { afterEach, expect, it, vi } from "vitest";
  4. import { useProjectFileTree } from "@/project-sidebar/useProjectFileTree.js";
  5. import type {
  6. ProjectSidebarProps,
  7. SidebarFileTreeTarget,
  8. VirtualProject,
  9. ProjectTask,
  10. } from "@/project-sidebar/contract.js";
  11. import {
  12. useRemoteWorkspaceSessionStore,
  13. type RemoteWorkspaceSession,
  14. } from "@/store/remoteWorkspaceSessionStore.js";
  15. const onOpenFileManager = vi.fn();
  16. afterEach(() => {
  17. onOpenFileManager.mockClear();
  18. useRemoteWorkspaceSessionStore.setState({ sessionsById: {}, sessionIdByWorkspaceIdentity: {} });
  19. });
  20. const resolveTarget = vi.hoisted(() => vi.fn());
  21. vi.mock("@/project-sidebar/project-file-navigation.js", () => ({
  22. resolveProjectFileTarget: resolveTarget,
  23. }));
  24. it("drops a late file target after another selection or unmount", async () => {
  25. const requests: ((value: SidebarFileTreeTarget) => void)[] = [];
  26. resolveTarget.mockImplementation(() => new Promise((done) => requests.push(done)));
  27. const { result, unmount } = renderHook(() =>
  28. useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
  29. hosts: [],
  30. entries: [],
  31. }),
  32. );
  33. let first!: Promise<void>;
  34. let second!: Promise<void>;
  35. act(() => {
  36. first = result.current.openFiles({} as VirtualProject, "/one");
  37. second = result.current.openFiles({} as VirtualProject, "/two");
  38. });
  39. await act(async () => {
  40. requests[1]({ workspacePath: "/two", workspaceName: "Two" });
  41. await second;
  42. });
  43. await act(async () => {
  44. requests[0]({ workspacePath: "/one", workspaceName: "One" });
  45. await first;
  46. });
  47. expect(onOpenFileManager).toHaveBeenCalledExactlyOnceWith({
  48. workspacePath: "/two",
  49. workspaceName: "Two",
  50. });
  51. let third!: Promise<void>;
  52. act(() => {
  53. third = result.current.openFiles({} as VirtualProject, "/three");
  54. unmount();
  55. });
  56. await act(async () => {
  57. requests[2]({ workspacePath: "/three", workspaceName: "Three" });
  58. await third;
  59. });
  60. expect(onOpenFileManager).toHaveBeenCalledTimes(1);
  61. });
  62. it("consumes each open request once without reopening on rerender", () => {
  63. const target = { workspacePath: "/example", workspaceName: "Example" };
  64. const props = {
  65. onOpenFileManager,
  66. fileTreeOpenRequest: { id: 1, target },
  67. } as unknown as ProjectSidebarProps;
  68. const { rerender } = renderHook(
  69. (value) => useProjectFileTree(value, { hosts: [], entries: [] }),
  70. { initialProps: props },
  71. );
  72. rerender({ ...props });
  73. expect(onOpenFileManager).toHaveBeenCalledExactlyOnceWith(target);
  74. rerender({ ...props, fileTreeOpenRequest: { id: 2, target } });
  75. expect(onOpenFileManager).toHaveBeenCalledTimes(2);
  76. });
  77. it("opens pinned task cwd without a tab and never borrows a local remote session", async () => {
  78. const { result } = renderHook(() =>
  79. useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
  80. hosts: [],
  81. entries: [],
  82. }),
  83. );
  84. await act(async () => {
  85. await result.current.openTaskFiles(
  86. { workspacePath: "/task-cwd", hostId: "local" } as ProjectTask,
  87. "wrong-remote",
  88. );
  89. });
  90. expect(onOpenFileManager).toHaveBeenLastCalledWith({
  91. workspacePath: "/task-cwd",
  92. workspaceName: "task-cwd",
  93. });
  94. await act(async () => {
  95. await expect(
  96. result.current.openTaskFiles({
  97. workspacePath: "/task-cwd",
  98. workspaceIdentity: "remote:missing",
  99. hostId: "remote-host",
  100. } as ProjectTask),
  101. ).rejects.toThrow("project.connection-unavailable");
  102. });
  103. expect(onOpenFileManager).toHaveBeenCalledTimes(1);
  104. });
  105. it("a late mobile task switch cannot reopen an unmounted sidebar", async () => {
  106. let finish!: () => void;
  107. const switchWorkspace = vi.fn(
  108. () =>
  109. new Promise<void>((resolve) => {
  110. finish = resolve;
  111. }),
  112. );
  113. const props = {
  114. onOpenFileManager,
  115. workspacePath: "/current",
  116. webRemoteControlWorkspaceSwitcher: { switchWorkspace },
  117. } as unknown as ProjectSidebarProps;
  118. const { result, unmount } = renderHook(() =>
  119. useProjectFileTree(props, { hosts: [], entries: [] }),
  120. );
  121. let pending!: Promise<void>;
  122. act(() => {
  123. pending = result.current.openTaskFiles({
  124. workspacePath: "/task-cwd",
  125. taskId: "task-cwd",
  126. hostId: "local",
  127. } as ProjectTask);
  128. unmount();
  129. });
  130. await act(async () => {
  131. finish();
  132. await pending;
  133. });
  134. expect(switchWorkspace).toHaveBeenCalledWith("/task-cwd", {
  135. taskHostId: "local",
  136. fileTreeTaskId: "task-cwd",
  137. mobileNavigationIntent: "chat",
  138. });
  139. expect(onOpenFileManager).not.toHaveBeenCalled();
  140. });
  141. it("opens only the remote session matching the pinned task identity", async () => {
  142. useRemoteWorkspaceSessionStore.setState({
  143. sessionsById: {
  144. matching: {
  145. sessionId: "matching",
  146. workspaceIdentity: "remote:task",
  147. } as RemoteWorkspaceSession,
  148. other: { sessionId: "other", workspaceIdentity: "remote:other" } as RemoteWorkspaceSession,
  149. },
  150. });
  151. const { result } = renderHook(() =>
  152. useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
  153. hosts: [],
  154. entries: [],
  155. }),
  156. );
  157. const task = {
  158. workspacePath: "/same-path",
  159. workspaceIdentity: "remote:task",
  160. hostId: "remote-host",
  161. } as ProjectTask;
  162. await act(async () => {
  163. await expect(result.current.openTaskFiles(task, "other")).rejects.toThrow(
  164. "project.connection-unavailable",
  165. );
  166. });
  167. expect(onOpenFileManager).not.toHaveBeenCalled();
  168. await act(async () => {
  169. await result.current.openTaskFiles(task, "matching");
  170. });
  171. expect(onOpenFileManager).toHaveBeenLastCalledWith({
  172. workspacePath: "/same-path",
  173. workspaceName: "same-path",
  174. workspaceIdentity: "remote:task",
  175. workspaceRemoteSessionId: "matching",
  176. });
  177. });
  178. it("hands the file request to the new Root after a mobile workspace switch", async () => {
  179. const switchWorkspace = vi.fn(async () => {
  180. useRemoteWorkspaceSessionStore.setState({
  181. sessionsById: {
  182. fresh: { sessionId: "fresh", workspaceIdentity: "remote:task" } as RemoteWorkspaceSession,
  183. },
  184. sessionIdByWorkspaceIdentity: { "remote:task": "fresh" },
  185. });
  186. });
  187. const props = {
  188. onOpenFileManager,
  189. workspacePath: "/current",
  190. webRemoteControlWorkspaceSwitcher: { switchWorkspace },
  191. } as unknown as ProjectSidebarProps;
  192. const { result } = renderHook(() => useProjectFileTree(props, { hosts: [], entries: [] }));
  193. await act(async () => {
  194. await result.current.openTaskFiles(
  195. {
  196. workspacePath: "/task",
  197. workspaceIdentity: "remote:task",
  198. hostId: "remote-host",
  199. } as ProjectTask,
  200. "stale",
  201. );
  202. });
  203. expect(onOpenFileManager).not.toHaveBeenCalled();
  204. expect(switchWorkspace).toHaveBeenCalledWith(
  205. "remote:task",
  206. expect.objectContaining({ mobileNavigationIntent: "chat" }),
  207. );
  208. });
  209. it("accepts an identity-bound mobile session without duplicated session metadata", async () => {
  210. const state = useRemoteWorkspaceSessionStore.getState();
  211. state.registerSession({ sessionId: "mobile", services: {} } as RemoteWorkspaceSession);
  212. state.bindWorkspaceIdentity("remote:task", "mobile");
  213. const { result } = renderHook(() =>
  214. useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
  215. hosts: [],
  216. entries: [],
  217. }),
  218. );
  219. await act(async () => {
  220. await result.current.openTaskFiles({
  221. taskId: "task",
  222. hostId: "remote-host",
  223. workspacePath: "/same-path",
  224. workspaceIdentity: "remote:task",
  225. } as ProjectTask);
  226. });
  227. expect(onOpenFileManager).toHaveBeenLastCalledWith(
  228. expect.objectContaining({ workspaceRemoteSessionId: "mobile" }),
  229. );
  230. });