diff --git a/src/ollama.test.ts b/src/ollama.test.ts index e0beba6..6a6d73e 100644 --- a/src/ollama.test.ts +++ b/src/ollama.test.ts @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import { it } from "node:test"; -import { generateResponse } from "./ollama.js"; +import { checkOllamaHealth, generateResponse } from "./ollama.js"; it("returns generated text from Ollama", async () => { const originalFetch = globalThis.fetch; @@ -79,3 +79,38 @@ it("propagates an error when an Ollama request times out", async () => { globalThis.fetch = originalFetch; } }); + +it("returns true when Ollama is healthy", async () => { + const originalFetch = globalThis.fetch; + globalThis.fetch = async () => new Response("{}", { status: 200 }); + + try { + assert.equal(await checkOllamaHealth(), true); + } finally { + globalThis.fetch = originalFetch; + } +}); + +it("returns false when Ollama returns a non-OK health response", async () => { + const originalFetch = globalThis.fetch; + globalThis.fetch = async () => new Response("Unavailable", { status: 503 }); + + try { + assert.equal(await checkOllamaHealth(), false); + } finally { + globalThis.fetch = originalFetch; + } +}); + +it("returns false when Ollama is unreachable during a health check", async () => { + const originalFetch = globalThis.fetch; + globalThis.fetch = async () => { + throw new TypeError("fetch failed"); + }; + + try { + assert.equal(await checkOllamaHealth(), false); + } finally { + globalThis.fetch = originalFetch; + } +});