import * as fs from "node:fs"; import * as path from "node:path"; import * as os from "node:os"; import { ToolRegistry, TOOL_DEFINITIONS } from "../backends/tool-registry.js"; describe("ToolRegistry Extended", () => { let tempDir: string; let registry: ToolRegistry; beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ciagent-tool-registry-ext-")); registry = new ToolRegistry(tempDir); }); afterEach(() => { fs.rmSync(tempDir, { recursive: true, force: true }); }); describe("readFile edge cases", () => { it("reads empty file", () => { const filePath = path.join(tempDir, "empty.txt"); fs.writeFileSync(filePath, ""); const result = registry.execute({ name: "readFile", arguments: { path: filePath } }); expect(result.content).toBe(""); expect(result.isError).toBeFalsy(); }); it("reads file with unicode content", () => { const filePath = path.join(tempDir, "unicode.txt"); fs.writeFileSync(filePath, "héllo wörld 🌍"); const result = registry.execute({ name: "readFile", arguments: { path: filePath } }); expect(result.content).toBe("héllo wörld 🌍"); }); it("handles unreadable file gracefully", () => { if (process.getuid?.() === 0) return; const filePath = path.join(tempDir, "unreadable.txt"); fs.writeFileSync(filePath, "data"); fs.chmodSync(filePath, 0o000); const result = registry.execute({ name: "readFile", arguments: { path: filePath } }); expect(result.isError).toBe(true); fs.chmodSync(filePath, 0o644); }); }); describe("writeFile edge cases", () => { it("overwrites existing file", () => { const filePath = path.join(tempDir, "overwrite.txt"); fs.writeFileSync(filePath, "old"); const result = registry.execute({ name: "writeFile", arguments: { path: filePath, content: "new" } }); expect(result.isError).toBeFalsy(); expect(fs.readFileSync(filePath, "utf-8")).toBe("new"); }); it("creates nested directories", () => { const filePath = path.join(tempDir, "a", "b", "c", "deep.txt"); const result = registry.execute({ name: "writeFile", arguments: { path: filePath, content: "deep" } }); expect(result.isError).toBeFalsy(); expect(fs.readFileSync(filePath, "utf-8")).toBe("deep"); }); }); describe("editFile edge cases", () => { it("replaces only first occurrence", () => { const filePath = path.join(tempDir, "multi.txt"); fs.writeFileSync(filePath, "aaa bbb aaa"); const result = registry.execute({ name: "editFile", arguments: { path: filePath, old: "aaa", new: "zzz" } }); expect(result.isError).toBeFalsy(); expect(fs.readFileSync(filePath, "utf-8")).toBe("zzz bbb aaa"); }); it("handles empty old string", () => { const filePath = path.join(tempDir, "empty-old.txt"); fs.writeFileSync(filePath, "hello"); const result = registry.execute({ name: "editFile", arguments: { path: filePath, old: "", new: "X" } }); expect(fs.readFileSync(filePath, "utf-8")).toContain("X"); }); }); describe("runBash edge cases", () => { it("respects cwd argument", () => { const subDir = path.join(tempDir, "subdir"); fs.mkdirSync(subDir); const result = registry.execute({ name: "runBash", arguments: { command: "pwd", cwd: subDir } }); expect(result.content).toContain("subdir"); expect(result.isError).toBeFalsy(); }); it("respects timeout argument", () => { const result = registry.execute({ name: "runBash", arguments: { command: "sleep 100", timeout: 500 } }); expect(result.isError).toBe(true); }); it("captures stderr in error output", () => { const result = registry.execute({ name: "runBash", arguments: { command: "echo error >&2 && exit 1" } }); expect(result.isError).toBe(true); expect(result.content).toContain("error"); }); }); describe("glob edge cases", () => { it("finds files in subdirectories", () => { const subDir = path.join(tempDir, "src"); fs.mkdirSync(subDir); fs.writeFileSync(path.join(subDir, "app.ts"), ""); fs.writeFileSync(path.join(subDir, "util.ts"), ""); const result = registry.execute({ name: "glob", arguments: { pattern: "**/*.ts" } }); const matches = JSON.parse(result.content); expect(matches.length).toBeGreaterThanOrEqual(2); }); it("returns empty array for no matches", () => { const result = registry.execute({ name: "glob", arguments: { pattern: "*.xyz" } }); const matches = JSON.parse(result.content); expect(matches).toEqual([]); }); }); describe("grep edge cases", () => { it("supports include pattern filter", () => { fs.writeFileSync(path.join(tempDir, "app.ts"), "const x = 1;\n"); fs.writeFileSync(path.join(tempDir, "app.js"), "const x = 1;\n"); const result = registry.execute({ name: "grep", arguments: { pattern: "const", include: "*.ts" } }); const matches = JSON.parse(result.content); expect(matches.every((m: { file: string }) => m.file.endsWith(".ts"))).toBe(true); }); it("returns empty for no matches", () => { fs.writeFileSync(path.join(tempDir, "app.ts"), "nothing interesting\n"); const result = registry.execute({ name: "grep", arguments: { pattern: "NONEXISTENT_PATTERN_XYZ", include: "*.ts" } }); const matches = JSON.parse(result.content); expect(matches).toEqual([]); }); }); });