Cotton code
103 lines · 5.0 KB · RustRaw
use super::*;use crate::shell::client::phone_tests::attached;use serde_json::json;fn todo(client: &ClientController, due_at: &str) -> String {    let made = crate::phone::records::call(        client,        "records.create",        &json!({"table":"todo","data":{"title":"Synthetic post the letter","due_at":due_at}}),    )    .unwrap();    ["/record/id", "/id", "/after/id", "/change/after/id"]        .iter()        .find_map(|pointer| made.pointer(pointer).and_then(Value::as_str))        .unwrap_or_else(|| panic!("the made todo's id: {made}"))        .to_string()}fn due(client: &ClientController, id: &str) -> Value {    let todo = crate::phone::records::call(client, "records.get", &json!({"table":"todo","id":id})).unwrap();    todo.get("data").cloned().unwrap_or(todo)["due_at"].clone()}#[test]fn the_when_is_a_write() {    assert!(!crate::methods::read_only("cards.when"));}#[test]fn a_when_keeps_the_half_it_was_not_given() {    let (_dir, client) = attached();    let id = todo(&client, "2026-10-24T09:30:00Z");    let at = |request: Value| {        let mut request = request;        request["table"] = json!("todo");        request["id"] = json!(id);        request["timezone"] = json!("Asia/Kolkata");        when(&client, &request).unwrap();        due(&client, &id)    };    assert_eq!(at(json!({"date":"2026-10-30"})), "2026-10-30T09:30:00.000Z");    assert_eq!(at(json!({"time":"09:00"})), "2026-10-30T03:30:00.000Z");    assert_eq!(at(json!({"time":null})), "2026-10-29T18:30:00.000Z", "no time is local midnight");    assert_eq!(at(json!({"date":"2026-11-02"})), "2026-11-01T18:30:00.000Z", "a new day keeps no time");    assert_eq!(at(json!({"date":"2026-11-03","time":"18:00"})), "2026-11-03T12:30:00.000Z");}#[test]fn a_when_with_nothing_to_write_is_refused() {    let (_dir, client) = attached();    let id = todo(&client, "2026-10-24T09:30:00Z");    assert_eq!(when(&client, &json!({"table":"todo","id":id})).unwrap_err().code, "bad_request");    assert_eq!(when(&client, &json!({"table":"todo","id":id,"time":"9am"})).unwrap_err().code, "bad_request");}/// RULE: a money row's When writes its day, bare, and offers no time./// WHY: held in Money's Future, the expected Rent receipt took When → Tue →/// No time and stayed on its old day: `cards.when` wrote local midnight as an/// instant into `occurred_on`, the money engine refused it, and nothing moved.#[test]fn a_money_rows_when_writes_its_day_and_offers_no_time() {    let (_dir, client) = attached();    let made = crate::phone::records::call(        &client,        "records.create",        &json!({"table":"transaction","data":{"occurred_on":"2026-11-09","due_date":"2026-11-09","description":"Synthetic rent",            "status":"expected","certainty":"fixed","direction":"out","amount_minor":1000,"currency":"INR"}}),    )    .unwrap();    let id = made["record"]["id"].as_str().unwrap().to_string();    let card = json!({"table":"transaction","id":id,"timezone":"Asia/Kolkata"});    let with = |extra: Value| {        let mut request = card.clone();        request.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());        request    };    let times = crate::phone::opened::call(&client, &with(json!({"step":"time"}))).unwrap();    assert_eq!(times["step"]["rows"], json!([]), "a day takes no time: the day is the whole edit");    let wrote = when(&client, &with(json!({"date":"2026-11-10"}))).unwrap_or_else(|error| panic!("{}: {}", error.code, error.message));    assert_eq!((wrote["column"].clone(), wrote["value"].clone()), (json!("occurred_on"), json!("2026-11-10")));    let day = || crate::phone::records::call(&client, "records.get", &json!({"table":"transaction","id":id})).unwrap()["data"]["occurred_on"].clone();    assert_eq!(day(), "2026-11-10");    assert_eq!(when(&client, &with(json!({"date":"2026-11-11","time":"09:30"}))).unwrap_err().code, "bad_request");    assert_eq!(day(), "2026-11-10", "a refused time writes nothing");}/// RULE: a person's When writes their birthday as a bare day./// WHY: on a signed-in phone in India, When → Tomorrow on a person wrote/// local midnight as an instant ("2026-11-07T18:30:00.000Z" for the 8th), so/// the object kept the day before and the card said "Birthday today".#[test]fn a_persons_when_writes_their_birthday_as_a_day() {    let (_dir, client) = attached();    let made = crate::phone::records::call(&client, "records.create", &json!({"table":"person","data":{"full_name":"Invented Dasko"}})).unwrap();    let id = made["record"]["id"].as_str().unwrap().to_string();    let card = json!({"table":"person","id":id,"timezone":"Asia/Kolkata","date":"2026-11-08"});    let wrote = when(&client, &card).unwrap_or_else(|error| panic!("{}: {}", error.code, error.message));    assert_eq!((wrote["column"].clone(), wrote["value"].clone()), (json!("birthday"), json!("2026-11-08")));    let birthday = crate::phone::records::call(&client, "records.get", &json!({"table":"person","id":id})).unwrap()["data"]["birthday"].clone();    assert_eq!(birthday, json!("2026-11-08"));}