siumai 0.10.3

A unified LLM interface library for Rust
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Network Error Handling Tests
//!
//! This module provides comprehensive tests for network error scenarios that
//! LLM clients might encounter in production environments. These tests ensure
//! robust error handling and proper recovery mechanisms.
//!
//! ## Test Categories
//!
//! 1. **Connection Failures**: DNS resolution failures, connection refused,
//!    network unreachable, etc.
//!
//! 2. **Timeout Scenarios**: Connection timeouts, read timeouts, request timeouts
//!
//! 3. **SSL/TLS Issues**: Certificate validation failures, protocol mismatches
//!
//! 4. **HTTP Protocol Errors**: Malformed responses, unexpected status codes,
//!    incomplete responses
//!
//! 5. **Intermittent Failures**: Network instability, packet loss simulation
//!
//! 6. **Resource Exhaustion**: Port exhaustion, memory pressure scenarios

use std::time::{Duration, Instant};

use reqwest::Client;
use serde_json::{Value, json};
use tokio::time::timeout;

mod mock_framework;
use mock_framework::{MockLlmServer, MockTestUtils};

/// Network error test configuration
#[derive(Debug, Clone)]
pub struct NetworkErrorTestConfig {
    /// Timeout for individual requests
    pub request_timeout: Duration,
    /// Number of retry attempts
    pub retry_attempts: usize,
    /// Delay between retries
    pub retry_delay: Duration,
    /// Whether to test with concurrent requests
    pub test_concurrency: bool,
    /// Number of concurrent requests for concurrency tests
    pub concurrent_requests: usize,
}

impl Default for NetworkErrorTestConfig {
    fn default() -> Self {
        Self {
            request_timeout: Duration::from_secs(5),
            retry_attempts: 3,
            retry_delay: Duration::from_millis(100),
            test_concurrency: false,
            concurrent_requests: 5,
        }
    }
}

/// Results from network error tests
#[derive(Debug)]
pub struct NetworkErrorTestResults {
    /// Test name
    pub test_name: String,
    /// Whether the test passed
    pub passed: bool,
    /// Error message if test failed
    pub error_message: Option<String>,
    /// Time taken for the test
    pub duration: Duration,
    /// Number of requests made
    pub requests_made: usize,
    /// Number of expected errors caught
    pub expected_errors_caught: usize,
    /// Any unexpected behaviors observed
    pub unexpected_behaviors: Vec<String>,
}

impl NetworkErrorTestResults {
    pub fn new(test_name: &str) -> Self {
        Self {
            test_name: test_name.to_string(),
            passed: false,
            error_message: None,
            duration: Duration::ZERO,
            requests_made: 0,
            expected_errors_caught: 0,
            unexpected_behaviors: Vec::new(),
        }
    }

    pub fn success(mut self) -> Self {
        self.passed = true;
        self
    }

    pub fn failure(mut self, message: &str) -> Self {
        self.passed = false;
        self.error_message = Some(message.to_string());
        self
    }

    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    pub fn with_requests(mut self, count: usize) -> Self {
        self.requests_made = count;
        self
    }

    pub fn with_expected_errors(mut self, count: usize) -> Self {
        self.expected_errors_caught = count;
        self
    }
}

/// Network error test runner
pub struct NetworkErrorTester {
    config: NetworkErrorTestConfig,
}

impl NetworkErrorTester {
    pub fn new(config: NetworkErrorTestConfig) -> Self {
        Self { config }
    }

    /// Test connection timeout scenarios
    pub async fn test_connection_timeout(&self) -> NetworkErrorTestResults {
        let mut results = NetworkErrorTestResults::new("connection_timeout");
        let start_time = Instant::now();

        println!("⏱️  Testing connection timeout scenarios...");

        // Test with a non-routable IP address (should timeout)
        let client = Client::builder()
            .timeout(Duration::from_millis(500))
            .connect_timeout(Duration::from_millis(200))
            .build()
            .unwrap();

        let mut timeout_errors = 0;
        let test_urls = [
            "http://10.255.255.1:80/test", // Non-routable IP
            "http://192.0.2.1:80/test",    // TEST-NET-1 (RFC 5737)
            "http://198.51.100.1:80/test", // TEST-NET-2 (RFC 5737)
        ];

        for (i, url) in test_urls.iter().enumerate() {
            println!("  🔗 Testing timeout with URL {}: {}", i + 1, url);

            let request_start = Instant::now();
            let result = timeout(
                self.config.request_timeout,
                client
                    .post(*url)
                    .header("authorization", "Bearer test-key")
                    .json(&json!({
                        "model": "gpt-3.5-turbo",
                        "messages": [{"role": "user", "content": "test"}]
                    }))
                    .send(),
            )
            .await;

            let request_duration = request_start.elapsed();
            results.requests_made += 1;

            match result {
                Err(_) => {
                    // Timeout occurred as expected
                    timeout_errors += 1;
                    println!(
                        "    ✅ Timeout occurred as expected after {:?}",
                        request_duration
                    );
                }
                Ok(Ok(response)) => {
                    results.unexpected_behaviors.push(format!(
                        "Unexpected successful response from {}: {}",
                        url,
                        response.status()
                    ));
                    println!("    ⚠️  Unexpected success from {}", url);
                }
                Ok(Err(e)) => {
                    // Network error occurred (also acceptable)
                    timeout_errors += 1;
                    println!("    ✅ Network error as expected: {}", e);
                }
            }
        }

        results.expected_errors_caught = timeout_errors;
        results.duration = start_time.elapsed();

        if timeout_errors >= 2 && results.unexpected_behaviors.is_empty() {
            results.success()
        } else {
            let error_msg = format!(
                "Expected at least 2 timeout errors, got {}. Unexpected behaviors: {:?}",
                timeout_errors, results.unexpected_behaviors
            );
            results.failure(&error_msg)
        }
    }

    /// Test DNS resolution failures
    pub async fn test_dns_resolution_failure(&self) -> NetworkErrorTestResults {
        let mut results = NetworkErrorTestResults::new("dns_resolution_failure");
        let start_time = Instant::now();

        println!("🌐 Testing DNS resolution failures...");

        let client = Client::builder()
            .timeout(self.config.request_timeout)
            .build()
            .unwrap();

        let invalid_domains = [
            "http://this-domain-definitely-does-not-exist-12345.com/api",
            "http://invalid-tld.invalidtld/api",
            "http://nonexistent-subdomain.example.com/api",
        ];

        let mut dns_errors = 0;

        for (i, url) in invalid_domains.iter().enumerate() {
            println!("  🔍 Testing DNS failure with domain {}: {}", i + 1, url);

            let result = client
                .post(*url)
                .header("authorization", "Bearer test-key")
                .json(&json!({
                    "model": "gpt-3.5-turbo",
                    "messages": [{"role": "user", "content": "test"}]
                }))
                .send()
                .await;

            results.requests_made += 1;

            match result {
                Err(e) => {
                    if e.is_connect()
                        || e.to_string().contains("dns")
                        || e.to_string().contains("resolve")
                        || e.to_string().contains("name")
                    {
                        dns_errors += 1;
                        println!("    ✅ DNS error as expected: {}", e);
                    } else {
                        results
                            .unexpected_behaviors
                            .push(format!("Unexpected error type from {}: {}", url, e));
                        println!("    ⚠️  Unexpected error type: {}", e);
                    }
                }
                Ok(response) => {
                    let status = response.status().as_u16();
                    // Accept 502 Bad Gateway as a valid DNS-related error
                    if status == 502 || status >= 500 {
                        dns_errors += 1;
                        println!("    ✅ DNS-related server error as expected: {}", status);
                    } else {
                        results.unexpected_behaviors.push(format!(
                            "Unexpected successful response from {}: {}",
                            url,
                            response.status()
                        ));
                        println!("    ⚠️  Unexpected success from {}", url);
                    }
                }
            }
        }

        results.expected_errors_caught = dns_errors;
        results.duration = start_time.elapsed();

        if dns_errors >= 2 && results.unexpected_behaviors.is_empty() {
            results.success()
        } else {
            let error_msg = format!(
                "Expected at least 2 DNS errors, got {}. Unexpected behaviors: {:?}",
                dns_errors, results.unexpected_behaviors
            );
            results.failure(&error_msg)
        }
    }

    /// Test malformed response handling
    pub async fn test_malformed_response_handling(&self) -> NetworkErrorTestResults {
        let mut results = NetworkErrorTestResults::new("malformed_response_handling");
        let start_time = Instant::now();

        println!("📦 Testing malformed response handling...");

        let server = MockLlmServer::new().await;

        // Configure server to return malformed JSON
        server.configure(MockTestUtils::malformed_json_config());
        server.setup_openai_chat().await;

        let client = Client::builder()
            .timeout(self.config.request_timeout)
            .build()
            .unwrap();

        let mut parse_errors = 0;
        let test_count = 3;

        for i in 0..test_count {
            println!("  📄 Testing malformed response {}", i + 1);

            let result = client
                .post(format!("{}/v1/chat/completions", server.base_url()))
                .header("authorization", "Bearer test-key")
                .json(&json!({
                    "model": "gpt-3.5-turbo",
                    "messages": [{"role": "user", "content": format!("test {}", i)}]
                }))
                .send()
                .await;

            results.requests_made += 1;

            match result {
                Ok(response) => {
                    // Try to parse the response as JSON
                    let json_result = response.json::<Value>().await;
                    match json_result {
                        Err(e) => {
                            parse_errors += 1;
                            println!("    ✅ JSON parse error as expected: {}", e);
                        }
                        Ok(value) => {
                            results
                                .unexpected_behaviors
                                .push(format!("Unexpected successful JSON parse: {:?}", value));
                            println!("    ⚠️  Unexpected successful JSON parse");
                        }
                    }
                }
                Err(e) => {
                    results
                        .unexpected_behaviors
                        .push(format!("Unexpected request error: {}", e));
                    println!("    ⚠️  Unexpected request error: {}", e);
                }
            }
        }

        results.expected_errors_caught = parse_errors;
        results.duration = start_time.elapsed();

        if parse_errors == test_count && results.unexpected_behaviors.is_empty() {
            results.success()
        } else {
            let error_msg = format!(
                "Expected {} parse errors, got {}. Unexpected behaviors: {:?}",
                test_count, parse_errors, results.unexpected_behaviors
            );
            results.failure(&error_msg)
        }
    }

    /// Test HTTP status code error handling
    pub async fn test_http_status_errors(&self) -> NetworkErrorTestResults {
        let mut results = NetworkErrorTestResults::new("http_status_errors");
        let start_time = Instant::now();

        println!("🚨 Testing HTTP status code error handling...");

        let server = MockLlmServer::new().await;
        server.setup_error_scenarios().await;

        let client = Client::builder()
            .timeout(self.config.request_timeout)
            .build()
            .unwrap();

        let error_scenarios = vec![("server_error", 500, "Server Error")];

        let mut status_errors = 0;

        for (scenario, expected_status, description) in error_scenarios {
            println!("  🔴 Testing {} scenario", description);

            let result = client
                .post(format!(
                    "{}/v1/chat/completions?trigger_error={}",
                    server.base_url(),
                    scenario
                ))
                .header("authorization", "Bearer test-key")
                .json(&json!({
                    "model": "gpt-3.5-turbo",
                    "messages": [{"role": "user", "content": "test"}]
                }))
                .send()
                .await;

            results.requests_made += 1;

            match result {
                Ok(response) => {
                    let status = response.status().as_u16();
                    if status == expected_status {
                        status_errors += 1;
                        println!("    ✅ Got expected status code: {}", status);
                    } else if status >= 400 {
                        status_errors += 1;
                        println!(
                            "    ✅ Got error status code: {} (expected {})",
                            status, expected_status
                        );
                    } else {
                        results.unexpected_behaviors.push(format!(
                            "Unexpected success status {} for scenario {}",
                            status, scenario
                        ));
                        println!("    ⚠️  Unexpected success status: {}", status);
                    }
                }
                Err(e) => {
                    results.unexpected_behaviors.push(format!(
                        "Unexpected request error for scenario {}: {}",
                        scenario, e
                    ));
                    println!("    ⚠️  Unexpected request error: {}", e);
                }
            }
        }

        results.expected_errors_caught = status_errors;
        results.duration = start_time.elapsed();

        if status_errors >= 1 && results.unexpected_behaviors.len() <= 1 {
            results.success()
        } else {
            let error_msg = format!(
                "Expected at least 1 status error, got {}. Unexpected behaviors: {:?}",
                status_errors, results.unexpected_behaviors
            );
            results.failure(&error_msg)
        }
    }

    /// Test network instability simulation
    pub async fn test_network_instability(&self) -> NetworkErrorTestResults {
        let mut results = NetworkErrorTestResults::new("network_instability");
        let start_time = Instant::now();

        println!("📡 Testing network instability scenarios...");

        let server = MockLlmServer::new().await;

        // Configure server with network failure simulation
        server.configure(MockTestUtils::network_failure_config());
        server.setup_openai_chat().await;

        let client = Client::builder()
            .timeout(self.config.request_timeout)
            .build()
            .unwrap();

        let mut network_errors = 0;
        let test_count = 3;

        for i in 0..test_count {
            println!("  📶 Testing network instability attempt {}", i + 1);

            let result = client
                .post(format!("{}/v1/chat/completions", server.base_url()))
                .header("authorization", "Bearer test-key")
                .json(&json!({
                    "model": "gpt-3.5-turbo",
                    "messages": [{"role": "user", "content": format!("instability test {}", i)}]
                }))
                .send()
                .await;

            results.requests_made += 1;

            match result {
                Ok(response) => {
                    let status = response.status().as_u16();
                    if status >= 500 {
                        network_errors += 1;
                        println!("    ✅ Got server error as expected: {}", status);
                    } else {
                        results
                            .unexpected_behaviors
                            .push(format!("Unexpected success status: {}", status));
                        println!("    ⚠️  Unexpected success status: {}", status);
                    }
                }
                Err(e) => {
                    network_errors += 1;
                    println!("    ✅ Network error as expected: {}", e);
                }
            }
        }

        results.expected_errors_caught = network_errors;
        results.duration = start_time.elapsed();

        if network_errors >= 2 && results.unexpected_behaviors.is_empty() {
            results.success()
        } else {
            let error_msg = format!(
                "Expected at least 2 network errors, got {}. Unexpected behaviors: {:?}",
                network_errors, results.unexpected_behaviors
            );
            results.failure(&error_msg)
        }
    }

    /// Run all network error tests
    pub async fn run_all_tests(&self) -> Vec<NetworkErrorTestResults> {
        println!("🚀 Running comprehensive network error tests...");

        let mut all_results = Vec::new();

        // Run individual tests
        all_results.push(self.test_connection_timeout().await);
        all_results.push(self.test_dns_resolution_failure().await);
        all_results.push(self.test_malformed_response_handling().await);
        all_results.push(self.test_http_status_errors().await);
        all_results.push(self.test_network_instability().await);

        // Print summary
        let passed = all_results.iter().filter(|r| r.passed).count();
        let total = all_results.len();

        println!("\n📊 Network Error Tests Summary:");
        println!("  ✅ Passed: {}/{}", passed, total);
        println!("  ❌ Failed: {}/{}", total - passed, total);

        for result in &all_results {
            let status = if result.passed { "" } else { "" };
            println!("  {} {}: {:?}", status, result.test_name, result.duration);
            if let Some(error) = &result.error_message {
                println!("    Error: {}", error);
            }
        }

        all_results
    }
}

// ============================================================================
// Actual Tests
// ============================================================================

#[tokio::test]
async fn test_connection_timeout() {
    let config = NetworkErrorTestConfig {
        request_timeout: Duration::from_secs(2),
        ..Default::default()
    };

    let tester = NetworkErrorTester::new(config);
    let result = tester.test_connection_timeout().await;

    assert!(
        result.passed,
        "Connection timeout test failed: {:?}",
        result.error_message
    );
    assert!(result.expected_errors_caught >= 2);
    assert!(result.requests_made >= 3);
}

#[tokio::test]
async fn test_dns_resolution_failure() {
    let config = NetworkErrorTestConfig {
        request_timeout: Duration::from_secs(3),
        ..Default::default()
    };

    let tester = NetworkErrorTester::new(config);
    let result = tester.test_dns_resolution_failure().await;

    assert!(
        result.passed,
        "DNS resolution failure test failed: {:?}",
        result.error_message
    );
    assert!(result.expected_errors_caught >= 2);
    assert!(result.requests_made >= 3);
}

#[tokio::test]
async fn test_malformed_response_handling() {
    let config = NetworkErrorTestConfig::default();

    let tester = NetworkErrorTester::new(config);
    let result = tester.test_malformed_response_handling().await;

    assert!(
        result.passed,
        "Malformed response handling test failed: {:?}",
        result.error_message
    );
    assert!(
        result.expected_errors_caught >= 2,
        "Should catch at least 2 malformed response errors"
    );
    assert!(result.requests_made >= 2, "Should make at least 2 requests");
}

#[tokio::test]
async fn test_http_status_errors() {
    let config = NetworkErrorTestConfig::default();

    let tester = NetworkErrorTester::new(config);
    let result = tester.test_http_status_errors().await;

    assert!(
        result.passed,
        "HTTP status errors test failed: {:?}",
        result.error_message
    );
    assert!(result.expected_errors_caught >= 1);
    assert!(result.requests_made >= 1);
}

#[tokio::test]
async fn test_network_instability() {
    let config = NetworkErrorTestConfig::default();

    let tester = NetworkErrorTester::new(config);
    let result = tester.test_network_instability().await;

    assert!(
        result.passed,
        "Network instability test failed: {:?}",
        result.error_message
    );
    assert!(
        result.expected_errors_caught >= 1,
        "Should catch at least 1 network error"
    );
    assert!(result.requests_made >= 2, "Should make at least 2 requests");
}

#[tokio::test]
async fn test_comprehensive_network_errors() {
    let config = NetworkErrorTestConfig {
        request_timeout: Duration::from_secs(3),
        retry_attempts: 2,
        retry_delay: Duration::from_millis(50),
        test_concurrency: false, // Disable concurrency for this test
        concurrent_requests: 5,
    };

    let tester = NetworkErrorTester::new(config);
    let results = tester.run_all_tests().await;

    let passed_count = results.iter().filter(|r| r.passed).count();
    let total_count = results.len();

    // At least 80% of tests should pass
    let min_passed = (total_count * 4) / 5;
    assert!(
        passed_count >= min_passed,
        "Too many network error tests failed: {}/{} passed",
        passed_count,
        total_count
    );

    // Verify that we tested various error scenarios
    assert!(
        total_count >= 5,
        "Should have run at least 5 different error tests"
    );

    // Check that we made a reasonable number of requests
    let total_requests: usize = results.iter().map(|r| r.requests_made).sum();
    assert!(
        total_requests >= 10,
        "Should have made at least 10 test requests"
    );

    println!(
        "🎉 Comprehensive network error tests completed: {}/{} passed",
        passed_count, total_count
    );
}