feat: add more tests for tool built-ins (#5)
This commit is contained in:
68
src/tests/tools/edit.test.ts
Normal file
68
src/tests/tools/edit.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user