Remove use of Bun.file (#14215)

This commit is contained in:
Dax
2026-02-19 11:32:32 -05:00
committed by GitHub
parent 0fcba68d4c
commit 02a9495063
44 changed files with 634 additions and 473 deletions

View File

@@ -7,6 +7,7 @@ import path from "path"
import fs from "fs/promises"
import { pathToFileURL } from "url"
import { Global } from "../../src/global"
import { Filesystem } from "../../src/util/filesystem"
// Get managed config directory from environment (set in preload.ts)
const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR!
@@ -17,11 +18,11 @@ afterEach(async () => {
async function writeManagedSettings(settings: object, filename = "opencode.json") {
await fs.mkdir(managedConfigDir, { recursive: true })
await Bun.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
await Filesystem.write(path.join(managedConfigDir, filename), JSON.stringify(settings))
}
async function writeConfig(dir: string, config: object, name = "opencode.json") {
await Bun.write(path.join(dir, name), JSON.stringify(config))
await Filesystem.write(path.join(dir, name), JSON.stringify(config))
}
test("loads config with defaults when no files exist", async () => {
@@ -58,7 +59,7 @@ test("loads JSON config file", async () => {
test("loads JSONC config file", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
`{
// This is a comment
@@ -144,7 +145,7 @@ test("preserves env variables when adding $schema to config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Config without $schema - should trigger auto-add
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
theme: "{env:PRESERVE_VAR}",
@@ -159,7 +160,7 @@ test("preserves env variables when adding $schema to config", async () => {
expect(config.theme).toBe("secret_value")
// Read the file to verify the env variable was preserved
const content = await Bun.file(path.join(tmp.path, "opencode.json")).text()
const content = await Filesystem.readText(path.join(tmp.path, "opencode.json"))
expect(content).toContain("{env:PRESERVE_VAR}")
expect(content).not.toContain("secret_value")
expect(content).toContain("$schema")
@@ -177,7 +178,7 @@ test("preserves env variables when adding $schema to config", async () => {
test("handles file inclusion substitution", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "included.txt"), "test_theme")
await Filesystem.write(path.join(dir, "included.txt"), "test_theme")
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
theme: "{file:included.txt}",
@@ -196,7 +197,7 @@ test("handles file inclusion substitution", async () => {
test("handles file inclusion with replacement tokens", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
await Filesystem.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
theme: "{file:included.md}",
@@ -233,7 +234,7 @@ test("validates config schema and throws on invalid fields", async () => {
test("throws error for invalid JSON", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(path.join(dir, "opencode.json"), "{ invalid json }")
await Filesystem.write(path.join(dir, "opencode.json"), "{ invalid json }")
},
})
await Instance.provide({
@@ -336,7 +337,7 @@ test("handles command configuration", async () => {
test("migrates autoshare to share field", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -358,7 +359,7 @@ test("migrates autoshare to share field", async () => {
test("migrates mode field to agent field", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -395,7 +396,7 @@ test("loads config from .opencode directory", async () => {
const agentDir = path.join(opencodeDir, "agent")
await fs.mkdir(agentDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentDir, "test.md"),
`---
model: test/model
@@ -428,7 +429,7 @@ test("loads agents from .opencode/agents (plural)", async () => {
const agentsDir = path.join(opencodeDir, "agents")
await fs.mkdir(path.join(agentsDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentsDir, "helper.md"),
`---
model: test/model
@@ -437,7 +438,7 @@ mode: subagent
Helper agent prompt`,
)
await Bun.write(
await Filesystem.write(
path.join(agentsDir, "nested", "child.md"),
`---
model: test/model
@@ -479,7 +480,7 @@ test("loads commands from .opencode/command (singular)", async () => {
const commandDir = path.join(opencodeDir, "command")
await fs.mkdir(path.join(commandDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(commandDir, "hello.md"),
`---
description: Test command
@@ -487,7 +488,7 @@ description: Test command
Hello from singular command`,
)
await Bun.write(
await Filesystem.write(
path.join(commandDir, "nested", "child.md"),
`---
description: Nested command
@@ -524,7 +525,7 @@ test("loads commands from .opencode/commands (plural)", async () => {
const commandsDir = path.join(opencodeDir, "commands")
await fs.mkdir(path.join(commandsDir, "nested"), { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(commandsDir, "hello.md"),
`---
description: Test command
@@ -532,7 +533,7 @@ description: Test command
Hello from plural commands`,
)
await Bun.write(
await Filesystem.write(
path.join(commandsDir, "nested", "child.md"),
`---
description: Nested command
@@ -568,7 +569,7 @@ test("updates config and writes to file", async () => {
const newConfig = { model: "updated/model" }
await Config.update(newConfig as any)
const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text())
const writtenConfig = await Filesystem.readJson(path.join(tmp.path, "config.json"))
expect(writtenConfig.model).toBe("updated/model")
},
})
@@ -639,8 +640,8 @@ test("installs dependencies in writable OPENCODE_CONFIG_DIR", async () => {
},
})
expect(await Bun.file(path.join(tmp.extra, "package.json")).exists()).toBe(true)
expect(await Bun.file(path.join(tmp.extra, ".gitignore")).exists()).toBe(true)
expect(await Filesystem.exists(path.join(tmp.extra, "package.json"))).toBe(true)
expect(await Filesystem.exists(path.join(tmp.extra, ".gitignore"))).toBe(true)
} finally {
if (prev === undefined) delete process.env.OPENCODE_CONFIG_DIR
else process.env.OPENCODE_CONFIG_DIR = prev
@@ -653,12 +654,12 @@ test("resolves scoped npm plugins in config", async () => {
const pluginDir = path.join(dir, "node_modules", "@scope", "plugin")
await fs.mkdir(pluginDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "package.json"),
JSON.stringify({ name: "config-fixture", version: "1.0.0", type: "module" }, null, 2),
)
await Bun.write(
await Filesystem.write(
path.join(pluginDir, "package.json"),
JSON.stringify(
{
@@ -672,9 +673,9 @@ test("resolves scoped npm plugins in config", async () => {
),
)
await Bun.write(path.join(pluginDir, "index.js"), "export default {}\n")
await Filesystem.write(path.join(pluginDir, "index.js"), "export default {}\n")
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: ["@scope/plugin"] }, null, 2),
)
@@ -708,7 +709,7 @@ test("merges plugin arrays from global and local configs", async () => {
await fs.mkdir(opencodeDir, { recursive: true })
// Global config with plugins
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -717,7 +718,7 @@ test("merges plugin arrays from global and local configs", async () => {
)
// Local .opencode config with different plugins
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -753,7 +754,7 @@ test("does not error when only custom agent is a subagent", async () => {
const agentDir = path.join(opencodeDir, "agent")
await fs.mkdir(agentDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(agentDir, "helper.md"),
`---
model: test/model
@@ -784,7 +785,7 @@ test("merges instructions arrays from global and local configs", async () => {
const opencodeDir = path.join(projectDir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -792,7 +793,7 @@ test("merges instructions arrays from global and local configs", async () => {
}),
)
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -823,7 +824,7 @@ test("deduplicates duplicate instructions from global and local configs", async
const opencodeDir = path.join(projectDir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -831,7 +832,7 @@ test("deduplicates duplicate instructions from global and local configs", async
}),
)
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -867,7 +868,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
await fs.mkdir(opencodeDir, { recursive: true })
// Global config with plugins
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -876,7 +877,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
)
// Local .opencode config with some overlapping plugins
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -915,7 +916,7 @@ test("deduplicates duplicate plugins from global and local configs", async () =>
test("migrates legacy tools config to permissions - allow", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -946,7 +947,7 @@ test("migrates legacy tools config to permissions - allow", async () => {
test("migrates legacy tools config to permissions - deny", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -977,7 +978,7 @@ test("migrates legacy tools config to permissions - deny", async () => {
test("migrates legacy write tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1086,7 +1087,7 @@ test("missing managed settings file is not an error", async () => {
test("migrates legacy edit tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1115,7 +1116,7 @@ test("migrates legacy edit tool to edit permission", async () => {
test("migrates legacy patch tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1144,7 +1145,7 @@ test("migrates legacy patch tool to edit permission", async () => {
test("migrates legacy multiedit tool to edit permission", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1173,7 +1174,7 @@ test("migrates legacy multiedit tool to edit permission", async () => {
test("migrates mixed legacy tools config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1208,7 +1209,7 @@ test("migrates mixed legacy tools config", async () => {
test("merges legacy tools with existing permission config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1241,7 +1242,7 @@ test("merges legacy tools with existing permission config", async () => {
test("permission config preserves key order", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1289,7 +1290,7 @@ test("project config can override MCP server enabled status", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Simulates a base config (like from remote .well-known) with disabled MCP
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1308,7 +1309,7 @@ test("project config can override MCP server enabled status", async () => {
}),
)
// Project config enables just jira
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1347,7 +1348,7 @@ test("MCP config deep merges preserving base config properties", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Base config with full MCP definition
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.jsonc"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1364,7 +1365,7 @@ test("MCP config deep merges preserving base config properties", async () => {
}),
)
// Override just enables it, should preserve other properties
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1399,7 +1400,7 @@ test("local .opencode config can override MCP from project config", async () =>
await using tmp = await tmpdir({
init: async (dir) => {
// Project config with disabled MCP
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1415,7 +1416,7 @@ test("local .opencode config can override MCP from project config", async () =>
// Local .opencode directory config enables it
const opencodeDir = path.join(dir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(opencodeDir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1483,7 +1484,7 @@ test("project config overrides remote well-known config", async () => {
git: true,
init: async (dir) => {
// Project config enables jira (overriding remote default)
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1576,7 +1577,7 @@ describe("deduplicatePlugins", () => {
const pluginDir = path.join(opencodeDir, "plugin")
await fs.mkdir(pluginDir, { recursive: true })
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1584,7 +1585,7 @@ describe("deduplicatePlugins", () => {
}),
)
await Bun.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
await Filesystem.write(path.join(pluginDir, "my-plugin.js"), "export default {}")
},
})
@@ -1611,7 +1612,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Create a project config that would normally be loaded
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1649,7 +1650,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
// Create a .opencode directory with a command
const opencodeDir = path.join(dir, ".opencode", "command")
await fs.mkdir(opencodeDir, { recursive: true })
await Bun.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
await Filesystem.write(path.join(opencodeDir, "test-cmd.md"), "# Test Command\nThis is a test command.")
},
})
await Instance.provide({
@@ -1706,7 +1707,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using tmp = await tmpdir({
init: async (dir) => {
// Create a config with relative instruction path
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1714,7 +1715,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
}),
)
// Create the instruction file (should be skipped)
await Bun.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
await Filesystem.write(path.join(dir, "CUSTOM.md"), "# Custom Instructions")
},
})
@@ -1752,7 +1753,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using configDirTmp = await tmpdir({
init: async (dir) => {
// Create config in the custom config dir
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -1765,7 +1766,7 @@ describe("OPENCODE_DISABLE_PROJECT_CONFIG", () => {
await using projectTmp = await tmpdir({
init: async (dir) => {
// Create config in project (should be ignored)
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",

View File

@@ -3,11 +3,12 @@ import path from "path"
import fs from "fs/promises"
import { File } from "../../src/file"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
describe("file/index Bun.file patterns", () => {
describe("file/index Filesystem patterns", () => {
describe("File.read() - text content", () => {
test("reads text file via Bun.file().text()", async () => {
test("reads text file via Filesystem.readText()", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "test.txt")
await fs.writeFile(filepath, "Hello World", "utf-8")
@@ -22,7 +23,7 @@ describe("file/index Bun.file patterns", () => {
})
})
test("reads with Bun.file().exists() check", async () => {
test("reads with Filesystem.exists() check", async () => {
await using tmp = await tmpdir()
await Instance.provide({
@@ -81,7 +82,7 @@ describe("file/index Bun.file patterns", () => {
})
describe("File.read() - binary content", () => {
test("reads binary file via Bun.file().arrayBuffer()", async () => {
test("reads binary file via Filesystem.readArrayBuffer()", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "image.png")
const binaryContent = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
@@ -115,8 +116,8 @@ describe("file/index Bun.file patterns", () => {
})
})
describe("File.read() - Bun.file().type", () => {
test("detects MIME type via Bun.file().type", async () => {
describe("File.read() - Filesystem.mimeType()", () => {
test("detects MIME type via Filesystem.mimeType()", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "test.json")
await fs.writeFile(filepath, '{"key": "value"}', "utf-8")
@@ -124,8 +125,7 @@ describe("file/index Bun.file patterns", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
const bunFile = Bun.file(filepath)
expect(bunFile.type).toContain("application/json")
expect(Filesystem.mimeType(filepath)).toContain("application/json")
const result = await File.read("test.json")
expect(result.type).toBe("text")
@@ -149,16 +149,15 @@ describe("file/index Bun.file patterns", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
const bunFile = Bun.file(filepath)
expect(bunFile.type).toContain(mime)
expect(Filesystem.mimeType(filepath)).toContain(mime)
},
})
}
})
})
describe("File.list() - Bun.file().exists() and .text()", () => {
test("reads .gitignore via Bun.file().exists() and .text()", async () => {
describe("File.list() - Filesystem.exists() and readText()", () => {
test("reads .gitignore via Filesystem.exists() and readText()", async () => {
await using tmp = await tmpdir({ git: true })
await Instance.provide({
@@ -168,10 +167,9 @@ describe("file/index Bun.file patterns", () => {
await fs.writeFile(gitignorePath, "node_modules\ndist\n", "utf-8")
// This is used internally in File.list()
const bunFile = Bun.file(gitignorePath)
expect(await bunFile.exists()).toBe(true)
expect(await Filesystem.exists(gitignorePath)).toBe(true)
const content = await bunFile.text()
const content = await Filesystem.readText(gitignorePath)
expect(content).toContain("node_modules")
},
})
@@ -186,9 +184,8 @@ describe("file/index Bun.file patterns", () => {
const ignorePath = path.join(tmp.path, ".ignore")
await fs.writeFile(ignorePath, "*.log\n.env\n", "utf-8")
const bunFile = Bun.file(ignorePath)
expect(await bunFile.exists()).toBe(true)
expect(await bunFile.text()).toContain("*.log")
expect(await Filesystem.exists(ignorePath)).toBe(true)
expect(await Filesystem.readText(ignorePath)).toContain("*.log")
},
})
})
@@ -200,8 +197,7 @@ describe("file/index Bun.file patterns", () => {
directory: tmp.path,
fn: async () => {
const gitignorePath = path.join(tmp.path, ".gitignore")
const bunFile = Bun.file(gitignorePath)
expect(await bunFile.exists()).toBe(false)
expect(await Filesystem.exists(gitignorePath)).toBe(false)
// File.list() should still work
const nodes = await File.list()
@@ -211,8 +207,8 @@ describe("file/index Bun.file patterns", () => {
})
})
describe("File.changed() - Bun.file().text() for untracked files", () => {
test("reads untracked files via Bun.file().text()", async () => {
describe("File.changed() - Filesystem.readText() for untracked files", () => {
test("reads untracked files via Filesystem.readText()", async () => {
await using tmp = await tmpdir({ git: true })
await Instance.provide({
@@ -222,8 +218,7 @@ describe("file/index Bun.file patterns", () => {
await fs.writeFile(untrackedPath, "new content\nwith multiple lines", "utf-8")
// This is how File.changed() reads untracked files
const bunFile = Bun.file(untrackedPath)
const content = await bunFile.text()
const content = await Filesystem.readText(untrackedPath)
const lines = content.split("\n").length
expect(lines).toBe(2)
},
@@ -232,7 +227,7 @@ describe("file/index Bun.file patterns", () => {
})
describe("Error handling", () => {
test("handles errors gracefully in Bun.file().text()", async () => {
test("handles errors gracefully in Filesystem.readText()", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "readonly.txt")
await fs.writeFile(filepath, "content", "utf-8")
@@ -240,9 +235,9 @@ describe("file/index Bun.file patterns", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
const nonExistentFile = Bun.file(path.join(tmp.path, "does-not-exist.txt"))
// Bun.file().text() on non-existent file throws
await expect(nonExistentFile.text()).rejects.toThrow()
const nonExistentPath = path.join(tmp.path, "does-not-exist.txt")
// Filesystem.readText() on non-existent file throws
await expect(Filesystem.readText(nonExistentPath)).rejects.toThrow()
// But File.read() handles this gracefully
const result = await File.read("does-not-exist.txt")
@@ -251,14 +246,14 @@ describe("file/index Bun.file patterns", () => {
})
})
test("handles errors in Bun.file().arrayBuffer()", async () => {
test("handles errors in Filesystem.readArrayBuffer()", async () => {
await using tmp = await tmpdir()
await Instance.provide({
directory: tmp.path,
fn: async () => {
const nonExistentFile = Bun.file(path.join(tmp.path, "does-not-exist.bin"))
const buffer = await nonExistentFile.arrayBuffer().catch(() => new ArrayBuffer(0))
const nonExistentPath = path.join(tmp.path, "does-not-exist.bin")
const buffer = await Filesystem.readArrayBuffer(nonExistentPath).catch(() => new ArrayBuffer(0))
expect(buffer.byteLength).toBe(0)
},
})
@@ -272,7 +267,6 @@ describe("file/index Bun.file patterns", () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
const bunFile = Bun.file(filepath)
// File.read() handles missing images gracefully
const result = await File.read("broken.png")
expect(result.type).toBe("text")

View File

@@ -3,6 +3,7 @@ import path from "path"
import fs from "fs/promises"
import { FileTime } from "../../src/file/time"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
describe("file/time", () => {
@@ -312,8 +313,8 @@ describe("file/time", () => {
})
})
describe("stat() Bun.file pattern", () => {
test("reads file modification time via Bun.file().stat()", async () => {
describe("stat() Filesystem.stat pattern", () => {
test("reads file modification time via Filesystem.stat()", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "file.txt")
await fs.writeFile(filepath, "content", "utf-8")
@@ -323,9 +324,9 @@ describe("file/time", () => {
fn: async () => {
FileTime.read(sessionID, filepath)
const stats = await Bun.file(filepath).stat()
expect(stats.mtime).toBeInstanceOf(Date)
expect(stats.mtime.getTime()).toBeGreaterThan(0)
const stats = Filesystem.stat(filepath)
expect(stats?.mtime).toBeInstanceOf(Date)
expect(stats!.mtime.getTime()).toBeGreaterThan(0)
// FileTime.assert uses this stat internally
await FileTime.assert(sessionID, filepath)
@@ -343,14 +344,14 @@ describe("file/time", () => {
fn: async () => {
FileTime.read(sessionID, filepath)
const originalStat = await Bun.file(filepath).stat()
const originalStat = Filesystem.stat(filepath)
// Wait and modify
await new Promise((resolve) => setTimeout(resolve, 100))
await fs.writeFile(filepath, "modified", "utf-8")
const newStat = await Bun.file(filepath).stat()
expect(newStat.mtime.getTime()).toBeGreaterThan(originalStat.mtime.getTime())
const newStat = Filesystem.stat(filepath)
expect(newStat!.mtime.getTime()).toBeGreaterThan(originalStat!.mtime.getTime())
await expect(FileTime.assert(sessionID, filepath)).rejects.toThrow()
},

View File

@@ -4,6 +4,7 @@ import { Log } from "../../src/util/log"
import { $ } from "bun"
import path from "path"
import { tmpdir } from "../fixture/fixture"
import { Filesystem } from "../../src/util/filesystem"
import { GlobalBus } from "../../src/bus/global"
Log.init({ print: false })
@@ -78,7 +79,7 @@ describe("Project.fromDirectory", () => {
expect(project.worktree).toBe(tmp.path)
const opencodeFile = path.join(tmp.path, ".git", "opencode")
const fileExists = await Bun.file(opencodeFile).exists()
const fileExists = await Filesystem.exists(opencodeFile)
expect(fileExists).toBe(false)
})
@@ -94,7 +95,7 @@ describe("Project.fromDirectory", () => {
expect(project.worktree).toBe(tmp.path)
const opencodeFile = path.join(tmp.path, ".git", "opencode")
const fileExists = await Bun.file(opencodeFile).exists()
const fileExists = await Filesystem.exists(opencodeFile)
expect(fileExists).toBe(true)
})

View File

@@ -4,6 +4,7 @@ import fs from "fs/promises"
import path from "path"
import { Instance } from "../../src/project/instance"
import { Worktree } from "../../src/worktree"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
describe("Worktree.remove", () => {
@@ -53,7 +54,7 @@ describe("Worktree.remove", () => {
})()
expect(ok).toBe(true)
expect(await Bun.file(dir).exists()).toBe(false)
expect(await Filesystem.exists(dir)).toBe(false)
const list = await $`git worktree list --porcelain`.cwd(root).quiet().text()
expect(list).not.toContain(`worktree ${dir}`)

View File

@@ -7,11 +7,12 @@ import { Instance } from "../../src/project/instance"
import { Provider } from "../../src/provider/provider"
import { Env } from "../../src/env"
import { Global } from "../../src/global"
import { Filesystem } from "../../src/util/filesystem"
test("Bedrock: config region takes precedence over AWS_REGION env var", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -43,7 +44,7 @@ test("Bedrock: config region takes precedence over AWS_REGION env var", async ()
test("Bedrock: falls back to AWS_REGION env var when no config region", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -68,7 +69,7 @@ test("Bedrock: falls back to AWS_REGION env var when no config region", async ()
test("Bedrock: loads when bearer token from auth.json is present", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -89,14 +90,14 @@ test("Bedrock: loads when bearer token from auth.json is present", async () => {
// Save original auth.json if it exists
let originalAuth: string | undefined
try {
originalAuth = await Bun.file(authPath).text()
originalAuth = await Filesystem.readText(authPath)
} catch {
// File doesn't exist, that's fine
}
try {
// Write test auth.json
await Bun.write(
await Filesystem.write(
authPath,
JSON.stringify({
"amazon-bedrock": {
@@ -122,7 +123,7 @@ test("Bedrock: loads when bearer token from auth.json is present", async () => {
} finally {
// Restore original or delete
if (originalAuth !== undefined) {
await Bun.write(authPath, originalAuth)
await Filesystem.write(authPath, originalAuth)
} else {
try {
await unlink(authPath)
@@ -136,7 +137,7 @@ test("Bedrock: loads when bearer token from auth.json is present", async () => {
test("Bedrock: config profile takes precedence over AWS_PROFILE env var", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -169,7 +170,7 @@ test("Bedrock: config profile takes precedence over AWS_PROFILE env var", async
test("Bedrock: includes custom endpoint in options when specified", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -202,7 +203,7 @@ test("Bedrock: includes custom endpoint in options when specified", async () =>
test("Bedrock: autoloads when AWS_WEB_IDENTITY_TOKEN_FILE is present", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -240,7 +241,7 @@ test("Bedrock: autoloads when AWS_WEB_IDENTITY_TOKEN_FILE is present", async ()
test("Bedrock: model with us. prefix should not be double-prefixed", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -277,7 +278,7 @@ test("Bedrock: model with us. prefix should not be double-prefixed", async () =>
test("Bedrock: model with global. prefix should not be prefixed", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -313,7 +314,7 @@ test("Bedrock: model with global. prefix should not be prefixed", async () => {
test("Bedrock: model with eu. prefix should not be double-prefixed", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
@@ -349,7 +350,7 @@ test("Bedrock: model with eu. prefix should not be double-prefixed", async () =>
test("Bedrock: model without prefix in US region should get us. prefix added", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
await Filesystem.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",

View File

@@ -7,6 +7,7 @@ import { Instance } from "../../src/project/instance"
import { Provider } from "../../src/provider/provider"
import { ProviderTransform } from "../../src/provider/transform"
import { ModelsDev } from "../../src/provider/models"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
import type { Agent } from "../../src/agent/agent"
import type { MessageV2 } from "../../src/session/message-v2"
@@ -185,7 +186,7 @@ function createChatStream(text: string) {
async function loadFixture(providerID: string, modelID: string) {
const fixturePath = path.join(import.meta.dir, "../tool/fixtures/models-api.json")
const data = (await Bun.file(fixturePath).json()) as Record<string, ModelsDev.Provider>
const data = await Filesystem.readJson<Record<string, ModelsDev.Provider>>(fixturePath)
const provider = data[providerID]
if (!provider) {
throw new Error(`Missing provider in fixture: ${providerID}`)

View File

@@ -1,5 +1,6 @@
import { describe, test, expect } from "bun:test"
import { Discovery } from "../../src/skill/discovery"
import { Filesystem } from "../../src/util/filesystem"
import path from "path"
const CLOUDFLARE_SKILLS_URL = "https://developers.cloudflare.com/.well-known/skills/"
@@ -11,7 +12,7 @@ describe("Discovery.pull", () => {
for (const dir of dirs) {
expect(dir).toStartWith(Discovery.dir())
const md = path.join(dir, "SKILL.md")
expect(await Bun.file(md).exists()).toBe(true)
expect(await Filesystem.exists(md)).toBe(true)
}
}, 30_000)
@@ -20,7 +21,7 @@ describe("Discovery.pull", () => {
expect(dirs.length).toBeGreaterThan(0)
for (const dir of dirs) {
const md = path.join(dir, "SKILL.md")
expect(await Bun.file(md).exists()).toBe(true)
expect(await Filesystem.exists(md)).toBe(true)
}
}, 30_000)
@@ -40,7 +41,7 @@ describe("Discovery.pull", () => {
const agentsSdk = dirs.find((d) => d.endsWith("/agents-sdk"))
if (agentsSdk) {
const refs = path.join(agentsSdk, "references")
expect(await Bun.file(path.join(agentsSdk, "SKILL.md")).exists()).toBe(true)
expect(await Filesystem.exists(path.join(agentsSdk, "SKILL.md"))).toBe(true)
// agents-sdk has reference files per the index
const refDir = await Array.fromAsync(new Bun.Glob("**/*.md").scan({ cwd: refs, onlyFiles: true }))
expect(refDir.length).toBeGreaterThan(0)

View File

@@ -1,7 +1,9 @@
import { test, expect } from "bun:test"
import { $ } from "bun"
import fs from "fs/promises"
import { Snapshot } from "../../src/snapshot"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
async function bootstrap() {
@@ -11,8 +13,8 @@ async function bootstrap() {
const unique = Math.random().toString(36).slice(2)
const aContent = `A${unique}`
const bContent = `B${unique}`
await Bun.write(`${dir}/a.txt`, aContent)
await Bun.write(`${dir}/b.txt`, bContent)
await Filesystem.write(`${dir}/a.txt`, aContent)
await Filesystem.write(`${dir}/b.txt`, bContent)
await $`git add .`.cwd(dir).quiet()
await $`git commit --no-gpg-sign -m init`.cwd(dir).quiet()
return {
@@ -46,11 +48,16 @@ test("revert should remove new files", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/new.txt`, "NEW")
await Filesystem.write(`${tmp.path}/new.txt`, "NEW")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/new.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -64,11 +71,16 @@ test("revert in subdirectory", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/sub`.quiet()
await Bun.write(`${tmp.path}/sub/file.txt`, "SUB")
await Filesystem.write(`${tmp.path}/sub/file.txt`, "SUB")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/sub/file.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/sub/file.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
// Note: revert currently only removes files, not directories
// The empty subdirectory will remain
},
@@ -84,18 +96,23 @@ test("multiple file operations", async () => {
expect(before).toBeTruthy()
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/c.txt`, "C")
await Filesystem.write(`${tmp.path}/c.txt`, "C")
await $`mkdir -p ${tmp.path}/dir`.quiet()
await Bun.write(`${tmp.path}/dir/d.txt`, "D")
await Bun.write(`${tmp.path}/b.txt`, "MODIFIED")
await Filesystem.write(`${tmp.path}/dir/d.txt`, "D")
await Filesystem.write(`${tmp.path}/b.txt`, "MODIFIED")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
expect(await Bun.file(`${tmp.path}/c.txt`).exists()).toBe(false)
expect(await fs.readFile(`${tmp.path}/a.txt`, "utf-8")).toBe(tmp.extra.aContent)
expect(
await fs
.access(`${tmp.path}/c.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
// Note: revert currently only removes files, not directories
// The empty directory will remain
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
expect(await fs.readFile(`${tmp.path}/b.txt`, "utf-8")).toBe(tmp.extra.bContent)
},
})
})
@@ -123,13 +140,18 @@ test("binary file handling", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
await Filesystem.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(`${tmp.path}/image.png`)
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/image.png`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/image.png`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -157,7 +179,7 @@ test("large file handling", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
await Filesystem.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/large.txt`)
},
@@ -173,11 +195,16 @@ test("nested directory revert", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/level1/level2/level3`.quiet()
await Bun.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
await Filesystem.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/level1/level2/level3/deep.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -190,9 +217,9 @@ test("special characters in filenames", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/file with spaces.txt`, "SPACES")
await Bun.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
await Bun.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
await Filesystem.write(`${tmp.path}/file with spaces.txt`, "SPACES")
await Filesystem.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
await Filesystem.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
const files = (await Snapshot.patch(before!)).files
expect(files).toContain(`${tmp.path}/file with spaces.txt`)
@@ -225,7 +252,7 @@ test("patch with invalid hash", async () => {
expect(before).toBeTruthy()
// Create a change
await Bun.write(`${tmp.path}/test.txt`, "TEST")
await Filesystem.write(`${tmp.path}/test.txt`, "TEST")
// Try to patch with invalid hash - should handle gracefully
const patch = await Snapshot.patch("invalid-hash-12345")
@@ -273,7 +300,7 @@ test("unicode filenames", async () => {
]
for (const file of unicodeFiles) {
await Bun.write(file.path, file.content)
await Filesystem.write(file.path, file.content)
}
const patch = await Snapshot.patch(before!)
@@ -286,7 +313,12 @@ test("unicode filenames", async () => {
await Snapshot.revert([patch])
for (const file of unicodeFiles) {
expect(await Bun.file(file.path).exists()).toBe(false)
expect(
await fs
.access(file.path)
.then(() => true)
.catch(() => false),
).toBe(false)
}
},
})
@@ -300,14 +332,14 @@ test.skip("unicode filenames modification and restore", async () => {
const chineseFile = `${tmp.path}/文件.txt`
const cyrillicFile = `${tmp.path}/файл.txt`
await Bun.write(chineseFile, "original chinese")
await Bun.write(cyrillicFile, "original cyrillic")
await Filesystem.write(chineseFile, "original chinese")
await Filesystem.write(cyrillicFile, "original cyrillic")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(chineseFile, "modified chinese")
await Bun.write(cyrillicFile, "modified cyrillic")
await Filesystem.write(chineseFile, "modified chinese")
await Filesystem.write(cyrillicFile, "modified cyrillic")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(chineseFile)
@@ -315,8 +347,8 @@ test.skip("unicode filenames modification and restore", async () => {
await Snapshot.revert([patch])
expect(await Bun.file(chineseFile).text()).toBe("original chinese")
expect(await Bun.file(cyrillicFile).text()).toBe("original cyrillic")
expect(await fs.readFile(chineseFile, "utf-8")).toBe("original chinese")
expect(await fs.readFile(cyrillicFile, "utf-8")).toBe("original cyrillic")
},
})
})
@@ -331,13 +363,18 @@ test("unicode filenames in subdirectories", async () => {
await $`mkdir -p "${tmp.path}/目录/подкаталог"`.quiet()
const deepFile = `${tmp.path}/目录/подкаталог/文件.txt`
await Bun.write(deepFile, "deep unicode content")
await Filesystem.write(deepFile, "deep unicode content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(deepFile)
await Snapshot.revert([patch])
expect(await Bun.file(deepFile).exists()).toBe(false)
expect(
await fs
.access(deepFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -353,13 +390,18 @@ test("very long filenames", async () => {
const longName = "a".repeat(200) + ".txt"
const longFile = `${tmp.path}/${longName}`
await Bun.write(longFile, "long filename content")
await Filesystem.write(longFile, "long filename content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(longFile)
await Snapshot.revert([patch])
expect(await Bun.file(longFile).exists()).toBe(false)
expect(
await fs
.access(longFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -372,9 +414,9 @@ test("hidden files", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/.hidden`, "hidden content")
await Bun.write(`${tmp.path}/.gitignore`, "*.log")
await Bun.write(`${tmp.path}/.config`, "config content")
await Filesystem.write(`${tmp.path}/.hidden`, "hidden content")
await Filesystem.write(`${tmp.path}/.gitignore`, "*.log")
await Filesystem.write(`${tmp.path}/.config`, "config content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(`${tmp.path}/.hidden`)
@@ -393,7 +435,7 @@ test("nested symlinks", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/sub/dir`.quiet()
await Bun.write(`${tmp.path}/sub/dir/target.txt`, "target content")
await Filesystem.write(`${tmp.path}/sub/dir/target.txt`, "target content")
await $`ln -s ${tmp.path}/sub/dir/target.txt ${tmp.path}/sub/dir/link.txt`.quiet()
await $`ln -s ${tmp.path}/sub ${tmp.path}/sub-link`.quiet()
@@ -450,9 +492,9 @@ test("gitignore changes", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/.gitignore`, "*.ignored")
await Bun.write(`${tmp.path}/test.ignored`, "ignored content")
await Bun.write(`${tmp.path}/normal.txt`, "normal content")
await Filesystem.write(`${tmp.path}/.gitignore`, "*.ignored")
await Filesystem.write(`${tmp.path}/test.ignored`, "ignored content")
await Filesystem.write(`${tmp.path}/normal.txt`, "normal content")
const patch = await Snapshot.patch(before!)
@@ -477,7 +519,7 @@ test("concurrent file operations during patch", async () => {
// Start creating files
const createPromise = (async () => {
for (let i = 0; i < 10; i++) {
await Bun.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
await Filesystem.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
// Small delay to simulate concurrent operations
await new Promise((resolve) => setTimeout(resolve, 1))
}
@@ -504,7 +546,7 @@ test("snapshot state isolation between projects", async () => {
directory: tmp1.path,
fn: async () => {
const before1 = await Snapshot.track()
await Bun.write(`${tmp1.path}/project1.txt`, "project1 content")
await Filesystem.write(`${tmp1.path}/project1.txt`, "project1 content")
const patch1 = await Snapshot.patch(before1!)
expect(patch1.files).toContain(`${tmp1.path}/project1.txt`)
},
@@ -514,7 +556,7 @@ test("snapshot state isolation between projects", async () => {
directory: tmp2.path,
fn: async () => {
const before2 = await Snapshot.track()
await Bun.write(`${tmp2.path}/project2.txt`, "project2 content")
await Filesystem.write(`${tmp2.path}/project2.txt`, "project2 content")
const patch2 = await Snapshot.patch(before2!)
expect(patch2.files).toContain(`${tmp2.path}/project2.txt`)
@@ -544,7 +586,7 @@ test("patch detects changes in secondary worktree", async () => {
expect(before).toBeTruthy()
const worktreeFile = `${worktreePath}/worktree.txt`
await Bun.write(worktreeFile, "worktree content")
await Filesystem.write(worktreeFile, "worktree content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(worktreeFile)
@@ -569,7 +611,7 @@ test("revert only removes files in invoking worktree", async () => {
},
})
const primaryFile = `${tmp.path}/worktree.txt`
await Bun.write(primaryFile, "primary content")
await Filesystem.write(primaryFile, "primary content")
await Instance.provide({
directory: worktreePath,
@@ -578,16 +620,21 @@ test("revert only removes files in invoking worktree", async () => {
expect(before).toBeTruthy()
const worktreeFile = `${worktreePath}/worktree.txt`
await Bun.write(worktreeFile, "worktree content")
await Filesystem.write(worktreeFile, "worktree content")
const patch = await Snapshot.patch(before!)
await Snapshot.revert([patch])
expect(await Bun.file(worktreeFile).exists()).toBe(false)
expect(
await fs
.access(worktreeFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
expect(await Bun.file(primaryFile).text()).toBe("primary content")
expect(await fs.readFile(primaryFile, "utf-8")).toBe("primary content")
} finally {
await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
await $`rm -rf ${worktreePath}`.quiet()
@@ -614,10 +661,10 @@ test("diff reports worktree-only/shared edits and ignores primary-only", async (
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
await Bun.write(`${worktreePath}/shared.txt`, "worktree edit")
await Bun.write(`${tmp.path}/shared.txt`, "primary edit")
await Bun.write(`${tmp.path}/primary-only.txt`, "primary change")
await Filesystem.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
await Filesystem.write(`${worktreePath}/shared.txt`, "worktree edit")
await Filesystem.write(`${tmp.path}/shared.txt`, "primary edit")
await Filesystem.write(`${tmp.path}/primary-only.txt`, "primary change")
const diff = await Snapshot.diff(before!)
expect(diff).toContain("worktree-only.txt")
@@ -662,8 +709,8 @@ test("diff function with various changes", async () => {
// Make various changes
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Bun.write(`${tmp.path}/b.txt`, "modified content")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified content")
const diff = await Snapshot.diff(before!)
expect(diff).toContain("a.txt")
@@ -683,16 +730,26 @@ test("restore function", async () => {
// Make changes
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Bun.write(`${tmp.path}/b.txt`, "modified")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified")
// Restore to original state
await Snapshot.restore(before!)
expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
expect(
await fs
.access(`${tmp.path}/a.txt`)
.then(() => true)
.catch(() => false),
).toBe(true)
expect(await fs.readFile(`${tmp.path}/a.txt`, "utf-8")).toBe(tmp.extra.aContent)
expect(
await fs
.access(`${tmp.path}/new.txt`)
.then(() => true)
.catch(() => false),
).toBe(true) // New files should remain
expect(await fs.readFile(`${tmp.path}/b.txt`, "utf-8")).toBe(tmp.extra.bContent)
},
})
})
@@ -710,14 +767,19 @@ test("revert should not delete files that existed but were deleted in snapshot",
const snapshot2 = await Snapshot.track()
expect(snapshot2).toBeTruthy()
await Bun.write(`${tmp.path}/a.txt`, "recreated content")
await Filesystem.write(`${tmp.path}/a.txt`, "recreated content")
const patch = await Snapshot.patch(snapshot2!)
expect(patch.files).toContain(`${tmp.path}/a.txt`)
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/a.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -727,14 +789,14 @@ test("revert preserves file that existed in snapshot when deleted then recreated
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/existing.txt`, "original content")
await Filesystem.write(`${tmp.path}/existing.txt`, "original content")
const snapshot = await Snapshot.track()
expect(snapshot).toBeTruthy()
await $`rm ${tmp.path}/existing.txt`.quiet()
await Bun.write(`${tmp.path}/existing.txt`, "recreated")
await Bun.write(`${tmp.path}/newfile.txt`, "new")
await Filesystem.write(`${tmp.path}/existing.txt`, "recreated")
await Filesystem.write(`${tmp.path}/newfile.txt`, "new")
const patch = await Snapshot.patch(snapshot!)
expect(patch.files).toContain(`${tmp.path}/existing.txt`)
@@ -742,9 +804,19 @@ test("revert preserves file that existed in snapshot when deleted then recreated
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
expect(
await fs
.access(`${tmp.path}/newfile.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
expect(
await fs
.access(`${tmp.path}/existing.txt`)
.then(() => true)
.catch(() => false),
).toBe(true)
expect(await fs.readFile(`${tmp.path}/existing.txt`, "utf-8")).toBe("original content")
},
})
})
@@ -754,17 +826,17 @@ test("diffFull sets status based on git change type", async () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/grow.txt`, "one\n")
await Bun.write(`${tmp.path}/trim.txt`, "line1\nline2\n")
await Bun.write(`${tmp.path}/delete.txt`, "gone")
await Filesystem.write(`${tmp.path}/grow.txt`, "one\n")
await Filesystem.write(`${tmp.path}/trim.txt`, "line1\nline2\n")
await Filesystem.write(`${tmp.path}/delete.txt`, "gone")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/grow.txt`, "one\ntwo\n")
await Bun.write(`${tmp.path}/trim.txt`, "line1\n")
await Filesystem.write(`${tmp.path}/grow.txt`, "one\ntwo\n")
await Filesystem.write(`${tmp.path}/trim.txt`, "line1\n")
await $`rm ${tmp.path}/delete.txt`.quiet()
await Bun.write(`${tmp.path}/added.txt`, "new")
await Filesystem.write(`${tmp.path}/added.txt`, "new")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -803,7 +875,7 @@ test("diffFull with new file additions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -829,7 +901,7 @@ test("diffFull with file modifications", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/b.txt`, "modified content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified content")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -881,7 +953,7 @@ test("diffFull with multiple line additions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
await Filesystem.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -907,7 +979,7 @@ test("diffFull with addition and deletion", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/added.txt`, "added content")
await Filesystem.write(`${tmp.path}/added.txt`, "added content")
await $`rm ${tmp.path}/a.txt`.quiet()
const after = await Snapshot.track()
@@ -941,8 +1013,8 @@ test("diffFull with multiple additions and deletions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
await Bun.write(`${tmp.path}/multi2.txt`, "single line")
await Filesystem.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
await Filesystem.write(`${tmp.path}/multi2.txt`, "single line")
await $`rm ${tmp.path}/a.txt`.quiet()
await $`rm ${tmp.path}/b.txt`.quiet()
@@ -1000,7 +1072,7 @@ test("diffFull with binary file changes", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
await Filesystem.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -1020,11 +1092,11 @@ test("diffFull with whitespace changes", async () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
await Filesystem.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
await Filesystem.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
const after = await Snapshot.track()
expect(after).toBeTruthy()

View File

@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import path from "path"
import { BashTool } from "../../src/tool/bash"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
import type { PermissionNext } from "../../src/permission/next"
import { Truncate } from "../../src/tool/truncation"
@@ -388,7 +389,7 @@ describe("tool.bash truncation", () => {
const filepath = (result.metadata as any).outputPath
expect(filepath).toBeTruthy()
const saved = await Bun.file(filepath).text()
const saved = await Filesystem.readText(filepath)
const lines = saved.trim().split("\n")
expect(lines.length).toBe(lineCount)
expect(lines[0]).toBe("1")

View File

@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import path from "path"
import { ReadTool } from "../../src/tool/read"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
import { PermissionNext } from "../../src/permission/next"
import { Agent } from "../../src/agent/agent"
@@ -199,10 +200,10 @@ describe("tool.read truncation", () => {
test("truncates large file by bytes and sets truncated metadata", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
const base = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
const base = await Filesystem.readText(path.join(FIXTURES_DIR, "models-api.json"))
const target = 60 * 1024
const content = base.length >= target ? base : base.repeat(Math.ceil(target / base.length))
await Bun.write(path.join(dir, "large.json"), content)
await Filesystem.write(path.join(dir, "large.json"), content)
},
})
await Instance.provide({

View File

@@ -1,6 +1,7 @@
import { describe, test, expect, afterAll } from "bun:test"
import { Truncate } from "../../src/tool/truncation"
import { Identifier } from "../../src/id/id"
import { Filesystem } from "../../src/util/filesystem"
import fs from "fs/promises"
import path from "path"
@@ -9,7 +10,7 @@ const FIXTURES_DIR = path.join(import.meta.dir, "fixtures")
describe("Truncate", () => {
describe("output", () => {
test("truncates large json file by bytes", async () => {
const content = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
const content = await Filesystem.readText(path.join(FIXTURES_DIR, "models-api.json"))
const result = await Truncate.output(content)
expect(result.truncated).toBe(true)
@@ -69,7 +70,7 @@ describe("Truncate", () => {
})
test("large single-line file truncates with byte message", async () => {
const content = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
const content = await Filesystem.readText(path.join(FIXTURES_DIR, "models-api.json"))
const result = await Truncate.output(content)
expect(result.truncated).toBe(true)
@@ -88,7 +89,7 @@ describe("Truncate", () => {
expect(result.outputPath).toBeDefined()
expect(result.outputPath).toContain("tool_")
const written = await Bun.file(result.outputPath).text()
const written = await Filesystem.readText(result.outputPath!)
expect(written).toBe(lines)
})
@@ -139,21 +140,21 @@ describe("Truncate", () => {
const oldTimestamp = Date.now() - 10 * DAY_MS
const oldId = Identifier.create("tool", false, oldTimestamp)
oldFile = path.join(Truncate.DIR, oldId)
await Bun.write(Bun.file(oldFile), "old content")
await Filesystem.write(oldFile, "old content")
// Create a recent file (3 days ago)
const recentTimestamp = Date.now() - 3 * DAY_MS
const recentId = Identifier.create("tool", false, recentTimestamp)
recentFile = path.join(Truncate.DIR, recentId)
await Bun.write(Bun.file(recentFile), "recent content")
await Filesystem.write(recentFile, "recent content")
await Truncate.cleanup()
// Old file should be deleted
expect(await Bun.file(oldFile).exists()).toBe(false)
expect(await Filesystem.exists(oldFile)).toBe(false)
// Recent file should still exist
expect(await Bun.file(recentFile).exists()).toBe(true)
expect(await Filesystem.exists(recentFile)).toBe(true)
})
})
})

View File

@@ -285,4 +285,125 @@ describe("filesystem", () => {
expect(Filesystem.mimeType("Makefile")).toBe("application/octet-stream")
})
})
describe("writeStream()", () => {
test("writes from Web ReadableStream", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "streamed.txt")
const content = "Hello from stream!"
const encoder = new TextEncoder()
const stream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(content))
controller.close()
},
})
await Filesystem.writeStream(filepath, stream)
expect(await fs.readFile(filepath, "utf-8")).toBe(content)
})
test("writes from Node.js Readable stream", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "node-streamed.txt")
const content = "Hello from Node stream!"
const { Readable } = await import("stream")
const stream = Readable.from([content])
await Filesystem.writeStream(filepath, stream)
expect(await fs.readFile(filepath, "utf-8")).toBe(content)
})
test("writes binary data from Web ReadableStream", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "binary.dat")
const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0xff])
const stream = new ReadableStream({
start(controller) {
controller.enqueue(binaryData)
controller.close()
},
})
await Filesystem.writeStream(filepath, stream)
const read = await fs.readFile(filepath)
expect(Buffer.from(read)).toEqual(Buffer.from(binaryData))
})
test("writes large content in chunks", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "large.txt")
const chunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
const stream = new ReadableStream({
start(controller) {
for (const chunk of chunks) {
controller.enqueue(new TextEncoder().encode(chunk))
}
controller.close()
},
})
await Filesystem.writeStream(filepath, stream)
expect(await fs.readFile(filepath, "utf-8")).toBe(chunks.join(""))
})
test("creates parent directories", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "nested", "deep", "streamed.txt")
const content = "nested stream content"
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(content))
controller.close()
},
})
await Filesystem.writeStream(filepath, stream)
expect(await fs.readFile(filepath, "utf-8")).toBe(content)
})
test("writes with permissions", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "protected-stream.txt")
const content = "secret stream content"
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(content))
controller.close()
},
})
await Filesystem.writeStream(filepath, stream, 0o600)
const stats = await fs.stat(filepath)
if (process.platform !== "win32") {
expect(stats.mode & 0o777).toBe(0o600)
}
})
test("writes executable with permissions", async () => {
await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "script.sh")
const content = "#!/bin/bash\necho hello"
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(content))
controller.close()
},
})
await Filesystem.writeStream(filepath, stream, 0o755)
const stats = await fs.stat(filepath)
if (process.platform !== "win32") {
expect(stats.mode & 0o777).toBe(0o755)
}
expect(await fs.readFile(filepath, "utf-8")).toBe(content)
})
})
})