mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-27 23:06:54 +05:00
feat(ipuaro): add CLI entry point (v0.15.0)
- Add onboarding module for pre-flight checks (Redis, Ollama, model, project) - Implement start command with TUI rendering and dependency injection - Implement init command for .ipuaro.json config file creation - Implement index command for standalone project indexing - Add CLI options: --auto-apply, --model, --help, --version - Register all 18 tools via tools-setup helper - Add 29 unit tests for CLI commands - Update CHANGELOG and ROADMAP for v0.15.0
This commit is contained in:
117
packages/ipuaro/tests/unit/cli/commands/init.test.ts
Normal file
117
packages/ipuaro/tests/unit/cli/commands/init.test.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
||||
import * as fs from "node:fs/promises"
|
||||
import * as path from "node:path"
|
||||
import { executeInit } from "../../../../src/cli/commands/init.js"
|
||||
|
||||
vi.mock("node:fs/promises")
|
||||
|
||||
describe("executeInit", () => {
|
||||
const testPath = "/test/project"
|
||||
const configPath = path.join(testPath, ".ipuaro.json")
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.spyOn(console, "warn").mockImplementation(() => {})
|
||||
vi.spyOn(console, "error").mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it("should create .ipuaro.json file successfully", async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error("ENOENT"))
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined)
|
||||
|
||||
const result = await executeInit(testPath)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.filePath).toBe(configPath)
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
configPath,
|
||||
expect.stringContaining('"redis"'),
|
||||
"utf-8",
|
||||
)
|
||||
})
|
||||
|
||||
it("should skip existing file without force option", async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined)
|
||||
|
||||
const result = await executeInit(testPath)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.skipped).toBe(true)
|
||||
expect(fs.writeFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should overwrite existing file with force option", async () => {
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined)
|
||||
|
||||
const result = await executeInit(testPath, { force: true })
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.skipped).toBeUndefined()
|
||||
expect(fs.writeFile).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should handle write errors", async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error("ENOENT"))
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockRejectedValue(new Error("Permission denied"))
|
||||
|
||||
const result = await executeInit(testPath)
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.error).toContain("Permission denied")
|
||||
})
|
||||
|
||||
it("should create parent directories if needed", async () => {
|
||||
vi.mocked(fs.access)
|
||||
.mockRejectedValueOnce(new Error("ENOENT"))
|
||||
.mockRejectedValueOnce(new Error("ENOENT"))
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined)
|
||||
|
||||
const result = await executeInit(testPath)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(expect.any(String), { recursive: true })
|
||||
})
|
||||
|
||||
it("should use current directory as default", async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error("ENOENT"))
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined)
|
||||
|
||||
const result = await executeInit()
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.filePath).toContain(".ipuaro.json")
|
||||
})
|
||||
|
||||
it("should include expected config sections", async () => {
|
||||
vi.mocked(fs.access).mockRejectedValue(new Error("ENOENT"))
|
||||
vi.mocked(fs.mkdir).mockResolvedValue(undefined)
|
||||
vi.mocked(fs.writeFile).mockResolvedValue(undefined)
|
||||
|
||||
await executeInit(testPath)
|
||||
|
||||
const writeCall = vi.mocked(fs.writeFile).mock.calls[0]
|
||||
const content = writeCall[1] as string
|
||||
const config = JSON.parse(content) as {
|
||||
redis: unknown
|
||||
llm: unknown
|
||||
edit: unknown
|
||||
}
|
||||
|
||||
expect(config).toHaveProperty("redis")
|
||||
expect(config).toHaveProperty("llm")
|
||||
expect(config).toHaveProperty("edit")
|
||||
expect(config.redis).toHaveProperty("host", "localhost")
|
||||
expect(config.redis).toHaveProperty("port", 6379)
|
||||
expect(config.llm).toHaveProperty("model", "qwen2.5-coder:7b-instruct")
|
||||
expect(config.edit).toHaveProperty("autoApply", false)
|
||||
})
|
||||
})
|
||||
353
packages/ipuaro/tests/unit/cli/commands/onboarding.test.ts
Normal file
353
packages/ipuaro/tests/unit/cli/commands/onboarding.test.ts
Normal file
@@ -0,0 +1,353 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
||||
import {
|
||||
checkRedis,
|
||||
checkOllama,
|
||||
checkModel,
|
||||
checkProjectSize,
|
||||
runOnboarding,
|
||||
} from "../../../../src/cli/commands/onboarding.js"
|
||||
import { RedisClient } from "../../../../src/infrastructure/storage/RedisClient.js"
|
||||
import { OllamaClient } from "../../../../src/infrastructure/llm/OllamaClient.js"
|
||||
import { FileScanner } from "../../../../src/infrastructure/indexer/FileScanner.js"
|
||||
|
||||
vi.mock("../../../../src/infrastructure/storage/RedisClient.js")
|
||||
vi.mock("../../../../src/infrastructure/llm/OllamaClient.js")
|
||||
vi.mock("../../../../src/infrastructure/indexer/FileScanner.js")
|
||||
|
||||
describe("onboarding", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe("checkRedis", () => {
|
||||
it("should return ok when Redis connects and pings successfully", async () => {
|
||||
const mockConnect = vi.fn().mockResolvedValue(undefined)
|
||||
const mockPing = vi.fn().mockResolvedValue(true)
|
||||
const mockDisconnect = vi.fn().mockResolvedValue(undefined)
|
||||
|
||||
vi.mocked(RedisClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
connect: mockConnect,
|
||||
ping: mockPing,
|
||||
disconnect: mockDisconnect,
|
||||
}) as unknown as RedisClient,
|
||||
)
|
||||
|
||||
const result = await checkRedis({
|
||||
host: "localhost",
|
||||
port: 6379,
|
||||
db: 0,
|
||||
keyPrefix: "ipuaro:",
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.error).toBeUndefined()
|
||||
expect(mockConnect).toHaveBeenCalled()
|
||||
expect(mockPing).toHaveBeenCalled()
|
||||
expect(mockDisconnect).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should return error when Redis connection fails", async () => {
|
||||
vi.mocked(RedisClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
connect: vi.fn().mockRejectedValue(new Error("Connection refused")),
|
||||
}) as unknown as RedisClient,
|
||||
)
|
||||
|
||||
const result = await checkRedis({
|
||||
host: "localhost",
|
||||
port: 6379,
|
||||
db: 0,
|
||||
keyPrefix: "ipuaro:",
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.error).toContain("Cannot connect to Redis")
|
||||
})
|
||||
|
||||
it("should return error when ping fails", async () => {
|
||||
vi.mocked(RedisClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
connect: vi.fn().mockResolvedValue(undefined),
|
||||
ping: vi.fn().mockResolvedValue(false),
|
||||
disconnect: vi.fn().mockResolvedValue(undefined),
|
||||
}) as unknown as RedisClient,
|
||||
)
|
||||
|
||||
const result = await checkRedis({
|
||||
host: "localhost",
|
||||
port: 6379,
|
||||
db: 0,
|
||||
keyPrefix: "ipuaro:",
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.error).toContain("Redis ping failed")
|
||||
})
|
||||
})
|
||||
|
||||
describe("checkOllama", () => {
|
||||
it("should return ok when Ollama is available", async () => {
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
isAvailable: vi.fn().mockResolvedValue(true),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
const result = await checkOllama({
|
||||
model: "qwen2.5-coder:7b-instruct",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.error).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should return error when Ollama is not available", async () => {
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
isAvailable: vi.fn().mockResolvedValue(false),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
const result = await checkOllama({
|
||||
model: "qwen2.5-coder:7b-instruct",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.error).toContain("Cannot connect to Ollama")
|
||||
})
|
||||
})
|
||||
|
||||
describe("checkModel", () => {
|
||||
it("should return ok when model is available", async () => {
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
hasModel: vi.fn().mockResolvedValue(true),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
const result = await checkModel({
|
||||
model: "qwen2.5-coder:7b-instruct",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.needsPull).toBe(false)
|
||||
})
|
||||
|
||||
it("should return needsPull when model is not available", async () => {
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
hasModel: vi.fn().mockResolvedValue(false),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
const result = await checkModel({
|
||||
model: "qwen2.5-coder:7b-instruct",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.needsPull).toBe(true)
|
||||
expect(result.error).toContain("not installed")
|
||||
})
|
||||
})
|
||||
|
||||
describe("checkProjectSize", () => {
|
||||
it("should return ok when file count is within limits", async () => {
|
||||
vi.mocked(FileScanner).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
scanAll: vi.fn().mockResolvedValue(
|
||||
Array.from({ length: 100 }, (_, i) => ({
|
||||
path: `file${String(i)}.ts`,
|
||||
type: "file" as const,
|
||||
size: 1000,
|
||||
lastModified: Date.now(),
|
||||
})),
|
||||
),
|
||||
}) as unknown as FileScanner,
|
||||
)
|
||||
|
||||
const result = await checkProjectSize("/test/path")
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.fileCount).toBe(100)
|
||||
expect(result.warning).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should return warning when file count exceeds limit", async () => {
|
||||
vi.mocked(FileScanner).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
scanAll: vi.fn().mockResolvedValue(
|
||||
Array.from({ length: 15000 }, (_, i) => ({
|
||||
path: `file${String(i)}.ts`,
|
||||
type: "file" as const,
|
||||
size: 1000,
|
||||
lastModified: Date.now(),
|
||||
})),
|
||||
),
|
||||
}) as unknown as FileScanner,
|
||||
)
|
||||
|
||||
const result = await checkProjectSize("/test/path", 10_000)
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.fileCount).toBe(15000)
|
||||
expect(result.warning).toContain("15")
|
||||
expect(result.warning).toContain("000 files")
|
||||
})
|
||||
|
||||
it("should return error when no files found", async () => {
|
||||
vi.mocked(FileScanner).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
scanAll: vi.fn().mockResolvedValue([]),
|
||||
}) as unknown as FileScanner,
|
||||
)
|
||||
|
||||
const result = await checkProjectSize("/test/path")
|
||||
|
||||
expect(result.ok).toBe(false)
|
||||
expect(result.fileCount).toBe(0)
|
||||
expect(result.warning).toContain("No supported files found")
|
||||
})
|
||||
})
|
||||
|
||||
describe("runOnboarding", () => {
|
||||
it("should return success when all checks pass", async () => {
|
||||
vi.mocked(RedisClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
connect: vi.fn().mockResolvedValue(undefined),
|
||||
ping: vi.fn().mockResolvedValue(true),
|
||||
disconnect: vi.fn().mockResolvedValue(undefined),
|
||||
}) as unknown as RedisClient,
|
||||
)
|
||||
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
isAvailable: vi.fn().mockResolvedValue(true),
|
||||
hasModel: vi.fn().mockResolvedValue(true),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
vi.mocked(FileScanner).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
scanAll: vi.fn().mockResolvedValue([{ path: "file.ts" }]),
|
||||
}) as unknown as FileScanner,
|
||||
)
|
||||
|
||||
const result = await runOnboarding({
|
||||
redisConfig: { host: "localhost", port: 6379, db: 0, keyPrefix: "ipuaro:" },
|
||||
llmConfig: {
|
||||
model: "test",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
},
|
||||
projectPath: "/test/path",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.redisOk).toBe(true)
|
||||
expect(result.ollamaOk).toBe(true)
|
||||
expect(result.modelOk).toBe(true)
|
||||
expect(result.projectOk).toBe(true)
|
||||
expect(result.errors).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should return failure when Redis fails", async () => {
|
||||
vi.mocked(RedisClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
connect: vi.fn().mockRejectedValue(new Error("Connection refused")),
|
||||
}) as unknown as RedisClient,
|
||||
)
|
||||
|
||||
vi.mocked(OllamaClient).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
isAvailable: vi.fn().mockResolvedValue(true),
|
||||
hasModel: vi.fn().mockResolvedValue(true),
|
||||
}) as unknown as OllamaClient,
|
||||
)
|
||||
|
||||
vi.mocked(FileScanner).mockImplementation(
|
||||
() =>
|
||||
({
|
||||
scanAll: vi.fn().mockResolvedValue([{ path: "file.ts" }]),
|
||||
}) as unknown as FileScanner,
|
||||
)
|
||||
|
||||
const result = await runOnboarding({
|
||||
redisConfig: { host: "localhost", port: 6379, db: 0, keyPrefix: "ipuaro:" },
|
||||
llmConfig: {
|
||||
model: "test",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
},
|
||||
projectPath: "/test/path",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.redisOk).toBe(false)
|
||||
expect(result.errors.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it("should skip checks when skip options are set", async () => {
|
||||
const result = await runOnboarding({
|
||||
redisConfig: { host: "localhost", port: 6379, db: 0, keyPrefix: "ipuaro:" },
|
||||
llmConfig: {
|
||||
model: "test",
|
||||
contextWindow: 128_000,
|
||||
temperature: 0.1,
|
||||
host: "http://localhost:11434",
|
||||
timeout: 120_000,
|
||||
},
|
||||
projectPath: "/test/path",
|
||||
skipRedis: true,
|
||||
skipOllama: true,
|
||||
skipModel: true,
|
||||
skipProject: true,
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.redisOk).toBe(true)
|
||||
expect(result.ollamaOk).toBe(true)
|
||||
expect(result.modelOk).toBe(true)
|
||||
expect(result.projectOk).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
111
packages/ipuaro/tests/unit/cli/commands/tools-setup.test.ts
Normal file
111
packages/ipuaro/tests/unit/cli/commands/tools-setup.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { registerAllTools } from "../../../../src/cli/commands/tools-setup.js"
|
||||
import { ToolRegistry } from "../../../../src/infrastructure/tools/registry.js"
|
||||
|
||||
describe("registerAllTools", () => {
|
||||
it("should register all 18 tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.size).toBe(18)
|
||||
})
|
||||
|
||||
it("should register all read tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("get_lines")).toBe(true)
|
||||
expect(registry.has("get_function")).toBe(true)
|
||||
expect(registry.has("get_class")).toBe(true)
|
||||
expect(registry.has("get_structure")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register all edit tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("edit_lines")).toBe(true)
|
||||
expect(registry.has("create_file")).toBe(true)
|
||||
expect(registry.has("delete_file")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register all search tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("find_references")).toBe(true)
|
||||
expect(registry.has("find_definition")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register all analysis tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("get_dependencies")).toBe(true)
|
||||
expect(registry.has("get_dependents")).toBe(true)
|
||||
expect(registry.has("get_complexity")).toBe(true)
|
||||
expect(registry.has("get_todos")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register all git tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("git_status")).toBe(true)
|
||||
expect(registry.has("git_diff")).toBe(true)
|
||||
expect(registry.has("git_commit")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register all run tools", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
expect(registry.has("run_command")).toBe(true)
|
||||
expect(registry.has("run_tests")).toBe(true)
|
||||
})
|
||||
|
||||
it("should register tools with correct categories", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
const readTools = registry.getByCategory("read")
|
||||
const editTools = registry.getByCategory("edit")
|
||||
const searchTools = registry.getByCategory("search")
|
||||
const analysisTools = registry.getByCategory("analysis")
|
||||
const gitTools = registry.getByCategory("git")
|
||||
const runTools = registry.getByCategory("run")
|
||||
|
||||
expect(readTools.length).toBe(4)
|
||||
expect(editTools.length).toBe(3)
|
||||
expect(searchTools.length).toBe(2)
|
||||
expect(analysisTools.length).toBe(4)
|
||||
expect(gitTools.length).toBe(3)
|
||||
expect(runTools.length).toBe(2)
|
||||
})
|
||||
|
||||
it("should register tools with requiresConfirmation flag", () => {
|
||||
const registry = new ToolRegistry()
|
||||
|
||||
registerAllTools(registry)
|
||||
|
||||
const confirmationTools = registry.getConfirmationTools()
|
||||
const safeTools = registry.getSafeTools()
|
||||
|
||||
expect(confirmationTools.length).toBeGreaterThan(0)
|
||||
expect(safeTools.length).toBeGreaterThan(0)
|
||||
|
||||
const confirmNames = confirmationTools.map((t) => t.name)
|
||||
expect(confirmNames).toContain("edit_lines")
|
||||
expect(confirmNames).toContain("create_file")
|
||||
expect(confirmNames).toContain("delete_file")
|
||||
expect(confirmNames).toContain("git_commit")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user