|
| 1 | +import { beforeEach, describe, expect, test } from '@jest/globals'; |
| 2 | +import { NextRequest, NextResponse } from 'next/server'; |
| 3 | +import type { OpenRouterModel } from '@/lib/organizations/organization-types'; |
| 4 | +import { getEnhancedOpenRouterModels } from '@/lib/ai-gateway/providers/openrouter'; |
| 5 | +import { getUserFromAuth } from '@/lib/user/server'; |
| 6 | +import { getDirectByokModelsForUser } from '@/lib/ai-gateway/providers/direct-byok'; |
| 7 | +import { getAvailableModelsForOrganization } from '@/lib/organizations/organization-models'; |
| 8 | +import { listAvailableExperimentModels } from '@/lib/ai-gateway/experiments/list-available-experiment-models'; |
| 9 | +import { POST } from './route'; |
| 10 | + |
| 11 | +jest.mock('@sentry/nextjs', () => ({ captureException: jest.fn() })); |
| 12 | +jest.mock('@/lib/user/server', () => ({ getUserFromAuth: jest.fn() })); |
| 13 | +jest.mock('@/lib/ai-gateway/providers/openrouter', () => ({ |
| 14 | + getEnhancedOpenRouterModels: jest.fn(), |
| 15 | +})); |
| 16 | +jest.mock('@/lib/ai-gateway/providers/direct-byok', () => ({ |
| 17 | + getDirectByokModelsForUser: jest.fn(), |
| 18 | +})); |
| 19 | +jest.mock('@/lib/organizations/organization-models', () => ({ |
| 20 | + getAvailableModelsForOrganization: jest.fn(), |
| 21 | +})); |
| 22 | +jest.mock('@/lib/ai-gateway/experiments/list-available-experiment-models', () => ({ |
| 23 | + listAvailableExperimentModels: jest.fn(), |
| 24 | +})); |
| 25 | + |
| 26 | +const mockedGetUserFromAuth = jest.mocked(getUserFromAuth); |
| 27 | +const mockedGetEnhancedOpenRouterModels = jest.mocked(getEnhancedOpenRouterModels); |
| 28 | +const mockedGetDirectByokModelsForUser = jest.mocked(getDirectByokModelsForUser); |
| 29 | +const mockedGetAvailableModelsForOrganization = jest.mocked(getAvailableModelsForOrganization); |
| 30 | +const mockedListAvailableExperimentModels = jest.mocked(listAvailableExperimentModels); |
| 31 | + |
| 32 | +function makeModel(id: string): OpenRouterModel { |
| 33 | + return { |
| 34 | + id, |
| 35 | + name: id, |
| 36 | + created: 0, |
| 37 | + description: '', |
| 38 | + architecture: { |
| 39 | + input_modalities: ['text'], |
| 40 | + output_modalities: ['text'], |
| 41 | + tokenizer: 'test', |
| 42 | + }, |
| 43 | + top_provider: { is_moderated: false }, |
| 44 | + pricing: { prompt: '0', completion: '0' }, |
| 45 | + context_length: 0, |
| 46 | + supported_parameters: ['tools'], |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function request(modelId: string) { |
| 51 | + return new NextRequest('http://localhost:3000/api/openrouter/models/validate', { |
| 52 | + method: 'POST', |
| 53 | + body: JSON.stringify({ modelId }), |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +describe('POST /api/openrouter/models/validate', () => { |
| 58 | + beforeEach(() => { |
| 59 | + jest.resetAllMocks(); |
| 60 | + mockedGetUserFromAuth.mockResolvedValue({ |
| 61 | + user: null, |
| 62 | + organizationId: null, |
| 63 | + authFailedResponse: null, |
| 64 | + } as never); |
| 65 | + mockedGetEnhancedOpenRouterModels.mockResolvedValue({ data: [makeModel('available/model')] }); |
| 66 | + mockedGetDirectByokModelsForUser.mockResolvedValue([]); |
| 67 | + mockedGetAvailableModelsForOrganization.mockResolvedValue(null); |
| 68 | + mockedListAvailableExperimentModels.mockResolvedValue([]); |
| 69 | + }); |
| 70 | + |
| 71 | + test('confirms a Kilo-eligible catalog model', async () => { |
| 72 | + const response = await POST(request('available/model')); |
| 73 | + |
| 74 | + expect(response.status).toBe(200); |
| 75 | + await expect(response.json()).resolves.toEqual({ valid: true }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('does not expose details for an unavailable model', async () => { |
| 79 | + const response = await POST(request('missing/model')); |
| 80 | + |
| 81 | + expect(response.status).toBe(200); |
| 82 | + await expect(response.json()).resolves.toEqual({ valid: false, reason: 'unavailable' }); |
| 83 | + }); |
| 84 | + |
| 85 | + test('uses the public catalog after failed optional authentication', async () => { |
| 86 | + mockedGetUserFromAuth.mockResolvedValue({ |
| 87 | + user: null, |
| 88 | + organizationId: null, |
| 89 | + authFailedResponse: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }), |
| 90 | + } as never); |
| 91 | + |
| 92 | + const response = await POST(request('available/model')); |
| 93 | + |
| 94 | + expect(response.status).toBe(200); |
| 95 | + await expect(response.json()).resolves.toEqual({ valid: true }); |
| 96 | + }); |
| 97 | + |
| 98 | + test('returns a service failure when catalog construction fails', async () => { |
| 99 | + mockedGetEnhancedOpenRouterModels.mockRejectedValue(new Error('catalog unavailable')); |
| 100 | + |
| 101 | + const response = await POST(request('available/model')); |
| 102 | + |
| 103 | + expect(response.status).toBe(500); |
| 104 | + await expect(response.json()).resolves.toEqual({ |
| 105 | + error: 'Failed to validate model', |
| 106 | + message: 'Error from model catalog', |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + test('rejects an invalid body without reading a catalog', async () => { |
| 111 | + const response = await POST( |
| 112 | + new NextRequest('http://localhost:3000/api/openrouter/models/validate', { |
| 113 | + method: 'POST', |
| 114 | + body: JSON.stringify({ modelId: '' }), |
| 115 | + }) |
| 116 | + ); |
| 117 | + |
| 118 | + expect(response.status).toBe(400); |
| 119 | + expect(mockedGetEnhancedOpenRouterModels).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments