learning_ai_common_plat/packages/ollama-client/src/health.test.ts
2026-03-29 12:43:01 -07:00

54 lines
1.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { checkHealth, checkHealthDetailed } from './health.js';
const BASE_URL = 'http://localhost:11434';
describe('checkHealth', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('returns true when server is healthy', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({ ok: true });
expect(await checkHealth(BASE_URL)).toBe(true);
});
it('returns false when server returns non-ok', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({ ok: false });
expect(await checkHealth(BASE_URL)).toBe(false);
});
it('returns false when fetch throws', async () => {
globalThis.fetch = vi.fn().mockRejectedValue(new Error('ECONNREFUSED'));
expect(await checkHealth(BASE_URL)).toBe(false);
});
});
describe('checkHealthDetailed', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('returns online + version when server is healthy', async () => {
globalThis.fetch = vi.fn().mockImplementation((url: string) => {
if (url.includes('/api/tags')) return Promise.resolve({ ok: true });
if (url.includes('/api/version'))
return Promise.resolve({
ok: true,
json: () => Promise.resolve({ version: '0.5.4' }),
});
return Promise.reject(new Error('unexpected'));
});
const result = await checkHealthDetailed(BASE_URL);
expect(result).toEqual({ online: true, version: '0.5.4', url: BASE_URL });
});
it('returns offline when server is unreachable', async () => {
globalThis.fetch = vi.fn().mockRejectedValue(new Error('ECONNREFUSED'));
const result = await checkHealthDetailed(BASE_URL);
expect(result).toEqual({ online: false, version: null, url: BASE_URL });
});
});