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
This commit is contained in:
Brandon Wolfgang 2026-10-21 21:39:21 -05:00
parent 0bc50f76a2
commit eb96f70736

View file

@ -1,7 +1,7 @@
import assert from "node:assert/strict"; import assert from "node:assert/strict";
import { it } from "node:test"; import { it } from "node:test";
import { generateResponse } from "./ollama.js"; import { checkOllamaHealth, generateResponse } from "./ollama.js";
it("returns generated text from Ollama", async () => { it("returns generated text from Ollama", async () => {
const originalFetch = globalThis.fetch; const originalFetch = globalThis.fetch;
@ -79,3 +79,38 @@ it("propagates an error when an Ollama request times out", async () => {
globalThis.fetch = originalFetch; 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;
}
});