Cotton code
275 lines · 9.2 KB · RustRaw
//! Synthetic handle records through the same generic verbs the person page uses.mod support;use serde_json::{json, Value};use support::harness::Harness;use support::mcp::{call_tool, call_tool_raw, mint_token};fn setup() -> (Harness, String, String, String) {    let mut h = Harness::new();    let token = mint_token(&mut h, &["read", "write"], "Synthetic handles agent");    let person = |name| {        call_tool(&h, &token, "create_person", json!({"fullName":name}))["id"]            .as_str()            .unwrap()            .to_owned()    };    let first = person("Invented Mira");    let second = person("Invented Rowan");    (h, token, first, second)}fn add(h: &Harness, token: &str, person: &str, kind: &str, value: &str) -> Value {    call_tool(        h,        token,        "create_record",        json!({"table":"person_handle","data":{"person_id":person,"kind":kind,"value":value}}),    )}// A duplicate generic create must refuse: reporting an existing handle as a new// birth would let Undo erase a handle this action did not create.#[test]fn person_handles_create_normalises_and_refuses_duplicate_births() {    let (h, token, first, second) = setup();    let added = add(&h, &token, &first, "instagram", " @Invented.Mira ");    let row = &added["record"];    assert_eq!(row["data"]["normalised"], "invented.mira");    assert_eq!(row["data"]["value"], "@Invented.Mira");    assert_eq!(row["data"]["source"], "manual");    assert_eq!(row["data"]["kept"], false);    for person in [&first, &second] {        let refused = call_tool_raw(            &h,            &token,            "create_record",            json!({"table":"person_handle","data":{"person_id":person,"kind":"instagram","value":"invented.mira"}}),        );        assert!(refused.is_error, "{}", refused.text);    }    let unchanged = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person_handle","id":row["id"]}),    );    assert_eq!(unchanged["data"], row["data"]);}// A person's bounded section and its All count must use the same person_id// selection. Phone and email APIs continue to read these very rows.#[test]fn person_handles_narrow_count_and_round_trip_the_canonical_rows() {    let (h, token, first, second) = setup();    for (kind, value) in [        ("phone", "+1 202 555 0147"),        ("email", "MIRA@example.test"),        ("instagram", "@invented.mira"),        ("facebook", "invented.mira"),        ("linkedin", "invented-mira"),    ] {        add(&h, &token, &first, kind, value);    }    add(&h, &token, &second, "email", "rowan@example.test");    let list = call_tool(        &h,        &token,        "list_records",        json!({"table":"person_handle","where":{"person_id":first},"limit":3}),    );    assert_eq!(list["total"], 5);    assert_eq!(list["records"].as_array().unwrap().len(), 3);    assert!(list["records"]        .as_array()        .unwrap()        .iter()        .all(|r| r["data"]["person_id"] == first));    let other = call_tool(        &h,        &token,        "list_records",        json!({"table":"person_handle","where":{"person_id":second}}),    );    assert_eq!(other["total"], 1);    let person = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person","id":first}),    );    assert_eq!(person["data"]["phones"], json!(["+1 202 555 0147"]));    assert_eq!(person["data"]["emails"], json!(["MIRA@example.test"]));}// Keep confirms the existing address without changing its owner or spelling;// Remove retracts only that relation, and Undo restores its provenance.#[test]fn person_handles_keep_remove_and_undo_preserve_the_person_and_other_handles() {    let (h, token, first, _) = setup();    let a = add(&h, &token, &first, "email", "mira@example.test");    let b = add(&h, &token, &first, "phone", "+12025550147");    let id = a["record"]["id"].clone();    let keep = call_tool(        &h,        &token,        "update_record",        json!({"table":"person_handle","id":id,"patch":{"kept":true}}),    );    assert_eq!(keep["record"]["data"]["kept"], true);    assert!(keep["record"]["data"]["verified_at"].is_string());    let again = call_tool_raw(        &h,        &token,        "update_record",        json!({"table":"person_handle","id":id,"patch":{"kept":true}}),    );    assert!(again.is_error, "a repeated Keep writes no new confirmation");    let unchanged = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person_handle","id":id}),    );    assert_eq!(        unchanged["data"]["verified_at"],        keep["record"]["data"]["verified_at"]    );    let removed = call_tool(        &h,        &token,        "delete_record",        json!({"table":"person_handle","id":id}),    );    let remaining = call_tool(        &h,        &token,        "list_records",        json!({"table":"person_handle","where":{"person_id":first}}),    );    assert_eq!(remaining["total"], 1);    assert_eq!(remaining["records"][0]["id"], b["record"]["id"]);    call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person","id":first}),    );    call_tool(        &h,        &token,        "undo",        json!({"change_id":removed["change"]["id"]}),    );    let restored = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person_handle","id":id}),    );    assert_eq!(restored["data"], keep["record"]["data"]);}// Canonical identity, attribution and confirmation timestamps are engine// facts, never generic patches supplied by a caller.#[test]fn person_handles_refuse_identity_and_provenance_patches() {    let (h, token, first, second) = setup();    let added = add(&h, &token, &first, "email", "mira@example.test");    for patch in [        json!({"person_id":second}),        json!({"kind":"phone"}),        json!({"value":"elsewhere@example.test"}),        json!({"normalised":"elsewhere@example.test"}),        json!({"source":"contacts"}),        json!({"verified_at":"2026-10-07T09:00:00Z"}),    ] {        let refused = call_tool_raw(            &h,            &token,            "update_record",            json!({"table":"person_handle","id":added["record"]["id"],"patch":patch}),        );        assert!(refused.is_error, "{}", refused.text);    }    let invalid = call_tool_raw(        &h,        &token,        "create_record",        json!({"table":"person_handle","data":{"person_id":first,"kind":"instagram","value":"not a handle"}}),    );    assert!(invalid.is_error, "{}", invalid.text);}// Undo confirms only what this act changed; a later owner's claim cannot be// stolen by undoing an earlier removal of the same deterministic row id.#[test]fn person_handles_undo_confirmation_and_refuse_to_steal_a_reclaimed_handle() {    let (h, token, first, second) = setup();    let added = add(&h, &token, &first, "email", "mira@example.test");    let id = &added["record"]["id"];    let keep = call_tool(        &h,        &token,        "update_record",        json!({"table":"person_handle","id":id,"patch":{"kept":true}}),    );    call_tool(        &h,        &token,        "undo",        json!({"change_id":keep["change"]["id"]}),    );    let clear = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person_handle","id":id}),    );    assert_eq!(clear["data"]["kept"], false);    assert!(clear["data"]["verified_at"].is_null());    let removed = call_tool(        &h,        &token,        "delete_record",        json!({"table":"person_handle","id":id}),    );    add(&h, &token, &second, "email", "mira@example.test");    let refused = call_tool_raw(        &h,        &token,        "undo",        json!({"change_id":removed["change"]["id"]}),    );    assert!(refused.is_error, "{}", refused.text);    let holder = call_tool(        &h,        &token,        "get_record_by_table",        json!({"table":"person_handle","id":id}),    );    assert_eq!(holder["data"]["person_id"], second);}// RULE: a phone written without its code is read in the country of the// owner's own number, and stays as typed while the owner has none (architect,// 22 Oct 2026: no COMPILED default country; the owner's data may state one).#[test]fn a_code_less_phone_is_read_in_the_owners_country() {    let (h, token, first, second) = setup();    let before = add(&h, &token, &second, "phone", "98765 43299");    assert_eq!(before["record"]["data"]["normalised"], "9876543299", "no owner number, no region");    add(&h, &token, &first, "phone", "+91 98765 43200");    h.with(|db| {        db.run(            "UPDATE cotton_workspaces SET owner_person_id = ? WHERE id = ?",            &[rusqlite::types::Value::Text(first.clone()), rusqlite::types::Value::Text(h.workspace_id.clone())],        )        .unwrap()    });    let after = add(&h, &token, &second, "phone", "098765 43211");    assert_eq!(after["record"]["data"]["normalised"], "919876543211");    assert_eq!(after["record"]["data"]["value"], "098765 43211");    let coded = add(&h, &token, &second, "phone", "+44 20 7946 0958");    assert_eq!(coded["record"]["data"]["normalised"], "442079460958");}