Cotton code
67 lines · 3.5 KB · RustRaw
//! The channels engine on one invented household.use serde_json::{json, Value};use super::*;use crate::definitions::TableDefinition;fn table(id: &str, name: &str, columns: Value) -> TableDefinition {    let title = columns[0]["key"].clone();    serde_json::from_value(json!({        "id": id, "name": name, "plural": format!("{name}s"), "picture": null, "builtin": false, "engine": null,        "columns": columns, "title": title, "date": null, "project": null, "order": null,        "slip": { "title": title, "subtitle": null, "trailing": null, "photo": null, "toggle": null },        "tile": { "face": "slip-list" }, "page": [], "instructions": "A fixture."    }))    .expect("parses")}fn seen(channel: &str, at: &str) -> HandleActivity {    HandleActivity { channel: channel.into(), at: at.into() }}fn said(channels: &[Channel]) -> Vec<(&str, &str, &str, Option<&str>)> {    channels.iter().map(|c| (c.id.as_str(), c.label.as_str(), c.act.as_str(), c.last_activity.as_deref())).collect()}/// RULE: A handle's channels are the ones this database holds activity with it on, newest first, then its kind's own (a number: call, text; an address: mail; a network handle: open), each said by a definition's word; a number with nothing still offers call and text.#[test]fn a_handle_lists_the_channels_its_own_threads_calls_and_mail_show_then_its_kinds() {    let handles = table("handle", "Handle", json!([        { "key": "value", "type": "text", "required": true },        { "key": "kind", "type": "choice", "required": true, "choices": ["phone", "email", "instagram"],          "labels": { "phone": "Phone", "email": "Email", "instagram": "Instagram" } }    ]));    let tables = vec![        table("message", "Message", json!([            { "key": "preview", "type": "text" },            { "key": "channel", "type": "choice", "choices": ["whatsapp", "imessage", "sms", "instagram"],              "labels": { "whatsapp": "WhatsApp", "imessage": "iMessage", "sms": "Text message", "instagram": "Instagram" } }        ])),        table("call", "Call", json!([{ "key": "handle", "type": "text" }])),        table("email", "Mail", json!([{ "key": "subject", "type": "text" }])),        handles.clone(),    ];    let channels = |kind: &str, evidence: &[HandleActivity]| channels_for_handle(kind, evidence, &handles, "kind", &tables);    // A number with a WhatsApp thread and a newer call: the call leads, then    // WhatsApp, then text (the kind's own; call is already listed).    let number = channels("phone", &[seen("whatsapp", "2026-10-01T09:00:00Z"), seen("call", "2026-10-20T18:30:00Z")]);    assert_eq!(said(&number), vec![        ("call", "Call", "call", Some("2026-10-20T18:30:00Z")),        ("whatsapp", "WhatsApp", "message", Some("2026-10-01T09:00:00Z")),        ("sms", "Text message", "message", None),    ]);    // An address with mail: mail, stamped.    assert_eq!(said(&channels("email", &[seen("mail", "2026-10-15T07:00:00Z")])), vec![("mail", "Mail", "mail", Some("2026-10-15T07:00:00Z"))]);    // A bare number: the kind's own channels, unstamped.    assert_eq!(said(&channels("phone", &[])), vec![("call", "Call", "call", None), ("sms", "Text message", "message", None)]);    // A network handle with direct messages: write to it there, then open its profile.    assert_eq!(said(&channels("instagram", &[seen("instagram", "2026-10-10T12:00:00Z")])), vec![        ("instagram", "Instagram", "message", Some("2026-10-10T12:00:00Z")),        ("instagram", "Instagram", "open", None),    ]);}