|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest, resetEnvMock, setEnv } from '@sim/testing' |
| 5 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockFetch, mockFilterBlacklistedModels, mockIsProviderBlacklisted } = vi.hoisted(() => ({ |
| 8 | + mockFetch: vi.fn(), |
| 9 | + mockFilterBlacklistedModels: vi.fn((models: string[]) => models), |
| 10 | + mockIsProviderBlacklisted: vi.fn(() => false), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('@/providers/utils', () => ({ |
| 14 | + filterBlacklistedModels: mockFilterBlacklistedModels, |
| 15 | + isProviderBlacklisted: mockIsProviderBlacklisted, |
| 16 | +})) |
| 17 | + |
| 18 | +import { GET } from '@/app/api/providers/vllm/models/route' |
| 19 | + |
| 20 | +const request = () => createMockRequest('GET') |
| 21 | + |
| 22 | +describe('vLLM models route', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks() |
| 25 | + mockFilterBlacklistedModels.mockImplementation((models: string[]) => models) |
| 26 | + mockIsProviderBlacklisted.mockReturnValue(false) |
| 27 | + mockFetch.mockResolvedValue({ |
| 28 | + ok: true, |
| 29 | + json: async () => ({ data: [{ id: 'local-model' }] }), |
| 30 | + }) |
| 31 | + vi.stubGlobal('fetch', mockFetch) |
| 32 | + setEnv({ VLLM_BASE_URL: 'http://localhost:8000', VLLM_API_KEY: undefined }) |
| 33 | + }) |
| 34 | + |
| 35 | + afterAll(() => { |
| 36 | + vi.unstubAllGlobals() |
| 37 | + resetEnvMock() |
| 38 | + }) |
| 39 | + |
| 40 | + it('discovers and prefixes models from a server-root URL', async () => { |
| 41 | + const response = await GET(request()) |
| 42 | + |
| 43 | + await expect(response.json()).resolves.toEqual({ models: ['vllm/local-model'] }) |
| 44 | + expect(mockFetch).toHaveBeenCalledWith( |
| 45 | + 'http://localhost:8000/v1/models', |
| 46 | + expect.objectContaining({ headers: { 'Content-Type': 'application/json' } }) |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it('uses an existing /v1 prefix once and forwards bearer authentication', async () => { |
| 51 | + setEnv({ VLLM_BASE_URL: 'http://localhost:1234/v1', VLLM_API_KEY: 'lm-token' }) |
| 52 | + |
| 53 | + await GET(request()) |
| 54 | + |
| 55 | + expect(mockFetch).toHaveBeenCalledWith( |
| 56 | + 'http://localhost:1234/v1/models', |
| 57 | + expect.objectContaining({ |
| 58 | + headers: { |
| 59 | + Authorization: 'Bearer lm-token', |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + }, |
| 62 | + }) |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + it('returns an empty model list when the configured base URL is unsupported', async () => { |
| 67 | + setEnv({ VLLM_BASE_URL: 'http://localhost:1234?token=value' }) |
| 68 | + |
| 69 | + const response = await GET(request()) |
| 70 | + |
| 71 | + await expect(response.json()).resolves.toEqual({ models: [] }) |
| 72 | + expect(mockFetch).not.toHaveBeenCalled() |
| 73 | + }) |
| 74 | +}) |
0 commit comments