-
Notifications
You must be signed in to change notification settings - Fork 45
feat(workspace): add 'Show hidden folders' toggle to the folder browser #1364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xingyaoww
wants to merge
4
commits into
main
Choose a base branch
from
feature/folder-browser-show-hidden
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
083952b
feat(workspace): add 'Show hidden folders' toggle to the folder browser
openhands-agent 0b6122a
fix(workspace): make hidden-folder path compliant with API-access guard
openhands-agent 45930a7
test(workspace): cover hidden-folder HTTP bridge directly + encode lo…
openhands-agent 446b9c8
Merge branch 'main' into feature/folder-browser-show-hidden
xingyaoww File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import type { Backend } from "#/api/backend-registry/types"; | ||
| import type { InternalAxiosRequestConfig } from "axios"; | ||
|
|
||
| const { | ||
| mockGet, | ||
| mockCallCloudProxy, | ||
| mockGetActive, | ||
| mockGetEffectiveLocal, | ||
| capturedInterceptors, | ||
| } = vi.hoisted(() => { | ||
| const interceptors: Array< | ||
| (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | ||
| > = []; | ||
| return { | ||
| mockGet: vi.fn(), | ||
| mockCallCloudProxy: vi.fn(), | ||
| mockGetActive: vi.fn(), | ||
| mockGetEffectiveLocal: vi.fn(), | ||
| capturedInterceptors: interceptors, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("axios", () => ({ | ||
| default: { | ||
| create: () => ({ | ||
| get: mockGet, | ||
| interceptors: { | ||
| request: { | ||
| use: ( | ||
| fn: ( | ||
| config: InternalAxiosRequestConfig, | ||
| ) => InternalAxiosRequestConfig, | ||
| ) => { | ||
| capturedInterceptors.push(fn); | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("#/api/cloud/proxy", () => ({ | ||
| callCloudProxy: mockCallCloudProxy, | ||
| })); | ||
|
|
||
| vi.mock("#/api/backend-registry/active-store", () => ({ | ||
| getActiveBackend: mockGetActive, | ||
| getEffectiveLocalBackend: mockGetEffectiveLocal, | ||
| })); | ||
|
|
||
| import { fileBrowserApi } from "#/api/file-browser/file-browser-api"; | ||
|
|
||
| const localBackend: Backend = { | ||
| id: "local-1", | ||
| name: "Local", | ||
| host: "http://localhost:8000", | ||
| apiKey: "session-key", | ||
| kind: "local", | ||
| }; | ||
|
|
||
| const cloudBackend: Backend = { | ||
| id: "cloud-1", | ||
| name: "Production", | ||
| host: "https://app.all-hands.dev", | ||
| apiKey: "bearer-key", | ||
| kind: "cloud", | ||
| }; | ||
|
|
||
| function makeAxiosConfig( | ||
| overrides: Partial<InternalAxiosRequestConfig> = {}, | ||
| ): InternalAxiosRequestConfig { | ||
| const headers = { | ||
| set: vi.fn(), | ||
| get: vi.fn(), | ||
| } as unknown as InternalAxiosRequestConfig["headers"]; | ||
| return { | ||
| headers, | ||
| ...overrides, | ||
| } as unknown as InternalAxiosRequestConfig; | ||
| } | ||
|
|
||
| describe("fileBrowserApi", () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| mockGet.mockReset(); | ||
| mockCallCloudProxy.mockReset(); | ||
| mockGetActive.mockReset(); | ||
| mockGetActive.mockReturnValue({ backend: localBackend, orgId: null }); | ||
| mockGetEffectiveLocal.mockReset(); | ||
| mockGetEffectiveLocal.mockReturnValue(localBackend); | ||
| }); | ||
|
|
||
| describe("searchSubdirectoriesWithHidden", () => { | ||
| it("sends include_hidden=true via the local axios path", async () => { | ||
| mockGet.mockResolvedValue({ | ||
| data: { | ||
| items: [{ name: ".config", path: "/h/.config" }], | ||
| next_page_id: null, | ||
| }, | ||
| }); | ||
|
|
||
| const result = await fileBrowserApi.searchSubdirectoriesWithHidden({ | ||
| path: "/home/me", | ||
| }); | ||
|
|
||
| expect(mockGet).toHaveBeenCalledTimes(1); | ||
| const [url] = mockGet.mock.calls[0]; | ||
| expect(url).toBe( | ||
| "/api/file/search_subdirs?path=%2Fhome%2Fme&include_hidden=true", | ||
| ); | ||
| expect(result.items[0]?.name).toBe(".config"); | ||
| }); | ||
|
|
||
| it("encodes pagination fields as page_id/limit and not pageId", async () => { | ||
| mockGet.mockResolvedValue({ data: { items: [], next_page_id: null } }); | ||
|
|
||
| await fileBrowserApi.searchSubdirectoriesWithHidden({ | ||
| path: "/home/me", | ||
| pageId: "p2", | ||
| limit: 25, | ||
| }); | ||
|
|
||
| const [url] = mockGet.mock.calls[0]; | ||
| expect(url).toContain("page_id=p2"); | ||
| expect(url).toContain("limit=25"); | ||
| expect(url).not.toContain("pageId"); | ||
| }); | ||
|
|
||
| it("routes through callCloudProxy for cloud backends", async () => { | ||
| mockGetActive.mockReturnValue({ backend: cloudBackend, orgId: null }); | ||
| mockCallCloudProxy.mockResolvedValue({ | ||
| items: [{ name: ".cache", path: "/h/.cache" }], | ||
| next_page_id: null, | ||
| }); | ||
|
|
||
| await fileBrowserApi.searchSubdirectoriesWithHidden({ path: "/home/me" }); | ||
|
|
||
| expect(mockGet).not.toHaveBeenCalled(); | ||
| expect(mockCallCloudProxy).toHaveBeenCalledWith({ | ||
| backend: cloudBackend, | ||
| method: "GET", | ||
| path: "/api/file/search_subdirs?path=%2Fhome%2Fme&include_hidden=true", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getHomeWithHidden", () => { | ||
| it("sends include_hidden=true via the local axios path", async () => { | ||
| mockGet.mockResolvedValue({ | ||
| data: { | ||
| home: "/home/me", | ||
| favorites: [{ label: ".cache", path: "/home/me/.cache" }], | ||
| }, | ||
| }); | ||
|
|
||
| const result = await fileBrowserApi.getHomeWithHidden(); | ||
|
|
||
| const [url] = mockGet.mock.calls[0]; | ||
| expect(url).toBe("/api/file/home?include_hidden=true"); | ||
| expect(result.favorites?.[0]?.label).toBe(".cache"); | ||
| }); | ||
|
|
||
| it("routes through callCloudProxy for cloud backends", async () => { | ||
| mockGetActive.mockReturnValue({ backend: cloudBackend, orgId: null }); | ||
| mockCallCloudProxy.mockResolvedValue({ home: "/home/me" }); | ||
|
|
||
| await fileBrowserApi.getHomeWithHidden(); | ||
|
|
||
| expect(mockGet).not.toHaveBeenCalled(); | ||
| expect(mockCallCloudProxy).toHaveBeenCalledWith({ | ||
| backend: cloudBackend, | ||
| method: "GET", | ||
| path: "/api/file/home?include_hidden=true", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("localFileAxios interceptor", () => { | ||
| it("sets X-Session-API-Key from the effective local backend", () => { | ||
| const interceptor = capturedInterceptors[0]; | ||
| expect(interceptor).toBeDefined(); | ||
| const config = makeAxiosConfig(); | ||
| interceptor(config); | ||
| expect(config.headers.set).toHaveBeenCalledWith( | ||
| "X-Session-API-Key", | ||
| "session-key", | ||
| ); | ||
| }); | ||
|
|
||
| it("sets baseURL from the effective local backend", () => { | ||
| const interceptor = capturedInterceptors[0]; | ||
| const config = makeAxiosConfig(); | ||
| interceptor(config); | ||
| expect(config.baseURL).toBe("http://localhost:8000"); | ||
| }); | ||
|
|
||
| it("throws when no local backend is available", () => { | ||
| mockGetEffectiveLocal.mockReturnValue(null); | ||
| const interceptor = capturedInterceptors[0]; | ||
| const config = makeAxiosConfig(); | ||
| expect(() => interceptor(config)).toThrow(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import React from "react"; | ||
| import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
| import { renderHook, waitFor } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { | ||
| useSearchSubdirs, | ||
| useHomeDirectory, | ||
| } from "#/hooks/query/use-search-subdirs"; | ||
|
|
||
| const backendMock = vi.hoisted(() => ({ | ||
| current: { | ||
| backend: { id: "local-1", kind: "local" as "local" | "cloud" }, | ||
| orgId: null as string | null, | ||
| }, | ||
| })); | ||
| vi.mock("#/contexts/active-backend-context", () => ({ | ||
| useActiveBackend: () => backendMock.current, | ||
| })); | ||
|
|
||
| vi.mock("#/api/agent-server-client-options", () => ({ | ||
| getAgentServerClientOptions: () => ({ host: "http://localhost" }), | ||
| })); | ||
|
|
||
| const fileSearch = vi.hoisted(() => vi.fn()); | ||
| const fileGetHome = vi.hoisted(() => vi.fn()); | ||
| vi.mock("@openhands/typescript-client/clients", () => ({ | ||
| FileClient: class { | ||
| searchSubdirectories = fileSearch; | ||
|
|
||
| getHome = fileGetHome; | ||
| }, | ||
| })); | ||
|
|
||
| const searchHidden = vi.hoisted(() => vi.fn()); | ||
| const getHomeHidden = vi.hoisted(() => vi.fn()); | ||
| vi.mock("#/api/file-browser/file-browser-api", () => ({ | ||
| fileBrowserApi: { | ||
| searchSubdirectoriesWithHidden: searchHidden, | ||
| getHomeWithHidden: getHomeHidden, | ||
| }, | ||
| })); | ||
|
|
||
| function wrapper({ children }: { children: React.ReactNode }) { | ||
| const client = new QueryClient({ | ||
| defaultOptions: { queries: { retry: false } }, | ||
| }); | ||
| return <QueryClientProvider client={client}>{children}</QueryClientProvider>; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| backendMock.current = { | ||
| backend: { id: "local-1", kind: "local" }, | ||
| orgId: null, | ||
| }; | ||
| }); | ||
|
|
||
| describe("useSearchSubdirs", () => { | ||
| it("uses the typed FileClient and omits hidden dirs by default", async () => { | ||
| fileSearch.mockResolvedValue({ items: [], next_page_id: null }); | ||
|
|
||
| const { result } = renderHook(() => useSearchSubdirs("/home/me"), { | ||
| wrapper, | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
| expect(fileSearch).toHaveBeenCalledWith("/home/me"); | ||
| expect(searchHidden).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("requests include_hidden when showing hidden dirs", async () => { | ||
| searchHidden.mockResolvedValue({ | ||
| items: [{ name: ".config", path: "/home/me/.config" }], | ||
| next_page_id: null, | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => useSearchSubdirs("/home/me", true), { | ||
| wrapper, | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
| expect(searchHidden).toHaveBeenCalledWith({ path: "/home/me" }); | ||
| expect(fileSearch).not.toHaveBeenCalled(); | ||
| expect(result.current.data?.items[0]?.name).toBe(".config"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("useHomeDirectory", () => { | ||
| it("uses the typed FileClient by default", async () => { | ||
| fileGetHome.mockResolvedValue({ home: "/home/me" }); | ||
|
|
||
| const { result } = renderHook(() => useHomeDirectory(), { wrapper }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
| expect(fileGetHome).toHaveBeenCalledTimes(1); | ||
| expect(getHomeHidden).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("requests include_hidden when showing hidden dirs", async () => { | ||
| getHomeHidden.mockResolvedValue({ | ||
| home: "/home/me", | ||
| favorites: [{ label: ".cache", path: "/home/me/.cache" }], | ||
| }); | ||
|
|
||
| const { result } = renderHook(() => useHomeDirectory(true), { wrapper }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
| expect(getHomeHidden).toHaveBeenCalledTimes(1); | ||
| expect(fileGetHome).not.toHaveBeenCalled(); | ||
| expect(result.current.data?.favorites?.[0]?.label).toBe(".cache"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.