Cotton code
91 lines · 4.2 KB · RustRaw
use serde_json::{json, Value};use super::*;fn deck() -> Value {    json!({        "table": "music_album", "view": "music/listening",        "cards": [            { "id": "a1", "face": "album.sleeve", "slots": { "title": "Lights", "photo": "c1" }, "tap": { "card": "open.whole" }, "acts": ["play"], "position": "1 / 2" },            { "id": "a2", "face": "album.sleeve", "slots": { "title": "Harbour", "subtitle": "Kelmoor", "photo": "c2" }, "tap": { "card": "open.whole" }, "acts": ["play"], "position": "2 / 2" },        ],    })}fn request(playing: Value) -> Value {    json!({ "table": "music_album", "view": "music/listening", "cursor": 0, "playing": playing })}fn song() -> Value {    json!({ "kind": "song", "table": "song", "id": "s1", "title": "Night Ferry", "artist": "Kelmoor", "album": "a2", "album_title": "Harbour", "duration_s": 201.5, "cover": "c9", "file": "k" })}#[test]fn the_album_playing_stands_in_front_on_the_players_face() {    let mut answer = deck();    deal(&mut answer, &request(json!({ "song": song(), "face": "song.tray" }))).unwrap();    let cards = answer["cards"].as_array().unwrap();    assert_eq!(cards.len(), 2);    let front = &cards[0];    assert_eq!(front["id"], "a2");    assert_eq!(front["face"], "song.tray");    assert_eq!(front["slots"]["title"], "Night Ferry");    assert_eq!(front["slots"]["artist"], "Kelmoor");    assert_eq!(front["slots"]["subtitle"], "Kelmoor");    assert_eq!(front["slots"]["album"], "Harbour");    assert_eq!(front["slots"]["duration"], 201.5);    assert_eq!(front["slots"]["photo"], "c2", "the album's own cover");    assert_eq!(front["slots"][MARK], "s1");    assert_eq!(front["tap"]["card"], "open.whole", "the card is still the album's: opening it opens the album");    assert_eq!(front["player"], json!({ "song": "s1", "single": false, "said": null }));    assert_eq!(cards[1]["id"], "a1");    assert_eq!((cards[0]["position"].as_str(), cards[1]["position"].as_str()), (Some("1 / 2"), Some("2 / 2")), "each place says where it now stands");}#[test]fn a_song_on_no_album_is_its_own_single() {    let mut answer = deck();    let mut alone = song();    alone.as_object_mut().unwrap().remove("album");    alone.as_object_mut().unwrap().remove("album_title");    deal(&mut answer, &request(json!({ "song": alone, "face": "song.cassette", "said": "Night Ferry is not on this phone." }))).unwrap();    let cards = answer["cards"].as_array().unwrap();    assert_eq!(cards.len(), 3);    assert_eq!(cards[0]["id"], "s1");    assert_eq!(cards[0]["face"], "song.cassette");    assert_eq!(cards[0]["slots"]["photo"], "c9", "the single's cover is the song's");    assert_eq!(cards[0]["slots"]["album"], "Night Ferry is not on this phone.", "why it cannot play stands in the album's place");    assert_eq!(cards[0]["tap"], json!({}), "a single opens nothing");    assert_eq!(cards[0]["player"]["single"], true);}#[test]fn a_phone_that_draws_some_faces_wears_the_first_of_the_ring_it_draws() {    let drawn = vec!["song.cassette".to_string(), "song.tray".to_string()];    assert_eq!(worn("song.hifi", Some(&drawn)).unwrap(), "song.tray");    assert_eq!(worn("song.cassette", Some(&drawn)).unwrap(), "song.cassette");    assert_eq!(worn("song.hifi", None).unwrap(), "song.hifi");    assert!(worn("song.hifi", Some(&["album.tray".to_string()])).is_err());    let mut answer = deck();    deal(&mut answer, &request(json!({ "song": song(), "face": "song.hifi", "drawn": ["song.tray", "song.cassette"] }))).unwrap();    assert_eq!(answer["cards"][0]["face"], "song.tray");}#[test]fn only_listenings_first_page_deals_the_player() {    let mut answer = deck();    let mut asked = request(json!({ "song": song(), "face": "song.tray" }));    asked["view"] = json!("music/albums");    deal(&mut answer, &asked).unwrap();    assert_eq!(answer, deck());    asked["view"] = json!(VIEW);    asked["cursor"] = json!(50);    deal(&mut answer, &asked).unwrap();    assert_eq!(answer, deck());    let mut episode = song();    episode["kind"] = json!("episode");    deal(&mut answer, &request(json!({ "song": episode, "face": "song.tray" }))).unwrap();    assert_eq!(answer, deck(), "an episode playing is no music player's");    deal(&mut answer, &json!({ "view": VIEW })).unwrap();    assert_eq!(answer, deck());}