use std::collections::{BTreeMap, BTreeSet};use cotton_store::create::apply_schema;use cotton_store::schema::ddl::SCHEMA_VERSION;use cotton_store::schema::move_intents::{floor, floor_of, Loss, INTENTS};use cotton_store::shape_move::LEDGER_BASE;use cotton_store::Driver;fn table_columns(db: &Driver) -> BTreeMap<String, BTreeSet<String>> { cotton_store::create::schema_objects(db) .unwrap() .into_iter() .filter(|object| object.kind == "table" && !object.is_fts5_virtual()) .map(|object| {let columns = db .all("SELECT name FROM pragma_table_info(?)", &[object.name.clone().into()], ) .unwrap() .into_iter() .map(|row| row.text("name").unwrap().to_owned()) .collect(); (object.name, columns) }) .collect()}// RULE: Every rise above the oldest stamp this build moves (141) explicitly declares its intended losses, including none, and no rise below it is declared, so a new stamp cannot silently inherit permission to discard data and no declaration outlives the move it authorized.#[test]fn every_schema_declares_its_intended_losses() { assert_eq!( INTENTS .iter() .map(|intent| intent.version) .collect::<BTreeSet<_>>(), (LEDGER_BASE + 1..=SCHEMA_VERSION).collect::<BTreeSet<_>>() );for intent in INTENTS { assert!( !intent.reason.trim().is_empty(),"v{} needs its reason", intent.version ); }}// RULE: Future losses are compared with the immediately preceding schema, including tables introduced after the oldest migration fixtures; each bump must freeze that predecessor's table and column shape.#[test]fn the_predecessor_shape_cannot_lose_undeclared_values() {// Each bump freezes its predecessor's shape: v157 reads v156's.let from = SCHEMA_VERSION - 1;let path = cotton_store::manifest_dir().join(format!("tests/fixtures/migration-columns-v{from}.json"));let bytes = std::fs::read(&path).unwrap_or_else(|_| { panic!("freeze schema {from}'s table/column shape at {} before advancing the schema", path.display() ) }); #[derive(serde::Deserialize)]structFrozenShape { schema_version: u32, tables: BTreeMap<String, BTreeSet<String>>, }let previous: FrozenShape = serde_json::from_slice(&bytes).unwrap(); assert_eq!(previous.schema_version, from);let db = Driver::open_in_memory().unwrap(); apply_schema(&db).unwrap();let current = table_columns(&db);letmut actual = BTreeSet::new();for (table, columns) in previous.tables {match current.get(&table) {None => { actual.insert((table, None)); }Some(now) => {for column in columns.difference(now) { actual.insert((table.clone(), Some(column.clone()))); } } } }let declared: BTreeSet<_> = cotton_store::schema::move_intents::losses_between(from, SCHEMA_VERSION) .map(|loss| match loss {Loss::DropTable { table } => ((*table).to_owned(), None),Loss::FoldColumn { table, column, .. } => { ((*table).to_owned(), Some((*column).to_owned())) } }) .collect(); assert_eq!( actual, declared,"the predecessor's losses need explicit destinations or retirement" );}// RULE: The three most recent frozen bases have no table or column that a// current shape silently omits. A non-additive rise must name every value it// folds or retires even when a device skips an intermediate release.#[test]fn the_last_three_frozen_bases_have_no_missing_key() { #[derive(serde::Deserialize)]structFrozenShape { schema_version: u32, tables: BTreeMap<String, BTreeSet<String>>, }let current_db = Driver::open_in_memory().unwrap(); apply_schema(¤t_db).unwrap();let current = table_columns(¤t_db);for base in (SCHEMA_VERSION - 3)..SCHEMA_VERSION {let path = cotton_store::manifest_dir() .join(format!("tests/fixtures/migration-columns-v{base}.json"));let frozen: FrozenShape = serde_json::from_slice(&std::fs::read(&path).unwrap_or_else(|_| { panic!("freeze schema {base}'s table/column shape at {}", path.display() ) })) .unwrap(); assert_eq!(frozen.schema_version, base);letmut actual = BTreeSet::new();for (table, columns) in frozen.tables {match current.get(&table) {None => { actual.insert((table, None)); }Some(now) => { actual.extend( columns .difference(now) .cloned() .map(|column| (table.clone(), Some(column))), ); } } }let declared: BTreeSet<_> = cotton_store::schema::move_intents::losses_between(base, SCHEMA_VERSION) .map(|loss| match loss {Loss::DropTable { table } => ((*table).to_owned(), None),Loss::FoldColumn { table, column, .. } => { ((*table).to_owned(), Some((*column).to_owned())) } }) .collect(); assert_eq!(actual, declared, "v{base} has an undeclared loss"); }}// RULE: floor_of a stamp is the last rise at or below it that is not additive — every rise above the floor is additive and the floor's own is not — and a stamp this build declares nothing about answers itself, because floor() says which older exchange shapes a build still serves and an unknown rise must never be taken as one it does.#[test]fn the_floor_is_the_last_rise_that_is_not_additive() {let first = INTENTS[0].version;for intent in INTENTS {let floor = floor_of(intent.version); assert!(floor <= intent.version); assert!(INTENTS .iter() .filter(|rise| rise.version > floor && rise.version <= intent.version) .all(|rise| rise.additive)); assert!( floor < first || INTENTS .iter() .any(|rise| rise.version == floor && !rise.additive) ); } assert_eq!(floor(), floor_of(SCHEMA_VERSION)); assert_eq!(floor_of(SCHEMA_VERSION + 1), SCHEMA_VERSION + 1); assert_eq!(floor_of(first - 2), first - 2);}