| 1 | import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | |
| 3 | import { ModelService } from "./ModelService.js"; |
| 4 | |
| 5 | describe("ModelService - Direct Testing", () => { |
| 6 | let modelService: ModelService; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | modelService = new ModelService(); |
| 10 | }); |
| 11 | |
| 12 | it("should throw 'ModelService not initialized' when switching without initialization", async () => { |
| 13 | // The service is created but not initialized (no assistant or authConfig) |
| 14 | expect(modelService.getCurrentModelIndex()).toBe(-1); |
| 15 | |
| 16 | // Try to switch model without initialization |
| 17 | try { |
| 18 | await modelService.switchModel(0); |
| 19 | // Should not reach here |
| 20 | expect(true).toBe(false); |
| 21 | } catch (error: any) { |
| 22 | // Should throw the specific error |
| 23 | expect(error.message).toBe( |
| 24 | "ModelService not initialized - assistant data missing", |
| 25 | ); |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | it("should handle model switching with null authConfig for non-proxy models", async () => { |
| 30 | // Initialize the service properly |
| 31 | const mockAssistant = { |
| 32 | name: "Test Assistant", |
| 33 | version: "1.0.0", |
| 34 | models: [ |
| 35 | { |
| 36 | name: "claude-3.5-sonnet", |
| 37 | provider: "anthropic", |
| 38 | model: "claude-3.5-sonnet", |
| 39 | apiKey: "test-anthropic-key", |
| 40 | roles: ["chat"], |
| 41 | }, |
| 42 | { |
| 43 | name: "GPT-4", |
| 44 | provider: "openai", |
| 45 | model: "gpt-4", |
| 46 | apiKey: "test-openai-key", |
| 47 | roles: ["chat"], |
| 48 | }, |
| 49 | ], |
| 50 | }; |
| 51 | |
| 52 | // Initialize with null authConfig - valid for non-proxy models |
| 53 | const initialState = await modelService.initialize( |
| 54 | mockAssistant as any, |
| 55 | null as any, |
| 56 | ); |
| 57 | |
| 58 | // Verify initialization succeeded with null authConfig |
| 59 | expect(initialState.assistant).toBe(mockAssistant); |
| 60 | expect(initialState.authConfig).toBe(null); |
| 61 | expect(initialState.model).toBeDefined(); |
| 62 | expect((initialState.model as any).name).toBe("claude-3.5-sonnet"); |
| 63 | |
| 64 | // Should be able to switch models even with null authConfig |
| 65 | const switchedState = await modelService.switchModel(1); |
| 66 | expect(switchedState.model).toBeDefined(); |
| 67 | expect((switchedState.model as any).name).toBe("GPT-4"); |
| 68 | expect(switchedState.assistant).toBe(mockAssistant); |
| 69 | expect(switchedState.authConfig).toBe(null); |
| 70 | |
| 71 | // Verify the model info |
| 72 | const modelInfo = modelService.getModelInfo(); |
| 73 | expect(modelInfo?.name).toBe("GPT-4"); |
| 74 | expect(modelInfo?.provider).toBe("openai"); |
| 75 | }); |
| 76 | |
| 77 | it("should successfully switch model after proper initialization", async () => { |
| 78 | // Initialize the service properly |
| 79 | const mockAssistant = { |
| 80 | name: "Test Assistant", |
| 81 | version: "1.0.0", |
| 82 | models: [ |
| 83 | { |
| 84 | name: "GPT-4", |
| 85 | provider: "openai", |
| 86 | model: "gpt-4", |
| 87 | apiKey: "test-key-1", |
| 88 | roles: ["chat"], |
| 89 | }, |
| 90 | { |
| 91 | name: "GPT-3.5", |
| 92 | provider: "openai", |
| 93 | model: "gpt-3.5-turbo", |
| 94 | apiKey: "test-key-2", |
| 95 | roles: ["chat"], |
| 96 | }, |
| 97 | ], |
| 98 | }; |
| 99 | |
| 100 | const mockAuthConfig = { |
| 101 | token: "test-token", |
| 102 | }; |
| 103 | |
| 104 | // Initialize the service (note: initialize calls doInitialize and sets the state) |
| 105 | const initialState = await modelService.initialize( |
| 106 | mockAssistant as any, |
| 107 | mockAuthConfig as any, |
| 108 | ); |
| 109 | |
| 110 | // Verify we have models and the first one is selected |
| 111 | const availableModels = modelService.getAvailableChatModels(); |
| 112 | expect(availableModels.length).toBe(2); |
| 113 | expect(initialState.model).toBeDefined(); |
| 114 | expect((initialState.model as any).name).toBe("GPT-4"); |
| 115 | |
| 116 | // Now switch should work |
| 117 | await modelService.switchModel(1); |
| 118 | expect(modelService.getCurrentModelIndex()).toBe(1); |
| 119 | |
| 120 | const modelInfo = modelService.getModelInfo(); |
| 121 | expect(modelInfo?.name).toBe("GPT-3.5"); |
| 122 | }); |
| 123 | |
| 124 | it("should handle switching to invalid model index", async () => { |
| 125 | // Initialize properly first |
| 126 | const mockAssistant = { |
| 127 | name: "Test Assistant", |
| 128 | version: "1.0.0", |
| 129 | models: [ |
| 130 | { |
| 131 | name: "GPT-4", |
| 132 | provider: "openai", |
| 133 | model: "gpt-4", |
| 134 | apiKey: "test-key", |
| 135 | }, |
| 136 | ], |
| 137 | }; |
| 138 | |
| 139 | const mockAuthConfig = { |
| 140 | token: "test-token", |
| 141 | }; |
| 142 | |
| 143 | await modelService.initialize(mockAssistant as any, mockAuthConfig as any); |
| 144 | |
| 145 | // Try to switch to invalid index |
| 146 | try { |
| 147 | await modelService.switchModel(5); // Only have 1 model at index 0 |
| 148 | expect(true).toBe(false); |
| 149 | } catch (error: any) { |
| 150 | expect(error.message).toContain("Invalid model index"); |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | it("should successfully switch model when service is properly initialized via container", async () => { |
| 155 | // This test simulates the fix where we ensure service is initialized before switching |
| 156 | const mockAssistant = { |
| 157 | name: "Test Assistant", |
| 158 | version: "1.0.0", |
| 159 | models: [ |
| 160 | { |
| 161 | name: "GPT-4", |
| 162 | provider: "openai", |
| 163 | model: "gpt-4", |
| 164 | apiKey: "test-key-1", |
| 165 | roles: ["chat"], |
| 166 | }, |
| 167 | { |
| 168 | name: "GPT-3.5", |
| 169 | provider: "openai", |
| 170 | model: "gpt-3.5-turbo", |
| 171 | apiKey: "test-key-2", |
| 172 | roles: ["chat"], |
| 173 | }, |
| 174 | ], |
| 175 | }; |
| 176 | |
| 177 | const mockAuthConfig = { |
| 178 | token: "test-token", |
| 179 | }; |
| 180 | |
| 181 | // Initialize the service properly through initialize method (which sets isInitialized) |
| 182 | await modelService.initialize(mockAssistant as any, mockAuthConfig as any); |
| 183 | |
| 184 | // Verify initialization |
| 185 | expect(modelService.isReady()).toBe(true); |
| 186 | expect(modelService.getCurrentModelIndex()).toBe(0); |
| 187 | |
| 188 | // Now switch should work and return success |
| 189 | const newState = await modelService.switchModel(1); |
| 190 | |
| 191 | // Verify switch was successful |
| 192 | expect(modelService.getCurrentModelIndex()).toBe(1); |
| 193 | expect(newState.model).toBeDefined(); |
| 194 | expect((newState.model as any).name).toBe("GPT-3.5"); |
| 195 | |
| 196 | // Get model info to verify success message data |
| 197 | const modelInfo = modelService.getModelInfo(); |
| 198 | expect(modelInfo).toEqual({ |
| 199 | provider: "openai", |
| 200 | name: "GPT-3.5", |
| 201 | }); |
| 202 | |
| 203 | // This is what the success message would use |
| 204 | const successMessage = `Switched to model: ${modelInfo?.provider}/${modelInfo?.name}`; |
| 205 | expect(successMessage).toBe("Switched to model: openai/GPT-3.5"); |
| 206 | }); |
| 207 | }); |