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", () => { let tempDir: string; let registry: ToolRegistry; beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "ci-tool-registry-test-")); registry = new ToolRegistry(tempDir); }); afterEach(() => { fs.rmSync(tempDir, { recursive: true, force: true }); }); describe("definitions", () => { it("provides 6 tool definitions", () => { expect(TOOL_DEFINITIONS).toHaveLength(6); const names = TOOL_DEFINITIONS.map((d) => d.name); expect(names).toContain("readFile"); expect(names).toContain("writeFile"); expect(names).toContain("editFile"); expect(names).toContain("runBash"); expect(names).toContain("glob"); expect(names).toContain("grep"); }); it("getOpenAIToolSchema returns function-type schema", () => { const schema = registry.getOpenAIToolSchema(); expect(schema.length).toBe(6); expect(schema[0].type).toBe("function"); expect((schema[0].function as Record).name).toBeDefined(); expect((schema[0].function as Record).parameters).toBeDefined(); }); }); describe("readFile", () => { it("reads an existing file", () => { const filePath = path.join(tempDir, "test.txt"); fs.writeFileSync(filePath, "hello world"); const result = registry.execute({ name: "readFile", arguments: { path: filePath } }); expect(result.name).toBe("readFile"); expect(result.content).toBe("hello world"); expect(result.isError).toBeFalsy(); }); it("returns error for missing file", () => { const result = registry.execute({ name: "readFile", arguments: { path: "/nonexistent/file.txt" } }); expect(result.isError).toBe(true); expect(result.content).toContain("not found"); }); it("returns error for files exceeding max size", () => { const bigRegistry = new ToolRegistry(tempDir, 10); const filePath = path.join(tempDir, "big.txt"); fs.writeFileSync(filePath, "x".repeat(100)); const result = bigRegistry.execute({ name: "readFile", arguments: { path: filePath } }); expect(result.isError).toBe(true); expect(result.content).toContain("too large"); }); }); describe("writeFile", () => { it("writes a file creating parent directories", () => { const filePath = path.join(tempDir, "sub", "dir", "test.txt"); const result = registry.execute({ name: "writeFile", arguments: { path: filePath, content: "written" } }); expect(result.isError).toBeFalsy(); expect(fs.readFileSync(filePath, "utf-8")).toBe("written"); }); }); describe("editFile", () => { it("replaces an exact string in a file", () => { const filePath = path.join(tempDir, "edit.txt"); fs.writeFileSync(filePath, "hello world"); const result = registry.execute({ name: "editFile", arguments: { path: filePath, old: "hello", new: "goodbye" } }); expect(result.isError).toBeFalsy(); expect(fs.readFileSync(filePath, "utf-8")).toBe("goodbye world"); }); it("returns error when old string not found", () => { const filePath = path.join(tempDir, "edit.txt"); fs.writeFileSync(filePath, "hello world"); const result = registry.execute({ name: "editFile", arguments: { path: filePath, old: "missing", new: "replacement" } }); expect(result.isError).toBe(true); }); it("returns error for missing file", () => { const result = registry.execute({ name: "editFile", arguments: { path: "/nonexistent", old: "a", new: "b" } }); expect(result.isError).toBe(true); }); }); describe("runBash", () => { it("executes a command and returns stdout", () => { const result = registry.execute({ name: "runBash", arguments: { command: "echo hello" } }); expect(result.content).toContain("hello"); expect(result.isError).toBeFalsy(); }); it("returns error with stderr for failing commands", () => { const result = registry.execute({ name: "runBash", arguments: { command: "false" } }); expect(result.isError).toBe(true); expect(result.content).toContain("Exit code"); }); }); describe("glob", () => { it("finds files matching a pattern", () => { fs.writeFileSync(path.join(tempDir, "app.ts"), ""); fs.writeFileSync(path.join(tempDir, "app.test.ts"), ""); fs.writeFileSync(path.join(tempDir, "README.md"), ""); const result = registry.execute({ name: "glob", arguments: { pattern: "*.ts" } }); const matches = JSON.parse(result.content); expect(matches.length).toBeGreaterThanOrEqual(2); }); }); describe("grep", () => { it("finds matching lines", () => { fs.writeFileSync(path.join(tempDir, "app.ts"), "export function main() {}\nconst x = 1;\n"); const result = registry.execute({ name: "grep", arguments: { pattern: "export", include: "*.ts" } }); const matches = JSON.parse(result.content); expect(matches.length).toBe(1); expect(matches[0].content).toContain("export"); }); }); describe("unknown tool", () => { it("returns error for unknown tool name", () => { const result = registry.execute({ name: "unknownTool", arguments: {} }); expect(result.isError).toBe(true); expect(result.content).toContain("Unknown tool"); }); }); });