mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-27 23:06:54 +05:00
feat(ipuaro): add read tools (v0.5.0)
- ToolRegistry: tool lifecycle management, execution with validation - GetLinesTool: read file lines with line numbers - GetFunctionTool: get function source using AST - GetClassTool: get class source using AST - GetStructureTool: directory tree with filtering 121 new tests, 540 total
This commit is contained in:
@@ -127,9 +127,7 @@ describe("ResponseParser", () => {
|
||||
|
||||
describe("formatToolCallsAsXml", () => {
|
||||
it("should format tool calls as XML", () => {
|
||||
const toolCalls = [
|
||||
createToolCall("1", "get_lines", { path: "src/index.ts", start: 1 }),
|
||||
]
|
||||
const toolCalls = [createToolCall("1", "get_lines", { path: "src/index.ts", start: 1 })]
|
||||
|
||||
const xml = formatToolCallsAsXml(toolCalls)
|
||||
|
||||
@@ -152,9 +150,7 @@ describe("ResponseParser", () => {
|
||||
})
|
||||
|
||||
it("should handle object values as JSON", () => {
|
||||
const toolCalls = [
|
||||
createToolCall("1", "test", { data: { key: "value" } }),
|
||||
]
|
||||
const toolCalls = [createToolCall("1", "test", { data: { key: "value" } })]
|
||||
|
||||
const xml = formatToolCallsAsXml(toolCalls)
|
||||
|
||||
|
||||
@@ -0,0 +1,348 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import {
|
||||
GetClassTool,
|
||||
type GetClassResult,
|
||||
} from "../../../../../src/infrastructure/tools/read/GetClassTool.js"
|
||||
import type { ToolContext } from "../../../../../src/domain/services/ITool.js"
|
||||
import type { IStorage } from "../../../../../src/domain/services/IStorage.js"
|
||||
import type { FileAST, ClassInfo } from "../../../../../src/domain/value-objects/FileAST.js"
|
||||
|
||||
function createMockClass(overrides: Partial<ClassInfo> = {}): ClassInfo {
|
||||
return {
|
||||
name: "TestClass",
|
||||
lineStart: 1,
|
||||
lineEnd: 10,
|
||||
methods: [
|
||||
{
|
||||
name: "testMethod",
|
||||
lineStart: 3,
|
||||
lineEnd: 5,
|
||||
params: [],
|
||||
isAsync: false,
|
||||
visibility: "public",
|
||||
isStatic: false,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
name: "testProp",
|
||||
line: 2,
|
||||
visibility: "private",
|
||||
isStatic: false,
|
||||
isReadonly: false,
|
||||
},
|
||||
],
|
||||
implements: [],
|
||||
isExported: true,
|
||||
isAbstract: false,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function createMockAST(classes: ClassInfo[] = []): FileAST {
|
||||
return {
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [],
|
||||
classes,
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
}
|
||||
}
|
||||
|
||||
function createMockStorage(
|
||||
fileData: { lines: string[] } | null = null,
|
||||
ast: FileAST | null = null,
|
||||
): IStorage {
|
||||
return {
|
||||
getFile: vi.fn().mockResolvedValue(fileData),
|
||||
setFile: vi.fn(),
|
||||
deleteFile: vi.fn(),
|
||||
getAllFiles: vi.fn(),
|
||||
getAST: vi.fn().mockResolvedValue(ast),
|
||||
setAST: vi.fn(),
|
||||
getMeta: vi.fn(),
|
||||
setMeta: vi.fn(),
|
||||
getSymbolIndex: vi.fn(),
|
||||
setSymbolIndex: vi.fn(),
|
||||
getDepsGraph: vi.fn(),
|
||||
setDepsGraph: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
setConfig: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
} as unknown as IStorage
|
||||
}
|
||||
|
||||
function createMockContext(storage?: IStorage): ToolContext {
|
||||
return {
|
||||
projectRoot: "/test/project",
|
||||
storage: storage ?? createMockStorage(),
|
||||
requestConfirmation: vi.fn().mockResolvedValue(true),
|
||||
onProgress: vi.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
describe("GetClassTool", () => {
|
||||
let tool: GetClassTool
|
||||
|
||||
beforeEach(() => {
|
||||
tool = new GetClassTool()
|
||||
})
|
||||
|
||||
describe("metadata", () => {
|
||||
it("should have correct name", () => {
|
||||
expect(tool.name).toBe("get_class")
|
||||
})
|
||||
|
||||
it("should have correct category", () => {
|
||||
expect(tool.category).toBe("read")
|
||||
})
|
||||
|
||||
it("should not require confirmation", () => {
|
||||
expect(tool.requiresConfirmation).toBe(false)
|
||||
})
|
||||
|
||||
it("should have correct parameters", () => {
|
||||
expect(tool.parameters).toHaveLength(2)
|
||||
expect(tool.parameters[0].name).toBe("path")
|
||||
expect(tool.parameters[0].required).toBe(true)
|
||||
expect(tool.parameters[1].name).toBe("name")
|
||||
expect(tool.parameters[1].required).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("validateParams", () => {
|
||||
it("should return null for valid params", () => {
|
||||
expect(tool.validateParams({ path: "src/index.ts", name: "MyClass" })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return error for missing path", () => {
|
||||
expect(tool.validateParams({ name: "MyClass" })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for empty path", () => {
|
||||
expect(tool.validateParams({ path: "", name: "MyClass" })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for missing name", () => {
|
||||
expect(tool.validateParams({ path: "test.ts" })).toBe(
|
||||
"Parameter 'name' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for empty name", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", name: "" })).toBe(
|
||||
"Parameter 'name' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("execute", () => {
|
||||
it("should return class code with line numbers", async () => {
|
||||
const lines = [
|
||||
"export class TestClass {",
|
||||
" private testProp: string",
|
||||
" testMethod() {",
|
||||
" return this.testProp",
|
||||
" }",
|
||||
"}",
|
||||
]
|
||||
const cls = createMockClass({
|
||||
name: "TestClass",
|
||||
lineStart: 1,
|
||||
lineEnd: 6,
|
||||
})
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "TestClass" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetClassResult
|
||||
expect(data.path).toBe("test.ts")
|
||||
expect(data.name).toBe("TestClass")
|
||||
expect(data.startLine).toBe(1)
|
||||
expect(data.endLine).toBe(6)
|
||||
expect(data.content).toContain("1│export class TestClass {")
|
||||
expect(data.content).toContain("6│}")
|
||||
})
|
||||
|
||||
it("should return class metadata", async () => {
|
||||
const lines = ["abstract class BaseService extends Service implements IService {", "}"]
|
||||
const cls = createMockClass({
|
||||
name: "BaseService",
|
||||
lineStart: 1,
|
||||
lineEnd: 2,
|
||||
isExported: false,
|
||||
isAbstract: true,
|
||||
extends: "Service",
|
||||
implements: ["IService"],
|
||||
methods: [
|
||||
{
|
||||
name: "init",
|
||||
lineStart: 2,
|
||||
lineEnd: 2,
|
||||
params: [],
|
||||
isAsync: true,
|
||||
visibility: "public",
|
||||
isStatic: false,
|
||||
},
|
||||
{
|
||||
name: "destroy",
|
||||
lineStart: 3,
|
||||
lineEnd: 3,
|
||||
params: [],
|
||||
isAsync: false,
|
||||
visibility: "protected",
|
||||
isStatic: false,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
name: "id",
|
||||
line: 2,
|
||||
visibility: "private",
|
||||
isStatic: false,
|
||||
isReadonly: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "service.ts", name: "BaseService" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetClassResult
|
||||
expect(data.isExported).toBe(false)
|
||||
expect(data.isAbstract).toBe(true)
|
||||
expect(data.extends).toBe("Service")
|
||||
expect(data.implements).toEqual(["IService"])
|
||||
expect(data.methods).toEqual(["init", "destroy"])
|
||||
expect(data.properties).toEqual(["id"])
|
||||
})
|
||||
|
||||
it("should return error when AST not found", async () => {
|
||||
const storage = createMockStorage({ lines: [] }, null)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "MyClass" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('AST not found for "test.ts"')
|
||||
})
|
||||
|
||||
it("should return error when class not found", async () => {
|
||||
const ast = createMockAST([
|
||||
createMockClass({ name: "ClassA" }),
|
||||
createMockClass({ name: "ClassB" }),
|
||||
])
|
||||
const storage = createMockStorage({ lines: [] }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "NonExistent" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('Class "NonExistent" not found')
|
||||
expect(result.error).toContain("Available: ClassA, ClassB")
|
||||
})
|
||||
|
||||
it("should return error when no classes available", async () => {
|
||||
const ast = createMockAST([])
|
||||
const storage = createMockStorage({ lines: [] }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "MyClass" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("Available: none")
|
||||
})
|
||||
|
||||
it("should return error for path outside project root", async () => {
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await tool.execute({ path: "../outside/file.ts", name: "MyClass" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Path must be within project root")
|
||||
})
|
||||
|
||||
it("should handle class with no extends", async () => {
|
||||
const lines = ["class Simple {}"]
|
||||
const cls = createMockClass({
|
||||
name: "Simple",
|
||||
lineStart: 1,
|
||||
lineEnd: 1,
|
||||
extends: undefined,
|
||||
})
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "Simple" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetClassResult
|
||||
expect(data.extends).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should handle class with empty implements", async () => {
|
||||
const lines = ["class NoInterfaces {}"]
|
||||
const cls = createMockClass({
|
||||
name: "NoInterfaces",
|
||||
lineStart: 1,
|
||||
lineEnd: 1,
|
||||
implements: [],
|
||||
})
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "NoInterfaces" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetClassResult
|
||||
expect(data.implements).toEqual([])
|
||||
})
|
||||
|
||||
it("should handle class with no methods or properties", async () => {
|
||||
const lines = ["class Empty {}"]
|
||||
const cls = createMockClass({
|
||||
name: "Empty",
|
||||
lineStart: 1,
|
||||
lineEnd: 1,
|
||||
methods: [],
|
||||
properties: [],
|
||||
})
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "Empty" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetClassResult
|
||||
expect(data.methods).toEqual([])
|
||||
expect(data.properties).toEqual([])
|
||||
})
|
||||
|
||||
it("should include callId in result", async () => {
|
||||
const lines = ["class Test {}"]
|
||||
const cls = createMockClass({ name: "Test", lineStart: 1, lineEnd: 1 })
|
||||
const ast = createMockAST([cls])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "Test" }, ctx)
|
||||
|
||||
expect(result.callId).toMatch(/^get_class-\d+$/)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,305 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import {
|
||||
GetFunctionTool,
|
||||
type GetFunctionResult,
|
||||
} from "../../../../../src/infrastructure/tools/read/GetFunctionTool.js"
|
||||
import type { ToolContext } from "../../../../../src/domain/services/ITool.js"
|
||||
import type { IStorage } from "../../../../../src/domain/services/IStorage.js"
|
||||
import type { FileAST, FunctionInfo } from "../../../../../src/domain/value-objects/FileAST.js"
|
||||
|
||||
function createMockFunction(overrides: Partial<FunctionInfo> = {}): FunctionInfo {
|
||||
return {
|
||||
name: "testFunction",
|
||||
lineStart: 1,
|
||||
lineEnd: 5,
|
||||
params: [{ name: "arg1", optional: false, hasDefault: false }],
|
||||
isAsync: false,
|
||||
isExported: true,
|
||||
returnType: "void",
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function createMockAST(functions: FunctionInfo[] = []): FileAST {
|
||||
return {
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions,
|
||||
classes: [],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
}
|
||||
}
|
||||
|
||||
function createMockStorage(
|
||||
fileData: { lines: string[] } | null = null,
|
||||
ast: FileAST | null = null,
|
||||
): IStorage {
|
||||
return {
|
||||
getFile: vi.fn().mockResolvedValue(fileData),
|
||||
setFile: vi.fn(),
|
||||
deleteFile: vi.fn(),
|
||||
getAllFiles: vi.fn(),
|
||||
getAST: vi.fn().mockResolvedValue(ast),
|
||||
setAST: vi.fn(),
|
||||
getMeta: vi.fn(),
|
||||
setMeta: vi.fn(),
|
||||
getSymbolIndex: vi.fn(),
|
||||
setSymbolIndex: vi.fn(),
|
||||
getDepsGraph: vi.fn(),
|
||||
setDepsGraph: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
setConfig: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
} as unknown as IStorage
|
||||
}
|
||||
|
||||
function createMockContext(storage?: IStorage): ToolContext {
|
||||
return {
|
||||
projectRoot: "/test/project",
|
||||
storage: storage ?? createMockStorage(),
|
||||
requestConfirmation: vi.fn().mockResolvedValue(true),
|
||||
onProgress: vi.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
describe("GetFunctionTool", () => {
|
||||
let tool: GetFunctionTool
|
||||
|
||||
beforeEach(() => {
|
||||
tool = new GetFunctionTool()
|
||||
})
|
||||
|
||||
describe("metadata", () => {
|
||||
it("should have correct name", () => {
|
||||
expect(tool.name).toBe("get_function")
|
||||
})
|
||||
|
||||
it("should have correct category", () => {
|
||||
expect(tool.category).toBe("read")
|
||||
})
|
||||
|
||||
it("should not require confirmation", () => {
|
||||
expect(tool.requiresConfirmation).toBe(false)
|
||||
})
|
||||
|
||||
it("should have correct parameters", () => {
|
||||
expect(tool.parameters).toHaveLength(2)
|
||||
expect(tool.parameters[0].name).toBe("path")
|
||||
expect(tool.parameters[0].required).toBe(true)
|
||||
expect(tool.parameters[1].name).toBe("name")
|
||||
expect(tool.parameters[1].required).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("validateParams", () => {
|
||||
it("should return null for valid params", () => {
|
||||
expect(tool.validateParams({ path: "src/index.ts", name: "myFunc" })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return error for missing path", () => {
|
||||
expect(tool.validateParams({ name: "myFunc" })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for empty path", () => {
|
||||
expect(tool.validateParams({ path: "", name: "myFunc" })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for missing name", () => {
|
||||
expect(tool.validateParams({ path: "test.ts" })).toBe(
|
||||
"Parameter 'name' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for empty name", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", name: "" })).toBe(
|
||||
"Parameter 'name' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for whitespace-only name", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", name: " " })).toBe(
|
||||
"Parameter 'name' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("execute", () => {
|
||||
it("should return function code with line numbers", async () => {
|
||||
const lines = [
|
||||
"function testFunction(arg1) {",
|
||||
" console.log(arg1)",
|
||||
" return arg1",
|
||||
"}",
|
||||
"",
|
||||
]
|
||||
const func = createMockFunction({
|
||||
name: "testFunction",
|
||||
lineStart: 1,
|
||||
lineEnd: 4,
|
||||
})
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "testFunction" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetFunctionResult
|
||||
expect(data.path).toBe("test.ts")
|
||||
expect(data.name).toBe("testFunction")
|
||||
expect(data.startLine).toBe(1)
|
||||
expect(data.endLine).toBe(4)
|
||||
expect(data.content).toContain("1│function testFunction(arg1) {")
|
||||
expect(data.content).toContain("4│}")
|
||||
})
|
||||
|
||||
it("should return function metadata", async () => {
|
||||
const lines = ["async function fetchData(url, options) {", " return fetch(url)", "}"]
|
||||
const func = createMockFunction({
|
||||
name: "fetchData",
|
||||
lineStart: 1,
|
||||
lineEnd: 3,
|
||||
isAsync: true,
|
||||
isExported: false,
|
||||
params: [
|
||||
{ name: "url", optional: false, hasDefault: false },
|
||||
{ name: "options", optional: true, hasDefault: false },
|
||||
],
|
||||
returnType: "Promise<Response>",
|
||||
})
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "api.ts", name: "fetchData" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetFunctionResult
|
||||
expect(data.isAsync).toBe(true)
|
||||
expect(data.isExported).toBe(false)
|
||||
expect(data.params).toEqual(["url", "options"])
|
||||
expect(data.returnType).toBe("Promise<Response>")
|
||||
})
|
||||
|
||||
it("should return error when AST not found", async () => {
|
||||
const storage = createMockStorage({ lines: [] }, null)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "myFunc" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('AST not found for "test.ts"')
|
||||
})
|
||||
|
||||
it("should return error when function not found", async () => {
|
||||
const ast = createMockAST([
|
||||
createMockFunction({ name: "existingFunc" }),
|
||||
createMockFunction({ name: "anotherFunc" }),
|
||||
])
|
||||
const storage = createMockStorage({ lines: [] }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "nonExistent" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain('Function "nonExistent" not found')
|
||||
expect(result.error).toContain("Available: existingFunc, anotherFunc")
|
||||
})
|
||||
|
||||
it("should return error when no functions available", async () => {
|
||||
const ast = createMockAST([])
|
||||
const storage = createMockStorage({ lines: [] }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "myFunc" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("Available: none")
|
||||
})
|
||||
|
||||
it("should return error for path outside project root", async () => {
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await tool.execute({ path: "../outside/file.ts", name: "myFunc" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Path must be within project root")
|
||||
})
|
||||
|
||||
it("should pad line numbers correctly for large files", async () => {
|
||||
const lines = Array.from({ length: 200 }, (_, i) => `line ${i + 1}`)
|
||||
const func = createMockFunction({
|
||||
name: "bigFunction",
|
||||
lineStart: 95,
|
||||
lineEnd: 105,
|
||||
})
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "big.ts", name: "bigFunction" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetFunctionResult
|
||||
expect(data.content).toContain(" 95│line 95")
|
||||
expect(data.content).toContain("100│line 100")
|
||||
expect(data.content).toContain("105│line 105")
|
||||
})
|
||||
|
||||
it("should include callId in result", async () => {
|
||||
const lines = ["function test() {}"]
|
||||
const func = createMockFunction({ name: "test", lineStart: 1, lineEnd: 1 })
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "test" }, ctx)
|
||||
|
||||
expect(result.callId).toMatch(/^get_function-\d+$/)
|
||||
})
|
||||
|
||||
it("should handle function with no return type", async () => {
|
||||
const lines = ["function noReturn() {}"]
|
||||
const func = createMockFunction({
|
||||
name: "noReturn",
|
||||
lineStart: 1,
|
||||
lineEnd: 1,
|
||||
returnType: undefined,
|
||||
})
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "noReturn" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetFunctionResult
|
||||
expect(data.returnType).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should handle function with no params", async () => {
|
||||
const lines = ["function noParams() {}"]
|
||||
const func = createMockFunction({
|
||||
name: "noParams",
|
||||
lineStart: 1,
|
||||
lineEnd: 1,
|
||||
params: [],
|
||||
})
|
||||
const ast = createMockAST([func])
|
||||
const storage = createMockStorage({ lines }, ast)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", name: "noParams" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetFunctionResult
|
||||
expect(data.params).toEqual([])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,273 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import {
|
||||
GetLinesTool,
|
||||
type GetLinesResult,
|
||||
} from "../../../../../src/infrastructure/tools/read/GetLinesTool.js"
|
||||
import type { ToolContext } from "../../../../../src/domain/services/ITool.js"
|
||||
import type { IStorage } from "../../../../../src/domain/services/IStorage.js"
|
||||
|
||||
function createMockStorage(fileData: { lines: string[] } | null = null): IStorage {
|
||||
return {
|
||||
getFile: vi.fn().mockResolvedValue(fileData),
|
||||
setFile: vi.fn(),
|
||||
deleteFile: vi.fn(),
|
||||
getAllFiles: vi.fn(),
|
||||
getAST: vi.fn(),
|
||||
setAST: vi.fn(),
|
||||
getMeta: vi.fn(),
|
||||
setMeta: vi.fn(),
|
||||
getSymbolIndex: vi.fn(),
|
||||
setSymbolIndex: vi.fn(),
|
||||
getDepsGraph: vi.fn(),
|
||||
setDepsGraph: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
setConfig: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
} as unknown as IStorage
|
||||
}
|
||||
|
||||
function createMockContext(storage?: IStorage): ToolContext {
|
||||
return {
|
||||
projectRoot: "/test/project",
|
||||
storage: storage ?? createMockStorage(),
|
||||
requestConfirmation: vi.fn().mockResolvedValue(true),
|
||||
onProgress: vi.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
describe("GetLinesTool", () => {
|
||||
let tool: GetLinesTool
|
||||
|
||||
beforeEach(() => {
|
||||
tool = new GetLinesTool()
|
||||
})
|
||||
|
||||
describe("metadata", () => {
|
||||
it("should have correct name", () => {
|
||||
expect(tool.name).toBe("get_lines")
|
||||
})
|
||||
|
||||
it("should have correct category", () => {
|
||||
expect(tool.category).toBe("read")
|
||||
})
|
||||
|
||||
it("should not require confirmation", () => {
|
||||
expect(tool.requiresConfirmation).toBe(false)
|
||||
})
|
||||
|
||||
it("should have correct parameters", () => {
|
||||
expect(tool.parameters).toHaveLength(3)
|
||||
expect(tool.parameters[0].name).toBe("path")
|
||||
expect(tool.parameters[0].required).toBe(true)
|
||||
expect(tool.parameters[1].name).toBe("start")
|
||||
expect(tool.parameters[1].required).toBe(false)
|
||||
expect(tool.parameters[2].name).toBe("end")
|
||||
expect(tool.parameters[2].required).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("validateParams", () => {
|
||||
it("should return null for valid params with path only", () => {
|
||||
expect(tool.validateParams({ path: "src/index.ts" })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return null for valid params with start and end", () => {
|
||||
expect(tool.validateParams({ path: "src/index.ts", start: 1, end: 10 })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return error for missing path", () => {
|
||||
expect(tool.validateParams({})).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for empty path", () => {
|
||||
expect(tool.validateParams({ path: "" })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
expect(tool.validateParams({ path: " " })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for non-string path", () => {
|
||||
expect(tool.validateParams({ path: 123 })).toBe(
|
||||
"Parameter 'path' is required and must be a non-empty string",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for non-integer start", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", start: 1.5 })).toBe(
|
||||
"Parameter 'start' must be an integer",
|
||||
)
|
||||
expect(tool.validateParams({ path: "test.ts", start: "1" })).toBe(
|
||||
"Parameter 'start' must be an integer",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for start < 1", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", start: 0 })).toBe(
|
||||
"Parameter 'start' must be >= 1",
|
||||
)
|
||||
expect(tool.validateParams({ path: "test.ts", start: -1 })).toBe(
|
||||
"Parameter 'start' must be >= 1",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for non-integer end", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", end: 1.5 })).toBe(
|
||||
"Parameter 'end' must be an integer",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for end < 1", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", end: 0 })).toBe(
|
||||
"Parameter 'end' must be >= 1",
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error for start > end", () => {
|
||||
expect(tool.validateParams({ path: "test.ts", start: 10, end: 5 })).toBe(
|
||||
"Parameter 'start' must be <= 'end'",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("execute", () => {
|
||||
it("should return all lines when no range specified", async () => {
|
||||
const lines = ["line 1", "line 2", "line 3"]
|
||||
const storage = createMockStorage({ lines })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.path).toBe("test.ts")
|
||||
expect(data.startLine).toBe(1)
|
||||
expect(data.endLine).toBe(3)
|
||||
expect(data.totalLines).toBe(3)
|
||||
expect(data.content).toContain("1│line 1")
|
||||
expect(data.content).toContain("2│line 2")
|
||||
expect(data.content).toContain("3│line 3")
|
||||
})
|
||||
|
||||
it("should return specific range", async () => {
|
||||
const lines = ["line 1", "line 2", "line 3", "line 4", "line 5"]
|
||||
const storage = createMockStorage({ lines })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", start: 2, end: 4 }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.startLine).toBe(2)
|
||||
expect(data.endLine).toBe(4)
|
||||
expect(data.content).toContain("2│line 2")
|
||||
expect(data.content).toContain("3│line 3")
|
||||
expect(data.content).toContain("4│line 4")
|
||||
expect(data.content).not.toContain("line 1")
|
||||
expect(data.content).not.toContain("line 5")
|
||||
})
|
||||
|
||||
it("should clamp start to 1 if less", async () => {
|
||||
const lines = ["line 1", "line 2"]
|
||||
const storage = createMockStorage({ lines })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", start: -5, end: 2 }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.startLine).toBe(1)
|
||||
})
|
||||
|
||||
it("should clamp end to totalLines if greater", async () => {
|
||||
const lines = ["line 1", "line 2", "line 3"]
|
||||
const storage = createMockStorage({ lines })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", start: 1, end: 100 }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.endLine).toBe(3)
|
||||
})
|
||||
|
||||
it("should pad line numbers correctly", async () => {
|
||||
const lines = Array.from({ length: 100 }, (_, i) => `line ${i + 1}`)
|
||||
const storage = createMockStorage({ lines })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts", start: 98, end: 100 }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.content).toContain(" 98│line 98")
|
||||
expect(data.content).toContain(" 99│line 99")
|
||||
expect(data.content).toContain("100│line 100")
|
||||
})
|
||||
|
||||
it("should return error for path outside project root", async () => {
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await tool.execute({ path: "../outside/file.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Path must be within project root")
|
||||
})
|
||||
|
||||
it("should return error when file not found", async () => {
|
||||
const storage = createMockStorage(null)
|
||||
storage.getFile = vi.fn().mockResolvedValue(null)
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "nonexistent.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("ENOENT")
|
||||
})
|
||||
|
||||
it("should include callId in result", async () => {
|
||||
const storage = createMockStorage({ lines: ["test"] })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.callId).toMatch(/^get_lines-\d+$/)
|
||||
})
|
||||
|
||||
it("should include executionTimeMs in result", async () => {
|
||||
const storage = createMockStorage({ lines: ["test"] })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.executionTimeMs).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
|
||||
it("should handle empty file", async () => {
|
||||
const storage = createMockStorage({ lines: [] })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "empty.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.totalLines).toBe(0)
|
||||
expect(data.content).toBe("")
|
||||
})
|
||||
|
||||
it("should handle single line file", async () => {
|
||||
const storage = createMockStorage({ lines: ["only line"] })
|
||||
const ctx = createMockContext(storage)
|
||||
|
||||
const result = await tool.execute({ path: "single.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetLinesResult
|
||||
expect(data.totalLines).toBe(1)
|
||||
expect(data.content).toBe("1│only line")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,274 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
||||
import { promises as fs } from "node:fs"
|
||||
import * as path from "node:path"
|
||||
import * as os from "node:os"
|
||||
import {
|
||||
GetStructureTool,
|
||||
type GetStructureResult,
|
||||
} from "../../../../../src/infrastructure/tools/read/GetStructureTool.js"
|
||||
import type { ToolContext } from "../../../../../src/domain/services/ITool.js"
|
||||
import type { IStorage } from "../../../../../src/domain/services/IStorage.js"
|
||||
|
||||
function createMockStorage(): IStorage {
|
||||
return {
|
||||
getFile: vi.fn(),
|
||||
setFile: vi.fn(),
|
||||
deleteFile: vi.fn(),
|
||||
getAllFiles: vi.fn(),
|
||||
getAST: vi.fn(),
|
||||
setAST: vi.fn(),
|
||||
getMeta: vi.fn(),
|
||||
setMeta: vi.fn(),
|
||||
getSymbolIndex: vi.fn(),
|
||||
setSymbolIndex: vi.fn(),
|
||||
getDepsGraph: vi.fn(),
|
||||
setDepsGraph: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
setConfig: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
} as unknown as IStorage
|
||||
}
|
||||
|
||||
function createMockContext(projectRoot: string): ToolContext {
|
||||
return {
|
||||
projectRoot,
|
||||
storage: createMockStorage(),
|
||||
requestConfirmation: vi.fn().mockResolvedValue(true),
|
||||
onProgress: vi.fn(),
|
||||
}
|
||||
}
|
||||
|
||||
describe("GetStructureTool", () => {
|
||||
let tool: GetStructureTool
|
||||
let tempDir: string
|
||||
|
||||
beforeEach(async () => {
|
||||
tool = new GetStructureTool()
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ipuaro-test-"))
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await fs.rm(tempDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
describe("metadata", () => {
|
||||
it("should have correct name", () => {
|
||||
expect(tool.name).toBe("get_structure")
|
||||
})
|
||||
|
||||
it("should have correct category", () => {
|
||||
expect(tool.category).toBe("read")
|
||||
})
|
||||
|
||||
it("should not require confirmation", () => {
|
||||
expect(tool.requiresConfirmation).toBe(false)
|
||||
})
|
||||
|
||||
it("should have correct parameters", () => {
|
||||
expect(tool.parameters).toHaveLength(2)
|
||||
expect(tool.parameters[0].name).toBe("path")
|
||||
expect(tool.parameters[0].required).toBe(false)
|
||||
expect(tool.parameters[1].name).toBe("depth")
|
||||
expect(tool.parameters[1].required).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("validateParams", () => {
|
||||
it("should return null for empty params", () => {
|
||||
expect(tool.validateParams({})).toBeNull()
|
||||
})
|
||||
|
||||
it("should return null for valid path", () => {
|
||||
expect(tool.validateParams({ path: "src" })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return null for valid depth", () => {
|
||||
expect(tool.validateParams({ depth: 3 })).toBeNull()
|
||||
})
|
||||
|
||||
it("should return error for non-string path", () => {
|
||||
expect(tool.validateParams({ path: 123 })).toBe("Parameter 'path' must be a string")
|
||||
})
|
||||
|
||||
it("should return error for non-integer depth", () => {
|
||||
expect(tool.validateParams({ depth: 2.5 })).toBe("Parameter 'depth' must be an integer")
|
||||
})
|
||||
|
||||
it("should return error for depth < 1", () => {
|
||||
expect(tool.validateParams({ depth: 0 })).toBe("Parameter 'depth' must be >= 1")
|
||||
})
|
||||
})
|
||||
|
||||
describe("execute", () => {
|
||||
it("should return tree structure for empty directory", async () => {
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.path).toBe(".")
|
||||
expect(data.tree.type).toBe("directory")
|
||||
expect(data.tree.children).toEqual([])
|
||||
expect(data.stats.directories).toBe(1)
|
||||
expect(data.stats.files).toBe(0)
|
||||
})
|
||||
|
||||
it("should return tree structure with files", async () => {
|
||||
await fs.writeFile(path.join(tempDir, "file1.ts"), "")
|
||||
await fs.writeFile(path.join(tempDir, "file2.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.tree.children).toHaveLength(2)
|
||||
expect(data.stats.files).toBe(2)
|
||||
expect(data.content).toContain("file1.ts")
|
||||
expect(data.content).toContain("file2.ts")
|
||||
})
|
||||
|
||||
it("should return nested directory structure", async () => {
|
||||
await fs.mkdir(path.join(tempDir, "src"))
|
||||
await fs.writeFile(path.join(tempDir, "src", "index.ts"), "")
|
||||
await fs.mkdir(path.join(tempDir, "src", "utils"))
|
||||
await fs.writeFile(path.join(tempDir, "src", "utils", "helper.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.stats.directories).toBe(3)
|
||||
expect(data.stats.files).toBe(2)
|
||||
expect(data.content).toContain("src")
|
||||
expect(data.content).toContain("index.ts")
|
||||
expect(data.content).toContain("utils")
|
||||
expect(data.content).toContain("helper.ts")
|
||||
})
|
||||
|
||||
it("should respect depth parameter", async () => {
|
||||
await fs.mkdir(path.join(tempDir, "level1"))
|
||||
await fs.mkdir(path.join(tempDir, "level1", "level2"))
|
||||
await fs.mkdir(path.join(tempDir, "level1", "level2", "level3"))
|
||||
await fs.writeFile(path.join(tempDir, "level1", "level2", "level3", "deep.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({ depth: 2 }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.content).toContain("level1")
|
||||
expect(data.content).toContain("level2")
|
||||
expect(data.content).not.toContain("level3")
|
||||
expect(data.content).not.toContain("deep.ts")
|
||||
})
|
||||
|
||||
it("should filter subdirectory when path specified", async () => {
|
||||
await fs.mkdir(path.join(tempDir, "src"))
|
||||
await fs.mkdir(path.join(tempDir, "tests"))
|
||||
await fs.writeFile(path.join(tempDir, "src", "index.ts"), "")
|
||||
await fs.writeFile(path.join(tempDir, "tests", "test.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({ path: "src" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.path).toBe("src")
|
||||
expect(data.content).toContain("index.ts")
|
||||
expect(data.content).not.toContain("test.ts")
|
||||
})
|
||||
|
||||
it("should ignore node_modules", async () => {
|
||||
await fs.mkdir(path.join(tempDir, "node_modules"))
|
||||
await fs.writeFile(path.join(tempDir, "node_modules", "pkg.js"), "")
|
||||
await fs.writeFile(path.join(tempDir, "index.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.content).not.toContain("node_modules")
|
||||
expect(data.content).toContain("index.ts")
|
||||
})
|
||||
|
||||
it("should ignore .git directory", async () => {
|
||||
await fs.mkdir(path.join(tempDir, ".git"))
|
||||
await fs.writeFile(path.join(tempDir, ".git", "config"), "")
|
||||
await fs.writeFile(path.join(tempDir, "index.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.content).not.toContain(".git")
|
||||
})
|
||||
|
||||
it("should sort directories before files", async () => {
|
||||
await fs.writeFile(path.join(tempDir, "aaa.ts"), "")
|
||||
await fs.mkdir(path.join(tempDir, "zzz"))
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
const zzzIndex = data.content.indexOf("zzz")
|
||||
const aaaIndex = data.content.indexOf("aaa.ts")
|
||||
expect(zzzIndex).toBeLessThan(aaaIndex)
|
||||
})
|
||||
|
||||
it("should return error for path outside project root", async () => {
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({ path: "../outside" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Path must be within project root")
|
||||
})
|
||||
|
||||
it("should return error for non-directory path", async () => {
|
||||
await fs.writeFile(path.join(tempDir, "file.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({ path: "file.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("is not a directory")
|
||||
})
|
||||
|
||||
it("should return error for non-existent path", async () => {
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({ path: "nonexistent" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("ENOENT")
|
||||
})
|
||||
|
||||
it("should include callId in result", async () => {
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.callId).toMatch(/^get_structure-\d+$/)
|
||||
})
|
||||
|
||||
it("should use tree icons in output", async () => {
|
||||
await fs.mkdir(path.join(tempDir, "src"))
|
||||
await fs.writeFile(path.join(tempDir, "index.ts"), "")
|
||||
const ctx = createMockContext(tempDir)
|
||||
|
||||
const result = await tool.execute({}, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
const data = result.data as GetStructureResult
|
||||
expect(data.content).toContain("📁")
|
||||
expect(data.content).toContain("📄")
|
||||
})
|
||||
})
|
||||
})
|
||||
449
packages/ipuaro/tests/unit/infrastructure/tools/registry.test.ts
Normal file
449
packages/ipuaro/tests/unit/infrastructure/tools/registry.test.ts
Normal file
@@ -0,0 +1,449 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import { ToolRegistry } from "../../../../src/infrastructure/tools/registry.js"
|
||||
import type {
|
||||
ITool,
|
||||
ToolContext,
|
||||
ToolParameterSchema,
|
||||
} from "../../../../src/domain/services/ITool.js"
|
||||
import type { ToolResult } from "../../../../src/domain/value-objects/ToolResult.js"
|
||||
import { IpuaroError } from "../../../../src/shared/errors/IpuaroError.js"
|
||||
|
||||
/**
|
||||
* Creates a mock tool for testing.
|
||||
*/
|
||||
function createMockTool(overrides: Partial<ITool> = {}): ITool {
|
||||
return {
|
||||
name: "mock_tool",
|
||||
description: "A mock tool for testing",
|
||||
parameters: [
|
||||
{
|
||||
name: "path",
|
||||
type: "string",
|
||||
description: "File path",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "optional",
|
||||
type: "number",
|
||||
description: "Optional param",
|
||||
required: false,
|
||||
},
|
||||
],
|
||||
requiresConfirmation: false,
|
||||
category: "read",
|
||||
execute: vi.fn().mockResolvedValue({
|
||||
callId: "test-123",
|
||||
success: true,
|
||||
data: { result: "success" },
|
||||
executionTimeMs: 10,
|
||||
}),
|
||||
validateParams: vi.fn().mockReturnValue(null),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock tool context for testing.
|
||||
*/
|
||||
function createMockContext(overrides: Partial<ToolContext> = {}): ToolContext {
|
||||
return {
|
||||
projectRoot: "/test/project",
|
||||
storage: {} as ToolContext["storage"],
|
||||
requestConfirmation: vi.fn().mockResolvedValue(true),
|
||||
onProgress: vi.fn(),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe("ToolRegistry", () => {
|
||||
let registry: ToolRegistry
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new ToolRegistry()
|
||||
})
|
||||
|
||||
describe("register", () => {
|
||||
it("should register a tool", () => {
|
||||
const tool = createMockTool()
|
||||
|
||||
registry.register(tool)
|
||||
|
||||
expect(registry.has("mock_tool")).toBe(true)
|
||||
expect(registry.size).toBe(1)
|
||||
})
|
||||
|
||||
it("should register multiple tools", () => {
|
||||
const tool1 = createMockTool({ name: "tool_1" })
|
||||
const tool2 = createMockTool({ name: "tool_2" })
|
||||
|
||||
registry.register(tool1)
|
||||
registry.register(tool2)
|
||||
|
||||
expect(registry.size).toBe(2)
|
||||
expect(registry.has("tool_1")).toBe(true)
|
||||
expect(registry.has("tool_2")).toBe(true)
|
||||
})
|
||||
|
||||
it("should throw error when registering duplicate tool name", () => {
|
||||
const tool1 = createMockTool({ name: "duplicate" })
|
||||
const tool2 = createMockTool({ name: "duplicate" })
|
||||
|
||||
registry.register(tool1)
|
||||
|
||||
expect(() => registry.register(tool2)).toThrow(IpuaroError)
|
||||
expect(() => registry.register(tool2)).toThrow('Tool "duplicate" is already registered')
|
||||
})
|
||||
})
|
||||
|
||||
describe("unregister", () => {
|
||||
it("should remove a registered tool", () => {
|
||||
const tool = createMockTool()
|
||||
registry.register(tool)
|
||||
|
||||
const result = registry.unregister("mock_tool")
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(registry.has("mock_tool")).toBe(false)
|
||||
expect(registry.size).toBe(0)
|
||||
})
|
||||
|
||||
it("should return false when tool not found", () => {
|
||||
const result = registry.unregister("nonexistent")
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("get", () => {
|
||||
it("should return registered tool", () => {
|
||||
const tool = createMockTool()
|
||||
registry.register(tool)
|
||||
|
||||
const result = registry.get("mock_tool")
|
||||
|
||||
expect(result).toBe(tool)
|
||||
})
|
||||
|
||||
it("should return undefined for unknown tool", () => {
|
||||
const result = registry.get("unknown")
|
||||
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe("getAll", () => {
|
||||
it("should return empty array when no tools registered", () => {
|
||||
const result = registry.getAll()
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it("should return all registered tools", () => {
|
||||
const tool1 = createMockTool({ name: "tool_1" })
|
||||
const tool2 = createMockTool({ name: "tool_2" })
|
||||
registry.register(tool1)
|
||||
registry.register(tool2)
|
||||
|
||||
const result = registry.getAll()
|
||||
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result).toContain(tool1)
|
||||
expect(result).toContain(tool2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getByCategory", () => {
|
||||
it("should return tools by category", () => {
|
||||
const readTool = createMockTool({ name: "read_tool", category: "read" })
|
||||
const editTool = createMockTool({ name: "edit_tool", category: "edit" })
|
||||
const gitTool = createMockTool({ name: "git_tool", category: "git" })
|
||||
registry.register(readTool)
|
||||
registry.register(editTool)
|
||||
registry.register(gitTool)
|
||||
|
||||
const readTools = registry.getByCategory("read")
|
||||
const editTools = registry.getByCategory("edit")
|
||||
|
||||
expect(readTools).toHaveLength(1)
|
||||
expect(readTools[0]).toBe(readTool)
|
||||
expect(editTools).toHaveLength(1)
|
||||
expect(editTools[0]).toBe(editTool)
|
||||
})
|
||||
|
||||
it("should return empty array for category with no tools", () => {
|
||||
const readTool = createMockTool({ category: "read" })
|
||||
registry.register(readTool)
|
||||
|
||||
const result = registry.getByCategory("analysis")
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("has", () => {
|
||||
it("should return true for registered tool", () => {
|
||||
registry.register(createMockTool())
|
||||
|
||||
expect(registry.has("mock_tool")).toBe(true)
|
||||
})
|
||||
|
||||
it("should return false for unknown tool", () => {
|
||||
expect(registry.has("unknown")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("execute", () => {
|
||||
it("should execute tool and return result", async () => {
|
||||
const tool = createMockTool()
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.data).toEqual({ result: "success" })
|
||||
expect(tool.execute).toHaveBeenCalledWith({ path: "test.ts" }, ctx)
|
||||
})
|
||||
|
||||
it("should return error result for unknown tool", async () => {
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await registry.execute("unknown", {}, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe('Tool "unknown" not found')
|
||||
})
|
||||
|
||||
it("should return error result when validation fails", async () => {
|
||||
const tool = createMockTool({
|
||||
validateParams: vi.fn().mockReturnValue("Missing required param: path"),
|
||||
})
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await registry.execute("mock_tool", {}, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Missing required param: path")
|
||||
expect(tool.execute).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should request confirmation for tools that require it", async () => {
|
||||
const tool = createMockTool({ requiresConfirmation: true })
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(ctx.requestConfirmation).toHaveBeenCalled()
|
||||
expect(tool.execute).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should not execute when confirmation is denied", async () => {
|
||||
const tool = createMockTool({ requiresConfirmation: true })
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext({
|
||||
requestConfirmation: vi.fn().mockResolvedValue(false),
|
||||
})
|
||||
|
||||
const result = await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("User cancelled operation")
|
||||
expect(tool.execute).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should not request confirmation for safe tools", async () => {
|
||||
const tool = createMockTool({ requiresConfirmation: false })
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(ctx.requestConfirmation).not.toHaveBeenCalled()
|
||||
expect(tool.execute).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should catch and return errors from tool execution", async () => {
|
||||
const tool = createMockTool({
|
||||
execute: vi.fn().mockRejectedValue(new Error("Execution failed")),
|
||||
})
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toBe("Execution failed")
|
||||
})
|
||||
|
||||
it("should include callId in result", async () => {
|
||||
const tool = createMockTool()
|
||||
registry.register(tool)
|
||||
const ctx = createMockContext()
|
||||
|
||||
const result = await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
|
||||
expect(result.callId).toMatch(/^mock_tool-\d+$/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getToolDefinitions", () => {
|
||||
it("should return empty array when no tools registered", () => {
|
||||
const result = registry.getToolDefinitions()
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it("should convert tools to LLM-compatible format", () => {
|
||||
const tool = createMockTool()
|
||||
registry.register(tool)
|
||||
|
||||
const result = registry.getToolDefinitions()
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0]).toEqual({
|
||||
name: "mock_tool",
|
||||
description: "A mock tool for testing",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
path: {
|
||||
type: "string",
|
||||
description: "File path",
|
||||
},
|
||||
optional: {
|
||||
type: "number",
|
||||
description: "Optional param",
|
||||
},
|
||||
},
|
||||
required: ["path"],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle tools with no parameters", () => {
|
||||
const tool = createMockTool({ parameters: [] })
|
||||
registry.register(tool)
|
||||
|
||||
const result = registry.getToolDefinitions()
|
||||
|
||||
expect(result[0].parameters).toEqual({
|
||||
type: "object",
|
||||
properties: {},
|
||||
required: [],
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle multiple tools", () => {
|
||||
registry.register(createMockTool({ name: "tool_1" }))
|
||||
registry.register(createMockTool({ name: "tool_2" }))
|
||||
|
||||
const result = registry.getToolDefinitions()
|
||||
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result.map((t) => t.name)).toEqual(["tool_1", "tool_2"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("clear", () => {
|
||||
it("should remove all tools", () => {
|
||||
registry.register(createMockTool({ name: "tool_1" }))
|
||||
registry.register(createMockTool({ name: "tool_2" }))
|
||||
|
||||
registry.clear()
|
||||
|
||||
expect(registry.size).toBe(0)
|
||||
expect(registry.getAll()).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("getNames", () => {
|
||||
it("should return all tool names", () => {
|
||||
registry.register(createMockTool({ name: "alpha" }))
|
||||
registry.register(createMockTool({ name: "beta" }))
|
||||
|
||||
const result = registry.getNames()
|
||||
|
||||
expect(result).toEqual(["alpha", "beta"])
|
||||
})
|
||||
|
||||
it("should return empty array when no tools", () => {
|
||||
const result = registry.getNames()
|
||||
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("getConfirmationTools", () => {
|
||||
it("should return only tools requiring confirmation", () => {
|
||||
registry.register(createMockTool({ name: "safe", requiresConfirmation: false }))
|
||||
registry.register(createMockTool({ name: "dangerous", requiresConfirmation: true }))
|
||||
registry.register(createMockTool({ name: "also_safe", requiresConfirmation: false }))
|
||||
|
||||
const result = registry.getConfirmationTools()
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].name).toBe("dangerous")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getSafeTools", () => {
|
||||
it("should return only tools not requiring confirmation", () => {
|
||||
registry.register(createMockTool({ name: "safe", requiresConfirmation: false }))
|
||||
registry.register(createMockTool({ name: "dangerous", requiresConfirmation: true }))
|
||||
registry.register(createMockTool({ name: "also_safe", requiresConfirmation: false }))
|
||||
|
||||
const result = registry.getSafeTools()
|
||||
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result.map((t) => t.name)).toEqual(["safe", "also_safe"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("size", () => {
|
||||
it("should return 0 for empty registry", () => {
|
||||
expect(registry.size).toBe(0)
|
||||
})
|
||||
|
||||
it("should return correct count", () => {
|
||||
registry.register(createMockTool({ name: "a" }))
|
||||
registry.register(createMockTool({ name: "b" }))
|
||||
registry.register(createMockTool({ name: "c" }))
|
||||
|
||||
expect(registry.size).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe("integration scenarios", () => {
|
||||
it("should handle full workflow: register, execute, unregister", async () => {
|
||||
const tool = createMockTool()
|
||||
const ctx = createMockContext()
|
||||
|
||||
registry.register(tool)
|
||||
expect(registry.has("mock_tool")).toBe(true)
|
||||
|
||||
const result = await registry.execute("mock_tool", { path: "test.ts" }, ctx)
|
||||
expect(result.success).toBe(true)
|
||||
|
||||
registry.unregister("mock_tool")
|
||||
expect(registry.has("mock_tool")).toBe(false)
|
||||
|
||||
const afterUnregister = await registry.execute("mock_tool", {}, ctx)
|
||||
expect(afterUnregister.success).toBe(false)
|
||||
})
|
||||
|
||||
it("should maintain isolation between registrations", () => {
|
||||
const registry1 = new ToolRegistry()
|
||||
const registry2 = new ToolRegistry()
|
||||
|
||||
registry1.register(createMockTool({ name: "tool_1" }))
|
||||
registry2.register(createMockTool({ name: "tool_2" }))
|
||||
|
||||
expect(registry1.has("tool_1")).toBe(true)
|
||||
expect(registry1.has("tool_2")).toBe(false)
|
||||
expect(registry2.has("tool_1")).toBe(false)
|
||||
expect(registry2.has("tool_2")).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user