netrunner_cli 0.7.1

A feature-rich Rust-based CLI to test and analyze your internet connection
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
//! Integration tests for geolocation functionality
//!
//! These tests verify that the geolocation services work correctly
//! and handle various edge cases and failures gracefully.

use netrunner_cli::modules::speed_test::{GeoLocation, SpeedTest};
use netrunner_cli::modules::types::{DetailLevel, TestConfig};
use std::time::Duration;

fn ensure_crypto_provider() {
    let _ = rustls::crypto::ring::default_provider().install_default();
}

/// Helper function to create a test config for geolocation testing
fn create_geo_test_config() -> TestConfig {
    TestConfig {
        server_url: "https://httpbin.org".to_string(),
        test_size_mb: 1,
        timeout_seconds: 10,
        json_output: true, // Suppress UI output during tests
        animation_enabled: false,
        detail_level: DetailLevel::Standard,
        max_servers: 2,
    }
}

#[tokio::test]
async fn test_geolocation_basic_structure() {
    let geo = GeoLocation {
        country: "United States".to_string(),
        city: "New York".to_string(),
        latitude: 40.7128,
        longitude: -74.0060,
        isp: Some("Test ISP".to_string()),
    };

    assert!(!geo.country.is_empty(), "Country should not be empty");
    assert!(!geo.city.is_empty(), "City should not be empty");
    assert!(
        geo.latitude >= -90.0 && geo.latitude <= 90.0,
        "Latitude should be valid"
    );
    assert!(
        geo.longitude >= -180.0 && geo.longitude <= 180.0,
        "Longitude should be valid"
    );
}

#[tokio::test]
async fn test_geolocation_without_isp() {
    let geo = GeoLocation {
        country: "Japan".to_string(),
        city: "Tokyo".to_string(),
        latitude: 35.6762,
        longitude: 139.6503,
        isp: None,
    };

    assert_eq!(geo.country, "Japan");
    assert_eq!(geo.city, "Tokyo");
    assert!(geo.isp.is_none(), "ISP can be optional");
}

#[tokio::test]
async fn test_geolocation_extreme_coordinates() {
    // Test polar regions
    let north_pole = GeoLocation {
        country: "Arctic".to_string(),
        city: "North Pole".to_string(),
        latitude: 90.0,
        longitude: 0.0,
        isp: None,
    };
    assert_eq!(north_pole.latitude, 90.0);

    let south_pole = GeoLocation {
        country: "Antarctica".to_string(),
        city: "South Pole".to_string(),
        latitude: -90.0,
        longitude: 0.0,
        isp: None,
    };
    assert_eq!(south_pole.latitude, -90.0);

    // Test international date line
    let date_line = GeoLocation {
        country: "Pacific".to_string(),
        city: "Date Line".to_string(),
        latitude: 0.0,
        longitude: 180.0,
        isp: None,
    };
    assert_eq!(date_line.longitude, 180.0);
}

#[tokio::test]
async fn test_geolocation_major_cities() {
    let cities = vec![
        ("London", "United Kingdom", 51.5074, -0.1278),
        ("Tokyo", "Japan", 35.6762, 139.6503),
        ("Sydney", "Australia", -33.8688, 151.2093),
        ("São Paulo", "Brazil", -23.5505, -46.6333),
        ("Mumbai", "India", 19.0760, 72.8777),
        ("Cairo", "Egypt", 30.0444, 31.2357),
    ];

    for (city, country, lat, lon) in cities {
        let geo = GeoLocation {
            country: country.to_string(),
            city: city.to_string(),
            latitude: lat,
            longitude: lon,
            isp: None,
        };

        assert!(!geo.city.is_empty());
        assert!(!geo.country.is_empty());
        assert!(geo.latitude >= -90.0 && geo.latitude <= 90.0);
        assert!(geo.longitude >= -180.0 && geo.longitude <= 180.0);
    }
}

#[tokio::test]
async fn test_geolocation_serialization() {
    let geo = GeoLocation {
        country: "Germany".to_string(),
        city: "Berlin".to_string(),
        latitude: 52.5200,
        longitude: 13.4050,
        isp: Some("Deutsche Telekom".to_string()),
    };

    // Test JSON serialization
    let json = serde_json::to_string(&geo);
    assert!(json.is_ok(), "GeoLocation should serialize to JSON");

    // Test deserialization
    if let Ok(json_str) = json {
        let deserialized: Result<GeoLocation, _> = serde_json::from_str(&json_str);
        assert!(
            deserialized.is_ok(),
            "GeoLocation should deserialize from JSON"
        );

        if let Ok(parsed_geo) = deserialized {
            assert_eq!(parsed_geo.country, "Germany");
            assert_eq!(parsed_geo.city, "Berlin");
            assert_eq!(parsed_geo.latitude, 52.5200);
            assert_eq!(parsed_geo.longitude, 13.4050);
            assert_eq!(parsed_geo.isp, Some("Deutsche Telekom".to_string()));
        }
    }
}

#[tokio::test]
async fn test_geolocation_distance_calculation() {
    // Test distance between known cities
    let new_york = GeoLocation {
        country: "USA".to_string(),
        city: "New York".to_string(),
        latitude: 40.7128,
        longitude: -74.0060,
        isp: None,
    };

    let london = GeoLocation {
        country: "UK".to_string(),
        city: "London".to_string(),
        latitude: 51.5074,
        longitude: -0.1278,
        isp: None,
    };

    // Haversine formula
    let r: f64 = 6371.0;
    let d_lat: f64 = (london.latitude - new_york.latitude).to_radians();
    let d_lon: f64 = (london.longitude - new_york.longitude).to_radians();
    let lat1: f64 = new_york.latitude.to_radians();
    let lat2: f64 = london.latitude.to_radians();

    let a: f64 = (d_lat / 2.0).sin() * (d_lat / 2.0).sin()
        + lat1.cos() * lat2.cos() * (d_lon / 2.0).sin() * (d_lon / 2.0).sin();
    let c: f64 = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
    let distance: f64 = r * c;

    // NYC to London is approximately 5,570 km
    assert!(
        distance > 5400.0 && distance < 5700.0,
        "Distance NYC-London should be ~5570km, got {}",
        distance
    );
}

#[tokio::test]
async fn test_geolocation_with_special_characters() {
    let geo = GeoLocation {
        country: "Côte d'Ivoire".to_string(),
        city: "São Paulo".to_string(),
        latitude: -23.5505,
        longitude: -46.6333,
        isp: Some("Société Générale".to_string()),
    };

    assert!(geo.country.contains("Côte"));
    assert!(geo.city.contains("São"));
    assert!(geo.isp.unwrap().contains("Société"));
}

#[tokio::test]
async fn test_geolocation_validation_invalid_latitude() {
    // Test that we can detect invalid latitudes
    let invalid_geo = GeoLocation {
        country: "Test".to_string(),
        city: "Test".to_string(),
        latitude: 91.0, // Invalid!
        longitude: 0.0,
        isp: None,
    };

    assert!(
        invalid_geo.latitude > 90.0,
        "Should be able to detect invalid latitude"
    );
}

#[tokio::test]
async fn test_geolocation_validation_invalid_longitude() {
    // Test that we can detect invalid longitudes
    let invalid_geo = GeoLocation {
        country: "Test".to_string(),
        city: "Test".to_string(),
        latitude: 0.0,
        longitude: 181.0, // Invalid!
        isp: None,
    };

    assert!(
        invalid_geo.longitude > 180.0,
        "Should be able to detect invalid longitude"
    );
}

#[tokio::test]
async fn test_default_fallback_location() {
    // Test that the default fallback location is valid
    let default_geo = GeoLocation {
        country: "United States".to_string(),
        city: "Kansas City".to_string(),
        latitude: 39.0997,
        longitude: -94.5786,
        isp: None,
    };

    // Kansas City is in the geographic center of the US
    assert_eq!(default_geo.country, "United States");
    assert_eq!(default_geo.city, "Kansas City");
    assert!(
        default_geo.latitude > 38.0 && default_geo.latitude < 40.0,
        "Kansas City latitude should be ~39°N"
    );
    assert!(
        default_geo.longitude > -95.0 && default_geo.longitude < -94.0,
        "Kansas City longitude should be ~-95°W"
    );
}

#[tokio::test]
async fn test_geolocation_clone() {
    let geo = GeoLocation {
        country: "France".to_string(),
        city: "Paris".to_string(),
        latitude: 48.8566,
        longitude: 2.3522,
        isp: Some("Orange".to_string()),
    };

    let cloned = geo.clone();

    assert_eq!(geo.country, cloned.country);
    assert_eq!(geo.city, cloned.city);
    assert_eq!(geo.latitude, cloned.latitude);
    assert_eq!(geo.longitude, cloned.longitude);
    assert_eq!(geo.isp, cloned.isp);
}

#[tokio::test]
async fn test_geolocation_debug_format() {
    let geo = GeoLocation {
        country: "Spain".to_string(),
        city: "Madrid".to_string(),
        latitude: 40.4168,
        longitude: -3.7038,
        isp: Some("Telefonica".to_string()),
    };

    let debug_str = format!("{:?}", geo);
    assert!(debug_str.contains("Spain"));
    assert!(debug_str.contains("Madrid"));
}

#[tokio::test]
async fn test_real_geolocation_detection() {
    ensure_crypto_provider();
    // This test actually tries to detect location using the APIs
    // It's designed to be robust and not fail in CI/CD environments
    let config = create_geo_test_config();
    let speed_test = SpeedTest::new(config).expect("Failed to create SpeedTest");

    // Run with a reasonable timeout since this makes real network calls
    let result = tokio::time::timeout(Duration::from_secs(20), speed_test.run_full_test()).await;

    match result {
        Ok(Ok(test_result)) => {
            // If we got a result, verify the location was detected properly
            assert!(
                !test_result.server_location.is_empty(),
                "Server location should be detected"
            );

            // Verify we got actual speed test results
            assert!(
                test_result.download_mbps >= 0.0,
                "Download speed should be non-negative"
            );
            assert!(
                test_result.upload_mbps >= 0.0,
                "Upload speed should be non-negative"
            );
            assert!(test_result.ping_ms >= 0.0, "Ping should be non-negative");

            println!("✓ Geolocation detected: {}", test_result.server_location);
            println!(
                "✓ Speed test completed: ↓{:.1} Mbps ↑{:.1} Mbps",
                test_result.download_mbps, test_result.upload_mbps
            );
        }
        Ok(Err(e)) => {
            // Network issues are acceptable in test environments
            // but we should still verify the error is reasonable
            let error_msg = format!("{}", e);
            println!(
                "âš  Speed test failed (acceptable in test env): {}",
                error_msg
            );

            // Verify it's a network-related error, not a code bug
            assert!(
                error_msg.contains("location")
                    || error_msg.contains("network")
                    || error_msg.contains("timeout")
                    || error_msg.contains("server")
                    || error_msg.contains("connect")
                    || error_msg.contains("HTTP"),
                "Error should be network-related, got: {}",
                error_msg
            );
        }
        Err(_) => {
            println!("âš  Test timed out (acceptable in test env - slow network)");
            // Timeout is acceptable - speed test structure is valid even if network is slow
        }
    }
}

#[tokio::test]
async fn test_geolocation_api_fallback_behavior() {
    // Test that the fallback location is used when all APIs fail
    // This tests the resilience of the system
    let fallback_geo = GeoLocation {
        country: "United States".to_string(),
        city: "Kansas City".to_string(),
        latitude: 39.0997,
        longitude: -94.5786,
        isp: None,
    };

    // Verify fallback location is valid
    assert!(!fallback_geo.country.is_empty());
    assert!(!fallback_geo.city.is_empty());
    assert!(fallback_geo.latitude >= -90.0 && fallback_geo.latitude <= 90.0);
    assert!(fallback_geo.longitude >= -180.0 && fallback_geo.longitude <= 180.0);
}

#[tokio::test]
async fn test_geolocation_with_minimal_data() {
    // Test geolocation with minimum required data (no ISP)
    let minimal_geo = GeoLocation {
        country: "Test Country".to_string(),
        city: "Test City".to_string(),
        latitude: 0.0,
        longitude: 0.0,
        isp: None,
    };

    assert_eq!(minimal_geo.country, "Test Country");
    assert_eq!(minimal_geo.city, "Test City");
    assert!(minimal_geo.isp.is_none());
}

#[tokio::test]
async fn test_geolocation_coordinate_precision() {
    // Test that high precision coordinates are preserved
    let precise_geo = GeoLocation {
        country: "Test".to_string(),
        city: "Test".to_string(),
        latitude: 40.712776,
        longitude: -74.005974,
        isp: None,
    };

    assert_eq!(precise_geo.latitude, 40.712776);
    assert_eq!(precise_geo.longitude, -74.005974);
}

#[tokio::test]
async fn test_continent_determination() {
    // Test continent determination based on coordinates
    let test_cases = vec![
        (40.7128, -74.0060, "North America"),  // New York
        (51.5074, -0.1278, "Europe"),          // London
        (35.6762, 139.6503, "Asia"),           // Tokyo
        (-33.8688, 151.2093, "Oceania"),       // Sydney
        (-23.5505, -46.6333, "South America"), // São Paulo
        (30.0444, 31.2357, "Africa"),          // Cairo
    ];

    for (lat, lon, expected_continent) in test_cases {
        let continent = determine_continent(lat, lon);
        assert!(
            !continent.is_empty(),
            "Should determine continent for {}, {}",
            lat,
            lon
        );
        // Note: The actual implementation might classify differently at borders
        println!(
            "Location ({}, {}) classified as: {} (expected: {})",
            lat, lon, continent, expected_continent
        );
    }
}

#[tokio::test]
async fn test_debug_mode_environment_variable() {
    // Test that debug mode can be enabled via environment variable
    // This doesn't test the actual output, just that the env var can be checked

    // Without debug mode
    assert!(std::env::var("NETRUNNER_DEBUG").is_err());

    // Set debug mode
    std::env::set_var("NETRUNNER_DEBUG", "1");
    assert!(std::env::var("NETRUNNER_DEBUG").is_ok());

    // Clean up
    std::env::remove_var("NETRUNNER_DEBUG");
    assert!(std::env::var("NETRUNNER_DEBUG").is_err());
}

#[tokio::test]
async fn test_geolocation_service_names() {
    // Test that we know all the geolocation service names
    let services = vec![
        "ipapi.co",
        "ip-api.com",
        "ipinfo.io",
        "freegeoip.app",
        "ipwhois.app",
    ];

    assert_eq!(services.len(), 5, "Should have 5 geolocation services");

    for service in services {
        assert!(!service.is_empty(), "Service name should not be empty");
        assert!(service.contains('.'), "Service should be a domain name");
    }
}

// Helper function to test continent determination
fn determine_continent(lat: f64, lon: f64) -> String {
    if lat > 15.0 && lon > -130.0 && lon < -50.0 {
        "North America".to_string()
    } else if lat < 15.0 && lat > -60.0 && lon > -85.0 && lon < -30.0 {
        "South America".to_string()
    } else if lat > 35.0 && lon > -15.0 && lon < 60.0 {
        "Europe".to_string()
    } else if lat > -40.0 && lat < 40.0 && lon > -20.0 && lon < 55.0 {
        "Africa".to_string()
    } else if lat > -15.0 && lon > 60.0 && lon < 180.0 {
        "Asia".to_string()
    } else if lat < -10.0 && lon > 110.0 && lon < 180.0 {
        "Oceania".to_string()
    } else {
        "Unknown".to_string()
    }
}

#[tokio::test]
async fn test_timezone_implications() {
    // Test locations across different timezones
    let locations = vec![
        ("Los Angeles", -118.2437, "UTC-8"),
        ("New York", -74.0060, "UTC-5"),
        ("London", -0.1278, "UTC+0"),
        ("Tokyo", 139.6503, "UTC+9"),
        ("Sydney", 151.2093, "UTC+10"),
    ];

    for (city, lon, _timezone) in locations {
        assert!(
            (-180.0..=180.0).contains(&lon),
            "Longitude for {} should be valid",
            city
        );
    }
}

#[tokio::test]
async fn test_geolocation_error_cases() {
    // Test that invalid data doesn't panic
    let test_cases = vec![
        (0.0, 0.0),    // Null Island (valid but suspicious)
        (90.0, 0.0),   // North Pole
        (-90.0, 0.0),  // South Pole
        (0.0, 180.0),  // Date line
        (0.0, -180.0), // Date line (other side)
    ];

    for (lat, lon) in test_cases {
        let geo = GeoLocation {
            country: "Test".to_string(),
            city: "Test".to_string(),
            latitude: lat,
            longitude: lon,
            isp: None,
        };

        // These should not panic
        assert_eq!(geo.latitude, lat);
        assert_eq!(geo.longitude, lon);
    }
}

#[tokio::test]
async fn test_geolocation_isp_parsing() {
    let test_cases = vec![
        "Comcast Cable",
        "Deutsche Telekom AG",
        "China Telecom",
        "Vodafone Group",
        "AT&T Services",
    ];

    for isp_name in test_cases {
        let geo = GeoLocation {
            country: "Test".to_string(),
            city: "Test".to_string(),
            latitude: 0.0,
            longitude: 0.0,
            isp: Some(isp_name.to_string()),
        };

        assert!(geo.isp.is_some());
        assert_eq!(geo.isp.unwrap(), isp_name);
    }
}