multi-llm 1.0.0

Unified multi-provider LLM client with support for OpenAI, Anthropic, Ollama, and LMStudio
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Unit Tests for UnifiedLLMClient Configuration and Factory Methods
//
// UNIT UNDER TEST: UnifiedLLMClient factory methods (from_config, from_env, create)
//
// BUSINESS RESPONSIBILITY:
//   - Creates appropriate provider instances from configuration
//   - Validates configuration completeness and correctness
//   - Handles provider selection and initialization errors
//
// TEST COVERAGE:
//   - Configuration-based provider creation (from_config)
//   - Environment-based configuration (from_env)
//   - Error handling for invalid/missing configurations
//   - Provider name validation
//
// NOTE: The actual LlmProvider interface testing is in trait_compliance.rs
// This file only tests the factory/configuration aspects of UnifiedLLMClient

use crate::client::UnifiedLLMClient;
use crate::config::AnthropicConfig;
use crate::error::LlmError;
use crate::providers::AnthropicProvider;
use crate::tests::helpers::create_test_config;

// NOTE: Provider delegation tests moved to trait_compliance.rs
// This file now focuses only on configuration and factory method testing

#[cfg(test)]
mod factory_method_tests {
    use super::*;
    use crate::provider::LlmProvider;

    #[test]
    fn test_from_config_with_anthropic() {
        // Test verifies client creation from LLMConfig with Anthropic provider
        // validates configuration parsing and provider instantiation

        // Arrange
        let config = create_test_config("anthropic");

        // Act
        let result = UnifiedLLMClient::from_config(config);

        // Assert
        assert!(
            result.is_ok(),
            "Should create Anthropic client from valid config"
        );
        let client = result.unwrap();
        assert_eq!(
            client.provider_name(),
            "anthropic",
            "Should identify as Anthropic provider"
        );
    }

    #[test]
    fn test_from_config_with_openai() {
        // Test verifies client creation from LLMConfig with OpenAI provider
        // validates configuration parsing and provider instantiation

        // Arrange
        let config = create_test_config("openai");

        // Act
        let result = UnifiedLLMClient::from_config(config);

        // Assert
        assert!(
            result.is_ok(),
            "Should create OpenAI client from valid config"
        );
        let client = result.unwrap();
        assert_eq!(
            client.provider_name(),
            "openai",
            "Should identify as OpenAI provider"
        );
    }

    #[test]
    fn test_from_config_with_lmstudio() {
        // Test verifies client creation from LLMConfig with LM Studio provider
        // validates local configuration parsing and provider instantiation

        // Arrange
        let config = create_test_config("lmstudio");

        // Act
        let result = UnifiedLLMClient::from_config(config);

        // Assert
        assert!(
            result.is_ok(),
            "Should create LM Studio client from valid config"
        );
        let client = result.unwrap();
        assert_eq!(
            client.provider_name(),
            "lmstudio",
            "Should identify as LM Studio provider"
        );
    }

    #[test]
    fn test_from_config_unsupported_provider_error() {
        // Test verifies proper error handling for unsupported provider names
        // prevents runtime failures with clear error messages

        // Arrange - create a config but try to use it with unsupported provider name

        // Act - The create() method validates provider names
        let config = create_test_config("openai");
        let result = UnifiedLLMClient::create("unsupported-provider", "model".to_string(), config);

        // Assert
        assert!(result.is_err(), "Should fail for unsupported provider");
        match result {
            Err(LlmError::UnsupportedProvider { provider }) => {
                assert_eq!(provider, "unsupported-provider");
            }
            Err(e) => panic!("Expected UnsupportedProvider error, got: {:?}", e),
            Ok(_) => panic!("Expected error, got success"),
        }
    }

    #[test]
    fn test_provider_creation_with_invalid_config() {
        // Test verifies proper error handling when provider config is invalid
        // prevents runtime failures from missing required configuration

        // Arrange
        let invalid_config = AnthropicConfig {
            api_key: None, // Missing required API key
            ..AnthropicConfig::default()
        };

        // Act
        let result =
            AnthropicProvider::new(invalid_config, crate::config::DefaultLLMParams::default());

        // Assert
        assert!(
            result.is_err(),
            "Provider construction should fail with missing API key"
        );

        match result {
            Err(LlmError::ConfigurationError { .. }) => {
                // Expected error type for missing configuration
            }
            _ => panic!("Expected ConfigurationError for missing API key"),
        }
    }

    #[test]
    fn test_from_config_propagates_provider_construction_errors() {
        // Test verifies that UnifiedLLMClient::from_config properly propagates
        // errors from underlying provider construction failures

        // Note: Test helper creates valid configs, so we test unsupported provider path
        // The main error propagation happens through the ? operator in create methods

        // Arrange - Test the unsupported provider path which is directly testable
        let config = create_test_config("openai");
        let result = UnifiedLLMClient::create("invalid-provider", "model".to_string(), config);

        // Act & Assert
        assert!(result.is_err(), "Should fail for unsupported provider");
        match result {
            Err(LlmError::UnsupportedProvider { provider }) => {
                assert_eq!(provider, "invalid-provider");
            }
            _ => panic!("Expected UnsupportedProvider error"),
        }
    }

    #[test]
    fn test_create_method_with_valid_parameters() {
        // Test verifies the primary create() factory method works correctly
        // with all required parameters

        // Arrange
        let config = create_test_config("openai");
        let model = "gpt-4".to_string();

        // Act
        let result = UnifiedLLMClient::create("openai", model.clone(), config);

        // Assert
        assert!(result.is_ok(), "Should create client with valid parameters");
        let client = result.unwrap();
        assert_eq!(client.provider_name(), "openai");
        // Note: model is stored internally but not exposed via public API
    }

    #[test]
    fn test_from_config_with_ollama() {
        // Test verifies client creation from LLMConfig with Ollama provider
        // Ensures local Ollama configuration works correctly

        // Arrange
        let config = create_test_config("ollama");

        // Act
        let result = UnifiedLLMClient::from_config(config);

        // Assert
        assert!(
            result.is_ok(),
            "Should create Ollama client from valid config"
        );
        let client = result.unwrap();
        assert_eq!(client.provider_name(), "ollama");
    }

    #[test]
    #[serial_test::serial]
    fn test_from_env_creates_client_from_environment_variables() {
        // Test verifies client creation directly from environment variables
        // Validates complete initialization flow from env vars to client instance

        // Arrange
        std::env::set_var("AI_PROVIDER", "anthropic");
        std::env::set_var("ANTHROPIC_API_KEY", "test-key-from-env");

        // Act
        let result = UnifiedLLMClient::from_env();

        // Assert
        assert!(
            result.is_ok(),
            "Should create client from environment variables"
        );
        let client = result.unwrap();
        assert_eq!(client.provider_name(), "anthropic");

        // Cleanup
        std::env::remove_var("AI_PROVIDER");
        std::env::remove_var("ANTHROPIC_API_KEY");
    }

    #[test]
    #[serial_test::serial]
    fn test_from_env_propagates_config_errors() {
        // Test verifies from_env properly propagates configuration errors
        // Ensures missing required variables result in appropriate errors

        // Arrange - Set provider but omit required API key
        std::env::set_var("AI_PROVIDER", "anthropic");
        std::env::remove_var("ANTHROPIC_API_KEY");

        // Act
        let result = UnifiedLLMClient::from_env();

        // Assert
        assert!(
            result.is_err(),
            "Should fail when required env vars are missing"
        );
        match result {
            Err(LlmError::ConfigurationError { .. }) => {
                // Expected error type
            }
            _ => panic!("Expected ConfigurationError for missing API key"),
        }

        // Cleanup
        std::env::remove_var("AI_PROVIDER");
    }
}

// Unit Tests for Provider Name Exposure
//
// UNIT UNDER TEST: UnifiedLLMClient::provider_name()
//
// BUSINESS RESPONSIBILITY:
//   - Exposes which LLM provider is being used for logging and monitoring
//   - Enables provider-specific behavior and diagnostics
//
// TEST COVERAGE:
//   - Provider name correctly identifies the underlying provider type
//   - All supported providers return correct identification strings

#[cfg(test)]
mod provider_name_tests {
    use super::*;
    use crate::provider::LlmProvider;

    #[test]
    fn test_anthropic_client_returns_correct_provider_name() {
        // Test verifies Anthropic client identifies itself correctly
        // Ensures monitoring and logging can distinguish provider types

        // Arrange
        let config = create_test_config("anthropic");
        let client = UnifiedLLMClient::from_config(config).unwrap();

        // Act
        let provider_name = client.provider_name();

        // Assert
        assert_eq!(provider_name, "anthropic");
    }

    #[test]
    fn test_openai_client_returns_correct_provider_name() {
        // Test verifies OpenAI client identifies itself correctly

        // Arrange
        let config = create_test_config("openai");
        let client = UnifiedLLMClient::from_config(config).unwrap();

        // Act
        let provider_name = client.provider_name();

        // Assert
        assert_eq!(provider_name, "openai");
    }

    #[test]
    fn test_lmstudio_client_returns_correct_provider_name() {
        // Test verifies LM Studio client identifies itself correctly

        // Arrange
        let config = create_test_config("lmstudio");
        let client = UnifiedLLMClient::from_config(config).unwrap();

        // Act
        let provider_name = client.provider_name();

        // Assert
        assert_eq!(provider_name, "lmstudio");
    }

    #[test]
    fn test_ollama_client_returns_correct_provider_name() {
        // Test verifies Ollama client identifies itself correctly

        // Arrange
        let config = create_test_config("ollama");
        let client = UnifiedLLMClient::from_config(config).unwrap();

        // Act
        let provider_name = client.provider_name();

        // Assert
        assert_eq!(provider_name, "ollama");
    }
}

// Unit Tests for Private Factory Helper Methods
//
// UNIT UNDER TEST: create_*_provider() private helper methods
//
// BUSINESS RESPONSIBILITY:
//   - Create properly configured provider instances from LLMConfig
//   - Validate configuration matches expected provider type
//   - Handle configuration errors gracefully
//
// TEST COVERAGE:
//   - Error handling when config type doesn't match provider
//   - Proper provider construction for each supported provider type

#[cfg(test)]
mod private_factory_tests {
    use super::*;

    #[test]
    fn test_create_anthropic_provider_with_wrong_config_type() {
        // Test verifies error handling when OpenAI config is passed to Anthropic factory
        // Ensures clear error messages for configuration mismatches

        // Arrange
        let config = create_test_config("openai"); // Wrong config type

        // Act
        let result = UnifiedLLMClient::create("anthropic", "claude-3".to_string(), config);

        // Assert
        assert!(result.is_err(), "Should fail with config type mismatch");
        match result {
            Err(LlmError::ConfigurationError { .. }) => {
                // Expected error
            }
            _ => panic!("Expected ConfigurationError"),
        }
    }

    #[test]
    fn test_create_openai_provider_with_wrong_config_type() {
        // Test verifies error handling when Anthropic config is passed to OpenAI factory
        // Ensures configuration validation catches type mismatches

        // Arrange
        let config = create_test_config("anthropic"); // Wrong config type

        // Act
        let result = UnifiedLLMClient::create("openai", "gpt-4".to_string(), config);

        // Assert
        assert!(result.is_err(), "Should fail with config type mismatch");
        match result {
            Err(LlmError::ConfigurationError { .. }) => {
                // Expected error
            }
            _ => panic!("Expected ConfigurationError"),
        }
    }

    #[test]
    fn test_create_lmstudio_provider_with_wrong_config_type() {
        // Test verifies error handling when wrong config is passed to LM Studio factory

        // Arrange
        let config = create_test_config("anthropic"); // Wrong config type

        // Act
        let result = UnifiedLLMClient::create("lmstudio", "local-model".to_string(), config);

        // Assert
        assert!(result.is_err(), "Should fail with config type mismatch");
    }

    #[test]
    fn test_create_ollama_provider_with_wrong_config_type() {
        // Test verifies error handling when wrong config is passed to Ollama factory

        // Arrange
        let config = create_test_config("openai"); // Wrong config type

        // Act
        let result = UnifiedLLMClient::create("ollama", "llama2".to_string(), config);

        // Assert
        assert!(result.is_err(), "Should fail with config type mismatch");
    }

    #[test]
    fn test_create_anthropic_provider_success() {
        // Test verifies successful Anthropic provider creation with correct config
        // Ensures happy path works for Anthropic

        // Arrange
        let config = create_test_config("anthropic");

        // Act
        let result = UnifiedLLMClient::create("anthropic", "claude-3".to_string(), config);

        // Assert
        assert!(
            result.is_ok(),
            "Should succeed with correct Anthropic config"
        );
    }

    #[test]
    fn test_create_openai_provider_success() {
        // Test verifies successful OpenAI provider creation with correct config
        // Ensures happy path works for OpenAI

        // Arrange
        let config = create_test_config("openai");

        // Act
        let result = UnifiedLLMClient::create("openai", "gpt-4".to_string(), config);

        // Assert
        assert!(result.is_ok(), "Should succeed with correct OpenAI config");
    }

    #[test]
    fn test_create_lmstudio_provider_success() {
        // Test verifies successful LM Studio provider creation with correct config
        // Ensures happy path works for LM Studio

        // Arrange
        let config = create_test_config("lmstudio");

        // Act
        let result = UnifiedLLMClient::create("lmstudio", "local-model".to_string(), config);

        // Assert
        assert!(
            result.is_ok(),
            "Should succeed with correct LM Studio config"
        );
    }

    #[test]
    fn test_create_ollama_provider_success() {
        // Test verifies successful Ollama provider creation with correct config
        // Ensures happy path works for Ollama

        // Arrange
        let config = create_test_config("ollama");

        // Act
        let result = UnifiedLLMClient::create("ollama", "llama2".to_string(), config);

        // Assert
        assert!(result.is_ok(), "Should succeed with correct Ollama config");
    }

    #[test]
    fn test_create_with_different_models() {
        // Test verifies client can be created with various model names
        // Ensures model parameter is properly stored

        // Arrange & Act
        let anthropic = UnifiedLLMClient::create(
            "anthropic",
            "claude-3-opus".to_string(),
            create_test_config("anthropic"),
        );
        let openai = UnifiedLLMClient::create(
            "openai",
            "gpt-4-turbo".to_string(),
            create_test_config("openai"),
        );

        // Assert
        assert!(anthropic.is_ok(), "Should create with Claude model");
        assert!(openai.is_ok(), "Should create with GPT-4 model");
    }
}