Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/app/src/components/directory-picker-domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,54 @@ test("searches from an absolute root without a default base", async () => {
expect(directories).toEqual(["/"])
})

test("browses directories instead of fuzzy finding when the filter is empty", async () => {
const calls = { list: [] as unknown[], find: [] as unknown[] }
const sdk = {
api: {
file: {
list: (input: { location?: { directory?: string }; path?: string }) => {
calls.list.push(input)
return Promise.resolve({
data: [
{ path: "projects/", type: "directory" },
{ path: "notes.txt", type: "file" },
],
})
},
find: (input: unknown) => {
calls.find.push(input)
return Promise.resolve({ data: [] })
},
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/repo" })

expect(await search("")).toEqual(["/repo/projects"])
expect(calls.find).toEqual([])
expect(calls.list).toEqual([{ location: { directory: "/repo" }, path: "" }])
})

test("uses fuzzy find for non-empty picker filters", async () => {
const calls = { list: [] as unknown[], find: [] as unknown[] }
const sdk = {
api: {
file: {
list: () => Promise.resolve({ data: [] }),
find: (input: { location?: { directory?: string }; query?: string }) => {
calls.find.push(input)
return Promise.resolve({ data: [{ path: "opencode" }] })
},
},
},
} as unknown as Parameters<typeof createDirectorySearch>[0]["sdk"]
const search = createDirectorySearch({ sdk, home: () => "/home/luke", base: () => "/repo" })

expect(await search("opencode")).toEqual(["/repo/opencode"])
expect(calls.find).toEqual([{ location: { directory: "/repo" }, query: "opencode", type: "directory", limit: 50 }])
expect(calls.list).toEqual([])
})

test("identifies the next directory level to preload", () => {
expect(
preloadTreeDirectories("src/", [
Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/components/directory-picker-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export function createDirectorySearch(args: { sdk: ServerSDK; base: () => string
const existing = cache.get(key)
if (existing) return existing
const request = args.sdk.api.file
.list({ location: { directory: key } })
.list({ location: { directory: key }, path: "" })
.then((result) => result.data)
.catch(() => [])
.then((nodes) =>
Expand Down Expand Up @@ -374,6 +374,11 @@ export function createDirectorySearch(args: { sdk: ServerSDK; base: () => string
const pathInput = raw.startsWith("~") || !!pickerRoot(raw) || raw.includes("/")
const query = normalizePickerDrive(input.path)
if (!pathInput) {
if (!query) {
const results = await match(input.directory, "", 50)
if (!active()) return []
return results
}
const results = await args.sdk.api.file
.find({ location: { directory: input.directory }, query, type: "directory", limit: 50 })
.then((result) => result.data.map((entry) => entry.path))
Expand Down
Loading