Cotton code
73 lines · 3.6 KB · RustRaw
mod support;use serde_json::json;use support::harness::Harness;use support::mcp::{call_tool, call_tool_raw, mint_token};/// RULE: External money rows retain their currency and person, distinct source ids remain distinct, and importing a repeated row never adds the same money twice.#[test]fn external_rows_are_idempotent_without_collapsing_distinct_payments_or_currencies() {    let mut h = Harness::new();    let token = mint_token(&mut h, &["read", "write"], "Synthetic external writer");    let person = call_tool(        &h,        &token,        "create_person",        json!({"fullName":"Synthetic payer"}),    );    let args = json!({"sourceLabel":"Synthetic shared expenses","rows":[        {"externalId":"first","date":"2026-10-17","amountMinor":100,"direction":"out","text":"Synthetic dinner","currency":"EUR","personId":person["id"]},        {"externalId":"first","date":"2026-10-17","amountMinor":100,"direction":"out","text":"Synthetic dinner","currency":"EUR","personId":person["id"]},        {"externalId":"second","date":"2026-10-17","amountMinor":100,"direction":"out","text":"Synthetic dinner","currency":"EUR","personId":person["id"]},        {"externalId":"third","date":"2026-10-17","amountMinor":100,"direction":"out","text":"Synthetic dinner","currency":"INR","personId":person["id"]}    ]});    call_tool(&h, &token, "match_external", args.clone());    let mut retry = args;    retry["rows"][0]["text"] = json!("Synthetic corrected source label");    call_tool(&h, &token, "match_external", retry);    let rows=h.with(|db|db.all("SELECT currency,counterparty_person_id,between_us,moves_money FROM cotton_money_transactions",&[])).unwrap();    assert_eq!(rows.len(), 3);    assert_eq!(        rows.iter()            .filter(|row| row.text("currency") == Some("EUR"))            .count(),        2    );    for row in rows {        assert_eq!(row.text("counterparty_person_id"), person["id"].as_str());        assert_eq!(row.int("between_us"), Some(1));        assert_eq!(row.int("moves_money"), Some(0));    }    assert!(call_tool_raw(&h,&token,"match_external",json!({"sourceLabel":"Synthetic missing currency","rows":[{"date":"2026-10-17","amountMinor":100,"direction":"out","text":"Synthetic unknown currency"}]})).is_error);}/// RULE: An external shared payment with an unresolved person waits for a link instead of becoming an unlinked row that vanishes from Between us.#[test]fn unresolved_people_are_reported_without_creating_unlinked_money() {    let mut h = Harness::new();    let token = mint_token(&mut h, &["read", "write"], "Synthetic unresolved import");    let input = json!({"sourceLabel":"Synthetic split","rows":[{"date":"2026-10-17","amountMinor":1250,"direction":"out","text":"Synthetic shared meal","currency":"EUR","person":"Synthetic missing person"}]});    let first = call_tool(&h, &token, "match_external", input.clone());    assert!(first["created"].as_array().unwrap().is_empty());    assert_eq!(first["unresolved"].as_array().unwrap().len(), 1);    assert!(        call_tool(&h, &token, "list_money_transactions", json!({}))["transactions"]            .as_array()            .unwrap()            .is_empty()    );    let person = call_tool(        &h,        &token,        "create_person",        json!({"fullName":"Synthetic missing person"}),    );    let imported = call_tool(&h, &token, "match_external", input);    assert_eq!(imported["created"].as_array().unwrap().len(), 1);    let rows = call_tool(        &h,        &token,        "list_money_transactions",        json!({"personId":person["id"]}),    );    assert_eq!(rows["transactions"][0]["betweenUs"], true);}