Files
ci/src/backends/tool-registry.test.ts
T
CI fb3f1df13e release(v0.4.0): purge learnship, migrate .planning→.ci, fix backends, add test coverage
- Remove all learnship references: Decision.learnship_equivalent field,
  agent persona prompts, opencode.json permissions, test fixtures
- Migrate verification layers from .planning/ to .ci/: structural
  checks .ci/ dir + ROADMAP.md, behavioral checks ROADMAP.md
- Fix ollama-local: remove sync require+curl blocking, use async
  fetchAvailableModels() in callModel
- Fix opencode.json: use __OPENCODE_DIR__ template tokens, remove
  legacy learnship permission entries
- Remove duplicate install script from package.json (keep postinstall)
- Fix quality any-regex false positives (target type annotations only)
- Add backends test coverage: backends.test.ts, tool-registry.test.ts
- Version bump 0.3.0 → 0.4.0
- Artifacts module: rename .planning→.ci internal paths
- Remove dead TODO_PATTERN/FIXME_PATTERN constants

---ci---
phase: 3
milestone: v0.4
status: complete
requirements:
  covered: [REQ-09, REQ-10, REQ-11, REQ-13, REQ-14, REQ-17]
  partial: []
decisions:
  - id: D-001
    decision: purge all learnship references from codebase
    rationale: project is CI-only, learnship is no longer a dependency
    confidence: 0.99
    category: scope
    alternatives: [keep for historical reference]
  - id: D-002
    decision: migrate verification from .planning/ to .ci/ paths
    rationale: .planning/ is removed schema, all current state lives in .ci/
    confidence: 0.95
    category: architecture
    alternatives: [keep dual-path support]
  - id: D-003
    decision: use __OPENCODE_DIR__ template tokens in opencode.json
    rationale: hardcoded ~ paths fail in containers and non-standard homes
    confidence: 0.90
    category: implementation_approach
    alternatives: [keep tilde expansion]
---/ci---
2026-05-29 16:18:30 +00:00

139 lines
5.4 KiB
TypeScript

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<string, unknown>).name).toBeDefined();
expect((schema[0].function as Record<string, unknown>).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");
});
});
});