Cotton code
200 lines · 13.2 KB · RustRaw
//! An act at the person's own object: one POST of an admitted act, under//! this phone's credential, its answer as the object gave it.use std::sync::Arc;use cotton_client::transport::object::ObjectAddress;use cotton_client::transport::scripted::{Reply, ScriptedExchange};use serde_json::{json, Value};use super::*;const CREDENTIAL: &str = "ctd_11111111-1111-4111-8111-111111111111_test-credential";fn door(replies: impl IntoIterator<Item = Reply>) -> (ObjectDoor, Arc<ScriptedExchange>) {    let exchange = ScriptedExchange::new(replies);    let door = ObjectDoor {        address: ObjectAddress::open("https://cottondb.example.test").expect("an address"),        exchange: exchange.clone(),        credential: CREDENTIAL.to_string(),    };    (door, exchange)}// RULE: an admitted act (a download's Cancel) is one POST to /v1/act under this phone's credential; Swift gets the object's answer, never the bearer.#[test]fn an_admitted_act_is_one_post_to_the_object() {    let answered = json!({"fetch": "fetch-1", "state": "cancelled"});    let (object, exchange) = door([Reply::ok(&answered.to_string())]);    let answer = act(&object, &json!({"act": "fetch_cancel", "args": {"id": "fetch-1"}})).expect("cancelled");    assert_eq!(answer, answered);    let sent = exchange.only();    assert_eq!((sent.method.as_str(), sent.path.as_str()), ("POST", "/v1/act"));    assert_eq!(sent.header("authorization"), Some(format!("Bearer {CREDENTIAL}").as_str()));    assert_eq!(serde_json::from_slice::<Value>(&sent.body).expect("json"), json!({"act": "fetch_cancel", "args": {"id": "fetch-1"}}));}// RULE: Books' Plus goes through the door with what was typed (looked up by the object, never here), one POST; a book's Start reading and Finished and a Goodreads export are the phone's own acts and never reach the door.#[test]fn the_books_acts_reach_the_object_as_they_are_given() {    let (object, exchange) = door([Reply::ok(r#"{"version":3,"changed":[]}"#)]);    act(&object, &json!({"act": "find_book", "args": {"query": "the lantern orchard"}})).expect("sent");    let only = exchange.only();    assert_eq!(only.path, "/v1/act");    assert_eq!(serde_json::from_slice::<Value>(&only.body).expect("json"), json!({"act": "find_book", "args": {"query": "the lantern orchard"}}));    let csv = "Book Id,Title,Author\n1,An Invented Book,A. Writer\n";    for request in [json!({"act": "start_book", "book": "book-1"}), json!({"act": "finish_book", "book": "book-1"}), json!({"act": "import_goodreads", "args": {"file": csv}})] {        let (object, exchange) = door(Vec::<Reply>::new());        assert_eq!(act(&object, &request).expect_err("the phone's own").code, "bad_request");        assert!(exchange.seen().is_empty());    }}// RULE: the music and cinemas kits' lookups go through the same door with what they name — a song's Find lyrics with its id, an album's cover with its id, a film found by its name or its id — one POST each, looked up by the object, never here.#[test]fn the_music_acts_reach_the_object_as_they_are_given() {    for (request, sent) in [        (json!({"act": "find_lyrics", "song": "song-1"}), json!({"act": "find_lyrics", "args": {"song": "song-1"}})),        (json!({"act": "find_film", "query": "Night Ferry"}), json!({"act": "find_film", "args": {"query": "Night Ferry"}})),        (json!({"act": "find_film", "film": "film-1"}), json!({"act": "find_film", "args": {"film": "film-1"}})),        (json!({"act": "fetch_cover", "args": {"album": "album-1"}}), json!({"act": "fetch_cover", "args": {"album": "album-1"}})),        (json!({"act": "fetch_add", "source": "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"}), json!({"act": "fetch_add", "args": {"source": "magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567"}})),        (json!({"act": "fetch_cancel", "fetch": "fetch-1"}), json!({"act": "fetch_cancel", "args": {"fetch": "fetch-1"}})),        (json!({"act": "fetch_retry", "fetch": "fetch-1"}), json!({"act": "fetch_retry", "args": {"fetch": "fetch-1"}})),    ] {        let (object, exchange) = door([Reply::ok(r#"{"version":3,"changed":[],"filled":[]}"#)]);        act(&object, &request).expect("sent");        let only = exchange.only();        assert_eq!(only.path, "/v1/act");        assert_eq!(serde_json::from_slice::<Value>(&only.body).expect("json"), sent);    }}// RULE (voice plan, 17 Nov 2026): a phone on charge claims a Cotton Meetings recording through the object as the Mac does — `meeting_claim { id, by: phone }`, refused 409 when another transcriber holds it — and gives one up with `meeting_failed`, so the phone and the Mac never both take one recording.#[test]fn the_meetings_queue_acts_reach_the_object_and_its_409_is_said_by_its_word() {    for (request, sent) in [        (json!({"act": "meeting_claim", "id": "m-1", "by": "phone"}), json!({"act": "meeting_claim", "args": {"id": "m-1", "by": "phone"}})),        (json!({"act": "meeting_failed", "id": "m-1", "why": "engine_failed"}), json!({"act": "meeting_failed", "args": {"id": "m-1", "why": "engine_failed"}})),    ] {        let (object, exchange) = door([Reply::ok(r#"{"meeting":"m-1","status":"transcribing","attempts":1}"#)]);        act(&object, &request).expect("sent");        let only = exchange.only();        assert_eq!(only.path, "/v1/act");        assert_eq!(serde_json::from_slice::<Value>(&only.body).expect("json"), sent);    }    let (object, _) = door([Reply::json(409, r#"{"code":"not_waiting","message":"this recording is transcribing: another transcriber may hold it"}"#)]);    let refused = act(&object, &json!({"act": "meeting_claim", "id": "m-1", "by": "phone"})).expect_err("held by another");    assert_eq!(refused.code, "not_waiting");    assert_eq!(refused.details["status"], 409);}// RULE: only the books, music, cinemas, downloads and meetings acts go through this door — no page can make the phone mint a token or write a definition — and nothing else is sent.#[test]fn only_the_admitted_acts_are_sent() {    let (object, exchange) = door(Vec::<Reply>::new());    for refused in [json!({"act": "mint_token", "args": {"name": "x", "scopes": ["owner"]}}), json!({"act": "definition", "args": {}}), json!({"act": "drop_old_music"}), json!({"act": "reshape_films"}), json!({"act": "fetch_claim", "args": {"id": "fetch-1", "machine": "m"}}), json!({"act": "fetch_landed", "args": {"id": "fetch-1", "landed": []}}), json!({"args": {}}), json!({"act": "find_book", "args": "x"}), json!({"act": "find_book", "args": {}, "query": "both"})] {        assert_eq!(act(&object, &refused).expect_err("refused").code, "bad_request", "{refused}");    }    assert!(exchange.seen().is_empty());}// RULE: the object's refusal reaches the person by its own word (feed_unreadable), and a request that reached nothing is offline.#[test]fn the_objects_refusal_is_said_by_its_word() {    let (object, _) = door([Reply::json(502, r#"{"code":"feed_unreadable","message":"the feed is not RSS or Atom"}"#)]);    let refused = act(&object, &json!({"act": "find_lyrics", "args": {"song": "song-1"}})).expect_err("refused");    assert_eq!(refused.code, "feed_unreadable");    assert!(refused.message.contains("RSS"));}// RULE (owner on Cotton Cinemas 1.0 (4), 7 Nov 2026): Find a film on an object holding no TMDB key is said in the kit's words, plainly; the object's own sentence stays for the assistants that read it.#[test]fn find_a_film_unconnected_is_said_plainly() {    let (object, _) = door([Reply::json(404, r#"{"code":"not_connected","message":"TMDB is not connected on this server: Find a film looks films up on TMDB, and this server holds no TMDB key"}"#)]);    let refused = act(&object, &json!({"act": "find_film", "args": {"query": "Synthetic Harbour Lights"}})).expect_err("refused");    assert_eq!(refused.code, "not_connected");    assert_eq!(refused.message, "Find a film is not connected yet. Add films from Files for now.");    assert_eq!(refused.details["status"], 404);}// RULE (android-finish's Cinemas walk, 25 Nov 2026): a film's Find details unconnected says so in its own words, not Find a film's.#[test]fn find_details_unconnected_is_said_as_find_details() {    let (object, _) = door([Reply::json(404, r#"{"code":"not_connected","message":"TMDB is not connected on this server"}"#)]);    let refused = act(&object, &json!({"act": "find_film", "args": {"film": "f-1"}})).expect_err("refused");    assert_eq!(refused.message, "Finding a film's details is not connected yet.");}// RULE: a book's Find details (`find_book { book }`) asks the object by the book's ISBN where it holds one, else by its title and author as this phone's copy says them; a typed name goes as it is.#[test]fn find_details_asks_by_the_books_isbn_else_its_title_and_author() {    use crate::shell::client::phone_tests::attached;    let (_dir, client) = attached();    let make = |data: Value| {        let made = crate::phone::records::call(&client, "records.create", &json!({"table": "book", "data": data})).expect("a book");        made.pointer("/record/id").and_then(Value::as_str).expect("an id").to_string()    };    let with_isbn = make(json!({"title": "The Lantern Orchard", "author": "Ada Vale", "isbn": "9780141439518"}));    let without = make(json!({"title": "The Lantern Orchard", "author": "Ada Vale"}));    for (request, sent) in [        (json!({"act": "find_book", "book": with_isbn}), json!({"act": "find_book", "args": {"book": with_isbn, "isbn": "9780141439518"}})),        (json!({"act": "find_book", "book": without}), json!({"act": "find_book", "args": {"book": without, "query": "The Lantern Orchard Ada Vale"}})),        (json!({"act": "find_book", "args": {"query": "winter orbits"}}), json!({"act": "find_book", "args": {"query": "winter orbits"}})),    ] {        let (object, exchange) = door([Reply::ok(r#"{"book":"b-1","made":false,"filled":["year"]}"#)]);        client.set_object_door(Some(Arc::new(object)));        call_on(&client, &request).expect("asked");        assert_eq!(serde_json::from_slice::<Value>(&exchange.only().body).expect("json"), sent);    }    client.set_object_door(None);}// RULE (e2e rulings R2): a film's Find details (`find_film { film }`) hands over its title, year and TMDB id as this phone's copy says them, since a sealed film's words are not the object's to read.#[test]fn find_film_details_hand_over_the_films_words() {    use crate::shell::client::phone_tests::attached;    let (_dir, client) = attached();    let made = crate::phone::records::call(&client, "records.create", &json!({"table": "film", "data": {"title": "Night Ferry", "year": "2019", "kind": "film"}})).expect("a film");    let film = made.pointer("/record/id").and_then(Value::as_str).expect("an id").to_string();    let (object, exchange) = door([Reply::ok(r#"{"film":"f-1","made":false,"filled":["minutes"]}"#)]);    client.set_object_door(Some(Arc::new(object)));    call_on(&client, &json!({"act": "find_film", "film": film})).expect("asked");    assert_eq!(serde_json::from_slice::<Value>(&exchange.only().body).expect("json"), json!({"act": "find_film", "args": {"film": film, "title": "Night Ferry", "year": "2019"}}));    client.set_object_door(None);}// RULE (e2e rulings R2): Find lyrics and Find cover hand over the song's or album's words as this phone's copy says them, the song naming its album's title, since a sealed row's words are not the object's to read.#[test]fn find_lyrics_and_cover_hand_over_the_rows_words() {    use crate::shell::client::phone_tests::attached;    let (_dir, client) = attached();    let make = |table: &str, data: Value| {        let made = crate::phone::records::call(&client, "records.create", &json!({"table": table, "data": data})).expect("a row");        made.pointer("/record/id").and_then(Value::as_str).expect("an id").to_string()    };    let album = make("music_album", json!({"title": "Harbour Lights", "artist_name": "Ana Vell"}));    let song = make("song", json!({"title": "Low Tide", "artist_name": "Ana Vell", "album": album}));    for (request, sent) in [        (json!({"act": "find_lyrics", "song": song}), json!({"act": "find_lyrics", "args": {"song": song, "title": "Low Tide", "artist_name": "Ana Vell", "album_title": "Harbour Lights"}})),        (json!({"act": "fetch_cover", "album": album}), json!({"act": "fetch_cover", "args": {"album": album, "title": "Harbour Lights", "artist_name": "Ana Vell"}})),    ] {        let (object, exchange) = door([Reply::ok(r#"{"filled":[]}"#)]);        client.set_object_door(Some(Arc::new(object)));        call_on(&client, &request).expect("asked");        assert_eq!(serde_json::from_slice::<Value>(&exchange.only().body).expect("json"), sent);    }    client.set_object_door(None);}// RULE: an act at the object (a download's Cancel) on a phone not signed in is refused sign_in_first, naming the act.#[test]fn an_object_act_before_sign_in_is_refused_sign_in_first() {    use crate::shell::client::phone_tests::attached;    let (_dir, client) = attached();    client.set_object_door(None);    let refused = call_on(&client, &json!({"act": "fetch_cancel", "args": {"id": "fetch-1"}})).expect_err("no object");    assert_eq!(refused.code, SIGN_IN_FIRST);    assert_eq!(refused.message, "Sign in first: this needs your account.");    assert_eq!(refused.details["act"], json!("fetch_cancel"));}