Files
united-workforce/packages/agent-builtin/__tests__/read-file.test.ts
T
xiaoju 06e959e7a5
CI / check (pull_request) Failing after 1m39s
test: add unit tests for core modules (#35)
Cover high-priority untested modules:
- util: base32, result, refs-field, storage-root, log-tag
- util-agent: storage (normalizeWorkflowConfig, resolveStorageRoot), run (parseArgv)
- agent-builtin: tools (read-file, write-file, run-command), session, detail

627 → 719 tests (+92), all passing.

Refs #35
2026-06-04 04:35:33 +00:00

52 lines
1.7 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { readFileTool } from "../src/tools/read-file.js";
import { writeFile, mkdir, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
const testDir = join(tmpdir(), `read-file-test-${Date.now()}`);
const ctx = { cwd: testDir, storageRoot: testDir };
beforeAll(async () => {
await mkdir(testDir, { recursive: true });
await writeFile(join(testDir, "hello.txt"), "hello world", "utf8");
});
afterAll(async () => {
await rm(testDir, { recursive: true, force: true });
});
describe("readFileTool", () => {
it("reads a file successfully", async () => {
const result = await readFileTool.execute({ path: "hello.txt" }, ctx);
expect(result).toBe("hello world");
});
it("returns error for non-existent file", async () => {
const result = await readFileTool.execute({ path: "nope.txt" }, ctx);
expect(result).toMatch(/^Error:/);
});
it("returns error for directory", async () => {
const result = await readFileTool.execute({ path: "." }, ctx);
expect(result).toBe("Error: not a file");
});
it("returns error when path is not a string", async () => {
const result = await readFileTool.execute({ path: 123 }, ctx);
expect(result).toBe("Error: path must be a string");
});
it("returns error when args is null", async () => {
const result = await readFileTool.execute(null, ctx);
expect(result).toBe("Error: path must be a string");
});
it("returns error for file exceeding 512KB limit", async () => {
const bigFile = join(testDir, "big.txt");
await writeFile(bigFile, Buffer.alloc(512 * 1024 + 1, 65));
const result = await readFileTool.execute({ path: "big.txt" }, ctx);
expect(result).toMatch(/Error:.*limit/);
});
});