| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- import { describe, expect } from "bun:test"
- import fs from "fs/promises"
- import path from "path"
- import { Environment } from "@opencode-ai/core/environment"
- import { AbsolutePath } from "@opencode-ai/core/schema"
- import { ReadToolFileSystem } from "@opencode-ai/core/tool/read-filesystem"
- import { CrossSpawnSpawner } from "@opencode-ai/util/cross-spawn-spawner"
- import { LayerNodePlatform } from "@opencode-ai/util/effect/app-node-platform"
- import { LayerNode } from "@opencode-ai/util/effect/layer-node"
- import { Effect, FileSystem } from "effect"
- import { ChildProcessSpawner } from "effect/unstable/process"
- import { testEffect } from "./lib/effect"
- const it = testEffect(LayerNode.compile(LayerNode.group([CrossSpawnSpawner.node, LayerNodePlatform.filesystem])))
- const fixture = Effect.gen(function* () {
- const files = yield* FileSystem.FileSystem
- const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
- const directory = yield* files.makeTempDirectoryScoped()
- return { environment: Environment.makeFiles(Environment.makeLocalDriver(spawner)), files, directory }
- })
- const absolute = (value: string) => AbsolutePath.make(value)
- describe("ReadToolFileSystem", () => {
- it.effect("preserves the environment not-found error", () =>
- Effect.gen(function* () {
- const { environment, directory } = yield* fixture
- const file = path.join(directory, "missing.txt")
- const error = yield* ReadToolFileSystem.read(environment, absolute(file), "missing.txt").pipe(Effect.flip)
- expect(error).toBeInstanceOf(Environment.NotFound)
- }),
- )
- it.effect("returns a listing when read reports a directory", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- yield* files.makeDirectory(path.join(directory, "folder"))
- yield* files.writeFileString(path.join(directory, "file.txt"), "hello")
- const result = yield* ReadToolFileSystem.read(environment, absolute(directory), "folder")
- expect(result).toMatchObject({
- type: "list-page",
- entries: [
- { path: `folder${path.sep}`, type: "directory" },
- { path: "file.txt", type: "file" },
- ],
- })
- }),
- )
- it.effect("reads malformed UTF-8 lossily and still rejects null-byte binary content", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const binary = path.join(directory, "archive.dat")
- const malformed = path.join(directory, "malformed.txt")
- yield* files.writeFile(binary, Uint8Array.of(0, 1, 2, 3))
- yield* files.writeFile(malformed, Uint8Array.of(0x68, 0x69, 0x80))
- const binaryError = yield* ReadToolFileSystem.read(environment, absolute(binary), "archive.dat").pipe(Effect.flip)
- const malformedResult = yield* ReadToolFileSystem.read(environment, absolute(malformed), "malformed.txt")
- expect(binaryError).toBeInstanceOf(ReadToolFileSystem.BinaryFileError)
- expect(binaryError.message).toBe("Cannot read binary file: archive.dat")
- expect(malformedResult).toMatchObject({ type: "file", content: "hi\uFFFD", encoding: "utf8" })
- }),
- )
- it.effect("reads text despite a binary-associated extension", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "notes.docx")
- yield* files.writeFileString(file, "plain text")
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "notes.docx")
- expect(result).toMatchObject({ type: "file", content: "plain text", encoding: "utf8" })
- }),
- )
- it.effect("lists unresolved symlinks, including broken and escaping links", () =>
- Effect.gen(function* () {
- if (process.platform === "win32") return
- const { environment, files, directory } = yield* fixture
- const outside = yield* files.makeTempDirectoryScoped()
- yield* files.makeDirectory(path.join(directory, "folder"))
- yield* files.writeFileString(path.join(directory, "file.txt"), "hello")
- yield* Effect.promise(() => fs.symlink(path.join(outside, "target.txt"), path.join(directory, "escape")))
- yield* Effect.promise(() => fs.symlink(path.join(directory, "missing.txt"), path.join(directory, "broken")))
- const result = yield* ReadToolFileSystem.read(environment, absolute(directory), "folder")
- expect(result.type).toBe("list-page")
- if (result.type !== "list-page") return
- expect(result.entries.map((entry) => ({ ...entry, path: String(entry.path) }))).toEqual([
- { path: `folder${path.sep}`, type: "directory" },
- { path: "broken", type: "symlink" },
- { path: "escape", type: "symlink" },
- { path: "file.txt", type: "file" },
- ])
- }),
- )
- it.effect("reads a symlinked directory as a listing", () =>
- Effect.gen(function* () {
- if (process.platform === "win32") return
- const { environment, files, directory } = yield* fixture
- const target = path.join(directory, "target")
- const link = path.join(directory, "link")
- yield* files.makeDirectory(target)
- yield* files.writeFileString(path.join(target, "file.txt"), "hello")
- yield* Effect.promise(() => fs.symlink(target, link))
- const result = yield* ReadToolFileSystem.read(environment, absolute(link), "link")
- expect(result).toMatchObject({
- type: "list-page",
- entries: [{ path: "file.txt", type: "file" }],
- })
- }),
- )
- it.effect("reports out-of-range pagination as a typed error", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "short.txt")
- yield* files.writeFileString(file, "one\n")
- const error = yield* ReadToolFileSystem.read(environment, absolute(file), "short.txt", { offset: 2 }).pipe(
- Effect.flip,
- )
- expect(error).toBeInstanceOf(ReadToolFileSystem.OffsetOutOfRangeError)
- expect(error.message).toBe("Offset 2 is out of range")
- }),
- )
- it.effect("pages text with one-based offsets", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "lines.txt")
- yield* files.writeFileString(file, "one\r\ntwo\nthree")
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "lines.txt", {
- offset: 2,
- limit: 1,
- })
- expect(result).toMatchObject({ type: "text-page", content: "two", offset: 2, truncated: true, next: 3 })
- }),
- )
- it.effect("truncates long lines", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "long.txt")
- yield* files.writeFileString(file, "a".repeat(2_001))
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "long.txt", { limit: 1 })
- expect(result).toMatchObject({
- type: "text-page",
- content: `${"a".repeat(2_000)}... (line truncated to 2000 chars)`,
- truncated: false,
- })
- }),
- )
- it.effect("enforces line and byte budgets with continuation offsets", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const linesFile = path.join(directory, "many-lines.txt")
- const bytesFile = path.join(directory, "many-bytes.txt")
- yield* files.writeFileString(linesFile, Array.from({ length: 2_001 }, (_, index) => String(index)).join("\n"))
- yield* files.writeFileString(bytesFile, Array.from({ length: 200 }, () => "a".repeat(2_000)).join("\n"))
- const ranges: Array<{ readonly offset: number; readonly length: number } | undefined> = []
- const tracked = {
- ...environment,
- read: (path: string, range?: { readonly offset: number; readonly length: number }) =>
- Effect.sync(() => ranges.push(range)).pipe(Effect.andThen(environment.read(path, range))),
- }
- const lines = yield* ReadToolFileSystem.read(environment, absolute(linesFile), "many-lines.txt", { limit: 2_000 })
- const bytes = yield* ReadToolFileSystem.read(tracked, absolute(bytesFile), "many-bytes.txt", {})
- expect(lines).toMatchObject({ type: "text-page", truncated: true, next: 2_001 })
- expect(lines.type === "text-page" ? lines.content.split("\n") : []).toHaveLength(2_000)
- expect(bytes).toMatchObject({ type: "text-page", truncated: true, next: 26 })
- expect(bytes.type === "text-page" ? Buffer.byteLength(bytes.content) : Infinity).toBeLessThanOrEqual(
- ReadToolFileSystem.MAX_READ_BYTES,
- )
- expect(ranges).toEqual([{ offset: 0, length: 256 * 1024 }])
- }),
- )
- it.effect("sorts and pages directory entries", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- yield* files.makeDirectory(path.join(directory, "z"))
- yield* files.makeDirectory(path.join(directory, "a"))
- yield* files.writeFileString(path.join(directory, "b.txt"), "")
- const result = yield* ReadToolFileSystem.read(environment, absolute(directory), "folder", {
- offset: 2,
- limit: 1,
- })
- expect(result).toMatchObject({
- type: "list-page",
- entries: [{ path: `z${path.sep}`, type: "directory" }],
- truncated: true,
- next: 3,
- })
- }),
- )
- it.effect("stops checking for null bytes after the requested page", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "nul.txt")
- yield* files.writeFile(file, Uint8Array.from([...new TextEncoder().encode("one\n"), 0]))
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "nul.txt", { limit: 1 })
- expect(result).toMatchObject({ type: "text-page", content: "one", truncated: true, next: 2 })
- }),
- )
- it.effect("reads page two after fetching more than the first 256KB range", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "large.txt")
- yield* files.writeFileString(file, `${"a".repeat(300 * 1024)}\nsecond\n`)
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "large.txt", {
- offset: 2,
- limit: 1,
- })
- expect(result).toMatchObject({ type: "text-page", content: "second", offset: 2, truncated: false })
- }),
- )
- it.effect("preserves the media ingestion limit message", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "oversized.png")
- yield* files.writeFile(file, Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
- yield* files.truncate(file, ReadToolFileSystem.MAX_MEDIA_INGEST_BYTES + 1)
- const error = yield* ReadToolFileSystem.read(environment, absolute(file), "oversized.png").pipe(Effect.flip)
- expect(error).toBeInstanceOf(ReadToolFileSystem.MediaIngestLimitError)
- expect(error.message).toBe(
- `Media exceeds ${ReadToolFileSystem.MAX_MEDIA_INGEST_BYTES} byte ingestion limit: oversized.png`,
- )
- }),
- )
- it.effect("reads PDFs as bounded media", () =>
- Effect.gen(function* () {
- const { environment, files, directory } = yield* fixture
- const file = path.join(directory, "document.pdf")
- yield* files.writeFileString(file, "%PDF-1.7\ncontent")
- const result = yield* ReadToolFileSystem.read(environment, absolute(file), "document.pdf")
- expect(result).toMatchObject({
- type: "file",
- content: Buffer.from("%PDF-1.7\ncontent").toString("base64"),
- encoding: "base64",
- mime: "application/pdf",
- })
- }),
- )
- })
|