import { BackendUnavailableError, emptyTokenUsage, emptyBackendResult, DEFAULT_BACKEND_CONFIG } from "../backends/types.js"; import { OllamaLocalBackend } from "../backends/ollama-local.js"; import { OllamaCloudBackend } from "../backends/ollama-cloud.js"; import { OpencodeBackend } from "../backends/opencode.js"; describe("BackendUnavailableError", () => { it("includes backend name in message", () => { const err = new BackendUnavailableError("ollama-local"); expect(err.message).toContain("ollama-local"); expect(err.backendName).toBe("ollama-local"); }); it("includes agent name when provided", () => { const err = new BackendUnavailableError("opencode", "executor"); expect(err.agentName).toBe("executor"); expect(err.message).toContain("executor"); }); }); describe("emptyTokenUsage", () => { it("returns zeroed usage", () => { const usage = emptyTokenUsage(); expect(usage.input_tokens).toBe(0); expect(usage.output_tokens).toBe(0); expect(usage.total_tokens).toBe(0); expect(usage.estimated_cost_usd).toBe(0); }); }); describe("emptyBackendResult", () => { it("returns failed result with no artifacts", () => { const result = emptyBackendResult("something failed"); expect(result.success).toBe(false); expect(result.error).toBe("something failed"); expect(result.artifacts).toEqual([]); expect(result.decisions).toEqual([]); expect(result.escalations).toEqual([]); }); it("returns result without error when no message provided", () => { const result = emptyBackendResult(); expect(result.success).toBe(false); expect(result.error).toBeUndefined(); }); }); describe("DEFAULT_BACKEND_CONFIG", () => { it("has auto provider by default", () => { expect(DEFAULT_BACKEND_CONFIG.provider).toBe("auto"); }); it("has opencode agent backend enabled", () => { expect(DEFAULT_BACKEND_CONFIG.agent_backends.opencode?.enabled).toBe(true); }); it("has ollama-local and ollama-cloud llm backends", () => { expect(DEFAULT_BACKEND_CONFIG.llm_backends["ollama-local"]).toBeDefined(); expect(DEFAULT_BACKEND_CONFIG.llm_backends["ollama-cloud"]).toBeDefined(); }); }); describe("OllamaLocalBackend", () => { it("has correct name and type", () => { const backend = new OllamaLocalBackend(); expect(backend.name).toBe("ollama-local"); expect(backend.type).toBe("llm"); }); it("returns false when local Ollama is not available", async () => { const backend = new OllamaLocalBackend({ base_url: "http://localhost:1", model_profile: "balanced", }); const available = await backend.isAvailable(); expect(available).toBe(false); }); it("uses default config when none provided", () => { const backend = new OllamaLocalBackend(); expect(backend).toBeDefined(); }); }); describe("OllamaCloudBackend", () => { it("has correct name and type", () => { const backend = new OllamaCloudBackend(); expect(backend.name).toBe("ollama-cloud"); expect(backend.type).toBe("llm"); }); it("returns false when no base_url configured", async () => { const backend = new OllamaCloudBackend({ base_url: "", api_key_env: "NONEXISTENT_KEY", model_profile: "quality", timeout_ms: 5000, }); const available = await backend.isAvailable(); expect(available).toBe(false); }); it("returns false when no API key available", async () => { const backend = new OllamaCloudBackend({ base_url: "https://example.com", api_key_env: "NONEXISTENT_CI_KEY_12345", model_profile: "quality", timeout_ms: 5000, }); const available = await backend.isAvailable(); expect(available).toBe(false); }); }); describe("OpencodeBackend", () => { it("has correct name and type", () => { const backend = new OpencodeBackend(); expect(backend.name).toBe("opencode"); expect(backend.type).toBe("agent"); }); it("returns false when opencode is not installed", async () => { const backend = new OpencodeBackend({ enabled: true, executable: "nonexistent-opencode-binary-xyz", }); const available = await backend.isAvailable(); expect(available).toBe(false); }); });