//! A customer's and a staff member's act picks over a synthetic cache: the //! stepper, a record pick over the served rows, and the answers handed back. use super::*; use crate::phone::business::Cache; const ZONE: &str = "Asia/Kolkata"; fn phone() -> (tempfile::TempDir, CottonHandle) { let dir = tempfile::tempdir().expect("a temp container"); let manifest: Value = serde_json::from_str(include_str!("../../../../apps/cotton/App/CottonManifest/flavour.json")).expect("a manifest"); let handle = CottonHandle::open(crate::handle::OpenConfig { mode: crate::handle::OpenMode::Phone { container_path: dir.path().to_string_lossy().to_string(), previous_container_path: None }, app_version: None, time_zone: Some(ZONE.to_string()), device_secret: Some(crate::fixture::paired_secret()), vault_secret: None, flavor: cotton_core::flavor::Flavor::Real, manifest: Some(manifest), }) .expect("an open core"); (dir, handle) } fn address() -> Value { json!({"server": "https://cotton-business.synthetic.workers.dev", "slug": "studio"}) } /// The shipped yoga kit, a session today and two members, seen as `role`. fn stocked(handle: &CottonHandle, role: &str) -> Value { let (_, cache): (_, Cache) = business::opened(handle, &address()).expect("a cache"); let kit = cotton_core::toolkits::shipped_kit_tools(); let tools: Vec = kit.iter().filter(|p| p.tool.id.starts_with("yoga")).map(|p| serde_json::to_value(&p.tool).unwrap()).collect(); let tables: Vec = kit.iter().filter(|p| p.tool.id == "yoga").flat_map(|p| p.tables.iter().map(|t| serde_json::to_value(t).unwrap())).collect(); cache.replace_definitions(&json!({"business": {"slug": "studio", "name": "Synthetic Studio", "currency": "INR", "timezone": ZONE}, "tools": tools, "tables": tables, "flavour": {}}), "etag-1").expect("definitions"); let today = cotton_core::transforms::zoned_clock::local_day_key(chrono::Utc::now().timestamp_millis(), ZONE).unwrap(); let row = |table: &str, id: &str, data: Value| json!({"table": table, "id": id, "data": data, "owner": null, "version": 3, "deleted": false}); cache.apply(&[ row("yoga_class", "c1", json!({"name": "Flow", "capacity": 10, "minutes": 60, "category": "daily"})), row("yoga_session", "s1", json!({"name": "Flow", "class": "c1", "on": today, "starts": "07:00"})), row("yoga_member", "m1", json!({"name": "Anita", "phone": "+15550001"})), row("yoga_member", "m2", json!({"name": "Bela", "phone": "+15550002"})), ], 3).expect("rows"); cache.set_meta("role", Some(role)).expect("a role"); let (object, cache) = business::opened(handle, &address()).expect("a cache"); object.pages(&cache, Some(ZONE)).expect("pages") } fn book_on_s1(pages: &Value) -> Value { let rows = served(pages); let session = rows.iter().find(|row| row["table"] == "yoga_session" && row["id"] == "s1").expect("the session is served"); session["acts"].as_array().unwrap().iter().find(|act| !act["picks"].as_array().unwrap().is_empty()).expect("an act that asks").clone() } fn asked(handle: &CottonHandle, act: &str, extra: Value) -> Value { let mut request = json!({"step": "pick", "business": address(), "table": "yoga_session", "id": "s1", "act": act, "timezone": ZONE}); for (key, value) in extra.as_object().unwrap() { request[key] = value.clone(); } crate::methods::dispatch(handle, "shell.opened", &request).expect("a step")["step"].clone() } fn committed(handle: &CottonHandle, writes: &Value, words: &str) -> Value { crate::methods::dispatch(handle, "shell.input", &json!({"op": "commit", "writes": writes, "words": words, "timezone": ZONE})).expect("a commit") } /// RULE: A customer's quantity is the core's stepper: it stands at the pick's default, turns by the count asked, offers no step below its min, and the last answer is handed back to post (no act runs in the core: it holds no credential). #[test] fn a_members_places_step_from_the_default_and_hand_back_their_answer() { let (_dir, handle) = phone(); let act = book_on_s1(&stocked(&handle, "member")); let id = act["id"].as_str().unwrap(); assert_eq!(act["act"], "book"); let step = asked(&handle, id, json!({})); let rows = step["rows"].as_array().unwrap(); let rest = step["rest"].as_u64().unwrap() as usize; assert_eq!(rows[rest]["does"], "answer", "the arc rests on the count"); let standing: f64 = rows[rest]["value"].as_str().unwrap().parse().unwrap(); assert!(rows.iter().all(|row| row["does"] != "count" || row["count"].as_f64().unwrap() >= 1.0), "no step below the min: {rows:?}"); let turned = asked(&handle, id, json!({"count": standing + 1.0})); let at = turned["rest"].as_u64().unwrap() as usize; assert_eq!(turned["rows"][at]["value"].as_str().unwrap().parse::().unwrap(), standing + 1.0); let done = committed(&handle, &turned["writes"], "2"); assert_eq!(done["answers"], json!(["2"]), "{done}"); assert!(done.get("step").is_none()); } /// RULE: A staff act's record pick offers the rows the object served of its table, by their titles, with Search; a query narrows them, and a query that finds nothing still stands on Search. #[test] fn a_staff_record_pick_offers_the_served_rows_and_searches_them() { let (_dir, handle) = phone(); let act = book_on_s1(&stocked(&handle, "staff")); let id = act["id"].as_str().unwrap(); let picks = act["picks"].as_array().unwrap(); let at = picks.iter().position(|pick| pick["kind"] == "record").expect("staff are asked whom"); let mut writes = asked(&handle, id, json!({"answers": vec![json!("1"); at]})); assert_eq!(writes["table"], "yoga_member"); let words: Vec<&str> = writes["rows"].as_array().unwrap().iter().map(|row| row["words"].as_str().unwrap()).collect(); assert!(words.contains(&"Anita") && words.contains(&"Bela"), "{words:?}"); assert_eq!(writes["rows"].as_array().unwrap().last().unwrap()["does"], "search"); let narrowed = asked(&handle, id, json!({"answers": vec![json!("1"); at], "query": "ani"})); let words: Vec<&str> = narrowed["rows"].as_array().unwrap().iter().map(|row| row["words"].as_str().unwrap()).collect(); assert_eq!(words.len(), 2, "Anita and Search: {words:?}"); assert_eq!(words[0], "Anita"); let none = asked(&handle, id, json!({"answers": vec![json!("1"); at], "query": "zzz"})); assert_eq!(none["rows"].as_array().unwrap().len(), 1, "Search stands"); assert!(none.get("nothing").is_none()); let answer = writes["rows"][0]["value"].as_str().unwrap().to_string(); let next = committed(&handle, &writes["writes"].take(), &answer); let answered = if at + 1 == picks.len() { next["answers"].clone() } else { next["step"]["writes"]["answers"].clone() }; assert_eq!(answered.as_array().unwrap().last().unwrap(), &json!(answer)); } /// RULE: An act the row does not offer this role is refused, never asked blind. #[test] fn an_act_the_row_does_not_offer_is_refused() { let (_dir, handle) = phone(); stocked(&handle, "public"); let refused = crate::methods::dispatch(&handle, "shell.opened", &json!({"step": "pick", "business": address(), "table": "yoga_session", "id": "s1", "act": "no_such_act", "timezone": ZONE})); assert!(refused.is_err()); }