mod support; use cotton_tools::mcp::read_db::{Filter, Query, ReadDb}; use rusqlite::types::Value as SqlValue; use serde_json::json; use support::harness::Harness; /// RULE: projecting selected Money rows checks settlement through the sparse /// settlement-link index, never once through the full transaction archive per /// row; only a live actual settlement in this workspace clears an expectation. #[test] fn selected_transactions_seek_their_settlement_links() { let h = Harness::new(); h.run("WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<4000) INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,description,created_at,updated_at) SELECT 'archive-'||i,?,'2025-01-01','Synthetic past payment','2025-01-01','2025-01-01' FROM n", &[SqlValue::Text(h.workspace_id.clone())]); h.run("WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<64) INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,description,status,created_at,updated_at) SELECT 'wanted-'||i,?,'2026-10-20','Synthetic expectation','expected','2026-10-01','2026-10-01' FROM n", &[SqlValue::Text(h.workspace_id.clone())]); for (id, target, status, trashed) in [ ("paid", "wanted-1", "actual", None), ("deleted", "wanted-2", "actual", Some("2026-10-01")), ("not-paid", "wanted-3", "expected", None), ] { h.run("INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,description,status,settles_transaction_id,trashed_at,created_at,updated_at) VALUES (?,?,'2026-10-20','Synthetic settlement',?,?,?,'2026-10-01','2026-10-01')", &[SqlValue::Text(id.into()),SqlValue::Text(h.workspace_id.clone()),SqlValue::Text(status.into()),SqlValue::Text(target.into()),trashed.map_or(SqlValue::Null,|day|SqlValue::Text(day.into()))]); } let elsewhere = support::synthetic_uuid("settlement-outside"); h.run("INSERT INTO cotton_workspaces (id,name,owner_user_id,kind,namespace,created_at) VALUES (?,'Other synthetic database',?,'shared',?,'2026-10-01')", &[SqlValue::Text(elsewhere.clone()),SqlValue::Text(support::synthetic_uuid("settlement-foreign-owner")),SqlValue::Text(elsewhere.clone())]); h.run("INSERT INTO cotton_money_transactions (id,workspace_id,occurred_on,status,settles_transaction_id,created_at,updated_at) VALUES ('outside-payment',?,'2026-10-20','actual','wanted-4','2026-10-01','2026-10-01')", &[SqlValue::Text(elsewhere.clone())]); h.run("WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i<4000) INSERT INTO cotton_money_evidence (id,workspace_id,transaction_id,kind,ref_id,excerpt,attached_by,created_at) SELECT 'evidence-'||i,?,'archive-'||i,'statement_line','synthetic-line-'||i,'Synthetic evidence','import','2025-01-01' FROM n", &[SqlValue::Text(h.workspace_id.clone())]); h.run("INSERT INTO cotton_money_evidence (id,workspace_id,transaction_id,kind,ref_id,excerpt,attached_by,created_at) VALUES ('booked-evidence',?,'wanted-5','statement_line','synthetic-booked','Synthetic evidence','import','2026-10-01')", &[SqlValue::Text(h.workspace_id.clone())]); h.run("INSERT INTO cotton_money_evidence (id,workspace_id,transaction_id,kind,ref_id,excerpt,attached_by,created_at) VALUES ('outside-evidence',?,'wanted-6','statement_line','synthetic-outside','Synthetic evidence','import','2026-10-01')", &[SqlValue::Text(elsewhere)]); let selected = (1..=64).map(|i| (format!("wanted-{i}"), json!({"status":"expected"}).as_object().unwrap().clone())).collect(); h.db.watch_statements(); let rows = cotton_tools::mcp::handlers::money_views::project_rows(&h.db, &h.workspace_id, "transaction", selected).unwrap(); assert_eq!(rows[0]["settled"], json!(true)); assert!(rows[1..].iter().all(|row| row["settled"] == json!(false)), "deleted or expected rows cannot settle another expectation"); assert!(rows[4].get("can_delete").is_none(), "permanent booked evidence protects its transaction"); assert_eq!(rows[5]["can_delete"], json!(true), "another workspace's evidence cannot protect this transaction"); let unpaid = ReadDb::new(&h.db, &h.workspace_id).select(&Query::on("cotton_money_transactions") .columns(&["id"]).eq("id", "wanted-2".to_string()) .filter(Filter::InQuery { column: "id".into(), negate: true, query: Box::new(Query::on("cotton_money_transactions").columns(&["settles_transaction_id"]) .eq("status", "actual".to_string()).filter(Filter::IsNull("trashed_at".into()))), })).unwrap(); assert!(unpaid.iter().any(|row| row.get("id") == Some(&json!("wanted-2"))), "a deleted payment and unrelated null settlement links cannot clear the expectation"); let statements = h.db.steps_by_statement(); let steps: u64 = statements.iter().map(|(steps,_)| *steps).sum(); println!("selected settlement projection: {steps} SQLite steps"); assert!(steps < 20_000, "settlement probes walked transaction history: {steps} steps: {statements:#?}"); }