1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createWriteFileTool } from "../../src/tools/write-file.js";
describe("write_file tool", () => {
let workDir: string;
beforeEach(async () => {
workDir = await mkdtemp(join(tmpdir(), "dispatch-test-"));
});
afterEach(async () => {
await rm(workDir, { recursive: true, force: true });
});
it("writes a new file", async () => {
const tool = createWriteFileTool(workDir);
const result = await tool.execute({
path: "output.txt",
content: "test content",
});
expect(result).toMatch(/successfully wrote/i);
const written = await readFile(join(workDir, "output.txt"), "utf8");
expect(written).toBe("test content");
});
it("creates parent directories", async () => {
const tool = createWriteFileTool(workDir);
const result = await tool.execute({
path: "nested/dir/file.txt",
content: "nested",
});
expect(result).toMatch(/successfully wrote/i);
const written = await readFile(join(workDir, "nested/dir/file.txt"), "utf8");
expect(written).toBe("nested");
});
it("blocks path traversal", async () => {
const tool = createWriteFileTool(workDir);
const result = await tool.execute({ path: "../evil.txt", content: "bad" });
expect(result).toMatch(/outside the working directory/i);
});
});
|