search.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, expect, test } from "bun:test"
  2. import os from "os"
  3. import path from "path"
  4. import { Effect, Layer } from "effect"
  5. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  6. import { FileSystem } from "@opencode-ai/core/filesystem"
  7. import { Protected } from "@opencode-ai/core/filesystem/protected"
  8. import { FileSystemSearch } from "@opencode-ai/core/filesystem/search"
  9. import { Location } from "@opencode-ai/core/location"
  10. import { Ripgrep } from "@opencode-ai/core/ripgrep"
  11. import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  12. import { location } from "../fixture/location"
  13. describe("FileSystemSearch", () => {
  14. test("bounds a home scan even when home is detected as a repository", async () => {
  15. let observed: Ripgrep.FindInput | undefined
  16. const home = AbsolutePath.make(os.homedir())
  17. const layer = AppNodeBuilder.build(FileSystemSearch.node, [
  18. [
  19. Location.node,
  20. Layer.succeed(
  21. Location.Service,
  22. Location.Service.of(
  23. location({ directory: home }, { vcs: { type: "git", store: AbsolutePath.make(path.join(home, ".git")) } }),
  24. ),
  25. ),
  26. ],
  27. [
  28. Ripgrep.node,
  29. Layer.succeed(
  30. Ripgrep.Service,
  31. Ripgrep.Service.of({
  32. find: (input) =>
  33. Effect.gen(function* () {
  34. observed = input
  35. if (input.onEntry)
  36. yield* input.onEntry(FileSystem.Entry.make({ path: RelativePath.make("src/index.ts"), type: "file" }))
  37. return []
  38. }),
  39. glob: () => Effect.succeed([]),
  40. grep: () => Effect.succeed([]),
  41. }),
  42. ),
  43. ],
  44. ])
  45. await Effect.runPromise(
  46. Effect.gen(function* () {
  47. const search = yield* FileSystemSearch.Service
  48. yield* Effect.sleep("10 millis")
  49. expect(observed?.limit).toBe(100_000)
  50. expect(observed?.exclude).toEqual([...Protected.names()].map((name) => `${name}/**`))
  51. expect((yield* search.find({ query: "src", type: "directory" }))[0]?.path).toBe(
  52. RelativePath.make(`src${path.sep}`),
  53. )
  54. }).pipe(Effect.provide(layer), Effect.scoped),
  55. )
  56. })
  57. })