| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // @vitest-environment jsdom
- import { act, renderHook } from "@testing-library/react";
- import { afterEach, expect, it, vi } from "vitest";
- import { useProjectFileTree } from "@/project-sidebar/useProjectFileTree.js";
- import type {
- ProjectSidebarProps,
- SidebarFileTreeTarget,
- VirtualProject,
- ProjectTask,
- } from "@/project-sidebar/contract.js";
- import {
- useRemoteWorkspaceSessionStore,
- type RemoteWorkspaceSession,
- } from "@/store/remoteWorkspaceSessionStore.js";
- const onOpenFileManager = vi.fn();
- afterEach(() => {
- onOpenFileManager.mockClear();
- useRemoteWorkspaceSessionStore.setState({ sessionsById: {}, sessionIdByWorkspaceIdentity: {} });
- });
- const resolveTarget = vi.hoisted(() => vi.fn());
- vi.mock("@/project-sidebar/project-file-navigation.js", () => ({
- resolveProjectFileTarget: resolveTarget,
- }));
- it("drops a late file target after another selection or unmount", async () => {
- const requests: ((value: SidebarFileTreeTarget) => void)[] = [];
- resolveTarget.mockImplementation(() => new Promise((done) => requests.push(done)));
- const { result, unmount } = renderHook(() =>
- useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
- hosts: [],
- entries: [],
- }),
- );
- let first!: Promise<void>;
- let second!: Promise<void>;
- act(() => {
- first = result.current.openFiles({} as VirtualProject, "/one");
- second = result.current.openFiles({} as VirtualProject, "/two");
- });
- await act(async () => {
- requests[1]({ workspacePath: "/two", workspaceName: "Two" });
- await second;
- });
- await act(async () => {
- requests[0]({ workspacePath: "/one", workspaceName: "One" });
- await first;
- });
- expect(onOpenFileManager).toHaveBeenCalledExactlyOnceWith({
- workspacePath: "/two",
- workspaceName: "Two",
- });
- let third!: Promise<void>;
- act(() => {
- third = result.current.openFiles({} as VirtualProject, "/three");
- unmount();
- });
- await act(async () => {
- requests[2]({ workspacePath: "/three", workspaceName: "Three" });
- await third;
- });
- expect(onOpenFileManager).toHaveBeenCalledTimes(1);
- });
- it("consumes each open request once without reopening on rerender", () => {
- const target = { workspacePath: "/example", workspaceName: "Example" };
- const props = {
- onOpenFileManager,
- fileTreeOpenRequest: { id: 1, target },
- } as unknown as ProjectSidebarProps;
- const { rerender } = renderHook(
- (value) => useProjectFileTree(value, { hosts: [], entries: [] }),
- { initialProps: props },
- );
- rerender({ ...props });
- expect(onOpenFileManager).toHaveBeenCalledExactlyOnceWith(target);
- rerender({ ...props, fileTreeOpenRequest: { id: 2, target } });
- expect(onOpenFileManager).toHaveBeenCalledTimes(2);
- });
- it("opens pinned task cwd without a tab and never borrows a local remote session", async () => {
- const { result } = renderHook(() =>
- useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
- hosts: [],
- entries: [],
- }),
- );
- await act(async () => {
- await result.current.openTaskFiles(
- { workspacePath: "/task-cwd", hostId: "local" } as ProjectTask,
- "wrong-remote",
- );
- });
- expect(onOpenFileManager).toHaveBeenLastCalledWith({
- workspacePath: "/task-cwd",
- workspaceName: "task-cwd",
- });
- await act(async () => {
- await expect(
- result.current.openTaskFiles({
- workspacePath: "/task-cwd",
- workspaceIdentity: "remote:missing",
- hostId: "remote-host",
- } as ProjectTask),
- ).rejects.toThrow("project.connection-unavailable");
- });
- expect(onOpenFileManager).toHaveBeenCalledTimes(1);
- });
- it("a late mobile task switch cannot reopen an unmounted sidebar", async () => {
- let finish!: () => void;
- const switchWorkspace = vi.fn(
- () =>
- new Promise<void>((resolve) => {
- finish = resolve;
- }),
- );
- const props = {
- onOpenFileManager,
- workspacePath: "/current",
- webRemoteControlWorkspaceSwitcher: { switchWorkspace },
- } as unknown as ProjectSidebarProps;
- const { result, unmount } = renderHook(() =>
- useProjectFileTree(props, { hosts: [], entries: [] }),
- );
- let pending!: Promise<void>;
- act(() => {
- pending = result.current.openTaskFiles({
- workspacePath: "/task-cwd",
- taskId: "task-cwd",
- hostId: "local",
- } as ProjectTask);
- unmount();
- });
- await act(async () => {
- finish();
- await pending;
- });
- expect(switchWorkspace).toHaveBeenCalledWith("/task-cwd", {
- taskHostId: "local",
- fileTreeTaskId: "task-cwd",
- mobileNavigationIntent: "chat",
- });
- expect(onOpenFileManager).not.toHaveBeenCalled();
- });
- it("opens only the remote session matching the pinned task identity", async () => {
- useRemoteWorkspaceSessionStore.setState({
- sessionsById: {
- matching: {
- sessionId: "matching",
- workspaceIdentity: "remote:task",
- } as RemoteWorkspaceSession,
- other: { sessionId: "other", workspaceIdentity: "remote:other" } as RemoteWorkspaceSession,
- },
- });
- const { result } = renderHook(() =>
- useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
- hosts: [],
- entries: [],
- }),
- );
- const task = {
- workspacePath: "/same-path",
- workspaceIdentity: "remote:task",
- hostId: "remote-host",
- } as ProjectTask;
- await act(async () => {
- await expect(result.current.openTaskFiles(task, "other")).rejects.toThrow(
- "project.connection-unavailable",
- );
- });
- expect(onOpenFileManager).not.toHaveBeenCalled();
- await act(async () => {
- await result.current.openTaskFiles(task, "matching");
- });
- expect(onOpenFileManager).toHaveBeenLastCalledWith({
- workspacePath: "/same-path",
- workspaceName: "same-path",
- workspaceIdentity: "remote:task",
- workspaceRemoteSessionId: "matching",
- });
- });
- it("hands the file request to the new Root after a mobile workspace switch", async () => {
- const switchWorkspace = vi.fn(async () => {
- useRemoteWorkspaceSessionStore.setState({
- sessionsById: {
- fresh: { sessionId: "fresh", workspaceIdentity: "remote:task" } as RemoteWorkspaceSession,
- },
- sessionIdByWorkspaceIdentity: { "remote:task": "fresh" },
- });
- });
- const props = {
- onOpenFileManager,
- workspacePath: "/current",
- webRemoteControlWorkspaceSwitcher: { switchWorkspace },
- } as unknown as ProjectSidebarProps;
- const { result } = renderHook(() => useProjectFileTree(props, { hosts: [], entries: [] }));
- await act(async () => {
- await result.current.openTaskFiles(
- {
- workspacePath: "/task",
- workspaceIdentity: "remote:task",
- hostId: "remote-host",
- } as ProjectTask,
- "stale",
- );
- });
- expect(onOpenFileManager).not.toHaveBeenCalled();
- expect(switchWorkspace).toHaveBeenCalledWith(
- "remote:task",
- expect.objectContaining({ mobileNavigationIntent: "chat" }),
- );
- });
- it("accepts an identity-bound mobile session without duplicated session metadata", async () => {
- const state = useRemoteWorkspaceSessionStore.getState();
- state.registerSession({ sessionId: "mobile", services: {} } as RemoteWorkspaceSession);
- state.bindWorkspaceIdentity("remote:task", "mobile");
- const { result } = renderHook(() =>
- useProjectFileTree({ onOpenFileManager } as unknown as ProjectSidebarProps, {
- hosts: [],
- entries: [],
- }),
- );
- await act(async () => {
- await result.current.openTaskFiles({
- taskId: "task",
- hostId: "remote-host",
- workspacePath: "/same-path",
- workspaceIdentity: "remote:task",
- } as ProjectTask);
- });
- expect(onOpenFileManager).toHaveBeenLastCalledWith(
- expect.objectContaining({ workspaceRemoteSessionId: "mobile" }),
- );
- });
|