|
| 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 | + |
| 113 | + it('reports an unreadable response as an error, not as absence', async () => { |
| 114 | + /** |
| 115 | + * Something answered 2xx but did not return a tag listing. Unlike a refused |
| 116 | + * connection that is a real fault, and must not be filed under "no Ollama here". |
| 117 | + */ |
| 118 | + mockFetch.mockResolvedValue({ |
| 119 | + ok: true, |
| 120 | + json: async () => { |
| 121 | + throw new SyntaxError('Unexpected token < in JSON at position 0') |
| 122 | + }, |
| 123 | + }) |
| 124 | + |
| 125 | + const response = await GET(request()) |
| 126 | + |
| 127 | + expect(response.status).toBe(200) |
| 128 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 129 | + expect(ollamaLogger.error).toHaveBeenCalledWith( |
| 130 | + 'Ollama returned a response this route cannot read', |
| 131 | + expect.objectContaining({ host: expect.any(String) }) |
| 132 | + ) |
| 133 | + }) |
| 134 | + |
| 135 | + it('reports a non-2xx response as unavailable', async () => { |
| 136 | + mockFetch.mockResolvedValue({ ok: false, status: 503, statusText: 'Service Unavailable' }) |
| 137 | + |
| 138 | + const response = await GET(request()) |
| 139 | + |
| 140 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 141 | + expect(ollamaLogger.warn).toHaveBeenCalled() |
| 142 | + expect(ollamaLogger.error).not.toHaveBeenCalled() |
| 143 | + }) |
| 144 | + |
| 145 | + it('reports a wrongly-shaped tag listing as an error', async () => { |
| 146 | + /** Reachable and 2xx, but the entries are not Ollama models. */ |
| 147 | + mockFetch.mockResolvedValue({ ok: true, json: async () => ({ models: [{ noName: true }] }) }) |
| 148 | + |
| 149 | + const response = await GET(request()) |
| 150 | + |
| 151 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 152 | + expect(ollamaLogger.error).toHaveBeenCalled() |
| 153 | + }) |
| 154 | + |
| 155 | + it('accepts a listing with no models as simply empty', async () => { |
| 156 | + /** The schema defaults `models` to [], so an empty answer is not a fault. */ |
| 157 | + mockFetch.mockResolvedValue({ ok: true, json: async () => ({}) }) |
| 158 | + |
| 159 | + const response = await GET(request()) |
| 160 | + |
| 161 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 162 | + expect(ollamaLogger.error).not.toHaveBeenCalled() |
| 163 | + }) |
| 164 | +}) |
0 commit comments