oximedia-packager 0.1.8

Adaptive streaming packaging (HLS/DASH) for OxiMedia
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#![allow(dead_code)]
//! Segment validation and integrity checking for adaptive streaming.
//!
//! This module provides tools for validating segment files, checking
//! segment consistency within a manifest, verifying duration tolerances,
//! and detecting common packaging errors.

use std::time::Duration;

/// Result of validating a single segment.
#[derive(Debug, Clone)]
pub struct SegmentValidationResult {
    /// Segment index.
    pub index: u64,
    /// Whether the segment is valid.
    pub is_valid: bool,
    /// Validation issues found.
    pub issues: Vec<ValidationIssue>,
}

impl SegmentValidationResult {
    /// Create a passing validation result.
    #[must_use]
    pub fn pass(index: u64) -> Self {
        Self {
            index,
            is_valid: true,
            issues: Vec::new(),
        }
    }

    /// Create a validation result with issues.
    #[must_use]
    pub fn with_issues(index: u64, issues: Vec<ValidationIssue>) -> Self {
        let is_valid = issues.iter().all(|i| i.severity != IssueSeverity::Error);
        Self {
            index,
            is_valid,
            issues,
        }
    }

    /// Check if there are any warnings.
    #[must_use]
    pub fn has_warnings(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == IssueSeverity::Warning)
    }

    /// Check if there are any errors.
    #[must_use]
    pub fn has_errors(&self) -> bool {
        self.issues
            .iter()
            .any(|i| i.severity == IssueSeverity::Error)
    }
}

/// Severity level for a validation issue.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueSeverity {
    /// Informational note.
    Info,
    /// Warning that may cause playback issues.
    Warning,
    /// Error that will cause playback failure.
    Error,
}

/// A specific validation issue found in a segment.
#[derive(Debug, Clone)]
pub struct ValidationIssue {
    /// Severity level.
    pub severity: IssueSeverity,
    /// Issue code for programmatic handling.
    pub code: ValidationCode,
    /// Human-readable description of the issue.
    pub message: String,
}

impl ValidationIssue {
    /// Create a new validation issue.
    #[must_use]
    pub fn new(severity: IssueSeverity, code: ValidationCode, message: impl Into<String>) -> Self {
        Self {
            severity,
            code,
            message: message.into(),
        }
    }

    /// Create an error issue.
    #[must_use]
    pub fn error(code: ValidationCode, message: impl Into<String>) -> Self {
        Self::new(IssueSeverity::Error, code, message)
    }

    /// Create a warning issue.
    #[must_use]
    pub fn warning(code: ValidationCode, message: impl Into<String>) -> Self {
        Self::new(IssueSeverity::Warning, code, message)
    }

    /// Create an info issue.
    #[must_use]
    pub fn info(code: ValidationCode, message: impl Into<String>) -> Self {
        Self::new(IssueSeverity::Info, code, message)
    }
}

/// Validation issue codes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationCode {
    /// Segment size is zero.
    EmptySegment,
    /// Segment duration exceeds tolerance.
    DurationOutOfRange,
    /// Segment does not start with a keyframe.
    MissingKeyframe,
    /// Segment index gap detected.
    IndexGap,
    /// Segment index is duplicated.
    DuplicateIndex,
    /// Segment size is abnormally large.
    OversizedSegment,
    /// Segment size is abnormally small.
    UndersizedSegment,
    /// Duration drift accumulated beyond threshold.
    DurationDrift,
    /// Timestamp discontinuity detected.
    TimestampDiscontinuity,
}

/// Metadata about a segment for validation purposes.
#[derive(Debug, Clone)]
pub struct SegmentMetadata {
    /// Segment index.
    pub index: u64,
    /// Segment duration.
    pub duration: Duration,
    /// Segment size in bytes.
    pub size_bytes: u64,
    /// Whether the segment starts with a keyframe.
    pub starts_with_keyframe: bool,
    /// Presentation timestamp of the segment start.
    pub pts_start_ms: u64,
}

impl SegmentMetadata {
    /// Create new segment metadata.
    #[must_use]
    pub fn new(index: u64, duration: Duration, size_bytes: u64) -> Self {
        Self {
            index,
            duration,
            size_bytes,
            starts_with_keyframe: true,
            pts_start_ms: 0,
        }
    }

    /// Set whether the segment starts with a keyframe.
    #[must_use]
    pub fn with_keyframe(mut self, starts_with_keyframe: bool) -> Self {
        self.starts_with_keyframe = starts_with_keyframe;
        self
    }

    /// Set the presentation timestamp.
    #[must_use]
    pub fn with_pts(mut self, pts_start_ms: u64) -> Self {
        self.pts_start_ms = pts_start_ms;
        self
    }
}

/// Configuration for segment validation.
#[derive(Debug, Clone)]
pub struct ValidationConfig {
    /// Target segment duration.
    pub target_duration: Duration,
    /// Allowed duration tolerance as a fraction (e.g., 0.1 for 10%).
    pub duration_tolerance: f64,
    /// Maximum allowed segment size in bytes.
    pub max_segment_size: u64,
    /// Minimum allowed segment size in bytes.
    pub min_segment_size: u64,
    /// Maximum accumulated duration drift in milliseconds.
    pub max_drift_ms: u64,
    /// Whether keyframe-at-start is required.
    pub require_keyframe_start: bool,
    /// Minimum duration multiplier relative to target (e.g. 0.5 = 50% of target).
    pub min_duration_factor: f64,
    /// Maximum duration multiplier relative to target (e.g. 2.0 = 200% of target).
    pub max_duration_factor: f64,
}

impl ValidationConfig {
    /// Create a default validation config with a target duration.
    #[must_use]
    pub fn new(target_duration: Duration) -> Self {
        Self {
            target_duration,
            duration_tolerance: 0.1,
            max_segment_size: 50 * 1024 * 1024, // 50 MB
            min_segment_size: 100,
            max_drift_ms: 500,
            require_keyframe_start: true,
            min_duration_factor: 0.5,
            max_duration_factor: 2.0,
        }
    }

    /// Set the duration tolerance.
    #[must_use]
    pub fn with_tolerance(mut self, tolerance: f64) -> Self {
        self.duration_tolerance = tolerance.clamp(0.0, 1.0);
        self
    }

    /// Set whether keyframe-at-start is required.
    #[must_use]
    pub fn with_keyframe_required(mut self, required: bool) -> Self {
        self.require_keyframe_start = required;
        self
    }

    /// Set the minimum duration factor (e.g. 0.5 means segment must be >= 50% of target).
    #[must_use]
    pub fn with_min_duration_factor(mut self, factor: f64) -> Self {
        self.min_duration_factor = factor.clamp(0.0, 1.0);
        self
    }

    /// Set the maximum duration factor (e.g. 2.0 means segment must be <= 200% of target).
    #[must_use]
    pub fn with_max_duration_factor(mut self, factor: f64) -> Self {
        self.max_duration_factor = factor.max(1.0);
        self
    }
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self::new(Duration::from_secs(6))
    }
}

/// Segment validator that checks a sequence of segments.
pub struct SegmentValidator {
    /// Validation configuration.
    config: ValidationConfig,
}

impl SegmentValidator {
    /// Create a new segment validator.
    #[must_use]
    pub fn new(config: ValidationConfig) -> Self {
        Self { config }
    }

    /// Validate a single segment.
    #[must_use]
    pub fn validate_segment(&self, metadata: &SegmentMetadata) -> SegmentValidationResult {
        let mut issues = Vec::new();

        // Check empty segment
        if metadata.size_bytes == 0 {
            issues.push(ValidationIssue::error(
                ValidationCode::EmptySegment,
                format!("Segment {} has zero bytes", metadata.index),
            ));
        }

        // Check size bounds
        if metadata.size_bytes > self.config.max_segment_size {
            issues.push(ValidationIssue::warning(
                ValidationCode::OversizedSegment,
                format!(
                    "Segment {} is {} bytes, exceeds max {}",
                    metadata.index, metadata.size_bytes, self.config.max_segment_size
                ),
            ));
        }

        if metadata.size_bytes > 0 && metadata.size_bytes < self.config.min_segment_size {
            issues.push(ValidationIssue::warning(
                ValidationCode::UndersizedSegment,
                format!(
                    "Segment {} is {} bytes, below min {}",
                    metadata.index, metadata.size_bytes, self.config.min_segment_size
                ),
            ));
        }

        // Check duration tolerance
        self.check_duration(metadata, &mut issues);

        // Check keyframe
        if self.config.require_keyframe_start && !metadata.starts_with_keyframe {
            issues.push(ValidationIssue::error(
                ValidationCode::MissingKeyframe,
                format!("Segment {} does not start with a keyframe", metadata.index),
            ));
        }

        SegmentValidationResult::with_issues(metadata.index, issues)
    }

    /// Check duration is within tolerance and hard bounds.
    #[allow(clippy::cast_precision_loss)]
    fn check_duration(&self, metadata: &SegmentMetadata, issues: &mut Vec<ValidationIssue>) {
        let target_ms = self.config.target_duration.as_millis() as f64;
        let actual_ms = metadata.duration.as_millis() as f64;

        if target_ms > 0.0 {
            // Soft tolerance check (warning)
            let deviation = (actual_ms - target_ms).abs() / target_ms;
            if deviation > self.config.duration_tolerance {
                issues.push(ValidationIssue::warning(
                    ValidationCode::DurationOutOfRange,
                    format!(
                        "Segment {} duration {:.0}ms deviates {:.1}% from target {:.0}ms",
                        metadata.index,
                        actual_ms,
                        deviation * 100.0,
                        target_ms
                    ),
                ));
            }

            // Hard bounds check (error): segment must be within [min_factor, max_factor] of target
            let min_ms = target_ms * self.config.min_duration_factor;
            let max_ms = target_ms * self.config.max_duration_factor;

            if actual_ms < min_ms {
                issues.push(ValidationIssue::error(
                    ValidationCode::DurationOutOfRange,
                    format!(
                        "Segment {} duration {:.0}ms is below hard minimum {:.0}ms ({:.0}x target)",
                        metadata.index, actual_ms, min_ms, self.config.min_duration_factor,
                    ),
                ));
            } else if actual_ms > max_ms {
                issues.push(ValidationIssue::error(
                    ValidationCode::DurationOutOfRange,
                    format!(
                        "Segment {} duration {:.0}ms exceeds hard maximum {:.0}ms ({:.0}x target)",
                        metadata.index, actual_ms, max_ms, self.config.max_duration_factor,
                    ),
                ));
            }
        }
    }

    /// Validate a sequence of segments for consistency.
    #[must_use]
    pub fn validate_sequence(&self, segments: &[SegmentMetadata]) -> Vec<SegmentValidationResult> {
        let mut results = Vec::with_capacity(segments.len());

        for segment in segments {
            results.push(self.validate_segment(segment));
        }

        // Check for index gaps and duplicates
        self.check_index_continuity(segments, &mut results);

        results
    }

    /// Check index continuity across segments.
    fn check_index_continuity(
        &self,
        segments: &[SegmentMetadata],
        results: &mut [SegmentValidationResult],
    ) {
        if segments.len() < 2 {
            return;
        }

        for i in 1..segments.len() {
            let expected = segments[i - 1].index + 1;
            let actual = segments[i].index;

            if actual != expected {
                results[i].issues.push(ValidationIssue::warning(
                    ValidationCode::IndexGap,
                    format!(
                        "Index gap: expected {} after {}, got {}",
                        expected,
                        segments[i - 1].index,
                        actual
                    ),
                ));
            }
        }
    }

    /// Validate that a segment duration is within the hard bounds
    /// `[min_factor * target, max_factor * target]`.
    ///
    /// Returns `Ok(())` if within bounds, or an `Err` describing the violation.
    #[allow(clippy::cast_precision_loss)]
    pub fn validate_duration_bounds(&self, segment: &SegmentMetadata) -> Result<(), String> {
        let target_ms = self.config.target_duration.as_millis() as f64;
        let actual_ms = segment.duration.as_millis() as f64;

        if target_ms <= 0.0 {
            return Ok(());
        }

        let min_ms = target_ms * self.config.min_duration_factor;
        let max_ms = target_ms * self.config.max_duration_factor;

        if actual_ms < min_ms {
            return Err(format!(
                "Segment {} duration {:.0}ms below minimum {:.0}ms",
                segment.index, actual_ms, min_ms,
            ));
        }
        if actual_ms > max_ms {
            return Err(format!(
                "Segment {} duration {:.0}ms exceeds maximum {:.0}ms",
                segment.index, actual_ms, max_ms,
            ));
        }
        Ok(())
    }

    /// Validate duration bounds for an entire sequence.
    /// Returns a list of (segment_index, error_message) for any violations.
    #[must_use]
    pub fn validate_sequence_bounds(&self, segments: &[SegmentMetadata]) -> Vec<(u64, String)> {
        let mut violations = Vec::new();
        for seg in segments {
            if let Err(msg) = self.validate_duration_bounds(seg) {
                violations.push((seg.index, msg));
            }
        }
        violations
    }

    /// Compute total validated duration.
    #[must_use]
    pub fn total_duration(&self, segments: &[SegmentMetadata]) -> Duration {
        segments.iter().map(|s| s.duration).sum()
    }
}

impl Default for SegmentValidator {
    fn default() -> Self {
        Self::new(ValidationConfig::default())
    }
}

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

    fn seg(index: u64, duration_ms: u64, size: u64) -> SegmentMetadata {
        SegmentMetadata::new(index, Duration::from_millis(duration_ms), size)
    }

    #[test]
    fn test_valid_segment() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_segment(&seg(0, 6000, 500_000));
        assert!(result.is_valid);
        assert!(result.issues.is_empty());
    }

    #[test]
    fn test_empty_segment() {
        let validator = SegmentValidator::default();
        let result = validator.validate_segment(&seg(0, 6000, 0));
        assert!(!result.is_valid);
        assert!(result.has_errors());
    }

    #[test]
    fn test_oversized_segment() {
        let validator = SegmentValidator::default();
        let result = validator.validate_segment(&seg(0, 6000, 100 * 1024 * 1024));
        assert!(result.has_warnings());
    }

    #[test]
    fn test_undersized_segment() {
        let validator = SegmentValidator::default();
        let result = validator.validate_segment(&seg(0, 6000, 50));
        assert!(result.has_warnings());
    }

    #[test]
    fn test_duration_out_of_range() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_segment(&seg(0, 10_000, 500_000));
        assert!(result.has_warnings());
        let has_dur_issue = result
            .issues
            .iter()
            .any(|i| i.code == ValidationCode::DurationOutOfRange);
        assert!(has_dur_issue);
    }

    #[test]
    fn test_missing_keyframe() {
        let validator = SegmentValidator::default();
        let meta = SegmentMetadata::new(0, Duration::from_secs(6), 500_000).with_keyframe(false);
        let result = validator.validate_segment(&meta);
        assert!(!result.is_valid);
    }

    #[test]
    fn test_validation_pass_result() {
        let result = SegmentValidationResult::pass(42);
        assert!(result.is_valid);
        assert!(!result.has_warnings());
        assert!(!result.has_errors());
    }

    #[test]
    fn test_validation_issue_constructors() {
        let err = ValidationIssue::error(ValidationCode::EmptySegment, "empty");
        assert_eq!(err.severity, IssueSeverity::Error);

        let warn = ValidationIssue::warning(ValidationCode::OversizedSegment, "big");
        assert_eq!(warn.severity, IssueSeverity::Warning);

        let info = ValidationIssue::info(ValidationCode::DurationDrift, "drift");
        assert_eq!(info.severity, IssueSeverity::Info);
    }

    #[test]
    fn test_validate_sequence() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let segments = vec![
            seg(0, 6000, 500_000),
            seg(1, 6000, 500_000),
            seg(2, 6000, 500_000),
        ];
        let results = validator.validate_sequence(&segments);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_valid));
    }

    #[test]
    fn test_validate_sequence_index_gap() {
        let validator = SegmentValidator::default();
        let segments = vec![seg(0, 6000, 500_000), seg(2, 6000, 500_000)];
        let results = validator.validate_sequence(&segments);
        let gap_issues: Vec<_> = results[1]
            .issues
            .iter()
            .filter(|i| i.code == ValidationCode::IndexGap)
            .collect();
        assert_eq!(gap_issues.len(), 1);
    }

    #[test]
    fn test_total_duration() {
        let validator = SegmentValidator::default();
        let segments = vec![seg(0, 6000, 500_000), seg(1, 6000, 500_000)];
        let total = validator.total_duration(&segments);
        assert_eq!(total, Duration::from_secs(12));
    }

    #[test]
    fn test_validation_config_builder() {
        let config = ValidationConfig::new(Duration::from_secs(4))
            .with_tolerance(0.2)
            .with_keyframe_required(false);
        assert_eq!(config.target_duration, Duration::from_secs(4));
        assert!((config.duration_tolerance - 0.2).abs() < f64::EPSILON);
        assert!(!config.require_keyframe_start);
    }

    #[test]
    fn test_segment_metadata_builder() {
        let meta = SegmentMetadata::new(5, Duration::from_secs(6), 1000)
            .with_keyframe(false)
            .with_pts(5000);
        assert_eq!(meta.index, 5);
        assert!(!meta.starts_with_keyframe);
        assert_eq!(meta.pts_start_ms, 5000);
    }

    #[test]
    fn test_default_validation_config() {
        let config = ValidationConfig::default();
        assert_eq!(config.target_duration, Duration::from_secs(6));
        assert!((config.duration_tolerance - 0.1).abs() < f64::EPSILON);
        assert!(config.require_keyframe_start);
        assert!((config.min_duration_factor - 0.5).abs() < f64::EPSILON);
        assert!((config.max_duration_factor - 2.0).abs() < f64::EPSILON);
    }

    // --- Duration bounds tests (0.5x - 2x target) ---

    #[test]
    fn test_duration_within_bounds() {
        // 6s target, segment at 6s => within [3s, 12s]
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_duration_bounds(&seg(0, 6000, 500_000));
        assert!(result.is_ok());
    }

    #[test]
    fn test_duration_at_lower_bound() {
        // 6s target, segment at 3s (0.5x) => just within bounds
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_duration_bounds(&seg(0, 3000, 500_000));
        assert!(result.is_ok());
    }

    #[test]
    fn test_duration_at_upper_bound() {
        // 6s target, segment at 12s (2.0x) => just within bounds
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_duration_bounds(&seg(0, 12000, 500_000));
        assert!(result.is_ok());
    }

    #[test]
    fn test_duration_below_lower_bound() {
        // 6s target, segment at 2s => below 3s (0.5x)
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_duration_bounds(&seg(0, 2000, 500_000));
        assert!(result.is_err());
        let msg = result.expect_err("should be err");
        assert!(msg.contains("below minimum"));
    }

    #[test]
    fn test_duration_above_upper_bound() {
        // 6s target, segment at 13s => above 12s (2.0x)
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let result = validator.validate_duration_bounds(&seg(0, 13000, 500_000));
        assert!(result.is_err());
        let msg = result.expect_err("should be err");
        assert!(msg.contains("exceeds maximum"));
    }

    #[test]
    fn test_duration_bounds_custom_factors() {
        let config = ValidationConfig::new(Duration::from_secs(6))
            .with_min_duration_factor(0.8)
            .with_max_duration_factor(1.2);
        let validator = SegmentValidator::new(config);

        // 6s * 0.8 = 4.8s min, 6s * 1.2 = 7.2s max
        assert!(validator
            .validate_duration_bounds(&seg(0, 5000, 1000))
            .is_ok());
        assert!(validator
            .validate_duration_bounds(&seg(0, 7000, 1000))
            .is_ok());
        assert!(validator
            .validate_duration_bounds(&seg(0, 4000, 1000))
            .is_err());
        assert!(validator
            .validate_duration_bounds(&seg(0, 8000, 1000))
            .is_err());
    }

    #[test]
    fn test_validate_segment_hard_bounds_error() {
        // Duration bounds violations produce errors, not just warnings
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        // 1s segment => well below 0.5x (3s)
        let result = validator.validate_segment(&seg(0, 1000, 500_000));
        assert!(!result.is_valid);
        assert!(result.has_errors());
    }

    #[test]
    fn test_validate_segment_over_2x_error() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        // 15s segment => above 2.0x (12s)
        let result = validator.validate_segment(&seg(0, 15000, 500_000));
        assert!(!result.is_valid);
        assert!(result.has_errors());
    }

    #[test]
    fn test_validate_sequence_bounds() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let segments = vec![
            seg(0, 6000, 500_000),
            seg(1, 2000, 500_000), // too short
            seg(2, 6000, 500_000),
            seg(3, 15000, 500_000), // too long
        ];
        let violations = validator.validate_sequence_bounds(&segments);
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].0, 1);
        assert_eq!(violations[1].0, 3);
    }

    #[test]
    fn test_validate_sequence_bounds_all_valid() {
        let validator = SegmentValidator::new(ValidationConfig::new(Duration::from_secs(6)));
        let segments = vec![
            seg(0, 5000, 500_000),
            seg(1, 7000, 500_000),
            seg(2, 6000, 500_000),
        ];
        let violations = validator.validate_sequence_bounds(&segments);
        assert!(violations.is_empty());
    }

    #[test]
    fn test_min_duration_factor_clamped() {
        let config = ValidationConfig::new(Duration::from_secs(6)).with_min_duration_factor(5.0); // should clamp to 1.0
        assert!((config.min_duration_factor - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_max_duration_factor_min_bound() {
        let config = ValidationConfig::new(Duration::from_secs(6)).with_max_duration_factor(0.5); // should clamp to 1.0
        assert!((config.max_duration_factor - 1.0).abs() < f64::EPSILON);
    }
}