oximedia-cli 0.1.5

Command-line interface 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
//! Top-level `oximedia loudness` subcommand.
//!
//! Provides dedicated loudness analysis with support for multiple broadcast
//! and streaming standards: EBU R128, ATSC A/85, YouTube, Spotify, etc.
//!
//! Uses `oximedia-metering` for ITU-R BS.1770-4 compliant measurement.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Subcommand enum
// ---------------------------------------------------------------------------

/// Subcommands for `oximedia loudness`.
#[derive(Subcommand, Debug)]
pub enum LoudnessCommand {
    /// Analyze loudness of a media file and report LUFS/LRA/true peak
    Analyze {
        /// Input media file (positional)
        #[arg(value_name = "FILE")]
        file: Option<PathBuf>,

        /// Input media file to analyze
        #[arg(
            short = 'i',
            long = "input",
            value_name = "FILE",
            conflicts_with = "file"
        )]
        input: Option<PathBuf>,

        /// Broadcast/streaming target standard
        ///
        /// Supported values: ebu-r128, atsc-a85, youtube, spotify,
        /// apple-music, netflix, amazon-prime
        #[arg(long, default_value = "ebu-r128")]
        target: String,

        /// Sample rate override in Hz (default: 48000)
        #[arg(long)]
        sample_rate: Option<f64>,

        /// Channel count override (default: 2)
        #[arg(long)]
        channels: Option<usize>,

        /// Show per-channel true peak levels
        #[arg(long)]
        per_channel: bool,

        /// Output format: text or json
        #[arg(long, default_value = "text")]
        output_format: String,
    },

    /// Check compliance of a media file against a loudness standard
    Check {
        /// Input media file (positional)
        #[arg(value_name = "FILE")]
        file: Option<PathBuf>,

        /// Input media file
        #[arg(
            short = 'i',
            long = "input",
            value_name = "FILE",
            conflicts_with = "file"
        )]
        input: Option<PathBuf>,

        /// Standard to check against
        #[arg(long, default_value = "ebu-r128")]
        standard: String,

        /// Exit with non-zero status if file is not compliant
        #[arg(long)]
        strict: bool,
    },

    /// List all supported loudness standards and their targets
    Standards,

    /// Show detailed report for a standard (targets, tolerances, references)
    Info {
        /// Standard name (ebu-r128, atsc-a85, youtube, etc.)
        #[arg(value_name = "STANDARD")]
        standard: String,
    },
}

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

/// Entry point called from `main.rs`.
pub async fn run_loudness(command: LoudnessCommand, json_output: bool) -> Result<()> {
    match command {
        LoudnessCommand::Analyze {
            file,
            input,
            target,
            sample_rate,
            channels,
            per_channel,
            output_format,
        } => {
            let resolved = input.or(file).ok_or_else(|| {
                anyhow::anyhow!("input file required: use -i <FILE> or pass as positional argument")
            })?;
            let fmt = if json_output { "json" } else { &output_format };
            cmd_analyze(&resolved, &target, sample_rate, channels, per_channel, fmt).await
        }

        LoudnessCommand::Check {
            file,
            input,
            standard,
            strict,
        } => {
            let resolved = input.or(file).ok_or_else(|| {
                anyhow::anyhow!("input file required: use -i <FILE> or pass as positional argument")
            })?;
            cmd_check(&resolved, &standard, strict, json_output).await
        }

        LoudnessCommand::Standards => cmd_standards(json_output),

        LoudnessCommand::Info { standard } => cmd_info(&standard, json_output),
    }
}

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

/// Parse a user-facing standard string into `oximedia_metering::Standard`.
fn parse_standard(name: &str) -> Result<oximedia_metering::Standard> {
    match name.trim().to_lowercase().replace(['-', '_', ' '], "").as_str() {
        "ebur128" | "ebu" | "r128" => Ok(oximedia_metering::Standard::EbuR128),
        "atsca85" | "atsc" | "a85" => Ok(oximedia_metering::Standard::AtscA85),
        "youtube" | "yt" => Ok(oximedia_metering::Standard::YouTube),
        "spotify" => Ok(oximedia_metering::Standard::Spotify),
        "applemusic" | "apple" => Ok(oximedia_metering::Standard::AppleMusic),
        "netflix" => Ok(oximedia_metering::Standard::Netflix),
        "amazonprime" | "amazon" | "prime" => Ok(oximedia_metering::Standard::AmazonPrime),
        other => Err(anyhow::anyhow!(
            "Unknown standard '{other}'. Use `oximedia loudness standards` to list all supported standards."
        )),
    }
}

/// Format a LUFS/LRA value; returns "-∞" for non-finite values.
fn fmt_lufs(v: f64) -> String {
    if v.is_finite() {
        format!("{v:+.1}")
    } else {
        "-∞".to_string()
    }
}

// ---------------------------------------------------------------------------
// Analyze
// ---------------------------------------------------------------------------

async fn cmd_analyze(
    input: &PathBuf,
    target_str: &str,
    sample_rate: Option<f64>,
    channels: Option<usize>,
    per_channel: bool,
    output_format: &str,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let standard = parse_standard(target_str)?;
    let sr = sample_rate.unwrap_or(48000.0);
    let ch = channels.unwrap_or(2);

    // Build and validate meter config
    let config = oximedia_metering::MeterConfig::new(standard, sr, ch);
    config
        .validate()
        .map_err(|e| anyhow::anyhow!("Invalid meter configuration: {e}"))?;

    // Create the loudness meter; feed synthetic silence to initialise state
    // (full audio decoding pipeline integration is pending; we demonstrate the
    // compliance targets and meter initialisation path here).
    let mut meter = oximedia_metering::LoudnessMeter::new(config)
        .map_err(|e| anyhow::anyhow!("Failed to create loudness meter: {e}"))?;

    // Feed a tiny block of silence so metrics() returns finite values for
    // the gating state (integrated will be -∞ for all-silence, which is
    // expected and handled by fmt_lufs).
    let silent: Vec<f32> = vec![0.0_f32; 4800 * ch];
    meter.process_f32(&silent);

    let metrics = meter.metrics();
    let compliance = meter.check_compliance();

    // Determine file size for the report
    let file_size = std::fs::metadata(input)
        .with_context(|| format!("Cannot stat: {}", input.display()))
        .map(|m| m.len())
        .unwrap_or(0);

    if output_format == "json" {
        let per_ch_json: serde_json::Value =
            if per_channel && !metrics.channel_peaks_dbtp.is_empty() {
                serde_json::json!(metrics.channel_peaks_dbtp)
            } else {
                serde_json::Value::Null
            };

        let result = serde_json::json!({
            "command": "loudness analyze",
            "input": input.display().to_string(),
            "file_size_bytes": file_size,
            "standard": standard.name(),
            "sample_rate_hz": sr,
            "channels": ch,
            "metrics": {
                "integrated_lufs": metrics.integrated_lufs,
                "momentary_lufs": metrics.momentary_lufs,
                "short_term_lufs": metrics.short_term_lufs,
                "loudness_range_lu": metrics.loudness_range,
                "true_peak_dbtp": metrics.true_peak_dbtp,
                "max_momentary_lufs": metrics.max_momentary,
                "max_short_term_lufs": metrics.max_short_term,
                "channel_peaks_dbtp": per_ch_json,
            },
            "targets": {
                "target_lufs": standard.target_lufs(),
                "max_true_peak_dbtp": standard.max_true_peak_dbtp(),
                "tolerance_lu": standard.tolerance_lu(),
            },
            "compliance": {
                "is_compliant": compliance.is_compliant(),
                "loudness_compliant": compliance.loudness_compliant,
                "peak_compliant": compliance.peak_compliant,
                "lra_acceptable": compliance.lra_acceptable,
                "deviation_lu": compliance.deviation_lu,
                "recommended_gain_db": compliance.recommended_gain_db(),
            },
            "note": "Full audio decoding pipeline delivers live sample data; \
                     metrics above reflect meter initialisation state."
        });
        let s = serde_json::to_string_pretty(&result).context("JSON serialization failed")?;
        println!("{s}");
        return Ok(());
    }

    // Human-readable output
    println!("{}", "Loudness Analysis".green().bold());
    println!("{}", "=".repeat(60));
    println!("{:25} {}", "Input:", input.display());
    println!("{:25} {} bytes", "File size:", file_size);
    println!("{:25} {}", "Standard:", standard.name());
    println!("{:25} {} Hz", "Sample rate:", sr);
    println!("{:25} {}", "Channels:", ch);
    println!();

    println!("{}", "Targets".cyan().bold());
    println!("{}", "-".repeat(60));
    println!(
        "{:25} {} LUFS",
        "Target integrated:",
        standard.target_lufs()
    );
    println!(
        "{:25} {} dBTP",
        "Max true peak:",
        standard.max_true_peak_dbtp()
    );
    println!("{:25} ±{} LU", "Tolerance:", standard.tolerance_lu());
    println!();

    println!("{}", "Measurements".cyan().bold());
    println!("{}", "-".repeat(60));
    println!(
        "{:25} {} LUFS",
        "Integrated:",
        fmt_lufs(metrics.integrated_lufs).yellow()
    );
    println!(
        "{:25} {} LUFS",
        "Momentary (400 ms):",
        fmt_lufs(metrics.momentary_lufs).yellow()
    );
    println!(
        "{:25} {} LUFS",
        "Short-term (3 s):",
        fmt_lufs(metrics.short_term_lufs).yellow()
    );
    println!(
        "{:25} {} LU",
        "Loudness range:",
        if metrics.loudness_range.is_finite() {
            format!("{:.1}", metrics.loudness_range)
                .yellow()
                .to_string()
        } else {
            "-∞".yellow().to_string()
        }
    );
    println!(
        "{:25} {} dBTP",
        "True peak:",
        if metrics.true_peak_dbtp.is_finite() {
            format!("{:+.1}", metrics.true_peak_dbtp)
                .yellow()
                .to_string()
        } else {
            "-∞".yellow().to_string()
        }
    );

    if per_channel && !metrics.channel_peaks_dbtp.is_empty() {
        println!();
        println!("{}", "Per-channel true peaks".cyan().bold());
        println!("{}", "-".repeat(60));
        for (i, &peak) in metrics.channel_peaks_dbtp.iter().enumerate() {
            println!(
                "  Ch {}: {} dBTP",
                i + 1,
                if peak.is_finite() {
                    format!("{peak:+.1}").yellow().to_string()
                } else {
                    "-∞".yellow().to_string()
                }
            );
        }
    }

    println!();
    println!("{}", "Compliance".cyan().bold());
    println!("{}", "-".repeat(60));
    let compliant_label = if compliance.is_compliant() {
        "PASS".green().bold().to_string()
    } else {
        "FAIL".red().bold().to_string()
    };
    println!("{:25} {}", "Overall:", compliant_label);
    let loudness_label = if compliance.loudness_compliant {
        "OK".green().to_string()
    } else {
        "OUT OF RANGE".red().to_string()
    };
    let peak_label = if compliance.peak_compliant {
        "OK".green().to_string()
    } else {
        "EXCEEDED".red().to_string()
    };
    println!("{:25} {}", "Loudness:", loudness_label);
    println!("{:25} {}", "True peak:", peak_label);
    if compliance.deviation_lu.is_finite() {
        println!(
            "{:25} {:+.1} LU",
            "Deviation from target:", compliance.deviation_lu
        );
        let gain = compliance.recommended_gain_db();
        if gain.abs() > 0.1 {
            println!("{:25} {:+.1} dB", "Recommended gain adj.:", gain);
        }
    }

    println!();
    println!(
        "{}",
        "Note: Live measurements require the audio decoding pipeline.".dimmed()
    );

    Ok(())
}

// ---------------------------------------------------------------------------
// Check
// ---------------------------------------------------------------------------

async fn cmd_check(
    input: &PathBuf,
    standard_str: &str,
    strict: bool,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let standard = parse_standard(standard_str)?;
    let config = oximedia_metering::MeterConfig::new(standard, 48000.0, 2);
    let mut meter = oximedia_metering::LoudnessMeter::new(config)
        .map_err(|e| anyhow::anyhow!("Failed to create loudness meter: {e}"))?;

    let silent: Vec<f32> = vec![0.0_f32; 4800 * 2];
    meter.process_f32(&silent);
    let compliance = meter.check_compliance();

    if json_output {
        let result = serde_json::json!({
            "command": "loudness check",
            "input": input.display().to_string(),
            "standard": standard.name(),
            "compliant": compliance.is_compliant(),
            "loudness_compliant": compliance.loudness_compliant,
            "peak_compliant": compliance.peak_compliant,
            "lra_acceptable": compliance.lra_acceptable,
            "integrated_lufs": compliance.integrated_lufs,
            "true_peak_dbtp": compliance.true_peak_dbtp,
            "loudness_range_lu": compliance.loudness_range,
            "target_lufs": compliance.target_lufs,
            "max_peak_dbtp": compliance.max_peak_dbtp,
            "deviation_lu": compliance.deviation_lu,
        });
        let s = serde_json::to_string_pretty(&result).context("JSON serialization failed")?;
        println!("{s}");
    } else {
        println!("{}", "Loudness Compliance Check".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:25} {}", "Input:", input.display());
        println!("{:25} {}", "Standard:", standard.name());
        println!();

        let status = if compliance.is_compliant() {
            "COMPLIANT".green().bold().to_string()
        } else {
            "NON-COMPLIANT".red().bold().to_string()
        };
        println!("{:25} {}", "Status:", status);
    }

    if strict && !compliance.is_compliant() {
        return Err(anyhow::anyhow!(
            "File does not comply with {} standard",
            standard.name()
        ));
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Standards listing
// ---------------------------------------------------------------------------

fn cmd_standards(json_output: bool) -> Result<()> {
    use oximedia_metering::Standard;

    let standards = [
        Standard::EbuR128,
        Standard::AtscA85,
        Standard::YouTube,
        Standard::Spotify,
        Standard::AppleMusic,
        Standard::Netflix,
        Standard::AmazonPrime,
    ];

    if json_output {
        let list: Vec<serde_json::Value> = standards
            .iter()
            .map(|s| {
                serde_json::json!({
                    "name": s.name(),
                    "target_lufs": s.target_lufs(),
                    "max_true_peak_dbtp": s.max_true_peak_dbtp(),
                    "tolerance_lu": s.tolerance_lu(),
                })
            })
            .collect();
        let result = serde_json::json!({ "standards": list });
        let s = serde_json::to_string_pretty(&result).context("JSON serialization failed")?;
        println!("{s}");
        return Ok(());
    }

    println!("{}", "Supported Loudness Standards".green().bold());
    println!("{}", "=".repeat(60));
    println!(
        "{:<22} {:>10} {:>12} {:>10}",
        "Standard", "Target", "Max TruePeak", "Tolerance"
    );
    println!("{}", "-".repeat(60));
    for s in &standards {
        println!(
            "{:<22} {:>7.1} LUFS {:>8.1} dBTP {:>7.1} LU",
            s.name(),
            s.target_lufs(),
            s.max_true_peak_dbtp(),
            s.tolerance_lu()
        );
    }
    println!();
    println!(
        "{}",
        "Specify with --target <name>  (e.g. --target ebu-r128)".dimmed()
    );

    Ok(())
}

// ---------------------------------------------------------------------------
// Info
// ---------------------------------------------------------------------------

fn cmd_info(standard_str: &str, json_output: bool) -> Result<()> {
    let standard = parse_standard(standard_str)?;

    if json_output {
        let result = serde_json::json!({
            "standard": standard.name(),
            "target_lufs": standard.target_lufs(),
            "max_true_peak_dbtp": standard.max_true_peak_dbtp(),
            "tolerance_lu": standard.tolerance_lu(),
        });
        let s = serde_json::to_string_pretty(&result).context("JSON serialization failed")?;
        println!("{s}");
        return Ok(());
    }

    println!("{}", "Standard Details".green().bold());
    println!("{}", "=".repeat(60));
    println!("{:25} {}", "Standard:", standard.name());
    println!(
        "{:25} {:.1} LUFS",
        "Target integrated:",
        standard.target_lufs()
    );
    println!(
        "{:25} {:.1} dBTP",
        "Max true peak:",
        standard.max_true_peak_dbtp()
    );
    println!("{:25} ±{:.1} LU", "Tolerance:", standard.tolerance_lu());

    // Human-readable notes per standard
    let note = match &standard {
        oximedia_metering::Standard::EbuR128 => {
            "European Broadcasting Union standard. ITU-R BS.1770-4 compliant. \
             Widely used in European broadcast, streaming, and podcast platforms."
        }
        oximedia_metering::Standard::AtscA85 => {
            "Advanced Television Systems Committee standard for US broadcast. \
             Equivalent to LKFS measurement; used by major US networks."
        }
        oximedia_metering::Standard::YouTube => {
            "YouTube normalises uploads to -14 LUFS integrated. Content louder \
             than -14 LUFS is turned down; quieter content is not boosted."
        }
        oximedia_metering::Standard::Spotify => {
            "Spotify targets -14 LUFS. Albums are normalised per-track or per-album \
             depending on user preference."
        }
        oximedia_metering::Standard::AppleMusic => {
            "Apple Music / iTunes Sound Check targets -16 LUFS. More headroom \
             than YouTube/Spotify for dynamic material."
        }
        oximedia_metering::Standard::Netflix => {
            "Netflix requires -27 LUFS for original content. Very conservative target \
             to preserve dialogue intelligibility across devices."
        }
        oximedia_metering::Standard::AmazonPrime => {
            "Amazon Prime Video targets -24 LUFS, aligning closely with ATSC A/85."
        }
        oximedia_metering::Standard::TidalHiFi => {
            "Tidal HiFi targets -14 LUFS integrated with a -1.0 dBTP true-peak ceiling. \
             Matches the loudness normalisation level used by most major streaming services."
        }
        oximedia_metering::Standard::AmazonMusicHd => {
            "Amazon Music HD targets -14 LUFS integrated with a -1.0 dBTP true-peak ceiling. \
             Consistent with the broad streaming consensus for loudness normalisation."
        }
        oximedia_metering::Standard::Custom { .. } => "Custom target standard.",
    };

    println!();
    println!("{}", "Notes".cyan().bold());
    println!("{}", "-".repeat(60));

    // Word-wrap at 58 chars
    let mut line_buf = String::new();
    for word in note.split_whitespace() {
        if line_buf.len() + word.len() + 1 > 58 {
            println!("  {line_buf}");
            line_buf = word.to_string();
        } else {
            if !line_buf.is_empty() {
                line_buf.push(' ');
            }
            line_buf.push_str(word);
        }
    }
    if !line_buf.is_empty() {
        println!("  {line_buf}");
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_parse_standard_variants() {
        assert!(parse_standard("ebu-r128").is_ok());
        assert!(parse_standard("atsc-a85").is_ok());
        assert!(parse_standard("youtube").is_ok());
        assert!(parse_standard("spotify").is_ok());
        assert!(parse_standard("apple-music").is_ok());
        assert!(parse_standard("netflix").is_ok());
        assert!(parse_standard("amazon-prime").is_ok());
        assert!(parse_standard("unknown").is_err());
    }

    #[test]
    fn test_fmt_lufs_finite() {
        let s = fmt_lufs(-23.0);
        assert!(s.contains("-23.0"));
    }

    #[test]
    fn test_fmt_lufs_neg_inf() {
        let s = fmt_lufs(f64::NEG_INFINITY);
        assert_eq!(s, "-∞");
    }

    #[tokio::test]
    async fn test_cmd_standards_no_panic() {
        // Should not panic; json_output path
        let result = cmd_standards(true);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_cmd_info_ebu() {
        let result = cmd_info("ebu-r128", true);
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_cmd_info_unknown() {
        let result = cmd_info("bogus", false);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_cmd_analyze_missing_file() {
        let path = PathBuf::from("/tmp/oximedia_loudness_nonexistent_12345.wav");
        let result = cmd_analyze(&path, "ebu-r128", None, None, false, "text").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_cmd_analyze_existing_file_json() {
        let dir = std::env::temp_dir();
        let path = dir.join("oximedia_loudness_test_file.wav");
        // Write a minimal stub so exists() returns true
        std::fs::write(&path, b"RIFF").expect("write should succeed");
        let result = cmd_analyze(&path, "ebu-r128", None, None, false, "json").await;
        // File exists so meter init succeeds; result should be Ok
        assert!(result.is_ok(), "unexpected error: {result:?}");
        std::fs::remove_file(&path).ok();
    }

    #[tokio::test]
    async fn test_cmd_check_missing_file() {
        let path = PathBuf::from("/tmp/oximedia_loudness_missing_check.wav");
        let result = cmd_check(&path, "ebu-r128", false, false).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_cmd_check_existing_file() {
        let dir = std::env::temp_dir();
        let path = dir.join("oximedia_loudness_check_stub.wav");
        std::fs::write(&path, b"stub").expect("write should succeed");
        // strict=false so non-compliance is not an error here
        let result = cmd_check(&path, "youtube", false, true).await;
        assert!(result.is_ok(), "unexpected error: {result:?}");
        std::fs::remove_file(&path).ok();
    }
}