radio-cli 0.8.2

A lightweight terminal radio with local favorites and recordings
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
use crate::error::{DomainError, ErrorCode};
use crate::{
    USER_AGENT,
    config::{Config, validate_url},
};
use anyhow::{Context, Result, bail};
use serde_json::{Value, json};
use std::{
    borrow::Cow,
    collections::HashMap,
    fs,
    io::{Read, Write},
    os::unix::net::UnixStream,
    path::{Path, PathBuf},
    process::{Child, Command, Stdio},
    sync::{
        Arc, Mutex, OnceLock,
        atomic::{AtomicBool, Ordering},
    },
    thread,
    time::{Duration, Instant},
};

#[derive(Debug)]
pub enum Event {
    Playing,
    Title(String),
    End(String),
    Failed(DomainError),
    Paused(bool),
    Volume(u8),
    Muted(bool),
}

pub struct Player {
    child: Child,
    stream: UnixStream,
    socket: PathBuf,
    buffer: Vec<u8>,
    audio_device: Option<String>,
}

#[derive(Debug, serde::Serialize)]
pub struct AudioDevice {
    pub name: String,
    pub description: String,
}

fn parse_audio_devices(output: &str) -> Vec<AudioDevice> {
    output
        .lines()
        .filter_map(|line| {
            let line = line.trim().strip_prefix('\'')?;
            let (name, description) = line.rsplit_once("' (")?;
            Some(AudioDevice {
                name: name.into(),
                description: description.strip_suffix(')')?.into(),
            })
        })
        .collect()
}

pub fn audio_devices(config: &Config, stop: &Arc<AtomicBool>) -> Result<Vec<AudioDevice>> {
    let output = crate::process::output(
        Command::new(&config.settings.mpv).args(["--no-config", "--audio-device=help"]),
        Duration::from_secs(3),
        stop,
    )?;
    let devices = parse_audio_devices(&String::from_utf8_lossy(&output));
    if devices.is_empty() {
        bail!(DomainError::new(ErrorCode::AudioDevicesUnavailable));
    }
    Ok(devices)
}

fn supports_normalization(config: &Config, stop: &Arc<AtomicBool>) -> Result<bool> {
    type Probe = Arc<Mutex<Option<bool>>>;
    static PROBES: OnceLock<Mutex<HashMap<String, Probe>>> = OnceLock::new();
    let probe = PROBES
        .get_or_init(Mutex::default)
        .lock()
        .unwrap()
        .entry(config.settings.mpv.clone())
        .or_default()
        .clone();
    // Serialize only probes for this executable, including concurrent starts.
    // Failed or cancelled subprocesses remain retryable on the next start.
    let mut cached = probe.lock().unwrap();
    if let Some(supported) = *cached {
        return Ok(supported);
    }
    let filters = crate::process::output(
        Command::new(&config.settings.mpv).args(["--no-config", "--af=help"]),
        Duration::from_secs(3),
        stop,
    )?;
    let supported = String::from_utf8_lossy(&filters)
        .lines()
        .any(|line| line.split_whitespace().next() == Some("dynaudnorm"));
    *cached = Some(supported);
    Ok(supported)
}

fn metadata_title(metadata: &Value) -> String {
    let Some(title) = metadata["icy-title"].as_str() else {
        return crate::clean_text(metadata["title"].as_str().unwrap_or(""));
    };
    icy_title(title)
}

pub(crate) fn icy_title(title: &str) -> String {
    // Repair Latin-1 byte runs before sanitizing: C1 characters can be
    // UTF-8 continuation bytes. ASCII and existing Unicode delimit runs,
    // so GBK guesses cannot consume ASCII letters or span separate words.
    let mut repaired = String::with_capacity(title.len());
    let mut start = 0;
    for (index, character) in title.char_indices() {
        if !matches!(character, '\u{80}'..='\u{ff}') {
            repaired.push_str(&repair_latin1_run(&title[start..index]));
            repaired.push(character);
            start = index + character.len_utf8();
        }
    }
    repaired.push_str(&repair_latin1_run(&title[start..]));
    crate::clean_text(&repaired)
}

pub(crate) fn icy_title_bytes(bytes: &[u8]) -> String {
    // Decode each non-ASCII run independently. Some GBK bytes happen to form
    // short UTF-8 sequences, so try a complete run before the byte fallback.
    let mut title = String::with_capacity(bytes.len());
    let mut start = 0;
    for (index, &byte) in bytes.iter().enumerate() {
        if byte.is_ascii() {
            decode_icy_run(&bytes[start..index], &mut title);
            title.push(char::from(byte));
            start = index + 1;
        }
    }
    decode_icy_run(&bytes[start..], &mut title);
    icy_title(&title)
}

fn decode_icy_run(bytes: &[u8], title: &mut String) {
    if let Ok(valid) = std::str::from_utf8(bytes) {
        title.push_str(valid);
        return;
    }
    // Existing UTF-8 Han text is stronger evidence than a GBK guess over
    // the entire run. Preserve it even when adjacent bytes are malformed.
    let has_utf8_han = bytes
        .utf8_chunks()
        .any(|chunk| chunk.valid().chars().any(is_cjk));
    if !has_utf8_han {
        let latin1: String = bytes.iter().map(|&byte| char::from(byte)).collect();
        if let Cow::Owned(repaired) = repair_latin1_run(&latin1) {
            title.push_str(&repaired);
            return;
        }
    }
    for chunk in bytes.utf8_chunks() {
        title.push_str(chunk.valid());
        title.extend(chunk.invalid().iter().map(|&byte| char::from(byte)));
    }
}

fn repair_latin1_run(run: &str) -> Cow<'_, str> {
    if run.is_empty() {
        return Cow::Borrowed(run);
    }
    let bytes: Vec<u8> = run.chars().map(|c| c as u8).collect();
    if let Ok(repaired) = std::str::from_utf8(&bytes) {
        return Cow::Owned(repaired.to_owned());
    }

    // GBK and genuine Latin-1 text overlap. Require a contiguous run of at
    // least two double-byte Han characters and some evidence beyond letters
    // (e.g. the symbols in "Íõ·Æ" or "ºÓͼ"). Letter-only runs such as "ää"
    // and "éééé" stay untouched. This remains a conservative heuristic, not
    // encoding detection; ambiguous titles may need a source-specific policy.
    if bytes.len() < 4
        || !run
            .chars()
            .any(|c| !c.is_alphabetic() && !c.is_control() && !c.is_whitespace())
    {
        return Cow::Borrowed(run);
    }
    if let Some(decoded) =
        encoding_rs::GB18030.decode_without_bom_handling_and_without_replacement(&bytes)
        && decoded.chars().all(is_cjk)
        && decoded.chars().count() * 2 == bytes.len()
    {
        Cow::Owned(decoded.into_owned())
    } else {
        Cow::Borrowed(run)
    }
}

fn is_cjk(character: char) -> bool {
    matches!(character, '\u{3400}'..='\u{9fff}' | '\u{f900}'..='\u{faff}')
}

/// `sockaddr_un.sun_path` holds 104 bytes on macOS (108 on Linux) including
/// the terminating NUL; stay at the lowest common denominator. The default
/// per-user data directory (`~/Library/Application Support` on macOS) plus a
/// full UUID socket name could exceed it, so the runtime-directory name keeps
/// only 12 hex characters; when the runtime directory runs deeper, fall back
/// to the system temp directory with the full UUID, which stays unguessable
/// in shared temp dirs. Do not lengthen the runtime-directory name again: the
/// default macOS path leaves room for little more than the truncation.
/// Session uniqueness comes from the random suffix; the application allows a
/// single media session per data directory anyway.
const SOCKET_PATH_LIMIT: usize = 100;

fn ipc_socket_path(runtime_dir: &Path) -> PathBuf {
    let id = uuid::Uuid::new_v4().simple().to_string();
    let primary = runtime_dir.join(format!("mpv-{}.sock", &id[..12]));
    if primary.as_os_str().len() <= SOCKET_PATH_LIMIT {
        return primary;
    }
    std::env::temp_dir().join(format!("mpv-{id}.sock"))
}

fn playback_command(config: &Config, socket: &Path) -> Command {
    let mut command = Command::new(&config.settings.mpv);
    command
        .args([
            "--no-config",
            "--load-scripts=no",
            "--no-video",
            "--no-audio-display",
            "--terminal=no",
            "--input-default-bindings=no",
            "--idle=yes",
            "--keep-open=no",
            "--cache=yes",
            "--cache-secs=3",
            "--demuxer-max-bytes=2097152",
            // Keep recent packets until playback observes timed ICY metadata.
            // Disabling the back buffer can lose one-shot title updates.
            "--demuxer-max-back-bytes=524288",
            "--demuxer-donate-buffer=no",
            "--audio-buffer=0.2",
        ])
        .arg(format!("--user-agent={USER_AGENT}"))
        .arg(format!("--volume={}", config.settings.volume))
        .arg(format!(
            "--network-timeout={}",
            config.settings.playback_timeout_secs
        ))
        .arg(format!("--input-ipc-server={}", socket.display()))
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    if config.settings.normalize_loudness {
        command.arg("--af=dynaudnorm=f=150:g=5");
    }
    if let Some(device) = config
        .settings
        .audio_device
        .as_deref()
        .filter(|s| !s.trim().is_empty())
    {
        command.arg(format!("--audio-device={device}"));
    }
    command
}

impl Player {
    pub fn start(config: &Config, url: &str, stop: &Arc<AtomicBool>) -> Result<Self> {
        validate_url(url)?;
        Self::start_input(config, url, stop)
    }

    pub fn start_local(
        config: &Config,
        path: &std::path::Path,
        stop: &Arc<AtomicBool>,
    ) -> Result<Self> {
        let path = fs::canonicalize(path)?;
        let url = reqwest::Url::from_file_path(path)
            .map_err(|_| DomainError::new(ErrorCode::InvalidUrl))?;
        Self::start_input(config, url.as_str(), stop)
    }

    fn start_input(config: &Config, url: &str, stop: &Arc<AtomicBool>) -> Result<Self> {
        let socket = ipc_socket_path(&config.runtime_dir);
        if socket.as_os_str().len() > SOCKET_PATH_LIMIT {
            // The runtime directory and the system temp directory are both
            // too deep; surface the path so the JSON report shows the culprit.
            bail!(DomainError::new(ErrorCode::RuntimePathTooLong).with("path", socket.display()));
        }
        if config.settings.normalize_loudness && !supports_normalization(config, stop)? {
            bail!(DomainError::new(ErrorCode::NormalizationUnavailable));
        }
        let command = playback_command(config, &socket);
        let mut child = crate::process::spawn_owned(command)
            .context(DomainError::new(ErrorCode::MissingDependency).with("program", "mpv"))?;
        let deadline = Instant::now() + Duration::from_secs(3);
        let stream = loop {
            if let Ok(stream) = UnixStream::connect(&socket) {
                break stream;
            }
            if child.try_wait()?.is_some()
                || Instant::now() >= deadline
                || stop.load(Ordering::Relaxed)
            {
                let _ = child.kill();
                let _ = child.wait();
                let _ = fs::remove_file(&socket);
                bail!(DomainError::new(ErrorCode::PlayerStartup));
            }
            thread::sleep(Duration::from_millis(20));
        };
        stream.set_read_timeout(Some(Duration::from_millis(1)))?;
        stream.set_write_timeout(Some(Duration::from_secs(1)))?;
        let mut player = Self {
            child,
            stream,
            socket,
            buffer: vec![],
            audio_device: config.settings.audio_device.clone(),
        };
        for (id, property) in [(1, "metadata"), (2, "pause"), (3, "volume"), (4, "mute")] {
            player.command(json!(["observe_property", id, property]))?;
        }
        player.command(json!(["loadfile", url, "replace"]))?;
        Ok(player)
    }

    pub fn command(&mut self, command: Value) -> Result<()> {
        let mut bytes = serde_json::to_vec(&json!({"command":command}))?;
        bytes.push(b'\n');
        self.stream.write_all(&bytes)?;
        Ok(())
    }

    pub fn toggle_pause(&mut self) -> Result<()> {
        self.command(json!(["cycle", "pause"]))
    }
    pub fn toggle_mute(&mut self) -> Result<()> {
        self.command(json!(["cycle", "mute"]))
    }
    pub fn volume(&mut self, delta: i32) -> Result<()> {
        self.command(json!(["add", "volume", delta]))
    }

    pub fn poll(&mut self) -> Result<Vec<Event>> {
        let mut out = vec![];
        let mut buf = [0u8; 8192];
        let mut closed = false;
        loop {
            match self.stream.read(&mut buf) {
                Ok(0) => {
                    closed = true;
                    break;
                }
                Ok(n) => self.buffer.extend_from_slice(&buf[..n]),
                Err(e)
                    if matches!(
                        e.kind(),
                        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
                    ) =>
                {
                    break;
                }
                Err(e) => return Err(e.into()),
            }
            if self.buffer.len() > 1024 * 1024 {
                bail!(DomainError::new(ErrorCode::PlayerMessageTooLarge));
            }
            if self.buffer.len() >= 8192 {
                break;
            }
        }
        while let Some(end) = self.buffer.iter().position(|b| *b == b'\n') {
            let line: Vec<_> = self.buffer.drain(..=end).collect();
            let Ok(value) = serde_json::from_slice::<Value>(&line) else {
                continue;
            };
            match value["event"].as_str().unwrap_or("") {
                "playback-restart" => out.push(Event::Playing),
                "end-file" => {
                    if value["file_error"]
                        .as_str()
                        .is_some_and(|error| error.contains("audio output"))
                        && let Some(device) = self
                            .audio_device
                            .as_deref()
                            .filter(|s| !s.trim().is_empty())
                    {
                        out.push(Event::Failed(
                            DomainError::new(ErrorCode::AudioDeviceFailed).with("device", device),
                        ));
                        continue;
                    }
                    out.push(Event::End(
                        value["reason"].as_str().unwrap_or("unknown").into(),
                    ));
                }
                "property-change" => match value["name"].as_str().unwrap_or("") {
                    "metadata" => {
                        out.push(Event::Title(metadata_title(&value["data"])));
                    }
                    "pause" => out.push(Event::Paused(value["data"].as_bool().unwrap_or(false))),
                    "mute" => out.push(Event::Muted(value["data"].as_bool().unwrap_or(false))),
                    "volume" => out.push(Event::Volume(
                        value["data"].as_f64().unwrap_or(80.).clamp(0., 100.) as u8,
                    )),
                    _ => {}
                },
                _ => {}
            }
        }
        // Deliver buffered end-file errors even when mpv closes IPC or exits
        // immediately afterward; otherwise their structured reason is lost.
        if out.is_empty() && closed {
            bail!(DomainError::new(ErrorCode::PlayerClosed));
        }
        if out.is_empty() && self.child.try_wait()?.is_some() {
            bail!(DomainError::new(ErrorCode::PlayerExited));
        }
        Ok(out)
    }
}

impl Drop for Player {
    fn drop(&mut self) {
        let _ = self.command(json!(["quit"]));
        let deadline = Instant::now() + Duration::from_millis(800);
        while Instant::now() < deadline {
            if self.child.try_wait().ok().flatten().is_some() {
                break;
            }
            thread::sleep(Duration::from_millis(20));
        }
        let _ = self.child.kill();
        let _ = self.child.wait();
        let _ = fs::remove_file(&self.socket);
    }
}

#[cfg(test)]
mod socket_path_tests {
    use super::{SOCKET_PATH_LIMIT, ipc_socket_path};
    use std::path::Path;

    #[test]
    fn prefers_runtime_dir_with_a_short_name() {
        let dir = Path::new("/tmp/radio-test-run");
        let path = ipc_socket_path(dir);
        assert!(path.starts_with(dir));
        assert!(path.as_os_str().len() <= SOCKET_PATH_LIMIT);
        let name = path.file_name().unwrap().to_str().unwrap();
        assert!(name.starts_with("mpv-") && name.ends_with(".sock"));
    }

    #[test]
    fn falls_back_to_temp_dir_for_deep_runtime_dirs() {
        let deep = std::env::temp_dir().join("a".repeat(160));
        let path = ipc_socket_path(&deep);
        // The function only guarantees the primary path fits; a fallback path
        // that is still too long is rejected by the caller's length check.
        assert!(path.starts_with(std::env::temp_dir()));
        assert!(!path.starts_with(&deep));
    }
}

#[cfg(test)]
mod tests {
    use super::{icy_title_bytes, metadata_title, parse_audio_devices, playback_command};
    use serde_json::json;

    #[test]
    fn device_names_preserve_backend_syntax_and_optional_arguments() {
        let parsed = parse_audio_devices(
            "List of detected audio devices:\n  'auto' (Autoselect device)\n  'alsa/plughw:CARD=USB,DEV=0' (Listener's USB (stereo))\nignored\n",
        );
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[1].name, "alsa/plughw:CARD=USB,DEV=0");
        assert_eq!(parsed[1].description, "Listener's USB (stereo)");
        let dir = tempfile::tempdir().unwrap();
        let mut config = crate::config::Config::load(None, Some(dir.path()), None).unwrap();
        let socket = std::path::Path::new("/tmp/radio.sock");
        let original: Vec<_> = playback_command(&config, socket)
            .get_args()
            .map(|a| a.to_owned())
            .collect();
        config.settings.audio_device = Some(String::new());
        assert_eq!(
            playback_command(&config, socket)
                .get_args()
                .collect::<Vec<_>>(),
            original
        );
        config.settings.audio_device = Some(parsed[1].name.clone());
        let command = playback_command(&config, socket);
        let args: Vec<_> = command.get_args().collect();
        assert_eq!(&args[..original.len()], original);
        assert_eq!(
            args.last().unwrap(),
            &"--audio-device=alsa/plughw:CARD=USB,DEV=0"
        );
    }

    #[test]
    fn normalization_changes_only_the_opt_in_filter_argument() {
        let dir = tempfile::tempdir().unwrap();
        let mut config = crate::config::Config::load(None, Some(dir.path()), None).unwrap();
        let socket = std::path::Path::new("/tmp/radio-test.sock");
        let command = playback_command(&config, socket);
        let original: Vec<_> = command
            .get_args()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            original,
            vec![
                "--no-config",
                "--load-scripts=no",
                "--no-video",
                "--no-audio-display",
                "--terminal=no",
                "--input-default-bindings=no",
                "--idle=yes",
                "--keep-open=no",
                "--cache=yes",
                "--cache-secs=3",
                "--demuxer-max-bytes=2097152",
                "--demuxer-max-back-bytes=524288",
                "--demuxer-donate-buffer=no",
                "--audio-buffer=0.2",
                &format!("--user-agent={}", crate::USER_AGENT),
                "--volume=80",
                "--network-timeout=12",
                "--input-ipc-server=/tmp/radio-test.sock",
            ]
        );
        config.settings.normalize_loudness = true;
        let command = playback_command(&config, socket);
        let enabled: Vec<_> = command
            .get_args()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        assert_eq!(&enabled[..original.len()], original);
        assert_eq!(&enabled[original.len()..], ["--af=dynaudnorm=f=150:g=5"]);
    }

    #[test]
    fn icy_titles_repair_latin1_mojibake_before_sanitizing() {
        // UTF-8 bytes for "邝美云 - 雪花" were decoded as Latin-1 and then
        // encoded as UTF-8. The C1 characters must survive until repaired.
        let title = "é\u{82}\u{9d}ç¾\u{8e}äº\u{91} - é\u{9b}ªè\u{8a}±\x1b\n";
        assert_eq!(
            metadata_title(&json!({"icy-title": title})),
            "邝美云 - 雪花"
        );
    }

    #[test]
    fn icy_titles_repair_gbk_segments_in_mixed_mojibake() {
        // AsiaFM sends GBK song text before UTF-8 station text. The final
        // metadata byte is already a literal question mark and cannot be
        // recovered from the stream, so only the recoverable part is repaired.
        let title = "\u{cd}\u{f5}\u{b7}\u{c6} -  (AsiaFM亚洲粤语\u{e5}\u{8f}?)";
        assert_eq!(
            metadata_title(&json!({"icy-title": title})),
            "王菲 -  (AsiaFM亚洲粤语å?)"
        );
        // A lone GBK pair decodes to a single character; guessing one
        // character from two ambiguous bytes is not worth the risk, so such
        // runs stay untouched. Rejecting a single-character guess is the safe
        // side: the raw bytes are shown instead of a wrong character.
        let lone_pair = "\u{b7}\u{c6} -  (AsiaFM亚洲粤语)";
        assert_eq!(
            metadata_title(&json!({"icy-title": lone_pair})),
            "\u{b7}\u{c6} -  (AsiaFM亚洲粤语)"
        );
        let another_lone_pair = "\u{cd}\u{f5} -  (AsiaFM亚洲粤语)";
        assert_eq!(
            metadata_title(&json!({"icy-title": another_lone_pair})),
            "\u{cd}\u{f5} -  (AsiaFM亚洲粤语)"
        );
        // Ordinary text and controls elsewhere must not veto a good segment.
        assert_eq!(
            metadata_title(&json!({"icy-title": "Íõ·Æ\n - Café(中文电台)"})),
            "王菲 - Café(中文电台)"
        );
    }

    #[test]
    fn icy_titles_repair_latin1_wrapped_gbk() {
        // A Zeno stream wraps GBK title bytes in Latin-1 and then UTF-8.
        let title = "ºÓͼ - ×Ï´¨Â¼";
        assert_eq!(
            metadata_title(&json!({"icy-title": title})),
            "河图 - 紫川录"
        );
    }

    #[test]
    fn icy_titles_keep_text_without_a_lossless_latin1_repair() {
        for title in [
            "Radio Swiss Jazz",
            "张柏芝 - 乱了感觉",
            "Café à côté",
            "Björk - Jóga",
            "Motörhead - Ace of Spades",
            "Sigur Rós - Hoppípolla",
            "Chloéë",
            "Chloéë - 中文电台",
            "Lääke - 中文电台",
            "Lääke - Jää(中文电台)",
            "éééé - 中文电台",
            "éé - àà(中文电台)",
            "Íõ· - 中文电台",
            "ÖÐÎÄ", // Also ambiguous with letter-only Latin-1 text.
            "’",
        ] {
            assert_eq!(metadata_title(&json!({"icy-title": title})), title);
            assert_eq!(icy_title_bytes(title.as_bytes()), title);
        }
        assert_eq!(metadata_title(&json!({"icy-title": "中文\x1b\n"})), "中文");
    }

    #[test]
    fn icy_titles_repair_utf8_runs_before_removing_c1_bytes() {
        for (title, expected) in [
            ("Beyoncé - Café", "Beyoncé - Café"),
            ("Café — Live", "Café — Live"),
            (
                "é\u{82}\u{9d}ç¾\u{8e}äº\u{91} - 中文电台\x1b\n",
                "邝美云 - 中文电台",
            ),
        ] {
            assert_eq!(metadata_title(&json!({"icy-title": title})), expected);
        }
    }

    #[test]
    fn icy_title_bytes_preserve_utf8_islands_and_latin1_fallbacks() {
        let mut raw = b"\xcd\xf5\xb7\xc6 - ".to_vec();
        raw.extend_from_slice("中文电台".as_bytes());
        assert_eq!(icy_title_bytes(&raw), "王菲 - 中文电台");
        let mut adjacent = b"\xcd\xf5\xb7\xc6".to_vec();
        adjacent.extend_from_slice("中文".as_bytes());
        assert_eq!(icy_title_bytes(&adjacent), "王菲中文");
        assert_eq!(icy_title_bytes(b"Beyonc\xe9"), "Beyoncé");
        assert_eq!(
            icy_title_bytes(b"\xba\xd3\xcd\xbc - \xd7\xcf\xb4\xa8\xc2\xbc"),
            "河图 - 紫川录"
        );
        assert_eq!(icy_title_bytes(b"Track\x1b\n\xff"), "Trackÿ");
    }

    #[test]
    fn metadata_titles_preserve_file_tags_and_explicit_empty_icy_titles() {
        // A literal Latin-1-looking sequence in a file tag is not ICY data.
        assert_eq!(metadata_title(&json!({"title": "©\x1b\n"})), "©");
        assert_eq!(
            metadata_title(&json!({"icy-title": "", "title": "File title"})),
            ""
        );
        assert_eq!(
            metadata_title(&json!({"icy-title": 1, "title": "File title"})),
            "File title"
        );
        assert_eq!(metadata_title(&json!(null)), "");
    }

    #[test]
    fn icy_titles_are_limited_after_repair() {
        let title = "ä¸\u{ad}".repeat(1100);
        assert_eq!(
            metadata_title(&json!({"icy-title": title})),
            "中".repeat(1024)
        );
    }
}