|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { OllamaProvider, OpenRouterProvider, TogetherAIProvider } from '@/config/modelProviders'; |
| 4 | +import { getServerConfig } from '@/config/server'; |
| 5 | +import { GlobalServerConfig } from '@/types/settings'; |
| 6 | + |
| 7 | +import { GET } from './route'; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + vi.resetAllMocks(); |
| 11 | +}); |
| 12 | + |
| 13 | +describe('GET /api/config', () => { |
| 14 | + describe('Model Provider env', () => { |
| 15 | + describe('OPENAI_MODEL_LIST', () => { |
| 16 | + it('custom deletion, addition, and renaming of models', async () => { |
| 17 | + process.env.OPENAI_MODEL_LIST = |
| 18 | + '-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k'; |
| 19 | + |
| 20 | + const response = await GET(); |
| 21 | + |
| 22 | + // Assert |
| 23 | + expect(response).toBeInstanceOf(Response); |
| 24 | + expect(response.status).toBe(200); |
| 25 | + |
| 26 | + const jsonResponse: GlobalServerConfig = await response.json(); |
| 27 | + |
| 28 | + const result = jsonResponse.languageModel?.openai?.serverModelCards; |
| 29 | + |
| 30 | + expect(result).toMatchSnapshot(); |
| 31 | + process.env.OPENAI_MODEL_LIST = ''; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should work correct with gpt-4', async () => { |
| 35 | + process.env.OPENAI_MODEL_LIST = |
| 36 | + '-all,+gpt-3.5-turbo-1106,+gpt-3.5-turbo,+gpt-3.5-turbo-16k,+gpt-4,+gpt-4-32k,+gpt-4-1106-preview,+gpt-4-vision-preview'; |
| 37 | + |
| 38 | + const response = await GET(); |
| 39 | + const jsonResponse: GlobalServerConfig = await response.json(); |
| 40 | + |
| 41 | + const result = jsonResponse.languageModel?.openai?.serverModelCards; |
| 42 | + |
| 43 | + expect(result).toMatchSnapshot(); |
| 44 | + |
| 45 | + process.env.OPENAI_MODEL_LIST = ''; |
| 46 | + }); |
| 47 | + |
| 48 | + it('duplicate naming model', async () => { |
| 49 | + process.env.OPENAI_MODEL_LIST = |
| 50 | + 'gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k'; |
| 51 | + |
| 52 | + const response = await GET(); |
| 53 | + const jsonResponse: GlobalServerConfig = await response.json(); |
| 54 | + |
| 55 | + const result = jsonResponse.languageModel?.openai?.serverModelCards; |
| 56 | + |
| 57 | + expect(result?.find((s) => s.id === 'gpt-4-0125-preview')?.displayName).toEqual( |
| 58 | + 'gpt-4-32k', |
| 59 | + ); |
| 60 | + |
| 61 | + process.env.OPENAI_MODEL_LIST = ''; |
| 62 | + }); |
| 63 | + |
| 64 | + it('should delete model', async () => { |
| 65 | + process.env.OPENAI_MODEL_LIST = '-gpt-4'; |
| 66 | + |
| 67 | + const res = await GET(); |
| 68 | + const data: GlobalServerConfig = await res.json(); |
| 69 | + |
| 70 | + const result = data.languageModel?.openai?.serverModelCards; |
| 71 | + |
| 72 | + expect(result?.find((r) => r.id === 'gpt-4')).toBeUndefined(); |
| 73 | + |
| 74 | + process.env.OPENAI_MODEL_LIST = ''; |
| 75 | + }); |
| 76 | + |
| 77 | + it('show the hidden model', async () => { |
| 78 | + process.env.OPENAI_MODEL_LIST = '+gpt-4-1106-preview'; |
| 79 | + |
| 80 | + const res = await GET(); |
| 81 | + const data: GlobalServerConfig = await res.json(); |
| 82 | + |
| 83 | + const result = data.languageModel?.openai?.serverModelCards; |
| 84 | + |
| 85 | + expect(result?.find((o) => o.id === 'gpt-4-1106-preview')).toEqual({ |
| 86 | + displayName: 'GPT-4 Turbo Preview (1106)', |
| 87 | + functionCall: true, |
| 88 | + enabled: true, |
| 89 | + id: 'gpt-4-1106-preview', |
| 90 | + tokens: 128000, |
| 91 | + }); |
| 92 | + |
| 93 | + process.env.OPENAI_MODEL_LIST = ''; |
| 94 | + }); |
| 95 | + |
| 96 | + it('only add the model', async () => { |
| 97 | + process.env.OPENAI_MODEL_LIST = 'model1,model2,model3,model4'; |
| 98 | + |
| 99 | + const res = await GET(); |
| 100 | + const data: GlobalServerConfig = await res.json(); |
| 101 | + |
| 102 | + const result = data.languageModel?.openai?.serverModelCards; |
| 103 | + |
| 104 | + expect(result).toContainEqual({ |
| 105 | + displayName: 'model1', |
| 106 | + functionCall: true, |
| 107 | + id: 'model1', |
| 108 | + enabled: true, |
| 109 | + vision: true, |
| 110 | + }); |
| 111 | + expect(result).toContainEqual({ |
| 112 | + displayName: 'model2', |
| 113 | + functionCall: true, |
| 114 | + enabled: true, |
| 115 | + id: 'model2', |
| 116 | + vision: true, |
| 117 | + }); |
| 118 | + expect(result).toContainEqual({ |
| 119 | + displayName: 'model3', |
| 120 | + enabled: true, |
| 121 | + functionCall: true, |
| 122 | + id: 'model3', |
| 123 | + vision: true, |
| 124 | + }); |
| 125 | + expect(result).toContainEqual({ |
| 126 | + displayName: 'model4', |
| 127 | + functionCall: true, |
| 128 | + enabled: true, |
| 129 | + id: 'model4', |
| 130 | + vision: true, |
| 131 | + }); |
| 132 | + |
| 133 | + process.env.OPENAI_MODEL_LIST = ''; |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('CUSTOM_MODELS', () => { |
| 138 | + it('custom deletion, addition, and renaming of models', async () => { |
| 139 | + process.env.CUSTOM_MODELS = |
| 140 | + '-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k'; |
| 141 | + |
| 142 | + const response = await GET(); |
| 143 | + |
| 144 | + // Assert |
| 145 | + expect(response).toBeInstanceOf(Response); |
| 146 | + expect(response.status).toBe(200); |
| 147 | + |
| 148 | + const jsonResponse: GlobalServerConfig = await response.json(); |
| 149 | + |
| 150 | + const result = jsonResponse.languageModel?.openai?.serverModelCards; |
| 151 | + |
| 152 | + expect(result).toMatchSnapshot(); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('OPENROUTER_MODEL_LIST', () => { |
| 157 | + it('custom deletion, addition, and renaming of models', async () => { |
| 158 | + process.env.OPENROUTER_MODEL_LIST = |
| 159 | + '-all,+google/gemma-7b-it,+mistralai/mistral-7b-instruct=Mistral-7B-Instruct'; |
| 160 | + |
| 161 | + const res = await GET(); |
| 162 | + const data: GlobalServerConfig = await res.json(); |
| 163 | + |
| 164 | + const result = data.languageModel?.openai?.serverModelCards; |
| 165 | + |
| 166 | + expect(result).toMatchSnapshot(); |
| 167 | + |
| 168 | + process.env.OPENROUTER_MODEL_LIST = ''; |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments