ripgrep.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { describe, expect } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { Effect } from "effect"
  5. import { Ripgrep } from "@opencode-ai/core/ripgrep"
  6. import { RelativePath } from "@opencode-ai/core/schema"
  7. import { tmpdir } from "./fixture/tmpdir"
  8. import { testEffect } from "./lib/effect"
  9. const it = testEffect(Ripgrep.defaultLayer)
  10. describe("Ripgrep", () => {
  11. it.live("allows caller globs to re-include git metadata", () =>
  12. Effect.acquireUseRelease(
  13. Effect.promise(() => tmpdir()),
  14. (tmp) =>
  15. Effect.gen(function* () {
  16. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, ".opencode")))
  17. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, ".opencode", "config"), "needle\n"))
  18. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, ".git")))
  19. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, ".git", "config"), "needle\n"))
  20. const ripgrep = yield* Ripgrep.Service
  21. const files = yield* ripgrep.find({ cwd: tmp.path, pattern: "**/*", limit: 10 })
  22. expect(files.map((item) => item.path)).toContain(RelativePath.make(".opencode/config"))
  23. expect(files.map((item) => item.path)).toContain(RelativePath.make(".git/config"))
  24. const observed: string[] = []
  25. const limited = yield* ripgrep.find({
  26. cwd: tmp.path,
  27. pattern: "**/*",
  28. limit: 1,
  29. onEntry: (entry) => Effect.sync(() => observed.push(entry.path)),
  30. })
  31. expect(observed).toEqual(limited.map((item) => item.path))
  32. const matches = yield* ripgrep.grep({ cwd: tmp.path, pattern: "needle", include: "config", limit: 10 })
  33. expect(matches.map((item) => item.entry.path)).toContain(RelativePath.make(".opencode/config"))
  34. expect(matches.map((item) => item.entry.path)).toContain(RelativePath.make(".git/config"))
  35. }),
  36. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  37. ),
  38. )
  39. })