//! Schema 148: Cotton Calling (12 Nov 2026). A call made in Cotton lands in //! `cotton_call_log` beside the imported history under source `cotton`, so //! the copy keeps no CHECK of its own on `source`: where a call came from is //! the object's definition to say. The table is rebuilt in place with every //! row and rowid; no column is added and none is lost. use std::collections::BTreeSet; use cotton_store::{ create::{apply_schema, fresh_schema, SchemaObject}, schema::move_intents::{rebuilt_above, INTENTS}, shape_move::{crossing, move_to_current}, Driver, }; const TABLE: &str = "cotton_call_log"; /// A file of this build whose call table is 147's, stamped 147. fn at_147() -> (Driver, Vec) { let had: Vec = serde_json::from_str(include_str!("fixtures/schema-v147.json")).unwrap(); let db = Driver::open_in_memory().unwrap(); apply_schema(&db).unwrap(); db.exec("PRAGMA foreign_keys=OFF").unwrap(); let old = had.iter().find(|o| o.kind == "table" && o.name == TABLE).unwrap(); db.exec(&format!("DROP TABLE {TABLE}")).unwrap(); db.exec(&old.sql).unwrap(); for object in had.iter().filter(|o| o.tbl_name == TABLE && o.kind != "table") { let _ = db.exec(&object.sql); } db.exec("PRAGMA foreign_keys=ON").unwrap(); db.exec("PRAGMA user_version=147").unwrap(); (db, had) } const SEED: &str = "INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','now'); INSERT INTO cotton_call_log(id,workspace_id,source,external_id,provider,direction,outcome,started_at,duration_s,handle,created_at,updated_at) VALUES ('c-1','synthetic','ios_call_history','ext-1','cellular','in','answered','2026-11-01T10:00:00Z',61,'+15550100001','now','now'), ('c-2','synthetic','android_call_log','ext-2','cellular','out','missed','2026-11-02T10:00:00Z',0,NULL,'now','now');"; const COLUMNS: &str = "rowid, id, workspace_id, source, external_id, provider, direction, outcome, started_at, duration_s, handle, contact_id, person_id, device, meta, created_at, updated_at, _hlc, _born"; fn cotton_call(db: &Driver, id: &str) -> cotton_store::Result<()> { db.exec(&format!( "INSERT INTO cotton_call_log(id,workspace_id,source,external_id,provider,direction,outcome,started_at,created_at,updated_at) VALUES ('{id}','synthetic','cotton','room-{id}','cotton','out','answered','2026-11-12T10:00:00Z','now','now')" )) } /// RULE: The 148 rise rebuilds cotton_call_log in place — every row, rowid and value kept — and afterwards a call under source `cotton` is kept, while the table's other CHECKs (direction, outcome, a non-empty external id) still hold. #[test] fn every_call_stays_and_a_cotton_call_is_kept() { let (db, had) = at_147(); db.exec(SEED).unwrap(); assert!(cotton_call(&db, "early").is_err(), "147 refuses a call from Cotton"); let before = db.all(&format!("SELECT {COLUMNS} FROM {TABLE} ORDER BY rowid"), &[]).unwrap(); let plan = crossing(&had, &fresh_schema().unwrap()).unwrap(); let rebuilt: BTreeSet<&str> = plan.rebuild.iter().map(String::as_str).collect(); assert_eq!( rebuilt.difference(&rebuilt_above(148)).copied().collect::>(), [TABLE].into_iter().collect(), "the call table, and beside it only a later rise's rebuilds" ); assert_eq!(rebuilt_above(147), rebuilt, "REBUILDS says so"); move_to_current(&db).unwrap(); assert_eq!(db.all(&format!("SELECT {COLUMNS} FROM {TABLE} ORDER BY rowid"), &[]).unwrap(), before, "every row and rowid stays"); cotton_call(&db, "made-here").expect("a call made in Cotton lands"); assert!( db.exec("INSERT INTO cotton_call_log(id,workspace_id,source,external_id,provider,direction,outcome,started_at,created_at,updated_at) VALUES ('c-x','synthetic','cotton','r','cotton','sideways','answered','t','now','now')").is_err(), "a direction is still in or out" ); assert!( db.exec("INSERT INTO cotton_call_log(id,workspace_id,source,external_id,provider,direction,outcome,started_at,created_at,updated_at) VALUES ('c-y','synthetic','cotton',' ','cotton','in','answered','t','now','now')").is_err(), "an external id is never empty" ); } /// RULE: 148 is one rise and not additive: it rebuilds the call table, and it discards no stored value. #[test] fn the_148_rise_rebuilds_and_discards_nothing() { let rises: Vec<_> = INTENTS.iter().filter(|intent| intent.version == 148).collect(); assert_eq!(rises.len(), 1); assert!(!rises[0].additive && rises[0].losses.is_empty()); }