Cotton code
49 lines · 2.6 KB · RustRaw
//! Mark watched and Mark unwatched. What plays, the resume point and the//! watch log are `shell.player`'s (`shell_player_kinds_tests`).use serde_json::json;use super::*;use crate::shell::client::phone_tests::attached;fn made_row(client: &ClientController, table: &str, data: Value) -> String {    let made = crate::phone::records::call(client, "records.create", &json!({"table": table, "data": data})).expect("a row");    ["/record/id", "/id", "/after/id", "/change/after/id"].iter().find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)).unwrap_or_else(|| panic!("an id: {made}")).to_string()}fn record(client: &ClientController, table: &str, id: &str) -> Value {    crate::phone::records::call(client, "records.get", &json!({"table": table, "id": id})).expect("the record")["data"].clone()}/// An invented series: one season, three episodes with files.fn series(client: &ClientController) -> (String, Vec<String>) {    let series = made_row(client, "film", json!({"title": "Lowmarsh", "kind": "series"}));    let season = made_row(client, "film_season", json!({"film": series, "number": 1, "title": "Season 1"}));    let episodes = (1..=3)        .map(|n| {            made_row(client, "film_episode", json!({"season": season, "series": series, "number": n, "title": format!("Invented {n}")}))        })        .collect();    (series, episodes)}// RULE: Mark watched stamps the day and puts it on Watched; Mark unwatched puts it back to want to watch; the last episode watched finishes its series.#[test]fn mark_watched_and_unwatched() {    let (_dir, client) = attached();    let film = made_row(&client, "film", json!({"title": "Night Ferry", "kind": "film"}));    let marked = call(&client, "films.mark", &json!({"table": "film", "id": film, "watched": true})).expect("marked");    assert_eq!(marked["said"], "Watched Night Ferry");    let row = record(&client, "film", &film);    assert_eq!(row["status"], "watched");    assert!(row["watched_on"].as_str().is_some() && row["last_watched_at"].as_str().is_some());    call(&client, "films.mark", &json!({"table": "film", "id": film, "watched": false})).expect("unmarked");    let row = record(&client, "film", &film);    assert_eq!(row["status"], "want_to_watch");    assert!(row.get("watched_on").is_none_or(Value::is_null));    let (series_id, episodes) = series(&client);    for episode in &episodes {        call(&client, "films.mark", &json!({"table": "film_episode", "id": episode, "watched": true})).expect("marked");    }    assert_eq!(record(&client, "film", &series_id)["status"], "watched", "every episode watched finishes the series");}