fix: stabilize fff file results (#31369)

This commit is contained in:
Shoubhit Dash 2026-06-08 19:43:38 +05:30 committed by GitHub
commit 4119051077
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 9 deletions

View file

@ -19,6 +19,8 @@ const tmpdir = (init?: (dir: string) => Effect.Effect<void>) =>
).pipe(Effect.tap((dir) => init?.(dir) ?? Effect.void))
const write = (file: string, data: string) => Effect.promise(() => Bun.write(file, data))
const waitForFileIndex = (search: Search.Interface, cwd: string) =>
search.glob({ cwd, pattern: "**/*", limit: 1 }).pipe(Effect.ignore)
describe("file.search", () => {
it.live("uses fff for Bun-backed grep", () =>
@ -43,6 +45,7 @@ describe("file.search", () => {
yield* write(path.join(dir, "README.md"), "hello\n")
const search = yield* Search.Service
yield* waitForFileIndex(search, dir)
const results = yield* search.file({ cwd: dir, query: "rdme", limit: 10 })
expect(results).toContain("README.md")
@ -57,6 +60,7 @@ describe("file.search", () => {
yield* write(path.join(dir, "src", "main.ts"), "export const main = true\n")
const search = yield* Search.Service
yield* waitForFileIndex(search, dir)
const results = yield* search.file({ cwd: dir, query: "", limit: 10, kind: "all" })
expect(results).toContain("README.md")
@ -65,6 +69,21 @@ describe("file.search", () => {
}),
)
it.live("stabilizes equal score file candidates by path length", () =>
Effect.gen(function* () {
expect(Fff.available()).toBe(true)
const dir = yield* tmpdir()
yield* write(path.join(dir, "src", "longer-name.ts"), "export const longer = true\n")
yield* write(path.join(dir, "a.ts"), "export const shorter = true\n")
const search = yield* Search.Service
yield* waitForFileIndex(search, dir)
const results = yield* search.file({ cwd: dir, query: "", limit: 10 })
expect(results?.slice(0, 2)).toEqual(["a.ts", "src/longer-name.ts"])
}),
)
it.live("keeps paging grep results without an explicit limit", () =>
Effect.gen(function* () {
expect(Fff.available()).toBe(true)
@ -142,6 +161,7 @@ describe("file.search", () => {
yield* write(path.join(dir, "alpha-target-two.ts"), "export const two = 2\n")
const search = yield* Search.Service
yield* waitForFileIndex(search, dir)
const results = yield* search.file({ cwd: dir, query: "alpha target two", limit: 10 })
expect(results).toContain("alpha-target-two.ts")