typecast-rust 0.3.0

Official Rust SDK for Typecast Text-to-Speech API
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
//! Unit tests for timestamp TTS types and captioning helpers.
//!
//! All fixture-based tests assert byte-for-byte equality against the shared
//! expected files in `test-fixtures/with-timestamps/expected/`.  These tests
//! never touch the real Typecast API.

use mockito::Server;
use serde_json;
use std::fs;
use std::path::PathBuf;
use std::time::Duration;
use typecast_rust::{
    timestamps::{TTSRequestWithTimestamps, TTSWithTimestampsResponse},
    ClientConfig, TTSModel, TypecastClient, TypecastError,
};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Locate the `test-fixtures/with-timestamps` directory by walking up from cwd.
fn fixture_dir() -> PathBuf {
    let mut dir: PathBuf = std::env::current_dir().unwrap();
    for _ in 0..8 {
        let cand = dir.join("test-fixtures").join("with-timestamps");
        if cand.is_dir() {
            return cand;
        }
        dir.pop();
    }
    panic!("test-fixtures/with-timestamps not found from {:?}", std::env::current_dir().unwrap());
}

fn load_fixture(name: &str) -> String {
    fs::read_to_string(fixture_dir().join(name))
        .unwrap_or_else(|e| panic!("failed to read fixture {}: {}", name, e))
}

fn load_expected(name: &str) -> String {
    fs::read_to_string(fixture_dir().join("expected").join(name))
        .unwrap_or_else(|e| panic!("failed to read expected {}: {}", name, e))
}

fn parse_fixture(name: &str) -> TTSWithTimestampsResponse {
    let json = load_fixture(name);
    serde_json::from_str(&json)
        .unwrap_or_else(|e| panic!("failed to parse fixture {}: {}", name, e))
}

fn make_client(server: &Server) -> TypecastClient {
    let config = ClientConfig::new("test-api-key")
        .base_url(server.url())
        .timeout(Duration::from_secs(5));
    TypecastClient::new(config).expect("client builds")
}

// ---------------------------------------------------------------------------
// SRT fixture tests
// ---------------------------------------------------------------------------

#[test]
fn to_srt_both_matches_expected() {
    let resp = parse_fixture("both.json");
    let actual = resp.to_srt().unwrap();
    let expected = load_expected("both.srt");
    assert_eq!(actual, expected, "SRT mismatch for both");
}

#[test]
fn to_srt_word_only_matches_expected() {
    let resp = parse_fixture("word_only.json");
    let actual = resp.to_srt().unwrap();
    let expected = load_expected("word_only.srt");
    assert_eq!(actual, expected, "SRT mismatch for word_only");
}

#[test]
fn to_srt_char_only_matches_expected() {
    let resp = parse_fixture("char_only.json");
    let actual = resp.to_srt().unwrap();
    let expected = load_expected("char_only.srt");
    assert_eq!(actual, expected, "SRT mismatch for char_only");
}

#[test]
fn to_srt_jpn_char_matches_expected() {
    let resp = parse_fixture("jpn_char.json");
    let actual = resp.to_srt().unwrap();
    let expected = load_expected("jpn_char.srt");
    assert_eq!(actual, expected, "SRT mismatch for jpn_char");
}

// ---------------------------------------------------------------------------
// VTT fixture tests
// ---------------------------------------------------------------------------

#[test]
fn to_vtt_both_matches_expected() {
    let resp = parse_fixture("both.json");
    let actual = resp.to_vtt().unwrap();
    let expected = load_expected("both.vtt");
    assert_eq!(actual, expected, "VTT mismatch for both");
}

#[test]
fn to_vtt_word_only_matches_expected() {
    let resp = parse_fixture("word_only.json");
    let actual = resp.to_vtt().unwrap();
    let expected = load_expected("word_only.vtt");
    assert_eq!(actual, expected, "VTT mismatch for word_only");
}

#[test]
fn to_vtt_char_only_matches_expected() {
    let resp = parse_fixture("char_only.json");
    let actual = resp.to_vtt().unwrap();
    let expected = load_expected("char_only.vtt");
    assert_eq!(actual, expected, "VTT mismatch for char_only");
}

#[test]
fn to_vtt_jpn_char_matches_expected() {
    let resp = parse_fixture("jpn_char.json");
    let actual = resp.to_vtt().unwrap();
    let expected = load_expected("jpn_char.vtt");
    assert_eq!(actual, expected, "VTT mismatch for jpn_char");
}

// ---------------------------------------------------------------------------
// Batch loop versions (documents which 4 fixtures are covered)
// ---------------------------------------------------------------------------

#[test]
fn to_srt_matches_all_fixtures() {
    for name in &["both", "word_only", "char_only", "jpn_char"] {
        let resp = parse_fixture(&format!("{}.json", name));
        let actual = resp.to_srt().unwrap();
        let expected = load_expected(&format!("{}.srt", name));
        assert_eq!(actual, expected, "SRT mismatch for {}", name);
    }
}

#[test]
fn to_vtt_matches_all_fixtures() {
    for name in &["both", "word_only", "char_only", "jpn_char"] {
        let resp = parse_fixture(&format!("{}.json", name));
        let actual = resp.to_vtt().unwrap();
        let expected = load_expected(&format!("{}.vtt", name));
        assert_eq!(actual, expected, "VTT mismatch for {}", name);
    }
}

// ---------------------------------------------------------------------------
// audio_bytes / save_audio
// ---------------------------------------------------------------------------

#[test]
fn audio_bytes_decodes_successfully() {
    let resp = parse_fixture("both.json");
    let bytes = resp.audio_bytes().unwrap();
    assert!(!bytes.is_empty(), "decoded audio bytes should be non-empty");
}

#[test]
fn audio_bytes_error_on_invalid_base64() {
    let resp = TTSWithTimestampsResponse {
        audio: "not!valid!base64!!!!".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 1.0,
        words: None,
        characters: None,
    };
    let err = resp.audio_bytes().unwrap_err();
    assert!(
        matches!(err, TypecastError::DecodeError(_)),
        "expected DecodeError, got {:?}",
        err
    );
}

#[test]
fn save_audio_writes_non_empty_file() {
    let resp = parse_fixture("both.json");
    let dir = std::env::temp_dir();
    let path = dir.join(format!("rust_tts_timestamps_test_{}.wav", std::process::id()));
    resp.save_audio(&path).unwrap();
    let meta = fs::metadata(&path).unwrap();
    assert!(meta.len() > 0, "written file should be non-empty");
    let _ = fs::remove_file(&path);
}

// ---------------------------------------------------------------------------
// Edge cases: no segments
// ---------------------------------------------------------------------------

#[test]
fn to_srt_errors_when_no_segments() {
    let resp = TTSWithTimestampsResponse {
        audio: "".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.0,
        words: None,
        characters: None,
    };
    let err = resp.to_srt().unwrap_err();
    assert!(
        matches!(err, TypecastError::CaptioningError(_)),
        "expected CaptioningError, got {:?}",
        err
    );
}

#[test]
fn to_vtt_errors_when_no_segments() {
    let resp = TTSWithTimestampsResponse {
        audio: "".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.0,
        words: None,
        characters: None,
    };
    let err = resp.to_vtt().unwrap_err();
    assert!(
        matches!(err, TypecastError::CaptioningError(_)),
        "expected CaptioningError, got {:?}",
        err
    );
}

// ---------------------------------------------------------------------------
// TTSRequestWithTimestamps builder
// ---------------------------------------------------------------------------

#[test]
fn request_builder_sets_all_fields() {
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30)
        .language("eng")
        .prompt(serde_json::json!({"emotion_type": "preset"}))
        .output(serde_json::json!({"audio_format": "wav"}))
        .seed(42);
    assert_eq!(req.voice_id, "tc_x");
    assert_eq!(req.text, "hello");
    assert_eq!(req.language.as_deref(), Some("eng"));
    assert!(req.prompt.is_some());
    assert!(req.output.is_some());
    assert_eq!(req.seed, Some(42));

    // Cover Debug + Clone
    let _ = format!("{req:?}");
    let _ = req.clone();
}

#[test]
fn request_serializes_without_skip_fields_when_none() {
    let req =
        TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let json = serde_json::to_string(&req).unwrap();
    assert!(!json.contains("language"));
    assert!(!json.contains("prompt"));
    assert!(!json.contains("output"));
    assert!(!json.contains("seed"));
}

// ---------------------------------------------------------------------------
// AlignmentSegment derives
// ---------------------------------------------------------------------------

#[test]
fn alignment_segment_derives() {
    use typecast_rust::timestamps::{AlignmentSegmentCharacter, AlignmentSegmentWord};

    let w = AlignmentSegmentWord {
        text: "hello".into(),
        start: 0.1,
        end: 0.4,
    };
    let _ = format!("{w:?}");
    let cloned = w.clone();
    assert_eq!(w, cloned);
    let json = serde_json::to_string(&w).unwrap();
    let parsed: AlignmentSegmentWord = serde_json::from_str(&json).unwrap();
    assert_eq!(parsed.text, "hello");

    let c = AlignmentSegmentCharacter {
        text: "A".into(),
        start: 0.0,
        end: 0.05,
    };
    let _ = format!("{c:?}");
    let _ = c.clone();
    assert_eq!(c, c.clone());
}

// ---------------------------------------------------------------------------
// Client method — mock-based integration tests
// ---------------------------------------------------------------------------

/// Build a minimal valid response JSON with a tiny valid Base64 WAV (4 bytes).
fn mock_ts_response_json() -> String {
    // "AAAA" in base64 decodes to three zero bytes — fine for unit tests.
    r#"{
        "audio": "AAAA",
        "audio_format": "wav",
        "audio_duration": 2.4,
        "words": [
            {"text": "Hello.", "start": 0.1, "end": 0.444},
            {"text": "How", "start": 1.271, "end": 1.6},
            {"text": "are", "start": 1.6, "end": 1.9},
            {"text": "you?", "start": 1.9, "end": 2.4}
        ],
        "characters": null
    }"#.to_string()
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_no_granularity() {
    let mut server = Server::new_async().await;
    let body = mock_ts_response_json();
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(&body)
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "Hello. How are you?", TTSModel::SsfmV30);
    let resp = client
        .text_to_speech_with_timestamps(&req, None)
        .await
        .unwrap();
    assert_eq!(resp.audio_format, "wav");
    assert!((resp.audio_duration - 2.4).abs() < 1e-9);
    assert!(resp.words.is_some());
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_word_granularity() {
    let mut server = Server::new_async().await;
    let body = mock_ts_response_json();
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .match_query(mockito::Matcher::UrlEncoded(
            "granularity".into(),
            "word".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(&body)
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let resp = client
        .text_to_speech_with_timestamps(&req, Some("word"))
        .await
        .unwrap();
    assert!(resp.words.is_some());
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_char_granularity() {
    let mut server = Server::new_async().await;
    let char_body = r#"{
        "audio": "AAAA",
        "audio_format": "wav",
        "audio_duration": 1.0,
        "words": null,
        "characters": [
            {"text": "H", "start": 0.0, "end": 0.1},
            {"text": "i", "start": 0.1, "end": 0.2}
        ]
    }"#;
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .match_query(mockito::Matcher::UrlEncoded(
            "granularity".into(),
            "char".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(char_body)
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "Hi", TTSModel::SsfmV30);
    let resp = client
        .text_to_speech_with_timestamps(&req, Some("char"))
        .await
        .unwrap();
    assert!(resp.characters.is_some());
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_invalid_granularity_is_validation_error() {
    let server = Server::new_async().await;
    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let err = client
        .text_to_speech_with_timestamps(&req, Some("sentence"))
        .await
        .unwrap_err();
    assert!(
        err.is_validation_error(),
        "expected ValidationError, got {:?}",
        err
    );
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_propagates_api_errors() {
    let mut server = Server::new_async().await;
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .with_status(401)
        .with_header("content-type", "application/json")
        .with_body(r#"{"detail":"bad key"}"#)
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let err = client
        .text_to_speech_with_timestamps(&req, None)
        .await
        .unwrap_err();
    assert!(err.is_unauthorized());
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_propagates_server_error() {
    let mut server = Server::new_async().await;
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .with_status(500)
        .with_header("content-type", "text/plain")
        .with_body("internal boom")
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let err = client
        .text_to_speech_with_timestamps(&req, None)
        .await
        .unwrap_err();
    assert!(err.is_server_error());
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_propagates_network_error() {
    // Use a port that nobody is listening on to trigger a connection-refused send error.
    use typecast_rust::ClientConfig;
    let config = ClientConfig::new("test-api-key")
        .base_url("http://127.0.0.1:1") // port 1 is always refused
        .timeout(Duration::from_secs(2));
    let client = TypecastClient::new(config).expect("client builds");
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let err = client
        .text_to_speech_with_timestamps(&req, None)
        .await
        .unwrap_err();
    assert!(matches!(err, TypecastError::HttpError(_)), "expected HttpError on connection failure, got {err:?}");
}

#[tokio::test]
async fn client_text_to_speech_with_timestamps_propagates_bad_json() {
    // 200 OK but body is not valid JSON → DecodeError on response.json().
    let mut server = Server::new_async().await;
    let _m = server
        .mock("POST", "/v1/text-to-speech/with-timestamps")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body("this is not json at all")
        .create_async()
        .await;

    let client = make_client(&server);
    let req = TTSRequestWithTimestamps::new("tc_x", "hello", TTSModel::SsfmV30);
    let err = client
        .text_to_speech_with_timestamps(&req, None)
        .await
        .unwrap_err();
    assert!(matches!(err, TypecastError::DecodeError(_)), "expected DecodeError, got {err:?}");
}

// ---------------------------------------------------------------------------
// single-word fallback (words.len() == 1, no characters)
// ---------------------------------------------------------------------------

#[test]
fn to_srt_single_word_fallback() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.5,
        words: Some(vec![AlignmentSegmentWord {
            text: "Hello.".to_string(),
            start: 0.0,
            end: 0.5,
        }]),
        characters: None,
    };
    let srt = resp.to_srt().expect("single-word SRT should succeed");
    assert!(srt.contains("Hello."), "SRT should contain the word text");
    assert!(srt.starts_with("1\n"), "SRT should start with cue index 1");
}

#[test]
fn to_vtt_single_word_fallback() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.3,
        words: Some(vec![AlignmentSegmentWord {
            text: "Hi.".to_string(),
            start: 0.0,
            end: 0.3,
        }]),
        characters: None,
    };
    let vtt = resp.to_vtt().expect("single-word VTT should succeed");
    assert!(vtt.starts_with("WEBVTT\n\n"), "VTT should start with WEBVTT header");
    assert!(vtt.contains("Hi."), "VTT should contain the word text");
}

// ---------------------------------------------------------------------------
// groupIntoCues: hard-cap flush (time-based and char-based)
// ---------------------------------------------------------------------------

#[test]
fn to_srt_hard_cap_by_time_produces_multiple_cues() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    // Words span > 7.0 s in total — triggers a time-based hard cap flush
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 8.0,
        words: Some(vec![
            AlignmentSegmentWord { text: "Word1".to_string(), start: 0.0, end: 2.0 },
            AlignmentSegmentWord { text: "Word2".to_string(), start: 2.0, end: 4.0 },
            AlignmentSegmentWord { text: "Word3".to_string(), start: 4.0, end: 6.0 },
            // Adding Word4 with end=8.0 makes span (8.0 - 0.0) = 8.0 > 7.0
            AlignmentSegmentWord { text: "Word4".to_string(), start: 6.0, end: 8.0 },
        ]),
        characters: None,
    };
    let srt = resp.to_srt().expect("hard-cap SRT should succeed");
    // Should have at least cue index "2"
    assert!(srt.contains("2\n"), "expected at least 2 cues after time hard-cap:\n{srt}");
}

#[test]
fn to_srt_hard_cap_by_chars_produces_multiple_cues() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    // 4 × "AAAAAAAAAA" joined with spaces = 43 chars > MAX_CAPTION_CHARS=42
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 2.0,
        words: Some(vec![
            AlignmentSegmentWord { text: "AAAAAAAAAA".to_string(), start: 0.0, end: 0.5 },
            AlignmentSegmentWord { text: "AAAAAAAAAA".to_string(), start: 0.5, end: 1.0 },
            AlignmentSegmentWord { text: "AAAAAAAAAA".to_string(), start: 1.0, end: 1.5 },
            AlignmentSegmentWord { text: "AAAAAAAAAA".to_string(), start: 1.5, end: 2.0 },
        ]),
        characters: None,
    };
    let srt = resp.to_srt().expect("char hard-cap SRT should succeed");
    assert!(srt.contains("2\n"), "expected at least 2 cues after char hard-cap:\n{srt}");
}

// ---------------------------------------------------------------------------
// groupIntoCues: final flush without sentence terminator
// ---------------------------------------------------------------------------

#[test]
fn to_srt_final_flush_without_sentence_terminator() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    // Words without sentence terminator — tests the trailing flush
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 1.0,
        words: Some(vec![
            AlignmentSegmentWord { text: "Hello".to_string(), start: 0.0, end: 0.5 },
            AlignmentSegmentWord { text: "World".to_string(), start: 0.5, end: 1.0 },
        ]),
        characters: None,
    };
    let srt = resp.to_srt().expect("final flush SRT should succeed");
    assert!(srt.contains("Hello World"), "SRT should contain the joined text");
    // Only one cue (no sentence split)
    assert!(!srt.contains("2\n"), "expected exactly 1 cue");
}

// ---------------------------------------------------------------------------
// format_captions: empty cues path (all segment text is empty)
// ---------------------------------------------------------------------------

#[test]
fn to_srt_all_empty_text_segments_errors() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 1.0,
        words: Some(vec![
            AlignmentSegmentWord { text: "".to_string(), start: 0.0, end: 0.5 },
            AlignmentSegmentWord { text: "".to_string(), start: 0.5, end: 1.0 },
        ]),
        characters: None,
    };
    let err = resp.to_srt().unwrap_err();
    assert!(
        matches!(err, TypecastError::CaptioningError(_)),
        "expected CaptioningError, got {:?}",
        err
    );
}

#[test]
fn to_vtt_all_empty_text_segments_errors() {
    use typecast_rust::timestamps::AlignmentSegmentWord;
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 1.0,
        words: Some(vec![
            AlignmentSegmentWord { text: "".to_string(), start: 0.0, end: 0.5 },
            AlignmentSegmentWord { text: "".to_string(), start: 0.5, end: 1.0 },
        ]),
        characters: None,
    };
    let err = resp.to_vtt().unwrap_err();
    assert!(
        matches!(err, TypecastError::CaptioningError(_)),
        "expected CaptioningError, got {:?}",
        err
    );
}

// ---------------------------------------------------------------------------
// save_audio and audio_bytes
// ---------------------------------------------------------------------------

#[test]
fn save_audio_writes_to_file() {
    let path = std::env::temp_dir().join("typecast_rust_test_save_audio.wav");
    let resp = TTSWithTimestampsResponse {
        // "AAAA" decodes to 3 zero bytes
        audio: "AAAA".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.0,
        words: None,
        characters: None,
    };
    resp.save_audio(&path).expect("save_audio should succeed");
    assert!(path.exists(), "output file should exist");
    let bytes = std::fs::read(&path).unwrap();
    assert_eq!(bytes.len(), 3, "decoded 'AAAA' should be 3 bytes");
    let _ = std::fs::remove_file(&path);
}

#[test]
fn save_audio_propagates_decode_error() {
    // When audio contains invalid base64, save_audio must propagate the error.
    let path = std::env::temp_dir().join("typecast_rust_test_save_audio_error.wav");
    let resp = TTSWithTimestampsResponse {
        audio: "!!!invalid-base64!!!".to_string(),
        audio_format: "wav".to_string(),
        audio_duration: 0.0,
        words: None,
        characters: None,
    };
    assert!(
        resp.save_audio(&path).is_err(),
        "save_audio should propagate audio_bytes() decode error"
    );
}

#[test]
fn save_audio_propagates_write_error() {
    // Writing to a nonexistent directory should cause fs::write to fail.
    let path = std::env::temp_dir()
        .join("typecast_rust_nonexistent_dir_xyzzy_12345")
        .join("output.wav");
    let resp = TTSWithTimestampsResponse {
        audio: "AAAA".to_string(), // valid base64
        audio_format: "wav".to_string(),
        audio_duration: 0.0,
        words: None,
        characters: None,
    };
    assert!(
        resp.save_audio(&path).is_err(),
        "save_audio should propagate fs::write error when parent dir does not exist"
    );
}