orion-accessor 0.8.0

Unified accessor layer for HTTP, Git, and local resources with redirect rules, proxy control, and env-aware templating.
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
691
692
693
694
695
696
697
698
699
700
701
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// 下载超时配置结构体
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct TimeoutConfig {
    /// 连接超时时间(秒)
    pub connect_timeout: u64,
    /// 读取/写入超时时间(秒)
    pub read_timeout: u64,
    /// 总操作超时时间(秒)
    pub total_timeout: u64,
}

impl TimeoutConfig {
    /// 创建默认配置
    pub fn new() -> Self {
        Self::default()
    }

    /// HTTP模式配置(适用于小文件)
    pub fn http_simple() -> Self {
        Self {
            connect_timeout: 30,
            read_timeout: 60,
            total_timeout: 300,
        }
    }

    /// HTTP大文件模式配置
    pub fn http_large_file() -> Self {
        Self {
            connect_timeout: 60,
            read_timeout: 300,
            total_timeout: 3600,
        }
    }

    /// Git操作配置
    pub fn git_operation() -> Self {
        Self {
            connect_timeout: 120,
            read_timeout: 180,
            total_timeout: 1800,
        }
    }

    /// 转为Duration格式
    pub fn connect_duration(&self) -> Duration {
        Duration::from_secs(self.connect_timeout)
    }

    pub fn read_duration(&self) -> Duration {
        Duration::from_secs(self.read_timeout)
    }

    pub fn total_duration(&self) -> Duration {
        Duration::from_secs(self.total_timeout)
    }

    /// 验证配置有效性
    pub fn validate(&self) -> bool {
        self.connect_timeout > 0 && self.read_timeout > 0 && self.total_timeout > 0
    }
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            connect_timeout: 30,
            read_timeout: 60,
            total_timeout: 300,
        }
    }
}

/// 下载进度监控器
#[derive(Debug, Clone)]
pub struct ProgressTracker {
    last_activity: std::time::Instant,
    timeout_duration: Duration,
    total_downloaded: u64,
    total_expected: Option<u64>,
}

impl ProgressTracker {
    /// 创建新的进度跟踪器
    pub fn new(timeout_duration: Duration) -> Self {
        Self {
            last_activity: std::time::Instant::now(),
            timeout_duration,
            total_downloaded: 0,
            total_expected: None,
        }
    }

    /// 更新进度信息
    pub fn update(&mut self, bytes: u64, total: Option<u64>) {
        self.last_activity = std::time::Instant::now();
        self.total_downloaded = bytes;
        self.total_expected = total;
    }

    pub fn reset(&mut self) {
        self.last_activity = std::time::Instant::now();
    }

    /// 检查是否超时
    pub fn has_timed_out(&self) -> bool {
        self.last_activity.elapsed() > self.timeout_duration
    }

    /// 获取下载进度百分比
    pub fn progress_percent(&self) -> Option<f64> {
        match self.total_expected {
            Some(total) if total > 0 => Some((self.total_downloaded as f64 / total as f64) * 100.0),
            _ => None,
        }
    }

    /// 获取已下载字节数
    pub fn downloaded(&self) -> u64 {
        self.total_downloaded
    }

    /// 获取总期望字节数
    pub fn total_expected(&self) -> Option<u64> {
        self.total_expected
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    // TimeoutConfig 测试
    mod timeout_config {
        use super::*;

        #[test]
        fn test_default_config() {
            let config = TimeoutConfig::default();
            assert_eq!(config.connect_timeout, 30);
            assert_eq!(config.read_timeout, 60);
            assert_eq!(config.total_timeout, 300);
        }

        #[test]
        fn test_new_config() {
            let config = TimeoutConfig::new();
            assert_eq!(config, TimeoutConfig::default());
        }

        #[test]
        fn test_http_simple_config() {
            let config = TimeoutConfig::http_simple();
            assert_eq!(config.connect_timeout, 30);
            assert_eq!(config.read_timeout, 60);
            assert_eq!(config.total_timeout, 300);
        }

        #[test]
        fn test_http_large_file_config() {
            let config = TimeoutConfig::http_large_file();
            assert_eq!(config.connect_timeout, 60);
            assert_eq!(config.read_timeout, 300);
            assert_eq!(config.total_timeout, 3600);
        }

        #[test]
        fn test_git_operation_config() {
            let config = TimeoutConfig::git_operation();
            assert_eq!(config.connect_timeout, 120);
            assert_eq!(config.read_timeout, 180);
            assert_eq!(config.total_timeout, 1800);
        }

        #[test]
        fn test_connect_duration() {
            let config = TimeoutConfig {
                connect_timeout: 45,
                read_timeout: 90,
                total_timeout: 180,
            };
            assert_eq!(config.connect_duration(), Duration::from_secs(45));
        }

        #[test]
        fn test_read_duration() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 120,
                total_timeout: 300,
            };
            assert_eq!(config.read_duration(), Duration::from_secs(120));
        }

        #[test]
        fn test_total_duration() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 600,
            };
            assert_eq!(config.total_duration(), Duration::from_secs(600));
        }

        #[test]
        fn test_duration_conversions_zero_values() {
            let config = TimeoutConfig {
                connect_timeout: 0,
                read_timeout: 0,
                total_timeout: 0,
            };
            assert_eq!(config.connect_duration(), Duration::from_secs(0));
            assert_eq!(config.read_duration(), Duration::from_secs(0));
            assert_eq!(config.total_duration(), Duration::from_secs(0));
        }

        #[test]
        fn test_duration_conversions_large_values() {
            let config = TimeoutConfig {
                connect_timeout: u64::MAX,
                read_timeout: u64::MAX,
                total_timeout: u64::MAX,
            };
            assert_eq!(config.connect_duration(), Duration::from_secs(u64::MAX));
            assert_eq!(config.read_duration(), Duration::from_secs(u64::MAX));
            assert_eq!(config.total_duration(), Duration::from_secs(u64::MAX));
        }

        #[test]
        fn test_validation_valid_config() {
            let config = TimeoutConfig {
                connect_timeout: 10,
                read_timeout: 20,
                total_timeout: 30,
            };
            assert!(config.validate());
        }

        #[test]
        fn test_validation_zero_connect_timeout() {
            let config = TimeoutConfig {
                connect_timeout: 0,
                read_timeout: 60,
                total_timeout: 300,
            };
            assert!(!config.validate());
        }

        #[test]
        fn test_validation_zero_read_timeout() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 0,
                total_timeout: 300,
            };
            assert!(!config.validate());
        }

        #[test]
        fn test_validation_zero_total_timeout() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 0,
            };
            assert!(!config.validate());
        }

        #[test]
        fn test_validation_all_zero_values() {
            let config = TimeoutConfig {
                connect_timeout: 0,
                read_timeout: 0,
                total_timeout: 0,
            };
            assert!(!config.validate());
        }

        #[test]
        fn test_validation_edge_case_one_second() {
            let config = TimeoutConfig {
                connect_timeout: 1,
                read_timeout: 1,
                total_timeout: 1,
            };
            assert!(config.validate());
        }

        #[test]
        fn test_partial_eq() {
            let config1 = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 300,
            };
            let config2 = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 300,
            };
            let config3 = TimeoutConfig {
                connect_timeout: 31,
                read_timeout: 60,
                total_timeout: 300,
            };

            assert_eq!(config1, config2);
            assert_ne!(config1, config3);
        }

        #[test]
        fn test_clone() {
            let config = TimeoutConfig {
                connect_timeout: 45,
                read_timeout: 90,
                total_timeout: 180,
            };
            let cloned_config = config.clone();
            assert_eq!(config, cloned_config);
        }

        #[test]
        fn test_debug_format() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 300,
            };
            let debug_str = format!("{config:?}");
            assert!(debug_str.contains("connect_timeout: 30"));
            assert!(debug_str.contains("read_timeout: 60"));
            assert!(debug_str.contains("total_timeout: 300"));
        }
    }

    // ProgressTracker 测试
    mod progress_tracker {
        use super::*;

        #[test]
        fn test_new_tracker() {
            let duration = Duration::from_secs(10);
            let tracker = ProgressTracker::new(duration);

            assert_eq!(tracker.timeout_duration, duration);
            assert_eq!(tracker.total_downloaded, 0);
            assert_eq!(tracker.total_expected, None);
            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_update_with_total() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, Some(100));

            assert_eq!(tracker.downloaded(), 50);
            assert_eq!(tracker.total_expected(), Some(100));
            assert_eq!(tracker.progress_percent(), Some(50.0));
            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_update_without_total() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, None);

            assert_eq!(tracker.downloaded(), 50);
            assert_eq!(tracker.total_expected(), None);
            assert_eq!(tracker.progress_percent(), None);
            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_progress_percent_zero_total() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, Some(0));

            assert_eq!(tracker.progress_percent(), None);
        }

        #[test]
        fn test_progress_percent_complete() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(100, Some(100));

            assert_eq!(tracker.progress_percent(), Some(100.0));
        }

        #[test]
        fn test_progress_percent_half() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, Some(100));

            assert_eq!(tracker.progress_percent(), Some(50.0));
        }

        #[test]
        fn test_progress_percent_partial() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(33, Some(100));

            let percent = tracker.progress_percent().unwrap();
            assert!((percent - 33.0).abs() < 0.01);
        }

        #[test]
        fn test_progress_percent_large_numbers() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(u64::MAX / 2, Some(u64::MAX));

            let percent = tracker.progress_percent().unwrap();
            assert!((percent - 50.0).abs() < 0.01);
        }

        #[test]
        fn test_reset() {
            let mut tracker = ProgressTracker::new(Duration::from_millis(100));

            // 更新进度
            tracker.update(50, Some(100));
            assert_eq!(tracker.downloaded(), 50);

            // 等待一段时间
            std::thread::sleep(Duration::from_millis(50));
            let old_time = tracker.last_activity;

            // 重置
            tracker.reset();
            assert_eq!(tracker.downloaded(), 50); // 下载字节数不变
            assert_eq!(tracker.total_expected(), Some(100)); // 总期望字节数不变
            assert!(tracker.last_activity > old_time); // 时间被重置
            assert!(!tracker.has_timed_out()); // 重置后不应该超时
        }

        #[test]
        fn test_timeout_behavior() {
            let mut tracker = ProgressTracker::new(Duration::from_millis(50));

            // 初始状态不应该超时
            assert!(!tracker.has_timed_out());

            // 更新进度
            tracker.update(10, Some(100));
            assert!(!tracker.has_timed_out());

            // 等待超时
            std::thread::sleep(Duration::from_millis(60));
            assert!(tracker.has_timed_out());

            // 更新进度后应该重置超时状态
            tracker.update(20, Some(100));
            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_zero_timeout_duration() {
            let tracker = ProgressTracker::new(Duration::from_millis(1));

            // 立即检查,可能还没超时
            let _timed_out = tracker.has_timed_out();
            // 短暂等待后应该超时
            std::thread::sleep(Duration::from_millis(2));
            assert!(tracker.has_timed_out());
        }

        #[test]
        fn test_large_timeout_duration() {
            let tracker = ProgressTracker::new(Duration::from_secs(3600)); // 1小时
            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_multiple_updates() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));

            // 多次更新进度
            tracker.update(10, Some(100));
            assert_eq!(tracker.progress_percent(), Some(10.0));

            tracker.update(25, Some(100));
            assert_eq!(tracker.progress_percent(), Some(25.0));

            tracker.update(75, Some(100));
            assert_eq!(tracker.progress_percent(), Some(75.0));

            tracker.update(100, Some(100));
            assert_eq!(tracker.progress_percent(), Some(100.0));

            assert!(!tracker.has_timed_out());
        }

        #[test]
        fn test_update_changes_total_expected() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));

            tracker.update(50, Some(100));
            assert_eq!(tracker.total_expected(), Some(100));
            assert_eq!(tracker.progress_percent(), Some(50.0));

            // 更新时改变总期望大小
            tracker.update(50, Some(200));
            assert_eq!(tracker.total_expected(), Some(200));
            assert_eq!(tracker.progress_percent(), Some(25.0));
        }

        #[test]
        fn test_clone_tracker() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, Some(100));

            let cloned_tracker = tracker.clone();

            assert_eq!(cloned_tracker.downloaded(), 50);
            assert_eq!(cloned_tracker.total_expected(), Some(100));
            assert_eq!(cloned_tracker.progress_percent(), Some(50.0));
            assert!(!cloned_tracker.has_timed_out());
        }

        #[test]
        fn test_debug_format_tracker() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(10));
            tracker.update(50, Some(100));

            let debug_str = format!("{tracker:?}");
            assert!(debug_str.contains("last_activity"));
            assert!(debug_str.contains("timeout_duration"));
            assert!(debug_str.contains("total_downloaded"));
            assert!(debug_str.contains("total_expected"));
        }

        #[test]
        fn test_tracker_behavior_under_load() {
            let mut tracker = ProgressTracker::new(Duration::from_millis(100));

            // 模拟频繁更新
            for i in 1..=100 {
                tracker.update(i, Some(100));
                assert_eq!(tracker.downloaded(), i);

                // 使用近似比较来处理浮点数精度问题
                let percent = tracker.progress_percent().unwrap();
                assert!(
                    (percent - i as f64).abs() < 0.0001,
                    "Expected {}, got {} for i={}",
                    i as f64,
                    percent,
                    i
                );

                assert!(!tracker.has_timed_out());

                // 每次更新后短暂等待,确保不会超时
                std::thread::sleep(Duration::from_millis(1));
            }
        }

        #[test]
        fn test_edge_case_max_values() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(1));

            tracker.update(u64::MAX, Some(u64::MAX));
            assert_eq!(tracker.downloaded(), u64::MAX);
            assert_eq!(tracker.total_expected(), Some(u64::MAX));
            assert_eq!(tracker.progress_percent(), Some(100.0));
        }

        #[test]
        fn test_edge_case_zero_values() {
            let mut tracker = ProgressTracker::new(Duration::from_secs(1));

            tracker.update(0, Some(0));
            assert_eq!(tracker.downloaded(), 0);
            assert_eq!(tracker.total_expected(), Some(0));
            assert_eq!(tracker.progress_percent(), None);

            tracker.update(0, None);
            assert_eq!(tracker.downloaded(), 0);
            assert_eq!(tracker.total_expected(), None);
            assert_eq!(tracker.progress_percent(), None);
        }
    }

    // 序列化/反序列化测试
    mod serde_tests {
        use super::*;

        #[test]
        fn test_serialize_yaml() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 300,
            };

            let yaml = serde_yaml::to_string(&config).unwrap();
            assert!(yaml.contains("connect-timeout: 30"));
            assert!(yaml.contains("read-timeout: 60"));
            assert!(yaml.contains("total-timeout: 300"));
        }

        #[test]
        fn test_deserialize_yaml() {
            let yaml = r#"
connect-timeout: 45
read-timeout: 90
total-timeout: 180
"#;

            let config: TimeoutConfig = serde_yaml::from_str(yaml).unwrap();
            assert_eq!(config.connect_timeout, 45);
            assert_eq!(config.read_timeout, 90);
            assert_eq!(config.total_timeout, 180);
        }

        #[test]
        fn test_serialize_json() {
            let config = TimeoutConfig {
                connect_timeout: 30,
                read_timeout: 60,
                total_timeout: 300,
            };

            let json = serde_json::to_string(&config).unwrap();
            let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

            assert_eq!(parsed["connect-timeout"], 30);
            assert_eq!(parsed["read-timeout"], 60);
            assert_eq!(parsed["total-timeout"], 300);
        }

        #[test]
        fn test_deserialize_json() {
            let json = r#"
{
    "connect-timeout": 45,
    "read-timeout": 90,
    "total-timeout": 180
}
"#;

            let config: TimeoutConfig = serde_json::from_str(json).unwrap();
            assert_eq!(config.connect_timeout, 45);
            assert_eq!(config.read_timeout, 90);
            assert_eq!(config.total_timeout, 180);
        }

        #[test]
        fn test_serialize_roundtrip_yaml() {
            let original = TimeoutConfig {
                connect_timeout: 120,
                read_timeout: 240,
                total_timeout: 600,
            };

            let yaml = serde_yaml::to_string(&original).unwrap();
            let deserialized: TimeoutConfig = serde_yaml::from_str(&yaml).unwrap();

            assert_eq!(original, deserialized);
        }

        #[test]
        fn test_serialize_roundtrip_json() {
            let original = TimeoutConfig {
                connect_timeout: 120,
                read_timeout: 240,
                total_timeout: 600,
            };

            let json = serde_json::to_string(&original).unwrap();
            let deserialized: TimeoutConfig = serde_json::from_str(&json).unwrap();

            assert_eq!(original, deserialized);
        }

        #[test]
        fn test_deserialize_missing_field() {
            let yaml = "connect-timeout: 30\nread-timeout: 60";
            let result: Result<TimeoutConfig, _> = serde_yaml::from_str(yaml);
            assert!(result.is_err());
        }

        #[test]
        fn test_deserialize_extra_field() {
            let yaml = r#"
connect-timeout: 30
read-timeout: 60
total-timeout: 300
extra-field: "ignored"
"#;

            let result: Result<TimeoutConfig, _> = serde_yaml::from_str(yaml);
            // 这应该成功,因为serde默认忽略额外字段
            assert!(result.is_ok());
        }
    }
}