use subtitler::model::{Subtitle, SubtitleFile, SubtitleFormat, ValidationIssue};
#[test]
fn validate_detects_overlap_in_unsorted_input() {
let file = SubtitleFile::Srt(vec![
Subtitle::new(2000, 5000, "A overlaps B"), Subtitle::new(1000, 4000, "B first"), ]);
let issues = file.validate();
assert!(
issues
.iter()
.any(|i| matches!(i, ValidationIssue::Overlap { .. })),
"expected an Overlap issue on unsorted input, got: {:?}",
issues
);
}
#[test]
fn validate_sorted_clean_input_has_no_overlap() {
let file = SubtitleFile::Srt(vec![
Subtitle::new(1000, 3000, "first"),
Subtitle::new(4000, 6000, "second"),
]);
let overlaps: Vec<_> = overlap_issues(&file);
assert!(overlaps.is_empty());
}
#[test]
fn validate_sorted_overlapping_input_detected() {
let file = SubtitleFile::Srt(vec![
Subtitle::new(1000, 3000, "first"),
Subtitle::new(2000, 4000, "overlaps"),
]);
assert!(!overlap_issues(&file).is_empty());
}
#[test]
fn validate_detects_overlap_skipped_after_break() {
let file = SubtitleFile::Srt(vec![
Subtitle::new(0, 2000, "A"), Subtitle::new(5000, 6000, "B"), Subtitle::new(1000, 1500, "C"), ]);
let overlaps = overlap_issues(&file);
assert!(
overlaps.iter().any(|i| matches!(
i,
ValidationIssue::Overlap {
index_a: 0,
index_b: 2,
..
}
)),
"expected Overlap between original indices 0 and 2, got: {:?}",
overlaps
);
}
fn overlap_issues(file: &SubtitleFile) -> Vec<ValidationIssue> {
file
.validate()
.iter()
.filter(|i| matches!(i, ValidationIssue::Overlap { .. }))
.cloned()
.collect()
}
#[test]
fn srt_output_uses_positional_indices() {
let mut a = Subtitle::new(1000, 2000, "first");
a.index = Some(99);
let mut b = Subtitle::new(3000, 4000, "second");
b.index = Some(1);
let out = subtitler::srt::to_string(&[a, b]);
assert!(
out.starts_with("1\n00:00:01,000 --> 00:00:02,000\nfirst"),
"expected positional index 1 first, got:\n{out}"
);
assert!(
out.contains("\n2\n00:00:03,000 --> 00:00:04,000\nsecond"),
"expected positional index 2 second, got:\n{out}"
);
}
#[test]
fn vtt_output_uses_positional_indices() {
let mut a = Subtitle::new(1000, 2000, "first");
a.index = Some(42);
let out = subtitler::vtt::to_string(&[a], None);
assert!(
out.contains("\n1\n00:00:01.000 --> 00:00:02.000\nfirst"),
"expected positional index 1, got:\n{out}"
);
}