subtitler 2.6.1

Parse, convert, validate, edit, and generate subtitles in 15 formats (SRT, VTT, ASS/SSA, MicroDVD, SubViewer, TTML, SBV, LRC, SAMI, MPL2, SCC, EBU STL, DFXP, Whisper JSON). Full CLI included.
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
#![cfg(not(target_arch = "wasm32"))]
use smallvec::SmallVec;
use std::path::PathBuf;
use subtitler::ass;
use subtitler::model::{
  Subtitle, SubtitleFile, SubtitleFormat, ValidationIssue, frames_to_ms, ms_to_frames,
};
use subtitler::srt;
use subtitler::vtt;

fn fixture_dir() -> PathBuf {
  let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
  d.push("examples");
  d
}

// --- SRT Tests ---

#[tokio::test]
async fn test_srt_parse_example_file() {
  let mut path = fixture_dir();
  path.push("example.srt");
  let subtitles = srt::parse_file(&path).await.unwrap();
  assert_eq!(subtitles.subtitles().len(), 10);
  assert_eq!(subtitles.subtitles()[0].index, Some(1));
  assert_eq!(subtitles.subtitles()[0].start, 1000);
  assert_eq!(subtitles.subtitles()[0].end, 3500);
  assert_eq!(subtitles.subtitles()[0].text, "Hello! How are you today?");
  assert_eq!(subtitles.subtitles()[9].index, Some(10));
  assert!(
    subtitles
      .subtitles()
      .iter()
      .all(|s| s.start > 0 && s.end > s.start)
  );
}

#[tokio::test]
async fn test_srt_round_trip_full() {
  let mut path = fixture_dir();
  path.push("example.srt");
  let original = srt::parse_file(&path).await.unwrap();

  let out_path = fixture_dir().join("_test_round_trip.srt");
  srt::generate(original.subtitles(), &out_path, None)
    .await
    .unwrap();
  let round_tripped = srt::parse_file(&out_path).await.unwrap();
  std::fs::remove_file(&out_path).ok();

  assert_eq!(original.subtitles().len(), round_tripped.subtitles().len());
  for (a, b) in original
    .subtitles()
    .iter()
    .zip(round_tripped.subtitles().iter())
  {
    assert_eq!(a.start, b.start);
    assert_eq!(a.end, b.end);
    assert_eq!(a.text, b.text);
  }
}

#[tokio::test]
async fn test_srt_parse_content_empty() {
  let result = srt::parse_content("").unwrap();
  assert!(result.subtitles().is_empty());
}

#[tokio::test]
async fn test_srt_parse_content_only_header_like() {
  let content = "1\n00:00:01,000 --> 00:00:03,000\nx\n\n2\n00:00:04,000 --> 00:00:06,000\ny\n\n";
  let result = srt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 2);
}

#[tokio::test]
async fn test_srt_consecutive_blank_lines() {
  let content =
    "1\n00:00:01,000 --> 00:00:03,500\nHello\n\n\n2\n00:00:04,000 --> 00:00:06,500\nWorld\n\n";
  let result = srt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 2);
}

#[tokio::test]
async fn test_srt_empty_text() {
  let content = "1\n00:00:01,000 --> 00:00:03,500\n\n\n";
  let result = srt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 1);
  assert_eq!(result.subtitles()[0].text, "");
}

#[tokio::test]
async fn test_srt_leading_newline() {
  let content = "\n1\n00:00:01,000 --> 00:00:03,500\nHello\n\n";
  let result = srt::parse_content(content).unwrap();
  assert_eq!(result.subtitles()[0].text, "Hello");
}

#[tokio::test]
async fn test_srt_missing_index() {
  let content = "00:00:01,000 --> 00:00:03,500\nNo index here\n\n";
  let result = srt::parse_content(content).unwrap();
  assert_eq!(result.subtitles()[0].index, None);
}

#[tokio::test]
async fn test_srt_generate_empty() {
  let path = fixture_dir().join("_test_empty.srt");
  srt::generate(&[], &path, None).await.unwrap();
  let content = std::fs::read_to_string(&path).unwrap();
  std::fs::remove_file(&path).ok();
  assert_eq!(content, "");
}

#[tokio::test]
async fn test_srt_timestamp_error_message() {
  let err = srt::parse_content("1\nnot a time\n").unwrap_err();
  let msg = format!("{}", err);
  assert!(msg.contains("expected timestamp") || msg.contains("Invalid SRT"));
}

// --- VTT Tests ---

#[tokio::test]
async fn test_vtt_parse_example_file() {
  let mut path = fixture_dir();
  path.push("example.vtt");
  let subtitles = vtt::parse_file(&path).await.unwrap();
  assert_eq!(subtitles.subtitles().len(), 10);
  assert_eq!(subtitles.subtitles()[0].start, 1000);
  assert_eq!(subtitles.subtitles()[0].end, 3500);
  assert_eq!(
    subtitles.subtitles()[0].text,
    "Hi there! How have you been?"
  );
  assert!(subtitles.subtitles().iter().all(|s| s.end >= s.start));
}

#[tokio::test]
async fn test_vtt_round_trip_full() {
  let mut path = fixture_dir();
  path.push("example.vtt");
  let original = vtt::parse_file(&path).await.unwrap();

  let out_path = fixture_dir().join("_test_round_trip.vtt");
  vtt::generate(original.subtitles(), &out_path, None)
    .await
    .unwrap();
  let round_tripped = vtt::parse_file(&out_path).await.unwrap();
  std::fs::remove_file(&out_path).ok();

  assert_eq!(original.subtitles().len(), round_tripped.subtitles().len());
  for (a, b) in original
    .subtitles()
    .iter()
    .zip(round_tripped.subtitles().iter())
  {
    assert_eq!(a.start, b.start);
    assert_eq!(a.end, b.end);
    assert_eq!(a.text, b.text);
  }
}

#[tokio::test]
async fn test_vtt_parse_content_empty() {
  let result = vtt::parse_content("WEBVTT\n\n").unwrap();
  assert!(result.subtitles().is_empty());
}

#[tokio::test]
async fn test_vtt_consecutive_blank_lines() {
  let content = "WEBVTT\n\n\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n\n\n2\n00:00:04.000 --> 00:00:06.500\nWorld\n\n";
  let result = vtt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 2);
}

#[tokio::test]
async fn test_vtt_leading_newline() {
  let content = "\nWEBVTT\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
  let result = vtt::parse_content(content).unwrap();
  assert_eq!(result.subtitles()[0].text, "Hello");
}

#[tokio::test]
async fn test_vtt_cue_id_is_string() {
  let content = "WEBVTT\n\nchapter1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
  let result = vtt::parse_content(content).unwrap();
  assert_eq!(result.subtitles()[0].index, None);
}

#[tokio::test]
async fn test_vtt_generate_empty() {
  let path = fixture_dir().join("_test_empty.vtt");
  vtt::generate(&[], &path, None).await.unwrap();
  let content = std::fs::read_to_string(&path).unwrap();
  std::fs::remove_file(&path).ok();
  assert_eq!(content, "WEBVTT\n\n");
}

#[tokio::test]
async fn test_vtt_timestamp_error_message() {
  let err = vtt::parse_content("WEBVTT\n\n1\nnot a time\n").unwrap_err();
  let msg = format!("{}", err);
  assert!(msg.contains("expected timestamp") || msg.contains("Invalid"));
}

#[tokio::test]
async fn test_vtt_parse_with_bom() {
  let content = "\u{FEFF}WEBVTT\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
  let result = vtt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 1);
  assert_eq!(result.subtitles()[0].text, "Hello");
}

#[tokio::test]
async fn test_vtt_parse_with_note() {
  let content = "WEBVTT\n\nNOTE This is a comment\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
  let result = vtt::parse_content(content).unwrap();
  assert_eq!(result.subtitles().len(), 1);
  assert_eq!(result.subtitles()[0].text, "Hello");
}

#[tokio::test]
async fn test_vtt_parse_header_preserved() {
  let content =
    "WEBVTT\nKind: captions\nLanguage: en\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
  let (header, subtitles) = vtt::parse_content_full(content).unwrap();
  assert_eq!(subtitles.len(), 1);
  assert!(header.is_some());
  assert!(header.unwrap().contains("Kind: captions"));
}

// --- ASS Tests ---

#[test]
fn test_ass_parse_content() {
  let content = "[Script Info]\nTitle: Test\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\nStyle: Default,Arial,48,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,0:00:01.00,0:00:03.50,Default,,0,0,0,,Hello!\nDialogue: 0,0:00:04.00,0:00:06.50,Default,,0,0,0,,World!\n";
  let file = ass::parse_content(content).unwrap();
  let subs = file.subtitles();
  assert_eq!(subs.len(), 2);
  assert_eq!(subs[0].start, 1000);
  assert_eq!(subs[0].end, 3500);
  assert_eq!(subs[0].text, "Hello!");
  assert_eq!(subs[0].style.as_deref(), Some("Default"));
}

#[test]
fn test_ass_parse_empty_events() {
  let content = "[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: ...\nStyle: Default,Arial,48,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n";
  let file = ass::parse_content(content).unwrap();
  assert!(file.subtitles().is_empty());
}

#[test]
fn test_ass_to_string_round_trip() {
  let content = "[Script Info]\nTitle: RT\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\nStyle: Default,Arial,48,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,0:00:01.00,0:00:03.50,Default,,0,0,0,,Hello\nDialogue: 0,0:00:04.00,0:00:06.50,Default,,0,0,0,,World\n";
  let parsed = ass::parse_content(content).unwrap();
  let regenerated = parsed.to_string();
  let reparsed = ass::parse_content(&regenerated).unwrap();
  assert_eq!(reparsed.subtitles().len(), 2);
  assert_eq!(reparsed.subtitles()[0].start, 1000);
  assert_eq!(reparsed.subtitles()[0].text, "Hello");
  assert_eq!(reparsed.subtitles()[1].start, 4000);
  assert_eq!(reparsed.subtitles()[1].text, "World");
}

#[test]
fn test_ass_detect_format() {
  let data = b"[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nStyle: Default,...\n";
  assert_eq!(
    ass::detect_format(data),
    Some(subtitler::model::Format::Ass)
  );
}

#[test]
fn test_ass_detect_format_negative() {
  let data = b"WEBVTT\n\n";
  assert_eq!(ass::detect_format(data), None);
}

#[test]
fn test_ass_ssa_format() {
  let content = "[Script Info]\nScriptType: v4.00\n\n[V4 Styles]\nFormat: ...\nStyle: Default,Arial,48,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,0:00:01.00,0:00:03.50,Default,,0,0,0,,Hello\n";
  let file = ass::parse_content(content).unwrap();
  assert_eq!(file.subtitles().len(), 1);
}

// --- SRT Stringify Tests ---

#[tokio::test]
async fn test_srt_to_string() {
  let mut sub1 = Subtitle::new(1000, 3500, "Hello");
  sub1.index = Some(1);
  let mut sub2 = Subtitle::new(4000, 6500, "World");
  sub2.index = Some(2);
  let subtitles = vec![sub1, sub2];
  let output = srt::to_string(&subtitles);
  assert!(output.contains("00:00:01,000 --> 00:00:03,500"));
  assert!(output.contains("00:00:04,000 --> 00:00:06,500"));
  assert!(output.contains("Hello\n\n2\n"));
  assert!(output.contains("World\n"));
}

// --- VTT Stringify Tests ---

#[tokio::test]
async fn test_vtt_to_string() {
  let subtitles = vec![
    Subtitle::new(1000, 3500, "Hello"),
    Subtitle::new(4000, 6500, "World"),
  ];
  let output = vtt::to_string(&subtitles, None);
  assert!(output.starts_with("WEBVTT\n\n"));
  assert!(output.contains("00:00:01.000 --> 00:00:03.500"));
  assert!(output.contains("00:00:04.000 --> 00:00:06.500"));
}

// --- Utility Method Tests ---

#[test]
fn test_subtitle_file_sort() {
  let mut file = SubtitleFile::Srt(vec![
    Subtitle::new(5000, 7000, "c"),
    Subtitle::new(1000, 3000, "a"),
    Subtitle::new(3000, 5000, "b"),
  ]);
  file.sort();
  assert_eq!(file.subtitles()[0].start, 1000);
  assert_eq!(file.subtitles()[1].start, 3000);
  assert_eq!(file.subtitles()[2].start, 5000);
}

#[test]
fn test_subtitle_file_validate_clean() {
  let file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "a"),
    Subtitle::new(4000, 6000, "b"),
  ]);
  assert!(file.validate().is_empty());
}

#[test]
fn test_subtitle_file_validate_overlap() {
  let file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "a"),
    Subtitle::new(2000, 4000, "b"),
  ]);
  let issues = file.validate();
  assert_eq!(issues.len(), 1);
  assert!(matches!(issues[0], ValidationIssue::Overlap { .. }));
}

#[test]
fn test_subtitle_file_validate_negative_duration() {
  let file = SubtitleFile::Srt(vec![Subtitle::new(3000, 1000, "bad")]);
  let issues = file.validate();
  assert_eq!(issues.len(), 1);
  assert!(matches!(
    issues[0],
    ValidationIssue::NegativeDuration { .. }
  ));
}

#[test]
fn test_subtitle_file_validate_zero_duration() {
  let file = SubtitleFile::Srt(vec![Subtitle::new(1000, 1000, "bad")]);
  let issues = file.validate();
  assert_eq!(issues.len(), 1);
  assert!(matches!(issues[0], ValidationIssue::ZeroDuration { .. }));
}

#[test]
fn test_subtitle_file_merge_adjacent() {
  let mut file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "first"),
    Subtitle::new(3100, 5000, "second"),
    Subtitle::new(7000, 9000, "third"),
  ]);
  file.merge_adjacent(500);
  let subs = file.subtitles();
  assert_eq!(subs.len(), 2);
  assert_eq!(subs[0].text, "first\nsecond");
  assert_eq!(subs[0].end, 5000);
}

#[test]
fn test_subtitle_file_merge_noop() {
  let mut file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "a"),
    Subtitle::new(5000, 7000, "b"),
  ]);
  file.merge_adjacent(100);
  assert_eq!(file.subtitles().len(), 2);
}

#[test]
fn test_subtitle_file_split_long() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(
    1000,
    5000,
    "this is a very long subtitle that must be split into smaller pieces",
  )]);
  file.split_long(20);
  assert!(file.subtitles().len() >= 2);
  for sub in file.subtitles() {
    assert!(sub.text.chars().count() <= 20 || sub.text.len() <= 20);
  }
}

#[test]
fn test_subtitle_file_split_long_noop() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(1000, 3000, "short")]);
  file.split_long(20);
  assert_eq!(file.subtitles().len(), 1);
}

#[test]
fn test_subtitle_file_transform_framerate() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(1000, 3000, "test")]);
  file.transform_framerate(23.976, 25.0);
  let sub = &file.subtitles()[0];
  assert!(sub.start >= 1040 && sub.start <= 1045);
}

#[test]
fn test_subtitle_file_shift_all() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(1000, 3000, "test")]);
  file.shift_all(500);
  assert_eq!(file.subtitles()[0].start, 1500);
  assert_eq!(file.subtitles()[0].end, 3500);
}

#[test]
fn test_subtitle_file_shift_all_negative_clamp() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(1000, 3000, "test")]);
  file.shift_all(-2000);
  assert_eq!(file.subtitles()[0].start, 0);
  assert_eq!(file.subtitles()[0].end, 1000);
}

#[test]
fn test_subtitle_file_map() {
  let file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "hello"),
    Subtitle::new(4000, 6000, "world"),
  ]);
  let file = file.map(|sub| {
    sub.text = sub.text.to_uppercase();
  });
  assert_eq!(file.subtitles()[0].text, "HELLO");
  assert_eq!(file.subtitles()[1].text, "WORLD");
}

#[test]
fn test_subtitle_file_filter() {
  let file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "keep"),
    Subtitle::new(4000, 6000, "drop"),
  ]);
  let file = file.filter(|sub| sub.text == "keep");
  assert_eq!(file.subtitles().len(), 1);
  assert_eq!(file.subtitles()[0].text, "keep");
}

#[test]
fn test_subtitle_file_to_string() {
  let file = SubtitleFile::Srt(vec![Subtitle::new(1000, 3500, "Hello")]);
  let s = file.to_string();
  assert!(s.contains("00:00:01,000 --> 00:00:03,500"));
  assert!(s.contains("Hello"));
}

#[test]
fn test_frame_conversion_round_trip() {
  for ms in [0, 1000, 3600000] {
    for fps in [23.976, 24.0, 25.0, 29.97, 30.0] {
      let frames = ms_to_frames(ms, fps);
      let back = frames_to_ms(frames, fps);
      let diff = (back as i64 - ms as i64).abs();
      let tolerance = (1000.0 / fps).ceil() as i64;
      assert!(
        diff <= tolerance,
        "round-trip failed: {}ms -> {}f @ {}fps -> {}ms (diff={}, tolerance={})",
        ms,
        frames,
        fps,
        back,
        diff,
        tolerance
      );
    }
  }
}

#[test]
fn test_subtitle_chars_per_second() {
  let sub = Subtitle::new(0, 2000, "Hello World");
  let cps = sub.chars_per_second();
  assert!((cps - 5.5).abs() < 0.01);
}

#[test]
fn test_subtitle_duration_ms() {
  let sub = Subtitle::new(1000, 5000, "test");
  assert_eq!(sub.duration_ms(), 4000);
}

// --- Serde Tests ---

#[test]
fn test_subtitle_serde_round_trip() {
  let sub = Subtitle {
    index: Some(1),
    start: 1000,
    end: 3500,
    text: "Hello".to_string(),
    settings: None,
    text_parts: SmallVec::new(),
    style: None,
    actor: None,
    is_comment: false,
  };
  let json = serde_json::to_string(&sub).unwrap();
  let parsed: Subtitle = serde_json::from_str(&json).unwrap();
  assert_eq!(sub, parsed);

  let sub_with_style = Subtitle {
    style: Some("Custom".into()),
    actor: Some("Alice".into()),
    ..Subtitle::new(1000, 2000, "styled")
  };
  let json2 = serde_json::to_string_pretty(&sub_with_style).unwrap();
  let parsed2: Subtitle = serde_json::from_str(&json2).unwrap();
  assert_eq!(sub_with_style, parsed2);
}

// --- Format Detection Tests ---

#[test]
fn test_detect_format_srt() {
  assert_eq!(
    subtitler::detect_format(b"1\n00:00:01,000 --> 00:00:03,500\nHello\n\n"),
    Some(subtitler::model::Format::Srt)
  );
}

#[test]
fn test_detect_format_vtt() {
  assert_eq!(
    subtitler::detect_format(b"WEBVTT\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n"),
    Some(subtitler::model::Format::Vtt)
  );
}

#[test]
fn test_detect_format_ass() {
  assert_eq!(
    subtitler::detect_format(b"[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nStyle: ...\n"),
    Some(subtitler::model::Format::Ass)
  );
}

#[test]
fn test_detect_format_unknown() {
  assert_eq!(subtitler::detect_format(b"not a subtitle"), None);
  assert_eq!(subtitler::detect_format(b""), None);
}

// ── Duration enforcement tests ──

#[test]
fn test_enforce_min_duration() {
  let mut file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 1200, "short"), // 200ms → should extend to 1000ms
    Subtitle::new(2000, 5000, "ok"),
  ]);
  file.enforce_min_duration(1000);
  assert_eq!(file.subtitles()[0].end, 2000);
  assert_eq!(file.subtitles()[1].end, 5000);
}

#[test]
fn test_enforce_max_duration() {
  let mut file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 8000, "very long"), // 7000ms → trim to 3000ms
    Subtitle::new(9000, 10000, "short"),
  ]);
  file.enforce_max_duration(3000);
  assert_eq!(file.subtitles()[0].end, 4000);
  assert_eq!(file.subtitles()[1].end, 10000);
}

#[test]
fn test_auto_extend_for_cps() {
  let mut file = SubtitleFile::Srt(vec![Subtitle::new(
    0,
    500,
    "This is a very long subtitle text that needs more time",
  )]);
  file.auto_extend_for_cps(20.0);
  // 54 chars / 20 cps = 2.7s = 2700ms
  assert!(file.subtitles()[0].end >= 2700);
}

// ── Batch operation tests ──

#[test]
fn test_extract_range() {
  let file = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "first"),
    Subtitle::new(4000, 6000, "second"),
    Subtitle::new(7000, 9000, "third"),
  ]);
  let extracted = file.extract_range(2000, 7000);
  assert_eq!(extracted.len(), 2);
  assert_eq!(extracted[0].text, "first");
  assert_eq!(extracted[0].start, 2000); // clamped
  assert_eq!(extracted[1].text, "second");
}

#[test]
fn test_concatenate() {
  let mut file1 = SubtitleFile::Srt(vec![
    Subtitle::new(1000, 3000, "A"),
    Subtitle::new(4000, 6000, "B"),
  ]);
  let file2 = SubtitleFile::Srt(vec![Subtitle::new(1000, 3000, "C")]);
  file1.concatenate(&file2, 1000);
  assert_eq!(file1.subtitles().len(), 3);
  assert_eq!(file1.subtitles()[2].text, "C");
  assert_eq!(file1.subtitles()[2].start, 8000);
}

// ── Color conversion tests ──

#[test]
fn test_parse_ass_color() {
  let (r, g, b, a) = subtitler::model::parse_ass_color("&H00FFFFFF");
  assert_eq!(r, 255);
  assert_eq!(g, 255);
  assert_eq!(b, 255);
  assert_eq!(a, 0);
}

#[test]
fn test_format_ass_color() {
  let color = subtitler::model::format_ass_color(255, 128, 0, 0);
  assert_eq!(color, "&H000080FF");
}

#[test]
fn test_color_round_trip() {
  for (r, g, b, a) in [(255u8, 255, 255, 0), (0, 0, 0, 255), (128, 64, 32, 128)] {
    let formatted = subtitler::model::format_ass_color(r, g, b, a);
    let (r2, g2, b2, a2) = subtitler::model::parse_ass_color(&formatted);
    assert_eq!((r, g, b, a), (r2, g2, b2, a2));
  }
}

// ── ASS tag parsing tests ──

#[test]
fn test_ass_to_plaintext() {
  assert_eq!(
    subtitler::ass::ass_to_plaintext("{\\i1}Hello{\\i0} World"),
    "Hello World"
  );
  assert_eq!(
    subtitler::ass::ass_to_plaintext("Line 1\\NLine 2"),
    "Line 1\nLine 2"
  );
}

#[test]
fn test_parse_ass_tags_bold() {
  let parts = subtitler::ass::parse_ass_tags("{\\b1}Bold text{\\b0}");
  assert_eq!(parts.len(), 1);
  assert!(parts[0].bold());
  assert_eq!(parts[0].text, "Bold text");
}

#[test]
fn test_parse_ass_tags_italic() {
  let parts = subtitler::ass::parse_ass_tags("{\\i1}Italic{\\i0}");
  assert_eq!(parts.len(), 1);
  assert!(parts[0].italic());
}

// ── Normalize tests ──

#[test]
fn test_normalize_whitespace_integration() {
  assert_eq!(
    subtitler::normalize::normalize_whitespace("hello   world"),
    "hello world"
  );
}

#[test]
fn test_strip_hearing_impaired_integration() {
  assert_eq!(
    subtitler::normalize::strip_hearing_impaired("Hello (LAUGHS)"),
    "Hello"
  );
}

// ── Subtitle.plaintext test ──

#[test]
fn test_subtitle_plaintext() {
  let sub = Subtitle::new(0, 1000, "<b>Hello</b> {\\i1}World{\\i0}");
  assert_eq!(sub.plaintext(), "Hello World");
}