//! `google.*` 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) -> (ObjectDoor, Arc) { 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/google/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, "accounts": [{"googleEmail": "owner@example.test", "calendar": {"phase": "synced", "syncing": false, "syncedAt": "2026-10-27T05:00:00.000Z", "lastError": null, "eventCount": 3}}]}); let (door, exchange) = door([Reply::ok(&status.to_string())]); assert_eq!(over(&door, "google.status", &json!({})).expect("an answer"), status); let sent = exchange.only(); assert_eq!((sent.method.as_str(), sent.path.as_str()), ("GET", "/v1/google/status")); assert_eq!(sent.header("authorization"), Some(format!("Bearer {CREDENTIAL}").as_str())); } // RULE: connecting is one GET answering Google'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.google.com/o/oauth2/v2/auth?x=1","state":"s"}"#)]); let answer = over(&door, "google.connect", &json!({})).expect("an answer"); assert_eq!(answer["authorizeUrl"], "https://accounts.google.com/o/oauth2/v2/auth?x=1"); let sent = exchange.only(); assert_eq!((sent.method.as_str(), sent.path.as_str()), ("GET", "/v1/google/connect")); assert!(sent.body.is_empty()); } // RULE: disconnect names the account only when the caller did; an empty body lets the object take the one connected. #[test] fn disconnecting_names_the_account_only_when_asked() { let (door, exchange) = door([Reply::ok(r#"{"ok":true,"account":"a@example.test"}"#), Reply::ok(r#"{"ok":true,"account":"a@example.test"}"#)]); over(&door, "google.disconnect", &json!({"account": "a@example.test"})).expect("an answer"); over(&door, "google.disconnect", &json!({"account": " "})).expect("an answer"); let seen = exchange.seen(); assert_eq!((seen[0].method.as_str(), seen[0].path.as_str()), ("POST", "/v1/google/disconnect")); assert_eq!(serde_json::from_slice::(&seen[0].body).expect("json"), json!({"account": "a@example.test"})); assert_eq!(serde_json::from_slice::(&seen[1].body).expect("json"), json!({})); } // RULE: the object's refusal reaches Swift by its own code: a Worker with no Google client says google_unavailable. #[test] fn the_objects_refusal_is_handed_back_by_its_code() { let (door, _exchange) = door([Reply::json(503, r#"{"code":"google_unavailable","message":"this Worker has no Google client yet"}"#)]); let error = over(&door, "google.connect", &json!({})).expect_err("a refusal"); assert_eq!(error.code, "google_unavailable"); } #[test] fn a_name_in_the_family_that_nothing_answers_is_told_so_and_sends_nothing() { let (door, exchange) = door([]); for method in ["google.nonesuch", "google.syncNow", "google.gmail", "google.accounts"] { assert_eq!(over(&door, method, &json!({})).expect_err("a refusal").code, "unknown_method", "{method}"); } assert_eq!(exchange.calls(), 0); } #[test] fn the_door_never_says_its_credential() { let (door, _exchange) = door([]); assert!(!format!("{door:?}").contains("test-credential")); }