spoolframe-types 0.16.3

Spoolframe's client-facing types: the media model and its identifiers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! The media model clients browse: libraries, and the movies, shows and
//! music in them.
//!
//! `playback` is filled in by the server for the session's own user; it
//! never comes from Plex.

use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::codec::AudioCodec;
use crate::date::CalendarDate;
use crate::duration_ms;
use crate::id::{ArtworkId, LibraryId, MediaId, PersonId};
use crate::search::PersonRole;

/// What a library holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LibraryKind {
    Movies,
    Shows,
    Music,
}

/// A library of one kind of media.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Library {
    pub id: LibraryId,
    pub title: String,
    pub kind: LibraryKind,
    /// The paired server a library is shared from (experimental
    /// `federation`): only on a library of another server's, and
    /// only to a client that named the feature. `None` for the server's
    /// own libraries, and left out then, as experimental fields are.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server: Option<LibraryServer>,
}

/// The server a library of another server's is shared from, so a
/// client groups libraries by server, an offline one last and greyed.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct LibraryServer {
    /// Its server id: the origin a `fed:` id names.
    pub server_id: String,
    /// What it calls itself.
    pub name: String,
    /// Whether this server is connected to it now. Offline, what it shared
    /// is still browsed from the cache, and its items do not play.
    pub online: bool,
}

/// The role a piece of artwork plays.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtworkKind {
    Poster,
    Background,
    Thumb,
}

/// A reference to artwork. Clients fetch the image through the client
/// protocol with the `id`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Artwork {
    pub id: ArtworkId,
    pub kind: ArtworkKind,
}

/// The session user's progress on an item.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlaybackState {
    /// Where playback stopped, or `None` if it never started.
    #[serde(with = "duration_ms::option")]
    pub position: Option<Duration>,
    pub watched: bool,
    /// When the user last viewed it, Unix seconds: their last progress
    /// report on it from any device, or its last mark as watched or
    /// unwatched. Filled where its reply says so (Home's rows, an item's
    /// details); `None` elsewhere, and for an item never viewed.
    ///
    /// This and the two below are read as none when left out: the SDK
    /// keeps an item with its download, and a download kept by an older
    /// SDK lacks the keys.
    #[serde(default)]
    pub last_viewed_at: Option<i64>,
    /// How many times the user played it through: their counted plays
    /// from any device. Filled where its reply says so (an
    /// item's details, Recently played); 0 elsewhere.
    #[serde(default)]
    pub play_count: u32,
    /// When the user last played it, Unix seconds: the end of their
    /// latest counted play. Filled where `play_count` is; `None` for an
    /// item never played.
    #[serde(default)]
    pub last_played_at: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Movie {
    pub id: MediaId,
    pub title: String,
    /// The year it came out: the catalog's own, else `release_date`'s.
    pub year: Option<u16>,
    /// The day it came out, `"YYYY-MM-DD"`.
    ///
    /// This and `cast` are read as `None` and no one when left out, though
    /// every server sends them: the SDK keeps an item with its download,
    /// and a download kept by an older SDK lacks the keys.
    pub release_date: Option<CalendarDate>,
    pub summary: Option<String>,
    /// The age rating as the catalog has it ("R", "TV-MA"), shown as
    /// is; `None` when it has none.
    pub content_rating: Option<String>,
    #[serde(with = "duration_ms::option")]
    pub duration: Option<Duration>,
    /// Who is in it and who directed it (see [`CastMember`]).
    #[serde(default)]
    pub cast: Vec<CastMember>,
    pub artwork: Vec<Artwork>,
    pub playback: PlaybackState,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Show {
    pub id: MediaId,
    pub title: String,
    pub year: Option<u16>,
    pub summary: Option<String>,
    /// The age rating as the catalog has it ("R", "TV-MA"), shown as
    /// is; `None` when it has none.
    pub content_rating: Option<String>,
    /// How many seasons it has, specials counted as one, as the catalog
    /// counts them; `None` when it does not say.
    pub season_count: Option<u32>,
    /// Its genres, by the catalog's names and in its order. A listing
    /// may hold only the first few: Plex gives two in listings and
    /// search, and all of them on the show itself.
    pub genres: Vec<String>,
    /// The day its latest episode in the library first aired, for the
    /// end of "2015–2018". Only the show's own item has it; `None` in
    /// listings and search, and where no episode has a date.
    ///
    /// A catalog that does not know whether a show has ended (Plex does
    /// not) still sets it on a running show: a client that shows a run
    /// from it may show one that has not ended.
    ///
    /// This and `cast` are read as `None` and no one when left out, as
    /// [`Movie::release_date`] is.
    pub last_aired: Option<CalendarDate>,
    /// Who is in it and who directed it (see [`CastMember`]).
    #[serde(default)]
    pub cast: Vec<CastMember>,
    pub artwork: Vec<Artwork>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Season {
    pub id: MediaId,
    pub show_id: MediaId,
    /// The show's title, so a season shown alone needs no second request.
    #[serde(deserialize_with = "crate::required::option")]
    pub show_title: Option<String>,
    /// The season number; 0 is specials.
    pub season: u32,
    pub title: String,
    pub artwork: Vec<Artwork>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Episode {
    pub id: MediaId,
    pub show_id: MediaId,
    /// The show's title, so an episode shown alone (Continue watching)
    /// needs no second request.
    #[serde(deserialize_with = "crate::required::option")]
    pub show_title: Option<String>,
    pub season_id: MediaId,
    pub season: u32,
    pub episode: u32,
    pub title: String,
    pub summary: Option<String>,
    /// The episode's own age rating, as on a movie; a client that has
    /// none here may show the show's.
    pub content_rating: Option<String>,
    /// The year it first aired, for "2019" on its page: the catalog's
    /// own, else `air_date`'s.
    ///
    /// This and `air_date` are not `required::option`, as
    /// [`Movie::release_date`] is not.
    pub year: Option<u16>,
    /// The day it first aired, `"YYYY-MM-DD"`.
    pub air_date: Option<CalendarDate>,
    #[serde(with = "duration_ms::option")]
    pub duration: Option<Duration>,
    /// Who is in it and who directed it (see [`CastMember`]): the
    /// episode's own, which may leave out the show's regulars. Left out,
    /// no one, as [`Movie::cast`].
    #[serde(default)]
    pub cast: Vec<CastMember>,
    pub artwork: Vec<Artwork>,
    pub playback: PlaybackState,
}

/// Someone in a movie's, show's or episode's credits: an actor, with the
/// part they play, or a director.
///
/// An item's `cast` is in the catalog's billing order, actors first, then
/// directors. Only the item's own read (`Item`) has it; it is empty in
/// listings, search and the home rows, where Plex gives names without
/// the ids a person is listed by.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CastMember {
    /// The id a person search gives them: their items are listed with
    /// `PersonItems`, as a found person's are.
    pub id: PersonId,
    pub name: String,
    pub role: PersonRole,
    /// The part an actor plays ("Ellen Ripley"), as the catalog has it;
    /// `None` for a director, and for an actor where it does not say.
    #[serde(deserialize_with = "crate::required::option")]
    pub character: Option<String>,
    /// Their portrait, fetched through the client protocol as any
    /// artwork is; `None` where the catalog has none.
    #[serde(deserialize_with = "crate::required::option")]
    pub photo: Option<ArtworkId>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Artist {
    pub id: MediaId,
    pub name: String,
    pub summary: Option<String>,
    pub artwork: Vec<Artwork>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Album {
    pub id: MediaId,
    pub artist_id: MediaId,
    /// The artist's name, for the same reason.
    #[serde(deserialize_with = "crate::required::option")]
    pub artist_name: Option<String>,
    pub title: String,
    pub year: Option<u16>,
    /// How many tracks it has, on every disc. `None` where the catalog
    /// does not say: Plex counts them on the album itself, not in
    /// listings or search.
    pub track_count: Option<u32>,
    pub artwork: Vec<Artwork>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Track {
    pub id: MediaId,
    pub album_id: MediaId,
    pub artist_id: MediaId,
    /// Who it is by, so a track shown alone (a search result, a queue)
    /// says whose it is: the track's own artist where its tags name one
    /// ("Adele with Chris Stapleton"), else its album's.
    pub artist_name: Option<String>,
    /// Its album's title, for the same reason.
    pub album_title: Option<String>,
    pub title: String,
    /// Position on its disc, when the tags say.
    pub track: Option<u32>,
    pub disc: Option<u32>,
    /// How many tracks its album has (as `Album::track_count`), for
    /// "track 3 of 9". Set on an album's own tracks, the children of
    /// its item; `None` elsewhere.
    pub album_track_count: Option<u32>,
    /// The audio codec, `None` when the catalog does not say.
    pub codec: Option<AudioCodec>,
    /// Bits per sample (16, 24); `None` when not known, and for lossy
    /// codecs, which have none. Known for an album's own tracks and a
    /// track's own item, not in other listings or search.
    pub bit_depth: Option<u32>,
    /// Samples per second, in Hz (44_100, 96_000); known where
    /// `bit_depth` is.
    pub sample_rate: Option<u32>,
    #[serde(with = "duration_ms::option")]
    pub duration: Option<Duration>,
    /// Its album's cover as the poster, and its artist's background.
    pub artwork: Vec<Artwork>,
    pub playback: PlaybackState,
}

/// What a marked stretch of an item is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MarkerKind {
    Intro,
    Credits,
    /// A kind this build does not know, from a newer server. Show nothing
    /// for it.
    #[serde(other)]
    Unknown,
}

/// A stretch of a movie or episode a viewer may want to skip.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Marker {
    pub kind: MarkerKind,
    /// Where it starts, in milliseconds from the start of the item.
    pub start_ms: u64,
    /// Where it ends. On a marker that runs to the end this may be a
    /// little off the item's duration, either way: go by `to_end`.
    pub end_ms: u64,
    /// Nothing follows it: credits with no scene after them. The catalog
    /// says so; it is never worked out from `end_ms` and the duration.
    pub to_end: bool,
}

/// Any one item a catalog returns. On the wire it is the item's own object
/// with a `kind` field added, for example `{"kind": "movie", "id": …}`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Item {
    Movie(Movie),
    Show(Show),
    Season(Season),
    Episode(Episode),
    Artist(Artist),
    Album(Album),
    Track(Track),
}

impl Item {
    pub fn id(&self) -> &MediaId {
        match self {
            Item::Movie(m) => &m.id,
            Item::Show(s) => &s.id,
            Item::Season(s) => &s.id,
            Item::Episode(e) => &e.id,
            Item::Artist(a) => &a.id,
            Item::Album(a) => &a.id,
            Item::Track(t) => &t.id,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fmt::Debug;

    use serde::de::DeserializeOwned;

    use super::*;
    use crate::id::{MediaKind, Namespace};

    fn round_trip<T: Serialize + DeserializeOwned + PartialEq + Debug>(value: T) {
        let json = serde_json::to_string(&value).unwrap();
        assert_eq!(serde_json::from_str::<T>(&json).unwrap(), value, "{json}");
    }

    fn media(kind: MediaKind, key: &str) -> MediaId {
        MediaId::new(Namespace::Plex, kind, key).unwrap()
    }

    fn poster() -> Vec<Artwork> {
        vec![Artwork {
            id: ArtworkId::new(Namespace::Plex, "/library/metadata/1/thumb/9").unwrap(),
            kind: ArtworkKind::Poster,
        }]
    }

    fn halfway() -> PlaybackState {
        PlaybackState {
            position: Some(Duration::from_millis(2_220_500)),
            watched: false,
            last_viewed_at: None,
            play_count: 0,
            last_played_at: None,
        }
    }

    fn ripley() -> CastMember {
        CastMember {
            id: PersonId::new(Namespace::Plex, "actor-13").unwrap(),
            name: "Sigourney Weaver".into(),
            role: PersonRole::Actor,
            character: Some("Ellen Ripley".into()),
            photo: Some(ArtworkId::new(Namespace::Plex, "/people/1/a/b.jpg").unwrap()),
        }
    }

    fn scott() -> CastMember {
        CastMember {
            id: PersonId::new(Namespace::Plex, "director-7").unwrap(),
            name: "Ridley Scott".into(),
            role: PersonRole::Director,
            character: None,
            photo: None,
        }
    }

    #[test]
    fn a_cast_member_is_their_person_id_role_and_photo_on_the_wire() {
        assert_eq!(
            serde_json::to_value(ripley()).unwrap(),
            serde_json::json!({
                "id": "plex:actor-13",
                "name": "Sigourney Weaver",
                "role": "actor",
                "character": "Ellen Ripley",
                "photo": "plex:/people/1/a/b.jpg",
            })
        );
        round_trip(ripley());
        round_trip(scott());
    }

    /// A new type, so every field is sent (PX-15): `null` for none.
    #[test]
    fn a_cast_member_s_character_and_photo_are_required() {
        crate::test_support::assert_each_required(&scott(), &["character", "photo"]);
    }

    /// `cast`, `release_date` and `last_aired` came without a revision
    /// bump: a server from before them leaves them out.
    #[test]
    fn items_from_a_server_without_cast_or_dates_still_decode() {
        let movie = Movie {
            id: media(MediaKind::Movie, "10"),
            title: "Alien".into(),
            year: Some(1979),
            release_date: CalendarDate::new(1979, 5, 25),
            summary: None,
            content_rating: None,
            duration: None,
            cast: vec![ripley()],
            artwork: Vec::new(),
            playback: PlaybackState::default(),
        };
        let mut json = serde_json::to_value(&movie).unwrap();
        assert_eq!(json["release_date"], "1979-05-25");
        let object = json.as_object_mut().unwrap();
        object.remove("release_date");
        object.remove("cast");
        let older: Movie = serde_json::from_value(json).unwrap();
        assert_eq!((older.release_date, older.cast), (None, Vec::new()));

        let show = Show {
            id: media(MediaKind::Show, "20"),
            title: "The Expanse".into(),
            year: Some(2015),
            summary: None,
            content_rating: None,
            season_count: None,
            genres: Vec::new(),
            last_aired: CalendarDate::new(2022, 4, 8),
            cast: vec![ripley()],
            artwork: Vec::new(),
        };
        let mut json = serde_json::to_value(&show).unwrap();
        assert_eq!(json["last_aired"], "2022-04-08");
        let object = json.as_object_mut().unwrap();
        object.remove("last_aired");
        object.remove("cast");
        let older: Show = serde_json::from_value(json).unwrap();
        assert_eq!((older.last_aired, older.cast), (None, Vec::new()));

        let episode = Episode {
            id: media(MediaKind::Episode, "22"),
            show_id: media(MediaKind::Show, "20"),
            show_title: None,
            season_id: media(MediaKind::Season, "21"),
            season: 1,
            episode: 1,
            title: "Dulcinea".into(),
            summary: None,
            content_rating: None,
            year: None,
            air_date: None,
            duration: None,
            cast: vec![scott()],
            artwork: Vec::new(),
            playback: PlaybackState::default(),
        };
        let mut json = serde_json::to_value(&episode).unwrap();
        json.as_object_mut().unwrap().remove("cast");
        let older: Episode = serde_json::from_value(json).unwrap();
        assert_eq!(older.cast, Vec::new());
    }

    #[test]
    fn durations_are_whole_milliseconds_on_the_wire() {
        let json = serde_json::to_value(halfway()).unwrap();
        assert_eq!(json["position"], 2_220_500);
    }

    #[test]
    fn an_unstarted_item_has_a_null_position() {
        let json = serde_json::to_value(PlaybackState::default()).unwrap();
        assert!(json["position"].is_null());
    }

    #[test]
    fn a_track_s_format_is_plain_numbers_on_the_wire() {
        let track = Track {
            id: media(MediaKind::Track, "224630"),
            album_id: media(MediaKind::Album, "224629"),
            artist_id: media(MediaKind::Artist, "224303"),
            artist_name: None,
            album_title: None,
            title: "Catch".into(),
            track: Some(1),
            disc: Some(1),
            album_track_count: Some(3),
            codec: Some(AudioCodec::Flac),
            bit_depth: Some(24),
            sample_rate: Some(96_000),
            duration: None,
            artwork: Vec::new(),
            playback: PlaybackState::default(),
        };
        let json = serde_json::to_value(&track).unwrap();
        assert_eq!(
            (
                &json["codec"],
                &json["bit_depth"],
                &json["sample_rate"],
                &json["album_track_count"]
            ),
            (
                &serde_json::json!("flac"),
                &serde_json::json!(24),
                &serde_json::json!(96_000),
                &serde_json::json!(3)
            )
        );
    }

    #[test]
    fn library_kinds_are_snake_case_on_the_wire() {
        assert_eq!(
            serde_json::to_string(&LibraryKind::Movies).unwrap(),
            r#""movies""#
        );
    }

    #[test]
    fn a_marker_is_milliseconds_and_a_snake_case_kind_on_the_wire() {
        let marker = Marker {
            kind: MarkerKind::Credits,
            start_ms: 2_662_962,
            end_ms: 2_709_958,
            to_end: true,
        };
        assert_eq!(
            serde_json::to_value(marker).unwrap(),
            serde_json::json!({
                "kind": "credits",
                "start_ms": 2_662_962,
                "end_ms": 2_709_958,
                "to_end": true,
            })
        );
        round_trip(marker);
    }

    #[test]
    fn a_marker_kind_from_a_newer_server_is_unknown() {
        let marker: Marker = serde_json::from_str(
            r#"{"kind": "recap", "start_ms": 0, "end_ms": 30000, "to_end": false}"#,
        )
        .unwrap();
        assert_eq!(marker.kind, MarkerKind::Unknown);
    }

    #[test]
    fn an_item_carries_its_kind_on_the_wire() {
        let item = Item::Show(Show {
            id: media(MediaKind::Show, "20"),
            title: "The Expanse".into(),
            year: None,
            summary: None,
            content_rating: None,
            season_count: None,
            genres: Vec::new(),
            last_aired: None,
            cast: Vec::new(),
            artwork: Vec::new(),
        });
        let json = serde_json::to_value(&item).unwrap();
        assert_eq!(json["kind"], "show");
        assert_eq!(json["id"], "plex:show:20");
        round_trip(item);
    }

    /// The parent's name on a season, an episode and an album was once
    /// `serde(default)`; every field is now required (PX-15).
    #[test]
    fn a_parent_s_name_left_out_is_refused() {
        let season = Season {
            id: media(MediaKind::Season, "21"),
            show_id: media(MediaKind::Show, "20"),
            show_title: None,
            season: 1,
            title: "Season 1".into(),
            artwork: Vec::new(),
        };
        crate::test_support::assert_each_required(&season, &["show_title"]);
        let episode = Episode {
            id: media(MediaKind::Episode, "22"),
            show_id: media(MediaKind::Show, "20"),
            show_title: None,
            season_id: media(MediaKind::Season, "21"),
            season: 1,
            episode: 1,
            title: "Dulcinea".into(),
            summary: None,
            content_rating: None,
            year: None,
            air_date: None,
            duration: None,
            cast: Vec::new(),
            artwork: Vec::new(),
            playback: PlaybackState::default(),
        };
        crate::test_support::assert_each_required(&episode, &["show_title"]);
        let album = Album {
            id: media(MediaKind::Album, "30"),
            artist_id: media(MediaKind::Artist, "31"),
            artist_name: None,
            title: "Moon Safari".into(),
            year: None,
            track_count: None,
            artwork: Vec::new(),
        };
        crate::test_support::assert_each_required(&album, &["artist_name"]);
        // `null` is how none is sent.
        round_trip(album);
    }

    #[test]
    fn an_episode_from_a_server_without_air_dates_still_decodes() {
        let episode = Episode {
            id: media(MediaKind::Episode, "22"),
            show_id: media(MediaKind::Show, "20"),
            show_title: None,
            season_id: media(MediaKind::Season, "21"),
            season: 1,
            episode: 1,
            title: "Dulcinea".into(),
            summary: None,
            content_rating: None,
            year: Some(2015),
            air_date: CalendarDate::new(2015, 11, 23),
            duration: None,
            cast: Vec::new(),
            artwork: Vec::new(),
            playback: PlaybackState::default(),
        };
        let mut json = serde_json::to_value(&episode).unwrap();
        assert_eq!(json["air_date"], "2015-11-23");
        let object = json.as_object_mut().unwrap();
        object.remove("year");
        object.remove("air_date");
        let older: Episode = serde_json::from_value(json).unwrap();
        assert_eq!((older.year, older.air_date), (None, None));
    }

    #[test]
    fn every_model_type_round_trips() {
        round_trip(Library {
            id: LibraryId::new(Namespace::Plex, "1").unwrap(),
            title: "Movies".into(),
            kind: LibraryKind::Movies,
            server: None,
        });
        round_trip(Movie {
            id: media(MediaKind::Movie, "10"),
            title: "Alien".into(),
            year: Some(1979),
            release_date: CalendarDate::new(1979, 5, 25),
            summary: Some("In space no one can hear you scream.".into()),
            content_rating: Some("R".into()),
            duration: Some(Duration::from_secs(7020)),
            cast: vec![ripley(), scott()],
            artwork: poster(),
            playback: halfway(),
        });
        round_trip(Show {
            id: media(MediaKind::Show, "20"),
            title: "The Expanse".into(),
            year: Some(2015),
            summary: None,
            content_rating: Some("TV-14".into()),
            season_count: Some(6),
            genres: vec!["Science Fiction".into(), "Drama".into()],
            last_aired: CalendarDate::new(2022, 4, 8),
            cast: vec![ripley()],
            artwork: poster(),
        });
        round_trip(Season {
            id: media(MediaKind::Season, "21"),
            show_id: media(MediaKind::Show, "20"),
            show_title: Some("The Expanse".into()),
            season: 1,
            title: "Season 1".into(),
            artwork: Vec::new(),
        });
        round_trip(Episode {
            id: media(MediaKind::Episode, "22"),
            show_id: media(MediaKind::Show, "20"),
            show_title: Some("The Expanse".into()),
            season_id: media(MediaKind::Season, "21"),
            season: 1,
            episode: 1,
            title: "Dulcinea".into(),
            summary: None,
            content_rating: Some("TV-14".into()),
            year: Some(2015),
            air_date: CalendarDate::new(2015, 11, 23),
            duration: Some(Duration::from_secs(2640)),
            cast: vec![scott()],
            artwork: poster(),
            playback: PlaybackState::default(),
        });
        round_trip(Artist {
            id: media(MediaKind::Artist, "30"),
            name: "Boards of Canada".into(),
            summary: None,
            artwork: poster(),
        });
        round_trip(Album {
            id: media(MediaKind::Album, "31"),
            artist_id: media(MediaKind::Artist, "30"),
            artist_name: Some("Boards of Canada".into()),
            title: "Music Has the Right to Children".into(),
            year: Some(1998),
            track_count: Some(18),
            artwork: poster(),
        });
        round_trip(Track {
            id: media(MediaKind::Track, "32"),
            album_id: media(MediaKind::Album, "31"),
            artist_id: media(MediaKind::Artist, "30"),
            artist_name: Some("Boards of Canada".into()),
            album_title: Some("Music Has the Right to Children".into()),
            title: "Roygbiv".into(),
            track: Some(10),
            disc: Some(1),
            album_track_count: Some(18),
            codec: Some(AudioCodec::Flac),
            bit_depth: Some(16),
            sample_rate: Some(44_100),
            duration: Some(Duration::from_secs(151)),
            artwork: poster(),
            playback: PlaybackState {
                position: None,
                watched: true,
                last_viewed_at: None,
                play_count: 0,
                last_played_at: None,
            },
        });
    }
}