From 6828f310031426f0abc8cf1eec544467b5a5d1a8 Mon Sep 17 00:00:00 2001 From: Brandon Wolfgang Date: Wed, 21 Oct 2026 21:20:27 -0500 Subject: [PATCH] test: cover Ollama health check outcomes - Verify healthy responses return true - Verify non-OK HTTP responses return false - Verify network failures return false without propagating errors - Restore the global fetch implementation after each test --- src/ollama.test.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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; + } +});