|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, resetEnvFlagsMock, setEnvFlags } from '@sim/testing' |
| 5 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { |
| 8 | + mockFilterBlacklistedModels, |
| 9 | + mockIsProviderBlacklisted, |
| 10 | + mockFetch, |
| 11 | + mockIsOllamaUrlConfigured, |
| 12 | + ollamaLogger, |
| 13 | +} = vi.hoisted(() => ({ |
| 14 | + mockFilterBlacklistedModels: vi.fn(), |
| 15 | + mockIsProviderBlacklisted: vi.fn(), |
| 16 | + mockFetch: vi.fn(), |
| 17 | + mockIsOllamaUrlConfigured: vi.fn(), |
| 18 | + ollamaLogger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@sim/logger', () => ({ |
| 22 | + createLogger: vi.fn(() => ollamaLogger), |
| 23 | + logger: ollamaLogger, |
| 24 | + runWithRequestContext: vi.fn(<T>(_ctx: unknown, fn: () => T): T => fn()), |
| 25 | + getRequestContext: vi.fn(() => undefined), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@/providers/utils', () => ({ |
| 29 | + filterBlacklistedModels: mockFilterBlacklistedModels, |
| 30 | + isProviderBlacklisted: mockIsProviderBlacklisted, |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@/lib/core/utils/urls', () => ({ |
| 34 | + getOllamaUrl: () => 'http://localhost:11434', |
| 35 | + isOllamaUrlConfigured: mockIsOllamaUrlConfigured, |
| 36 | +})) |
| 37 | + |
| 38 | +import { GET } from '@/app/api/providers/ollama/models/route' |
| 39 | + |
| 40 | +const request = () => createMockRequest('GET') |
| 41 | + |
| 42 | +describe('ollama models route', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + mockIsOllamaUrlConfigured.mockReturnValue(false) |
| 46 | + mockIsProviderBlacklisted.mockReturnValue(false) |
| 47 | + mockFilterBlacklistedModels.mockImplementation((models: string[]) => models) |
| 48 | + vi.stubGlobal('fetch', mockFetch) |
| 49 | + setEnvFlags({ isHosted: false }) |
| 50 | + }) |
| 51 | + |
| 52 | + afterAll(() => { |
| 53 | + vi.unstubAllGlobals() |
| 54 | + resetEnvFlagsMock() |
| 55 | + }) |
| 56 | + |
| 57 | + it('does not probe a loopback Ollama on the hosted platform', async () => { |
| 58 | + setEnvFlags({ isHosted: true }) |
| 59 | + |
| 60 | + const response = await GET(request()) |
| 61 | + |
| 62 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 63 | + expect(mockFetch).not.toHaveBeenCalled() |
| 64 | + }) |
| 65 | + |
| 66 | + it('still honours an explicit OLLAMA_URL on the hosted platform', async () => { |
| 67 | + setEnvFlags({ isHosted: true }) |
| 68 | + mockIsOllamaUrlConfigured.mockReturnValue(true) |
| 69 | + mockFetch.mockResolvedValue({ ok: true, json: async () => ({ models: [{ name: 'llama3' }] }) }) |
| 70 | + |
| 71 | + const response = await GET(request()) |
| 72 | + |
| 73 | + await expect(response.json()).resolves.toEqual({ models: ['llama3'] }) |
| 74 | + expect(mockFetch).toHaveBeenCalled() |
| 75 | + }) |
| 76 | + |
| 77 | + it('probes the default host when self-hosted, so no configuration is required', async () => { |
| 78 | + mockFetch.mockResolvedValue({ ok: true, json: async () => ({ models: [{ name: 'llama3' }] }) }) |
| 79 | + |
| 80 | + const response = await GET(request()) |
| 81 | + |
| 82 | + await expect(response.json()).resolves.toEqual({ models: ['llama3'] }) |
| 83 | + expect(mockFetch).toHaveBeenCalledWith(expect.stringContaining('/api/tags'), expect.anything()) |
| 84 | + }) |
| 85 | + |
| 86 | + it('reports an unreachable Ollama as an empty list rather than a failure', async () => { |
| 87 | + /** |
| 88 | + * A deployment that runs no Ollama refuses this connection on every poll. The |
| 89 | + * level is the point: an optional service being absent is not an error. |
| 90 | + */ |
| 91 | + mockFetch.mockRejectedValue(new Error('Unable to connect. Is the computer able to access it?')) |
| 92 | + |
| 93 | + const response = await GET(request()) |
| 94 | + |
| 95 | + expect(response.status).toBe(200) |
| 96 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 97 | + expect(ollamaLogger.error).not.toHaveBeenCalled() |
| 98 | + expect(ollamaLogger.info).toHaveBeenCalledWith( |
| 99 | + 'Ollama service is not reachable, returning empty models', |
| 100 | + expect.objectContaining({ host: expect.any(String) }) |
| 101 | + ) |
| 102 | + }) |
| 103 | + |
| 104 | + it('returns nothing when the provider is blacklisted', async () => { |
| 105 | + mockIsProviderBlacklisted.mockReturnValue(true) |
| 106 | + |
| 107 | + const response = await GET(request()) |
| 108 | + |
| 109 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 110 | + expect(mockFetch).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | +}) |
0 commit comments