Cotton code
84 lines · 4.0 KB · RustRaw
//! `spotify.*` against a scripted object: the route each method asks, the//! bearer it carries, the body it sends, and the object's answer or refusal//! handed back as it came.use std::sync::Arc;use cotton_client::transport::object::ObjectAddress;use cotton_client::transport::scripted::{Reply, ScriptedExchange};use serde_json::json;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: the status is one GET of the object's /v1/spotify/status with this phone's credential, answered as the object said it.#[test]fn the_status_is_one_get_to_the_object_with_the_phones_credential() {    let status = json!({"available": true, "account": {"userId": "synthetic-user", "displayName": "Sam Example", "phase": "synced", "syncing": false, "syncedAt": "2026-11-04T05:00:00.000Z", "lastError": null}});    let (door, exchange) = door([Reply::ok(&status.to_string())]);    assert_eq!(over(&door, "spotify.status", &json!({})).expect("an answer"), status);    let sent = exchange.only();    assert_eq!((sent.method.as_str(), sent.path.as_str()), ("GET", "/v1/spotify/status"));    assert_eq!(sent.header("authorization"), Some(format!("Bearer {CREDENTIAL}").as_str()));}// RULE: connecting is one GET answering Spotify's consent page; the phone opens it and makes no second call.#[test]fn connecting_is_one_get_answering_the_consent_page() {    let (door, exchange) = door([Reply::ok(r#"{"authorizeUrl":"https://accounts.spotify.com/authorize?x=1","state":"s"}"#)]);    let answer = over(&door, "spotify.connect", &json!({})).expect("an answer");    assert_eq!(answer["authorizeUrl"], "https://accounts.spotify.com/authorize?x=1");    let sent = exchange.only();    assert_eq!((sent.method.as_str(), sent.path.as_str()), ("GET", "/v1/spotify/connect"));    assert!(sent.body.is_empty());}// RULE: disconnect posts an empty body: the object holds one Spotify account at most and forgets it.#[test]fn disconnecting_posts_an_empty_body() {    let (door, exchange) = door([Reply::ok(r#"{"ok":true,"account":"synthetic-user"}"#)]);    let answer = over(&door, "spotify.disconnect", &json!({"account": "someone-else"})).expect("an answer");    assert_eq!(answer["account"], "synthetic-user");    let sent = exchange.only();    assert_eq!((sent.method.as_str(), sent.path.as_str()), ("POST", "/v1/spotify/disconnect"));    assert_eq!(serde_json::from_slice::<serde_json::Value>(&sent.body).expect("json"), json!({}));}// RULE: the object's refusal reaches Swift by its own code: a Worker with no Spotify client says so by its code.#[test]fn the_objects_refusal_is_handed_back_by_its_code() {    let (door, _exchange) = door([Reply::json(503, r#"{"code":"spotify_unavailable","message":"this Worker has no Spotify client yet"}"#)]);    let error = over(&door, "spotify.connect", &json!({})).expect_err("a refusal");    assert_eq!(error.code, "spotify_unavailable");}#[test]fn a_name_in_the_family_that_nothing_answers_is_told_so_and_sends_nothing() {    let (door, exchange) = door([]);    for method in ["spotify.nonesuch", "spotify.syncNow", "spotify.accounts"] {        assert_eq!(over(&door, method, &json!({})).expect_err("a refusal").code, "unknown_method", "{method}");    }    assert_eq!(exchange.calls(), 0);}// RULE: every spotify.* name is offered to the phone and answered by its arm.#[test]fn every_spotify_method_is_offered_to_the_phone() {    let answered = crate::phone::method_names();    for method in METHODS {        assert!(crate::methods::ALL_METHODS.contains(&method), "{method}");        assert!(crate::methods::CLIENT_METHODS.contains(&method), "{method}");        assert!(answered.contains(&method.to_string()), "{method}");    }}