Cotton code
56 lines · 3.2 KB · RustRaw
mod support;use rusqlite::types::Value as Sql;use serde_json::{json, Value};use support::harness::Harness;use support::mcp::{call_tool, mint_token};fn story() -> (Harness, String) {    let mut h = Harness::new();    h.clock.set(chrono::DateTime::parse_from_rfc3339("2026-10-07T10:00:00Z").unwrap().into());    let token = mint_token(&mut h, &["read"], "Synthetic story reader");    h.run("INSERT INTO cotton_stories (id,workspace_id,source,account,external_id,kind,caption,posted_at,expires_at,created_at) VALUES ('story-example',?,'instagram','wren.example','123','image','Synthetic story','2026-10-07T09:00:00Z','2026-10-08T09:00:00Z','2026-10-07T09:00:00Z')", &[Sql::Text(h.workspace_id.clone())]);    h.run("INSERT INTO cotton_news_settings (workspace_id,timezone,created_at,updated_at) VALUES (?,'UTC','2026-10-07','2026-10-07') ON CONFLICT(workspace_id) DO UPDATE SET timezone='UTC'", &[Sql::Text(h.workspace_id.clone())]);    (h, token)}fn read(h: &Harness, token: &str) -> Value {    call_tool(h, token, "get_record_by_table", json!({"table":"story","id":"story-example"}))["data"].clone()}// RULE: A kept story never says it is no longer kept, even when its original was already swept; only an unkept story with dropped media makes that claim.#[test]fn story_retention_words_follow_keep_and_media_facts() {    let (h, token) = story();    assert!(read(&h, &token)["retention_line"].is_null());    h.run("UPDATE cotton_stories SET media_dropped_at='2026-10-07T09:30:00Z'", &[]);    assert_eq!(read(&h, &token)["retention_line"], "The picture is no longer kept");    h.run("UPDATE cotton_stories SET kept=1", &[]);    assert_eq!(read(&h, &token)["retention_line"], "Kept — stays until you remove it");    h.run("UPDATE cotton_stories SET media_dropped_at=NULL", &[]);    assert_eq!(read(&h, &token)["retention_line"], "Kept — stays until you remove it");}// RULE: A story's app expiry uses the clock and calendar day, never calls a future story gone, and changes to gone at the expiry instant regardless of Keep.#[test]fn story_expiry_words_distinguish_future_days_from_already_gone() {    let (h, token) = story();    for (at, expected) in [        ("2026-10-07T11:00:00Z", "Goes today"),        ("2026-10-08T09:00:00Z", "Goes tomorrow"),        ("2026-10-10T09:00:00Z", "Goes in 3 days"),        ("2026-10-07T10:00:00Z", "Gone"),        ("2026-10-06T10:00:00Z", "Gone"),    ] {        h.run("UPDATE cotton_stories SET expires_at=?,kept=1", &[Sql::Text(at.into())]);        assert_eq!(read(&h, &token)["expiry_line"], expected, "{at}");    }    h.run("UPDATE cotton_news_settings SET timezone='Pacific/Auckland'", &[]);    h.run("UPDATE cotton_stories SET expires_at='2026-10-07T11:30:00Z'", &[]);    assert_eq!(read(&h, &token)["expiry_line"], "Goes tomorrow", "the calendar day is the workspace's, not UTC");    h.run("UPDATE cotton_stories SET expires_at='not-a-date'", &[]);    assert!(read(&h, &token)["expiry_line"].is_null());    h.run("DELETE FROM cotton_news_settings", &[]);    h.run("UPDATE cotton_stories SET expires_at='2026-10-08T09:00:00Z'", &[]);    assert!(read(&h, &token)["expiry_line"].is_null(), "an unknown calendar must not invent tomorrow");}