| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- import { describe, expect } from "bun:test"
- import fs from "fs/promises"
- import os from "os"
- import path from "path"
- import { Effect, Layer } from "effect"
- import { GrepTool } from "../../src/tool/grep"
- import { provideInstance, TestInstance, tmpdirScoped } from "../fixture/fixture"
- import { SessionID, MessageID } from "../../src/session/schema"
- import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
- import { Global } from "@opencode-ai/core/global"
- import { Truncate } from "@/tool/truncate"
- import { Agent } from "../../src/agent/agent"
- import { Ripgrep } from "../../src/file/ripgrep"
- import { AppFileSystem } from "@opencode-ai/core/filesystem"
- import { testEffect } from "../lib/effect"
- import { Reference } from "@/reference/reference"
- import { Permission } from "../../src/permission"
- import type * as Tool from "../../src/tool/tool"
- import { Config } from "@/config/config"
- import { RuntimeFlags } from "@/effect/runtime-flags"
- import { Git } from "@/git"
- import { Filesystem } from "@/util/filesystem"
- const referenceLayer = (flags: Partial<RuntimeFlags.Info> = {}) =>
- Reference.layer.pipe(
- Layer.provide(Config.defaultLayer),
- Layer.provide(AppFileSystem.defaultLayer),
- Layer.provide(Git.defaultLayer),
- Layer.provide(RuntimeFlags.layer(flags)),
- )
- const toolLayer = (flags: Partial<RuntimeFlags.Info> = {}) =>
- Layer.mergeAll(
- CrossSpawnSpawner.defaultLayer,
- AppFileSystem.defaultLayer,
- Ripgrep.defaultLayer,
- Truncate.defaultLayer,
- Agent.defaultLayer,
- Git.defaultLayer,
- referenceLayer(flags),
- )
- const it = testEffect(toolLayer())
- const scout = testEffect(toolLayer({ experimentalScout: true }))
- const ctx = {
- sessionID: SessionID.make("ses_test"),
- messageID: MessageID.make("msg_test"),
- callID: "",
- agent: "build",
- abort: AbortSignal.any([]),
- messages: [],
- metadata: () => Effect.void,
- ask: () => Effect.void,
- }
- const root = path.join(__dirname, "../..")
- const full = (p: string) => (process.platform === "win32" ? Filesystem.normalizePath(p) : p)
- const githubBase = <A, E, R>(url: string, self: Effect.Effect<A, E, R>) =>
- Effect.acquireUseRelease(
- Effect.sync(() => {
- const previous = process.env.OPENCODE_REPO_CLONE_GITHUB_BASE_URL
- process.env.OPENCODE_REPO_CLONE_GITHUB_BASE_URL = url
- return previous
- }),
- () => self,
- (previous) =>
- Effect.sync(() => {
- if (previous) process.env.OPENCODE_REPO_CLONE_GITHUB_BASE_URL = previous
- else delete process.env.OPENCODE_REPO_CLONE_GITHUB_BASE_URL
- }),
- )
- const git = Effect.fn("GrepToolTest.git")(function* (cwd: string, args: string[]) {
- return yield* Effect.promise(async () => {
- const proc = Bun.spawn(["git", ...args], {
- cwd,
- stdout: "pipe",
- stderr: "pipe",
- })
- const [stdout, stderr, code] = await Promise.all([
- new Response(proc.stdout).text(),
- new Response(proc.stderr).text(),
- proc.exited,
- ])
- if (code !== 0) throw new Error(stderr.trim() || stdout.trim() || `git ${args.join(" ")} failed`)
- return stdout.trim()
- })
- })
- describe("tool.grep", () => {
- it.live("basic search", () =>
- Effect.gen(function* () {
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* provideInstance(root)(
- grep.execute(
- {
- pattern: "export",
- path: path.join(root, "src/tool"),
- include: "*.ts",
- },
- ctx,
- ),
- )
- expect(result.metadata.matches).toBeGreaterThan(0)
- expect(result.output).toContain("Found")
- }),
- )
- it.instance("no matches returns correct output", () =>
- Effect.gen(function* () {
- const test = yield* TestInstance
- yield* Effect.promise(() => Bun.write(path.join(test.directory, "test.txt"), "hello world"))
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* grep.execute(
- {
- pattern: "xyznonexistentpatternxyz123",
- path: test.directory,
- },
- ctx,
- )
- expect(result.metadata.matches).toBe(0)
- expect(result.output).toBe("No files found")
- }),
- )
- it.instance("finds matches in tmp instance", () =>
- Effect.gen(function* () {
- const test = yield* TestInstance
- yield* Effect.promise(() => Bun.write(path.join(test.directory, "test.txt"), "line1\nline2\nline3"))
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* grep.execute(
- {
- pattern: "line",
- path: test.directory,
- },
- ctx,
- )
- expect(result.metadata.matches).toBeGreaterThan(0)
- }),
- )
- it.instance("supports exact file paths", () =>
- Effect.gen(function* () {
- const test = yield* TestInstance
- const file = path.join(test.directory, "test.txt")
- yield* Effect.promise(() => Bun.write(file, "line1\nline2\nline3"))
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* grep.execute(
- {
- pattern: "line2",
- path: file,
- },
- ctx,
- )
- expect(result.metadata.matches).toBe(1)
- expect(result.output).toContain(file)
- expect(result.output).toContain("Line 2: line2")
- }),
- )
- it.instance("does not ask for external_directory when alias path is allowed", () =>
- Effect.gen(function* () {
- if (process.platform === "win32") return
- yield* TestInstance
- const tmp = yield* Effect.acquireRelease(
- Effect.promise(() => fs.mkdtemp(path.join(os.tmpdir(), "opencode-grep-alias-"))),
- (dir) => Effect.promise(() => fs.rm(dir, { recursive: true, force: true })),
- )
- const real = path.join(tmp, "real")
- const alias = path.join(tmp, "alias")
- yield* Effect.promise(() => fs.mkdir(real))
- yield* Effect.promise(() => fs.symlink(real, alias, "dir"))
- yield* Effect.promise(() => Bun.write(path.join(real, "test.txt"), "needle"))
- const ruleset = Permission.fromConfig({
- grep: "allow",
- external_directory: {
- [path.join(alias, "*")]: "allow",
- },
- })
- const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
- const next: Tool.Context = {
- ...ctx,
- ask: (req) =>
- Effect.sync(() => {
- const needsAsk = req.patterns.some(
- (pattern) => Permission.evaluate(req.permission, pattern, ruleset).action !== "allow",
- )
- if (needsAsk) requests.push(req)
- }),
- }
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* grep.execute(
- {
- pattern: "needle",
- path: alias,
- include: "*.txt",
- },
- next,
- )
- expect(result.metadata.matches).toBe(1)
- expect(requests.find((req) => req.permission === "external_directory")).toBeUndefined()
- }),
- )
- scout.instance(
- "does not ask for external_directory permission inside configured git references",
- () =>
- Effect.gen(function* () {
- yield* TestInstance
- const appfs = yield* AppFileSystem.Service
- const cache = path.join(Global.Path.repos, "github.com", "opencode-grep-reference", "repo")
- yield* appfs.remove(cache, { recursive: true }).pipe(Effect.ignore)
- yield* Effect.addFinalizer(() => appfs.remove(cache, { recursive: true }).pipe(Effect.ignore))
- const source = yield* tmpdirScoped({ git: true })
- const remoteRoot = yield* tmpdirScoped()
- const remoteDir = path.join(remoteRoot, "opencode-grep-reference")
- const remoteRepo = path.join(remoteDir, "repo.git")
- yield* appfs.writeWithDirs(path.join(source, "src", "notes.md"), "needle\n")
- yield* git(source, ["add", "."])
- yield* git(source, ["commit", "-m", "add notes"])
- yield* appfs.makeDirectory(remoteDir, { recursive: true }).pipe(Effect.orDie)
- yield* git(remoteRoot, ["clone", "--bare", source, remoteRepo])
- const requests: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = []
- const next: Tool.Context = {
- ...ctx,
- ask: (req) =>
- Effect.sync(() => {
- requests.push(req)
- }),
- }
- const info = yield* GrepTool
- const grep = yield* info.init()
- const result = yield* githubBase(
- `file://${remoteRoot}/`,
- grep.execute({ pattern: "needle", path: path.join(cache, "src"), include: "*.md" }, next),
- )
- expect(result.metadata.matches).toBe(1)
- expect(full(result.output)).toContain(full(path.join(cache, "src", "notes.md")))
- expect(requests.find((req) => req.permission === "external_directory")).toBeUndefined()
- }),
- {
- config: {
- reference: {
- docs: "opencode-grep-reference/repo",
- },
- },
- },
- )
- })
|