Cotton code
47 lines · 2.3 KB · RustRaw
mod support;use rusqlite::types::Value as Sql;use serde_json::{json, Value};use support::harness::{synthetic_uuid, Harness, HARNESS_NOW};use support::mcp::{call_tool, mint_token};fn setup() -> (Harness, String, String, String) {    let mut h = Harness::new();    let token = mint_token(&mut h, &["read"], "Invented reaction reader");    let thread = synthetic_uuid("reaction-thread");    let message = synthetic_uuid("reaction-message");    h.run("INSERT INTO cotton_msg_threads(id,workspace_id,kind,title,created_at,updated_at) VALUES(?,?,'dm','Rhea Finch',?,?)",&[Sql::Text(thread.clone()),Sql::Text(h.workspace_id.clone()),Sql::Text(HARNESS_NOW.into()),Sql::Text(HARNESS_NOW.into())]);    let meta = json!({"source":{"site":"instagram","account":"wren.example","chat":"123","origin":"live",        "reactions":[{"userId":"1001","emoji":"🔥","fromMe":false,"handle":"rhea.example"}]}});    h.run("INSERT INTO cotton_msg_messages(id,thread_id,workspace_id,direction,channel,origin,external_id,body,meta,sent_at,created_at) VALUES(?,?,?,'in','instagram','imported','ig:456','🔥',?,?,?)",&[Sql::Text(message.clone()),Sql::Text(thread.clone()),Sql::Text(h.workspace_id.clone()),Sql::Text(meta.to_string()),Sql::Text(HARNESS_NOW.into()),Sql::Text(HARNESS_NOW.into())]);    (h, token, thread, message)}fn get(h: &Harness, token: &str, table: &str, id: &str) -> Value {    call_tool(        h,        token,        "get_record_by_table",        json!({"table":table,"id":id}),    )["data"]        .clone()}// RULE: Instagram reaction snapshots are readable facts, including non-owner actors; an explicit empty snapshot hides legacy reactions rather than resurrecting a removed reaction.#[test]fn instagram_reactions_read_from_source_facts() {    let (h, token, thread, message) = setup();    assert_eq!(get(&h, &token, "message", &message)["reactions"], "🔥");    let read = call_tool(        &h,        &token,        "read_message_thread",        json!({"threadId":thread}),    );    let rows = read["messages"].as_array().unwrap();    assert_eq!(rows[0]["reactions"][0]["by"], "@rhea.example");    assert_eq!(rows[0]["reactions"][0]["siteUserId"], "1001");    h.run(        "UPDATE cotton_msg_messages SET meta=json_set(meta,'$.source.reactions',json('[]'))",        &[],    );    assert!(get(&h, &token, "message", &message)["reactions"].is_null());}