trill 0.7.3

Lean terminal Spotify client: Spotify's own player, small and quiet when idle
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
//! Playback engines.
//!
//! - **Soloist**: Spotify's official headless player, run as a child process and driven
//!   over its local WebSocket API. Spotify handles decryption, so it works on accounts where
//!   librespot's audio keys are refused (librespot issue #1649).
//! - **Librespot**: in-process decoding. Lighter, but only works while Spotify still hands
//!   librespot audio keys for your account.
//!
//! Track ordering, shuffle and the play queue stay in trill for both engines.

use std::{
    fs,
    path::PathBuf,
    process::{Command, Stdio},
    sync::Arc,
    time::{Duration, Instant, SystemTime},
};

use futures_util::{SinkExt, StreamExt};
use librespot_core::SpotifyUri;
use librespot_playback::{
    mixer::Mixer,
    player::{Player, PlayerEvent},
};
use serde_json::{Value, json};
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
use tokio_tungstenite::tungstenite::Message;

use crate::api::{Kind, Row};

#[derive(Debug)]
pub enum EngineEvent {
    /// Position anchor; `playing: None` keeps the current play/pause state.
    Position { pos_ms: u32, playing: Option<bool> },
    Stopped,
    Ended,
    Unavailable,
    PreloadNext,
    /// Soloist reports the item now loaded (with its own metadata).
    TrackChanged(Row),
    /// Soloist went idle (nothing loaded / context finished).
    Idle,
    Volume(u32),
    Status(String),
    Exited(String),
    /// Soloist confirmed it gave up the Connect device (sent while quitting).
    Deactivated,
}

pub enum Engine {
    Librespot { player: Arc<Player>, mixer: Arc<dyn Mixer> },
    Soloist { tx: UnboundedSender<String>, pid: Option<u32> },
}

impl Engine {
    /// Soloist's process id (None for librespot or an external daemon).
    pub fn pid(&self) -> Option<u32> {
        match self {
            Engine::Soloist { pid, .. } => *pid,
            Engine::Librespot { .. } => None,
        }
    }
}

fn cmd(command: &str) -> Value {
    json!({ "type": "command", "command": command })
}

impl Engine {
    pub fn is_soloist(&self) -> bool {
        matches!(self, Engine::Soloist { .. })
    }

    fn send(&self, v: Value) {
        if let Engine::Soloist { tx, .. } = self {
            log::debug!("soloist <- {v}");
            let _ = tx.send(v.to_string());
        }
    }

    pub fn load(&self, uri: &str) {
        match self {
            Engine::Librespot { player, .. } => {
                if let Ok(u) = SpotifyUri::from_uri(uri) {
                    player.load(u, true, 0);
                }
            }
            Engine::Soloist { .. } => {
                let mut v = cmd("play");
                v["uri"] = uri.into();
                self.send(v);
            }
        }
    }

    pub fn preload(&self, uri: &str) {
        if let Engine::Librespot { player, .. } = self
            && let Ok(u) = SpotifyUri::from_uri(uri)
        {
            player.preload(u);
        }
    }

    pub fn resume(&self) {
        match self {
            Engine::Librespot { player, .. } => player.play(),
            Engine::Soloist { .. } => self.send(cmd("play")),
        }
    }

    pub fn pause(&self) {
        match self {
            Engine::Librespot { player, .. } => player.pause(),
            Engine::Soloist { .. } => self.send(cmd("pause")),
        }
    }

    pub fn stop(&self) {
        match self {
            Engine::Librespot { player, .. } => player.stop(),
            Engine::Soloist { .. } => self.send(cmd("pause")),
        }
    }

    /// Give up the Spotify Connect device, so Spotify (and Discord) show nothing playing.
    pub fn deactivate(&self) {
        self.send(cmd("deactivate"));
    }

    pub fn seek(&self, ms: u32) {
        match self {
            Engine::Librespot { player, .. } => player.seek(ms),
            Engine::Soloist { .. } => {
                let mut v = cmd("seek");
                v["position_ms"] = ms.into();
                self.send(v);
            }
        }
    }

    pub fn set_volume(&self, pct: u32) {
        let pct = pct.min(100);
        match self {
            Engine::Librespot { mixer, .. } => mixer.set_volume((pct * u16::MAX as u32 / 100) as u16),
            Engine::Soloist { .. } => {
                let mut v = cmd("set_volume");
                v["volume"] = pct.into();
                self.send(v);
            }
        }
    }

    pub fn add_to_queue(&self, uri: &str) {
        let mut v = cmd("add_to_queue");
        v["uri"] = uri.into();
        self.send(v);
    }

    pub fn skip_next(&self) {
        self.send(cmd("skip_next"));
    }

    /// Soloist loops the track natively for repeat-one (no reload gap). Its context repeat
    /// stays off: trill owns the play order, and a repeat restored from an old session loops
    /// the one-song context we play instead of taking the track we queue.
    pub fn set_repeat_track(&self, on: bool) {
        if self.is_soloist() {
            let mut v = cmd("set_repeat_track");
            v["enabled"] = on.into();
            self.send(v);
            let mut v = cmd("set_repeat_context");
            v["enabled"] = false.into();
            self.send(v);
        }
    }

    pub fn shutdown(&self) {
        match self {
            Engine::Librespot { player, .. } => player.stop(),
            Engine::Soloist { pid, .. } => {
                if let Some(pid) = pid {
                    stop_process(*pid);
                }
            }
        }
    }
}

/// Maps librespot player events, dropping stale ones from superseded load requests.
#[derive(Default)]
pub struct LibrespotEvents {
    req_id: Option<u64>,
}

impl LibrespotEvents {
    pub fn map(&mut self, ev: PlayerEvent) -> Option<EngineEvent> {
        use PlayerEvent::*;
        let ours = |id: u64| self.req_id.is_none_or(|r| r == id);
        Some(match ev {
            PlayRequestIdChanged { play_request_id } => {
                self.req_id = Some(play_request_id);
                return None;
            }
            Playing { play_request_id, position_ms, .. } if ours(play_request_id) => {
                EngineEvent::Position { pos_ms: position_ms, playing: Some(true) }
            }
            Paused { play_request_id, position_ms, .. } if ours(play_request_id) => {
                EngineEvent::Position { pos_ms: position_ms, playing: Some(false) }
            }
            Seeked { play_request_id, position_ms, .. } | PositionCorrection { play_request_id, position_ms, .. }
                if ours(play_request_id) =>
            {
                EngineEvent::Position { pos_ms: position_ms, playing: None }
            }
            Stopped { play_request_id, .. } if ours(play_request_id) => EngineEvent::Stopped,
            TimeToPreloadNextTrack { play_request_id, .. } if ours(play_request_id) => EngineEvent::PreloadNext,
            EndOfTrack { play_request_id, .. } if ours(play_request_id) => EngineEvent::Ended,
            Unavailable { play_request_id, .. } if ours(play_request_id) => EngineEvent::Unavailable,
            _ => return None,
        })
    }
}

#[derive(Clone)]
pub struct SoloistOpts {
    pub bin: PathBuf,
    pub api_key: String,
    pub device_name: String,
    pub data_dir: PathBuf,
    pub cache_dir: PathBuf,
    pub cache_mb: u64,
    /// Connect to an already-running daemon instead of spawning one.
    pub ws: Option<String>,
    /// PipeWire node to play into (the EQ), instead of the default output.
    pub pipewire_device: Option<String>,
    pub initial_volume: Option<u32>,
    /// Extra environment (private mode's relay, see relay.rs).
    pub env: Vec<(String, String)>,
}

impl SoloistOpts {
    pub fn base_command(&self) -> Command {
        let mut c = Command::new(&self.bin);
        c.args(["-n", &self.device_name, "-k", &self.api_key])
            .arg("-D")
            .arg(&self.data_dir)
            .arg("-C")
            .arg(&self.cache_dir);
        if self.cache_mb > 0 {
            c.args(["-z", &self.cache_mb.max(100).to_string()]);
        }
        if let Some(d) = &self.pipewire_device {
            c.args(["-d", d]);
        }
        if let Some(v) = self.initial_volume {
            c.args(["-i", &v.min(100).to_string()]);
        }
        if std::env::var_os("TRILL_SOLOIST_VERBOSE").is_some() {
            c.arg("-v");
        }
        c.envs(self.env.iter().map(|(k, v)| (k, v)));
        c
    }
}

/// Ask a child process to quit (SIGTERM; on Windows, which has no signals, `taskkill /F`).
pub fn stop_process(pid: u32) {
    #[cfg(unix)]
    // SAFETY: plain signal to our own child.
    unsafe {
        libc::kill(pid as i32, libc::SIGTERM);
    }
    #[cfg(windows)]
    let _ = std::process::Command::new("taskkill")
        .args(["/PID", &pid.to_string(), "/T", "/F"])
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

/// Whether a process is still running.
#[cfg(unix)]
pub fn process_alive(pid: u32) -> bool {
    // SAFETY: signal 0 only checks whether the process exists.
    unsafe { libc::kill(pid as i32, 0) == 0 }
}

#[cfg(windows)]
pub fn process_alive(pid: u32) -> bool {
    std::process::Command::new("tasklist")
        .args(["/FI", &format!("PID eq {pid}"), "/NH"])
        .output()
        .is_ok_and(|o| String::from_utf8_lossy(&o.stdout).contains(&pid.to_string()))
}

fn exit_message(status: std::io::Result<std::process::ExitStatus>) -> String {
    #[cfg(unix)]
    use std::os::unix::process::ExitStatusExt;
    match status {
        Ok(s) if s.code() == Some(10) => {
            "Soloist build expired (Spotify builds last 90 days). Quit and run: trill --update-soloist".into()
        }
        #[cfg(unix)]
        Ok(s) if s.signal() == Some(libc::SIGTERM) => "Soloist stopped.".into(),
        Ok(s) => format!("Soloist exited ({s}); see ~/.cache/trill/soloist.log"),
        Err(e) => format!("Soloist: {e}"),
    }
}

/// Spawn Soloist (unless `ws` is set), connect to its WebSocket and start pumping events.
pub async fn start_soloist(opts: &SoloistOpts, ev_tx: UnboundedSender<EngineEvent>) -> Result<Engine, String> {
    let (addr, pid) = match &opts.ws {
        Some(a) => (a.clone(), None),
        None => {
            let _ = fs::create_dir_all(&opts.data_dir);
            let _ = fs::create_dir_all(&opts.cache_dir);
            let port_file = opts.data_dir.join("ws.port");
            let addr_file = opts.data_dir.join("ws.addr");
            let _ = fs::remove_file(&port_file);
            let _ = fs::remove_file(&addr_file);

            let log = fs::File::create(opts.cache_dir.join("../soloist.log")).map_err(|e| e.to_string())?;
            let mut c = opts.base_command();
            c.args(["-w", "127.0.0.1:0"])
                .stdin(Stdio::null())
                .stdout(log.try_clone().map_err(|e| e.to_string())?)
                .stderr(log);
            // Its own process group: closing the terminal hangs up trill, which then stops
            // playback and Soloist cleanly. Soloist doesn't handle SIGHUP and would die mid-song.
            #[cfg(unix)]
            std::os::unix::process::CommandExt::process_group(&mut c, 0);
            // SAFETY: prctl is async-signal-safe; makes Soloist exit if trill dies.
            // shortcut: macOS has no PDEATHSIG, so Soloist outlives a crashed trill there; add a parent-pid watch if that bites.
            #[cfg(target_os = "linux")]
            unsafe {
                std::os::unix::process::CommandExt::pre_exec(&mut c, || {
                    libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
                    Ok(())
                });
            }
            let mut child = c
                .spawn()
                .map_err(|e| format!("can't start {}: {e}", opts.bin.display()))?;
            let pid = child.id();
            let tx = ev_tx.clone();
            let _ = std::thread::Builder::new().name("soloist-wait".into()).spawn(move || {
                let _ = tx.send(EngineEvent::Exited(exit_message(child.wait())));
            });

            let deadline = Instant::now() + Duration::from_secs(20);
            let port = loop {
                if let Some(p) = fs::read_to_string(&port_file).ok().and_then(|s| s.trim().parse::<u16>().ok()) {
                    break p;
                }
                if Instant::now() > deadline {
                    return Err("Soloist didn't open its WebSocket in 20s; see ~/.cache/trill/soloist.log".into());
                }
                tokio::time::sleep(Duration::from_millis(100)).await;
            };
            let host = fs::read_to_string(&addr_file)
                .ok()
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .unwrap_or_else(|| "127.0.0.1".into());
            let addr = if host.contains(':') && host.rsplit(':').next().is_some_and(|p| p.parse::<u16>().is_ok()) {
                host
            } else {
                format!("{host}:{port}")
            };
            (addr, Some(pid))
        }
    };

    let url = format!("ws://{addr}");
    let mut tries = 0;
    let ws = loop {
        match tokio_tungstenite::connect_async(url.as_str()).await {
            Ok((ws, _)) => break ws,
            Err(e) if tries >= 30 => return Err(format!("can't connect to Soloist at {url}: {e}")),
            Err(_) => {
                tries += 1;
                tokio::time::sleep(Duration::from_millis(200)).await;
            }
        }
    };
    let (mut sink, mut stream) = ws.split();
    let (tx, mut rx) = unbounded_channel::<String>();

    tokio::spawn(async move {
        while let Some(m) = rx.recv().await {
            if sink.send(Message::text(m)).await.is_err() {
                break;
            }
        }
    });

    // Soloist reports logged_in=false for a moment while it restores its session, so only
    // warn if it's still not signed in after a few seconds.
    let logged_in = Arc::new(std::sync::atomic::AtomicBool::new(false));
    {
        let (logged_in, ev_tx) = (logged_in.clone(), ev_tx.clone());
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_secs(8)).await;
            if !logged_in.load(std::sync::atomic::Ordering::Relaxed) {
                let _ = ev_tx.send(EngineEvent::Status(
                    "Soloist isn't signed in. Quit (q) and run: trill --pair".into(),
                ));
            }
        });
    }

    let ready = logged_in.clone();
    tokio::spawn(async move {
        let logged_in = ready;
        let mut anchor = Anchor::default();
        while let Some(Ok(msg)) = stream.next().await {
            if let Message::Text(t) = msg
                && let Ok(v) = serde_json::from_str::<Value>(t.as_str())
            {
                if v["type"] == "auth_state" && v["logged_in"] == true {
                    logged_in.store(true, std::sync::atomic::Ordering::Relaxed);
                } else if log::log_enabled!(log::Level::Debug) {
                    // TRILL_LOG=debug: what Soloist reports, minus the track metadata
                    let item = v["item"]["uri"].as_str().unwrap_or("");
                    log::debug!("soloist -> {} {} status={} position={} {item}", v["type"], v["command"], v["status"], v["position"]);
                }
                for ev in anchor.parse(&v) {
                    if ev_tx.send(ev).is_err() {
                        return;
                    }
                }
            }
        }
        let _ = ev_tx.send(EngineEvent::Exited("Lost connection to Soloist.".into()));
    });

    // Don't hand out the engine until Soloist has restored its session: commands sent
    // before that are rejected ("command requires authentication").
    let deadline = Instant::now() + Duration::from_secs(10);
    while !logged_in.load(std::sync::atomic::Ordering::Relaxed) && Instant::now() < deadline {
        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    Ok(Engine::Soloist { tx, pid })
}

fn now_ms() -> i64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

/// Tracks Soloist's position anchor so we can report "position now".
#[derive(Default)]
struct Anchor {
    pos_ms: i64,
    ts_ms: i64,
    speed: f64,
}

impl Anchor {
    fn current(&self) -> u32 {
        let elapsed = (now_ms() - self.ts_ms).max(0) as f64 * self.speed;
        (self.pos_ms as f64 + elapsed).max(0.0) as u32
    }

    fn set(&mut self, p: &Value) {
        self.pos_ms = p["position_ms"].as_i64().unwrap_or(0);
        self.ts_ms = p["timestamp_ms"].as_i64().unwrap_or_else(now_ms);
        self.speed = p["speed"].as_f64().unwrap_or(0.0);
    }

    fn status(&self, s: &str, out: &mut Vec<EngineEvent>) {
        match s {
            "playing" => out.push(EngineEvent::Position { pos_ms: self.current(), playing: Some(true) }),
            "paused" => out.push(EngineEvent::Position { pos_ms: self.current(), playing: Some(false) }),
            "idle" => out.push(EngineEvent::Idle),
            _ => {} // buffering
        }
    }

    fn parse(&mut self, v: &Value) -> Vec<EngineEvent> {
        let mut out = Vec::new();
        match v["type"].as_str().unwrap_or("") {
            "auth_state" => {}
            "playback_state" => {
                if let Some(row) = entity_row(&v["item"]) {
                    out.push(EngineEvent::TrackChanged(row));
                }
                self.set(&v["position"]);
                if let Some(vol) = v["volume"].as_u64() {
                    out.push(EngineEvent::Volume(vol as u32));
                }
                self.status(v["status"].as_str().unwrap_or(""), &mut out);
            }
            "track_changed" => {
                if let Some(row) = entity_row(&v["item"]) {
                    out.push(EngineEvent::TrackChanged(row));
                }
            }
            "playback_changed" => self.status(v["status"].as_str().unwrap_or(""), &mut out),
            "position_sync" => {
                self.set(&v["position"]);
                out.push(EngineEvent::Position { pos_ms: self.current(), playing: Some(self.speed > 0.0) });
            }
            "volume_changed" => {
                if let Some(vol) = v["volume"].as_u64() {
                    out.push(EngineEvent::Volume(vol as u32));
                }
            }
            "command_result" if v["command"] == "deactivate" => out.push(EngineEvent::Deactivated),
            "error" => out.push(EngineEvent::Status(format!(
                "Soloist: {}",
                v["message"].as_str().unwrap_or("error")
            ))),
            _ => {}
        }
        out
    }
}

fn entity_row(item: &Value) -> Option<Row> {
    let uri = item["uri"].as_str().filter(|u| !u.is_empty())?;
    let d = &item["decorations"];
    let artist = d["creators"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(|c| c["entity"]["decorations"]["identity"]["name"].as_str())
                .collect::<Vec<_>>()
                .join(", ")
        })
        .unwrap_or_default();
    Some(Row {
        uri: uri.to_string(),
        title: d["identity"]["name"].as_str().unwrap_or("").to_string(),
        artist,
        album: d["parent"]["entity"]["decorations"]["identity"]["name"].as_str().unwrap_or("").to_string(),
        duration_ms: d["playback"]["duration_ms"].as_u64().unwrap_or(0) as u32,
        kind: Kind::Track,
        art: cover(&d["visual_identity"]["cover"]),
        added_at: 0,
        members: String::new(),
        plays: 0,
    })
}

fn cover(v: &Value) -> Option<String> {
    let covers = v.as_array()?;
    ["large", "default", "xlarge", "small"]
        .iter()
        .find_map(|size| covers.iter().find(|c| c["size"] == *size))
        .and_then(|c| c["url"].as_str())
        .map(String::from)
}

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

    #[test]
    fn notices_deactivate_reply() {
        let mut a = Anchor::default();
        let ok: Value = serde_json::from_str(r#"{"type":"command_result","command":"deactivate"}"#).unwrap();
        assert!(matches!(a.parse(&ok)[..], [EngineEvent::Deactivated]));
        let other: Value = serde_json::from_str(r#"{"type":"command_result","command":"pause"}"#).unwrap();
        assert!(a.parse(&other).is_empty());
    }

    #[test]
    fn repeat_keeps_soloist_context_repeat_off() {
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        Engine::Soloist { tx, pid: None }.set_repeat_track(true);
        let sent: Vec<Value> = std::iter::from_fn(|| rx.try_recv().ok()).map(|m| serde_json::from_str(&m).unwrap()).collect();
        assert_eq!(sent.len(), 2);
        assert!(sent[0]["command"] == "set_repeat_track" && sent[0]["enabled"] == true);
        assert!(sent[1]["command"] == "set_repeat_context" && sent[1]["enabled"] == false);
    }

    #[test]
    fn parses_soloist_events() {
        let mut a = Anchor::default();
        let st: Value = serde_json::from_str(r#"{"type":"playback_state","status":"paused",
          "item":{"uri":"spotify:track:x","entity_type":"track","decorations":{"identity":{"name":"Song"},
            "creators":[{"entity":{"decorations":{"identity":{"name":"Art"}}}}],
            "parent":{"entity":{"decorations":{"identity":{"name":"Alb"}}}},
            "playback":{"duration_ms":210000}}},
          "position":{"position_ms":45000,"timestamp_ms":1,"speed":0.0},"volume":65}"#).unwrap();
        let evs = a.parse(&st);
        assert!(matches!(&evs[0], EngineEvent::TrackChanged(r) if r.title == "Song" && r.artist == "Art" && r.album == "Alb" && r.duration_ms == 210000));
        assert!(matches!(evs[1], EngineEvent::Volume(65)));
        assert!(matches!(evs[2], EngineEvent::Position { pos_ms: 45000, playing: Some(false) }));

        let idle: Value = serde_json::from_str(r#"{"type":"playback_changed","status":"idle"}"#).unwrap();
        assert!(matches!(a.parse(&idle)[0], EngineEvent::Idle));
        let empty: Value = serde_json::from_str(r#"{"type":"track_changed","item":{"uri":""}}"#).unwrap();
        assert!(a.parse(&empty).is_empty());
    }
}