Cotton code
92 lines · 4.9 KB · RustRaw
//! Schema 147: Cotton Keys becomes a whole password manager (12 Nov 2026).//! `cotton_secrets`' `kind IN (...)` CHECK goes — the object's definition//! decides which kinds a secret may be, and codes, wifi networks and//! addresses join — by rebuilding the table in place with every row and//! rowid; the table gains `favourite_at`, `uses`, `passkey_id`, `changed_at`//! and `history_enc`, empty on every row that was there.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_secrets";/// A file of this build whose secrets table is 146's, stamped 146.fn at_146() -> (Driver, Vec<SchemaObject>) {    let had: Vec<SchemaObject> = serde_json::from_str(include_str!("fixtures/schema-v146.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=146").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_secrets(id,workspace_id,kind,name,site,username,value_enc,vmk_version,created_at,updated_at)  VALUES ('s-1','synthetic','login','Invented Mail','mail.invented.test','ada','v2:SYNTHETIC',1,'now','now'),         ('s-2','synthetic','card','Invented Card',NULL,NULL,'v2:SYNTHETIC-2',1,'now','now');";const OLD_COLUMNS: &str = "rowid, id, workspace_id, kind, name, site, username, service, value_enc, vmk_version, hint, last_used_at, expires_at, trashed_at, created_at, updated_at, _hlc, _born";/// RULE: The 147 rise rebuilds cotton_secrets in place — every row, rowid and value kept, the new columns empty (uses 0) — and afterwards a code, a wifi network and an address are kept, while the table's other CHECKs still hold.#[test]fn every_secret_stays_and_the_new_kinds_are_kept() {    let (db, had) = at_146();    db.exec(SEED).unwrap();    assert!(        db.exec("INSERT INTO cotton_secrets(id,workspace_id,kind,name,value_enc,created_at,updated_at) VALUES ('s-early','synthetic','code','Invented','v2:x','now','now')").is_err(),        "146 refuses a code"    );    let before = db.all(&format!("SELECT {OLD_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(147)).copied().collect::<BTreeSet<_>>(),        [TABLE].into_iter().collect(),        "the secrets table, and beside it only a later rise's rebuilds"    );    assert_eq!(rebuilt_above(146), rebuilt, "REBUILDS says so");    move_to_current(&db).unwrap();    assert_eq!(db.all(&format!("SELECT {OLD_COLUMNS} FROM {TABLE} ORDER BY rowid"), &[]).unwrap(), before, "every row and rowid stays");    let added = db.all(&format!("SELECT favourite_at, uses, passkey_id, changed_at, history_enc FROM {TABLE} ORDER BY rowid"), &[]).unwrap();    for row in &added {        assert_eq!((row.text("favourite_at"), row.int("uses"), row.text("passkey_id"), row.text("changed_at"), row.text("history_enc")), (None, Some(0), None, None, None));    }    for kind in ["code", "wifi", "address"] {        db.exec(&format!(            "INSERT INTO cotton_secrets(id,workspace_id,kind,name,value_enc,created_at,updated_at) VALUES ('s-{kind}','synthetic','{kind}','Invented','v2:x','now','now')"        ))        .unwrap_or_else(|error| panic!("{kind}: {error}"));    }    assert!(        db.exec("INSERT INTO cotton_secrets(id,workspace_id,kind,name,value_enc,created_at,updated_at) VALUES ('s-x','synthetic','login','','v2:x','now','now')").is_err(),        "a secret still has a name"    );    assert!(        db.exec("INSERT INTO cotton_secrets(id,workspace_id,kind,name,history_enc,created_at,updated_at) VALUES ('s-y','synthetic','login','A','','now','now')").is_err(),        "an empty sealed history is refused"    );    let version = db.get("PRAGMA user_version", &[]).unwrap().unwrap();    assert_eq!(version.int("user_version"), Some(cotton_store::schema::ddl::SCHEMA_VERSION as i64));}/// RULE: 147 is one rise and not additive: it rebuilds the secrets table, and it discards no stored value.#[test]fn the_147_rise_rebuilds_and_discards_nothing() {    let rises: Vec<_> = INTENTS.iter().filter(|intent| intent.version == 147).collect();    assert_eq!(rises.len(), 1);    assert!(!rises[0].additive && rises[0].losses.is_empty());}