/** * @file Tests for the `/files` page's starting place: the workspace last * shown wins (from PhuNT → AC_SHOP, Files opens AC_SHOP — not the viewer's * own home, empty but for hidden files), the home is the fallback, and an * explicit `?home=` / `?lane=` is obeyed — also when it changes after the * page opened (an admin opening another user's home, then a folder in it). * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, vi, beforeEach } from "vitest"; import { act, render, waitFor } from "@testing-library/react"; import { MemoryRouter } from "react-router-dom"; import { FilesPage } from "../FilesPage"; import { api } from "../../lib/api"; type Props = { laneId?: number | null; initialHome?: string | null; onNavigate?: (place: unknown, path: string) => void; }; const seen: Array<{ laneId?: number | null; initialHome?: string | null }> = []; let nav: Props["onNavigate"]; vi.mock("../../components/lanes/FileExplorer", () => ({ FileBrowser: (p: Props) => { seen.push({ laneId: p.laneId, initialHome: p.initialHome }); nav = p.onNavigate; return null; }, })); vi.mock("../../lib/api", () => ({ isHomePlace: (p: unknown) => typeof p === "object" && p !== null && "home" in p, api: { lanes: { list: vi.fn() }, files: { homes: vi.fn() }, }, })); const open = (url = "/files") => render( ); beforeEach(() => { seen.length = 0; localStorage.clear(); vi.mocked(api.lanes.list).mockResolvedValue({ lanes: [ { id: 1, title: "AgentHub", cwd: "/a" }, { id: 2, title: "AC_SHOP", cwd: "/srv/agenthub/users/phunt/AC_SHOP" }, ], counts: {}, } as never); vi.mocked(api.files.homes).mockResolvedValue({ homes: [{ id: "me", label: "Vi", readOnly: false }], }); }); describe("FilesPage", () => { it("opens the workspace last shown, not the viewer's own home", async () => { localStorage.setItem("agenthub.workspace.selected", "2"); open(); await waitFor(() => expect(seen[seen.length - 1]).toEqual({ laneId: 2, initialHome: null })); }); it("falls back to the home when no workspace was shown", async () => { open(); await waitFor(() => expect(seen[seen.length - 1]).toEqual({ laneId: null, initialHome: "me" })); }); it("obeys an explicit ?home= or ?lane=", async () => { localStorage.setItem("agenthub.workspace.selected", "2"); open("/files?home=me"); await waitFor(() => expect(seen[seen.length - 1]).toEqual({ laneId: null, initialHome: "me" })); seen.length = 0; open("/files?lane=1"); await waitFor(() => expect(seen[seen.length - 1]).toEqual({ laneId: 1, initialHome: null })); }); it("follows a place picked in the browser, so a folder opened there stays there", async () => { open("/files?home=me"); await waitFor(() => expect(nav).toBeTruthy()); act(() => nav!({ home: "phunt" }, "")); act(() => nav!({ home: "phunt" }, "AC_SHOP")); await waitFor(() => expect(seen[seen.length - 1]).toEqual({ laneId: null, initialHome: "phunt" }) ); }); });