|
| 1 | +import { beforeEach, describe, expect, test } from '@jest/globals'; |
| 2 | +import { NextRequest } 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 { listAvailableExperimentModels } from '@/lib/ai-gateway/experiments/list-available-experiment-models'; |
| 8 | +import { addUserByokAvailability, getUserByokProviderIds } from '@/lib/ai-gateway/byok'; |
| 9 | +import { getAvailableModelsForOrganization } from '@/lib/organizations/organization-models'; |
| 10 | +import { GET } from './route'; |
| 11 | + |
| 12 | +jest.mock('@sentry/nextjs', () => ({ captureException: jest.fn() })); |
| 13 | +jest.mock('@/lib/user/server', () => ({ getUserFromAuth: jest.fn() })); |
| 14 | +jest.mock('@/lib/ai-gateway/providers/openrouter', () => ({ |
| 15 | + getEnhancedOpenRouterModels: jest.fn(), |
| 16 | +})); |
| 17 | +jest.mock('@/lib/ai-gateway/providers/direct-byok', () => ({ |
| 18 | + getDirectByokModelsForUser: jest.fn(), |
| 19 | +})); |
| 20 | +jest.mock('@/lib/ai-gateway/experiments/list-available-experiment-models', () => ({ |
| 21 | + listAvailableExperimentModels: jest.fn(), |
| 22 | +})); |
| 23 | +jest.mock('@/lib/ai-gateway/byok', () => ({ |
| 24 | + addUserByokAvailability: jest.fn(), |
| 25 | + getUserByokProviderIds: jest.fn(), |
| 26 | +})); |
| 27 | +jest.mock('@/lib/organizations/organization-models', () => ({ |
| 28 | + getAvailableModelsForOrganization: jest.fn(), |
| 29 | +})); |
| 30 | +jest.mock('@/lib/drizzle', () => ({ readDb: {} })); |
| 31 | + |
| 32 | +const mockedGetUserFromAuth = jest.mocked(getUserFromAuth); |
| 33 | +const mockedGetEnhancedOpenRouterModels = jest.mocked(getEnhancedOpenRouterModels); |
| 34 | +const mockedGetDirectByokModelsForUser = jest.mocked(getDirectByokModelsForUser); |
| 35 | +const mockedListAvailableExperimentModels = jest.mocked(listAvailableExperimentModels); |
| 36 | +const mockedAddUserByokAvailability = jest.mocked(addUserByokAvailability); |
| 37 | +const mockedGetUserByokProviderIds = jest.mocked(getUserByokProviderIds); |
| 38 | +const mockedGetAvailableModelsForOrganization = jest.mocked(getAvailableModelsForOrganization); |
| 39 | + |
| 40 | +function makeModel(id: string): OpenRouterModel { |
| 41 | + return { |
| 42 | + id, |
| 43 | + name: id, |
| 44 | + created: 0, |
| 45 | + description: '', |
| 46 | + architecture: { |
| 47 | + input_modalities: ['text'], |
| 48 | + output_modalities: ['text'], |
| 49 | + tokenizer: 'test', |
| 50 | + }, |
| 51 | + top_provider: { is_moderated: false }, |
| 52 | + pricing: { prompt: '0', completion: '0' }, |
| 53 | + context_length: 0, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function request() { |
| 58 | + return new NextRequest('http://localhost:3000/api/openrouter/models'); |
| 59 | +} |
| 60 | + |
| 61 | +describe('GET /api/openrouter/models', () => { |
| 62 | + beforeEach(() => { |
| 63 | + jest.resetAllMocks(); |
| 64 | + mockedGetUserFromAuth.mockResolvedValue({ |
| 65 | + user: null, |
| 66 | + organizationId: null, |
| 67 | + authFailedResponse: null, |
| 68 | + } as never); |
| 69 | + mockedGetEnhancedOpenRouterModels.mockResolvedValue({ data: [makeModel('public/model')] }); |
| 70 | + mockedGetDirectByokModelsForUser.mockResolvedValue([]); |
| 71 | + mockedListAvailableExperimentModels.mockResolvedValue([]); |
| 72 | + mockedGetUserByokProviderIds.mockResolvedValue([]); |
| 73 | + mockedGetAvailableModelsForOrganization.mockResolvedValue(null); |
| 74 | + }); |
| 75 | + |
| 76 | + test('leaves BYOK availability undefined for unauthenticated requests', async () => { |
| 77 | + const response = await GET(request()); |
| 78 | + |
| 79 | + expect(response.status).toBe(200); |
| 80 | + await expect(response.json()).resolves.toEqual({ data: [makeModel('public/model')] }); |
| 81 | + expect(mockedGetUserByokProviderIds).not.toHaveBeenCalled(); |
| 82 | + expect(mockedAddUserByokAvailability).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + test('returns BYOK availability for regular and direct authenticated models', async () => { |
| 86 | + const publicModel = makeModel('public/model'); |
| 87 | + const directModel = { ...makeModel('direct/model'), hasUserByokAvailable: true }; |
| 88 | + const experimentModel = makeModel('experiment/model'); |
| 89 | + mockedGetUserFromAuth.mockResolvedValue({ |
| 90 | + user: { id: 'user-id' }, |
| 91 | + organizationId: null, |
| 92 | + authFailedResponse: null, |
| 93 | + } as never); |
| 94 | + mockedGetDirectByokModelsForUser.mockResolvedValue([directModel] as never); |
| 95 | + mockedListAvailableExperimentModels.mockResolvedValue([experimentModel]); |
| 96 | + mockedGetUserByokProviderIds.mockResolvedValue(['anthropic']); |
| 97 | + mockedAddUserByokAvailability.mockResolvedValue([ |
| 98 | + { ...publicModel, hasUserByokAvailable: true }, |
| 99 | + ]); |
| 100 | + |
| 101 | + const response = await GET(request()); |
| 102 | + |
| 103 | + expect(response.status).toBe(200); |
| 104 | + await expect(response.json()).resolves.toEqual({ |
| 105 | + data: [{ ...publicModel, hasUserByokAvailable: true }, directModel, experimentModel], |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments