71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import { write_file_gemini } from "../../tools/impl/WriteFileGemini";
|
|
import { TestDirectory } from "../helpers/testFs";
|
|
|
|
describe("WriteFileGemini tool", () => {
|
|
let testDir: TestDirectory;
|
|
|
|
afterEach(() => {
|
|
testDir?.cleanup();
|
|
});
|
|
|
|
test("creates a new file", async () => {
|
|
testDir = new TestDirectory();
|
|
const filePath = testDir.resolve("new.txt");
|
|
|
|
await write_file_gemini({ file_path: filePath, content: "Hello, World!" });
|
|
|
|
expect(existsSync(filePath)).toBe(true);
|
|
expect(readFileSync(filePath, "utf-8")).toBe("Hello, World!");
|
|
});
|
|
|
|
test("overwrites existing file", async () => {
|
|
testDir = new TestDirectory();
|
|
const filePath = testDir.createFile("existing.txt", "Old content");
|
|
|
|
await write_file_gemini({ file_path: filePath, content: "New content" });
|
|
|
|
expect(readFileSync(filePath, "utf-8")).toBe("New content");
|
|
});
|
|
|
|
test("creates nested directories automatically", async () => {
|
|
testDir = new TestDirectory();
|
|
const filePath = testDir.resolve("nested/deep/file.txt");
|
|
|
|
await write_file_gemini({ file_path: filePath, content: "Nested file" });
|
|
|
|
expect(existsSync(filePath)).toBe(true);
|
|
expect(readFileSync(filePath, "utf-8")).toBe("Nested file");
|
|
});
|
|
|
|
test("writes UTF-8 content correctly", async () => {
|
|
testDir = new TestDirectory();
|
|
const filePath = testDir.resolve("unicode.txt");
|
|
const content = "Hello 世界 🌍\n╔═══╗";
|
|
|
|
await write_file_gemini({ file_path: filePath, content });
|
|
|
|
expect(readFileSync(filePath, "utf-8")).toBe(content);
|
|
});
|
|
|
|
test("throws error when file_path is missing", async () => {
|
|
await expect(
|
|
write_file_gemini({
|
|
content: "Hello",
|
|
} as Parameters<typeof write_file_gemini>[0]),
|
|
).rejects.toThrow(/file_path/);
|
|
});
|
|
|
|
test("throws error when content is missing", async () => {
|
|
testDir = new TestDirectory();
|
|
const filePath = testDir.resolve("test.txt");
|
|
|
|
await expect(
|
|
write_file_gemini({
|
|
file_path: filePath,
|
|
} as Parameters<typeof write_file_gemini>[0]),
|
|
).rejects.toThrow(/content/);
|
|
});
|
|
});
|