Cotton code
104 lines · 7.4 KB · RustRaw
//! `asks.*` over a real attached copy and a scripted object door, on a fixed clock: the card an assistant's ask stands//! as, and Allow and Not now posted to the object. Every address, guest and assistant is invented.use std::sync::Arc;use serde_json::{json, Value};use cotton_client::transport::scripted::Reply;use super::call_on;use crate::phone::unlock::tests::door;use crate::shell::client::phone_tests::attached_on_a_clock;use crate::shell::client::ClientController;/// The object's ask rows as a pull lays them in the served table.fn asks(client: &ClientController, rows: &[(&str, &str, &str, &str)]) {    let file = rusqlite::Connection::open(client.layout().replica(&client.current_database().unwrap())).expect("the copy");    file.execute_batch(        "CREATE TABLE IF NOT EXISTS cotton_assistant_ask (id TEXT PRIMARY KEY, workspace_id TEXT, kind TEXT, token_id TEXT, client TEXT, \"from\" TEXT, \"to\" TEXT, cc TEXT, subject TEXT, address TEXT, forward_to TEXT, title TEXT, guests TEXT, count REAL, notify INTEGER, asked_at TEXT, expires_at TEXT, state TEXT);",    )    .expect("the table");    for (id, kind, asked_at, state) in rows {        file.execute(            "INSERT INTO cotton_assistant_ask (id, workspace_id, kind, token_id, client, \"from\", \"to\", cc, subject, address, forward_to, title, guests, count, notify, asked_at, expires_at, state) VALUES (?1, 'w', ?2, 'tok-1', 'Invented Assistant', 'me@invented.example', '[\"asha@invented.example\"]', '[]', 'Invented plans', 'me@invented.example', 'elsewhere@invented.example', 'Invented picnic', '[\"a@invented.example\",\"b@invented.example\"]', 2, 1, ?3, '2099-01-01T00:00:00.000Z', ?4)",            rusqlite::params![id, kind, asked_at, state],        )        .expect("an ask");    }}/// RULE (ROUTES-AUDIT finding 8, coordinator 23 Nov 2026): an assistant's mail send, forwarding change or guest invitation stands as a card on the owner's phone, the oldest waiting first, in plain words; the phone polls while one waits.#[test]fn the_card_is_the_oldest_waiting_ask_in_plain_words() {    let (_dir, client, _clock) = attached_on_a_clock();    assert_eq!(call_on(&client, "asks.card", &json!({})).unwrap(), json!({ "card": null, "pollSeconds": null }));    asks(&client, &[("ask-old", "mail_forward", "2026-11-11T08:00:00.000Z", "refused"), ("ask-send", "mail_send", "2026-11-11T09:00:00.000Z", "waiting"), ("ask-guests", "guests", "2026-11-11T09:05:00.000Z", "waiting")]);    let answer = call_on(&client, "asks.card", &json!({})).unwrap();    assert_eq!(answer["pollSeconds"], 5);    assert_eq!(        answer["card"],        json!({            "askId": "ask-send",            "kind": "mail_send",            "heading": "Invented Assistant wants to send an email from your address",            "lines": ["From me@invented.example", "To asha@invented.example", "Subject: Invented plans"],            "text": null,            "more": null,            "attachments": [],            "allow": { "label": "Allow" },            "refuse": { "label": "Not now" },            "expiresAt": "2099-01-01T00:00:00.000Z",        })    );    let guests = super::card_of(&json!({ "id": "g", "kind": "guests", "client": "Invented Assistant", "title": "Invented picnic", "count": 2, "notify": 1 }), &crate::phone::unlock::lines_of(&client).unwrap());    assert_eq!(guests["heading"], "Invented Assistant wants to invite people to an event");    assert_eq!(guests["lines"], json!(["Invented picnic: 2 guests", "Google would email each of them"]));    let forward = super::card_of(&json!({ "id": "f", "kind": "mail_forward", "client": "", "address": "me@invented.example", "forward_to": null }), &crate::phone::unlock::lines_of(&client).unwrap());    assert_eq!((forward["heading"].as_str(), forward["lines"][0].as_str()), (Some("An assistant wants to change where your mail goes"), Some("Mail to me@invented.example would no longer be forwarded")));}/// RULE (AUDIT C 5, 8fa92ffb38): a mail's card says everything Allow sends: the hidden copy, the words (and how many/// more characters when the object kept only the head), and each attachment by name, type and size.#[test]fn a_mail_card_says_the_hidden_copy_the_words_and_the_files() {    let (_dir, client, _clock) = attached_on_a_clock();    let lines = crate::phone::unlock::lines_of(&client).unwrap();    let row = json!({        "id": "m", "kind": "mail_send", "client": "Invented Assistant", "from": "me@invented.example",        "to": "[\"asha@invented.example\"]", "cc": "[]", "bcc": "[\"quiet@invented.example\",\"also@invented.example\"]",        "subject": "Invented plans", "text": "Hello Asha", "text_chars": 4210,        "attachments": "[\"plan.pdf (application/pdf, 2048 bytes)\",\"unnamed (no type, 3 bytes)\"]",    });    let card = super::card_of(&row, &lines);    assert_eq!(card["lines"], json!(["From me@invented.example", "To asha@invented.example", "Hidden copy to quiet@invented.example, also@invented.example", "Subject: Invented plans"]));    assert_eq!(card["text"], "Hello Asha");    assert_eq!(card["more"], "4200 more characters");    assert_eq!(card["attachments"], json!(["Attached: plan.pdf (application/pdf, 2048 bytes)", "Attached: unnamed (no type, 3 bytes)"]));    let whole = super::card_of(&json!({ "id": "w", "kind": "mail_send", "from": "me@invented.example", "to": ["a@invented.example"], "text": "Hi", "text_chars": 2 }), &lines);    assert_eq!((whole["text"].as_str(), whole["more"].is_null(), whole["lines"].as_array().unwrap().len()), (Some("Hi"), true, 2));    let one = super::card_of(&json!({ "id": "o", "kind": "mail_send", "from": "me@invented.example", "to": ["a@invented.example"], "text": "Hi", "text_chars": 3 }), &lines);    assert_eq!(one["more"], "1 more character");}/// RULE: Allow and Not now go to the object (only it runs the kept request); an ask already answered is said so.#[test]fn allow_and_not_now_are_posted_to_the_object() {    let (_dir, client, _clock) = attached_on_a_clock();    let (object, exchange) = door([        Reply::ok(r#"{"ask_id":"ask-send","state":"done","status":200,"answer":{"ok":true}}"#),        Reply::ok(r#"{"ask_id":"ask-guests","state":"refused"}"#),        Reply::json(409, r#"{"code":"ask_not_waiting","message":"this ask was already answered"}"#),        Reply::ok(r#"{"ask_id":"ask-x","state":"failed","status":400,"answer":{"code":"bad_request","message":"no such address"}}"#),    ]);    client.set_object_door(Some(Arc::new(object)));    assert_eq!(call_on(&client, "asks.allow", &json!({ "askId": "ask-send" })).unwrap(), json!({ "state": "done", "said": "Done" }));    let posted = exchange.nth(0);    assert_eq!((posted.method.as_str(), posted.path.ends_with("/v1/asks/allow")), ("POST", true));    assert_eq!(serde_json::from_slice::<Value>(&posted.body).unwrap(), json!({ "ask_id": "ask-send" }));    assert_eq!(call_on(&client, "asks.refuse", &json!({ "askId": "ask-guests" })).unwrap(), json!({ "state": "refused", "said": "Not done" }));    assert!(exchange.nth(1).path.ends_with("/v1/asks/refuse"));    let gone = call_on(&client, "asks.allow", &json!({ "askId": "ask-send" })).unwrap_err();    assert_eq!((gone.code.as_str(), gone.message.as_str()), ("ask_not_waiting", "This ask was already answered, or its time ran out"));    assert_eq!(call_on(&client, "asks.allow", &json!({ "askId": "ask-x" })).unwrap()["said"], "It didn't work: no such address");    assert_eq!(call_on(&client, "asks.allow", &json!({})).unwrap_err().code, "bad_request");}