//! The generic row projection of a soft reference (design 3.9): the value is//! shown while the row it names is present, empty while it is not, and shown//! again when it comes back — nothing on the row is ever nulled for it.usesuper::*;use cotton_store::meta::Meta;const WORKSPACE: &str = "00000000-0000-4000-8000-00000000c0c1";const AT: &str = "2026-10-03T10:00:00.000Z";fn file() -> Driver {let db = cotton_store::test_template::create_workspace_in_memory_for_test(&Meta { workspace_id: WORKSPACE.to_string(), owner_id: "00000000-0000-4000-8000-0000000000ab".to_string(), device_id: "device:read-db-fixture".to_string(), last_server_seq: 0, last_hlc: None, }) .expect("a fresh file"); db.run("INSERT INTO cotton_workspaces (id, name, owner_user_id, created_at, _hlc) VALUES (?, 'Read fixture', \ '00000000-0000-4000-8000-0000000000ab', ?, '')", &[SqlValue::Text(WORKSPACE.to_string()), SqlValue::Text(AT.to_string())], ) .expect("the workspace row"); db}fn text(row: &Map<String, Value>, key: &str) -> Option<String> { row.get(key).and_then(Value::as_str).map(str::to_string)}/// RULE: A soft reference reads as empty while the row it names is not present, and reads as the name again when that row comes back, because the value stays on the row and only the projection hides it.#[test]fn soft_reference_to_a_retracted_row_reads_empty_and_returns_on_undo() {let db = file();let at = SqlValue::Text(AT.to_string()); db.run("INSERT INTO cotton_people (id, workspace_id, full_name, created_at, updated_at) VALUES ('p1', ?, 'Ada', ?, ?)", &[SqlValue::Text(WORKSPACE.to_string()), at.clone(), at.clone()], ) .expect("a person"); db.run("INSERT INTO cotton_msg_contacts (id, workspace_id, person_id, created_at, updated_at) VALUES ('c1', ?, 'p1', ?, ?)", &[SqlValue::Text(WORKSPACE.to_string()), at.clone(), at.clone()], ) .expect("a contact naming the person");let read = ReadDb::new(&db, WORKSPACE);let contact = || { read.by_id("cotton_msg_contacts", "c1") .expect("a read") .expect("the contact") };let named = |filter: Filter| { read.select(&Query::on("cotton_msg_contacts").columns(&["id"]).filter(filter)) .expect("a read") .len() };// Present: the name shows, and the fragment filters agree. assert_eq!(text(&contact(), "person_id").as_deref(), Some("p1")); assert_eq!(named(Filter::SoftNames("person_id".into())), 1); assert_eq!(named(Filter::SoftAbsent("person_id".into())), 0);// Archived (the person's retract today): the reference reads as empty,// but the row still holds it. db.run("UPDATE cotton_people SET archived_at = ? WHERE id = 'p1'", std::slice::from_ref(&at)) .expect("archive the person"); assert_eq!(text(&contact(), "person_id"), None, "a reference to an absent row shows nothing"); assert_eq!(named(Filter::SoftNames("person_id".into())), 0); assert_eq!(named(Filter::SoftAbsent("person_id".into())), 1);let raw = db .get("SELECT person_id FROM cotton_msg_contacts WHERE id = 'c1'", &[]) .expect("a read") .expect("the row"); assert_eq!(raw.text("person_id"), Some("p1"), "the projection hides it; the row keeps it");// Undo: the name is back without a write to the contact. db.run("UPDATE cotton_people SET archived_at = NULL WHERE id = 'p1'", &[]) .expect("unarchive the person"); assert_eq!(text(&contact(), "person_id").as_deref(), Some("p1"));// Gone altogether (still SET NULL today, so the column is nulled by SQLite;// either way the read is empty). db.run("DELETE FROM cotton_people WHERE id = 'p1'", &[]).expect("delete the person"); assert_eq!(text(&contact(), "person_id"), None);}/// A self-reference is the case an unqualified fragment gets wrong: the/// parent's column of the same name would shadow the child's.#[test]fn a_self_reference_names_its_present_parent_and_an_absent_one_reads_empty() {let db = file();let at = SqlValue::Text(AT.to_string());for (id, parent) in [("s1", SqlValue::Null), ("s2", SqlValue::Text("s1".into()))] { db.run("INSERT INTO cotton_sessions (id, workspace_id, tool, last_active_at, created_at, updated_at, \ parent_session_id) VALUES (?, ?, 'claude-code', ?, ?, ?, ?)", &[SqlValue::Text(id.into()),SqlValue::Text(WORKSPACE.to_string()), at.clone(), at.clone(), at.clone(), parent, ], ) .expect("a session"); }let read = ReadDb::new(&db, WORKSPACE);let child = read .by_id("cotton_sessions", "s2") .expect("a read") .expect("the child"); assert_eq!(text(&child, "parent_session_id").as_deref(), Some("s1")); assert_eq!( read.select(&Query::on("cotton_sessions").columns(&["id"]).filter(Filter::SoftNames("parent_session_id".into()))) .expect("a read") .len(),1 );// Only a soft column takes the fragment filters.let refused = read .select(&Query::on("cotton_sessions").columns(&["id"]).filter(Filter::SoftNames("tool".into()))) .expect_err("tool is not a reference"); assert!(refused.to_string().contains("not a soft reference"), "{refused}");}