feat: add more tests for tool built-ins (#5)

This commit is contained in:
Charles Packer
2025-10-25 11:33:30 -07:00
committed by GitHub
parent dd773bf285
commit da2c50cbeb
19 changed files with 792 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
import { afterEach, describe, expect, test } from "bun:test";
import { readFileSync } from "node:fs";
import { edit } from "../../tools/impl/Edit";
import { TestDirectory } from "../helpers/testFs";
describe("Edit tool", () => {
let testDir: TestDirectory;
afterEach(() => {
testDir?.cleanup();
});
test("replaces a simple string", async () => {
testDir = new TestDirectory();
const file = testDir.createFile("test.txt", "Hello, World!");
const result = await edit({
file_path: file,
old_string: "World",
new_string: "Bun",
});
expect(readFileSync(file, "utf-8")).toBe("Hello, Bun!");
expect(result.replacements).toBe(1);
});
test("throws error if old_string not found", async () => {
testDir = new TestDirectory();
const file = testDir.createFile("test.txt", "Hello, World!");
await expect(
edit({
file_path: file,
old_string: "NotFound",
new_string: "Something",
}),
).rejects.toThrow(/not found/);
});
test("replaces only first occurrence without replace_all", async () => {
testDir = new TestDirectory();
const file = testDir.createFile("duplicate.txt", "foo bar foo baz");
const result = await edit({
file_path: file,
old_string: "foo",
new_string: "qux",
});
expect(readFileSync(file, "utf-8")).toBe("qux bar foo baz");
expect(result.replacements).toBe(1);
});
test("replaces all occurrences with replace_all=true", async () => {
testDir = new TestDirectory();
const file = testDir.createFile("duplicate.txt", "foo bar foo baz foo");
const result = await edit({
file_path: file,
old_string: "foo",
new_string: "qux",
replace_all: true,
});
expect(readFileSync(file, "utf-8")).toBe("qux bar qux baz qux");
expect(result.replacements).toBe(3);
});
});