//! One seeded file, for every test that needs a real one.//!//! Built from `schema::ddl::SCHEMA_DDL` rather than by hand: a helper//! that carried its own DDL would be a second answer to "what shape is this//! table", and the tests would keep passing after the shape changed under them.usecrate::driver::Driver;usecrate::schema::ddl::{SCHEMA_DDL, SCHEMA_VERSION};usecrate::meta::{seed_meta, Meta};pubconst WORKSPACE: &str = "11111111-1111-4111-8111-111111111111";pubconst OWNER: &str = "owner-1";/// An in-memory file carrying the whole schema and nothing else.pubfn empty_file() -> Driver {let db = Driver::open_in_memory().expect("in-memory file opens");for statement in SCHEMA_DDL { db.exec(statement).expect("the schema applies"); }// Stamped, as every file this build creates is: the exchange reads the// stamp to tell a receipt of a newer shape from one it folds. db.exec(&format!("PRAGMA user_version = {SCHEMA_VERSION}")).expect("the stamp"); db}/// The suites' `seeded()`: the schema, an identity, and the workspace row every/// domain row's foreign key points at.pubfn seeded_file(device_id: &str) -> Driver {let db = empty_file(); seed_meta( &db, &Meta { workspace_id: WORKSPACE.to_string(), owner_id: OWNER.to_string(), device_id: device_id.to_string(), last_server_seq: 0, last_hlc: None, }, SCHEMA_VERSION, ) .expect("seeds the identity"); fold_create( &db,"cotton_workspaces", WORKSPACE, serde_json::json!({"id": WORKSPACE,"name": "Asha","owner_user_id": OWNER,"created_at": "2026-08-22T10:30:00.000Z", }), SEED_AUTHOR, SEED_MS, ); db}/// A deterministic stand-in for the caller's randomness — no RNG in a test/// either, so a failure is always reproducible.pubfn counter_seed() -> implFnMut(&mut [u8]) {letmut tick = 0u8;move |bytes: &mut [u8]| {for byte in bytes.iter_mut() { *byte = tick; tick = tick.wrapping_add(7); } }}/// The author a seeded row is created under: not this file's, not any peer a/// test names.pubconst SEED_AUTHOR: &str = "device:seed";/// Before every instant a test writes at, so any write a test makes to a/// seeded row is newer than its create.pubconst SEED_MS: i64 = 1;/// A row CREATED the way a real one is: a create receipt under `author` at/// `ms`, folded. A row inserted by plain SQL has no create receipt, so a later/// change to it would find nothing live under it and leave it dormant.pubfn fold_create( db: &Driver, table: &str, row_id: &str, patch: serde_json::Value, author: &str, ms: i64,) {let patch = patch.as_object().cloned().expect("a patch is an object");let hlc = format!("{ms:013}:0000:{author}");let op = crate::wire::op_of_change( db, &crate::mint::Change::create(table, row_id, patch), &hlc, author, &crate::fixtures::fixture_actor(), format!("op-seed-{table}-{row_id}"), ) .expect("the create's receipt");letmut ops = author_fact_if_owed(db, author, ms);let owed = ops.len(); ops.push(op);let folded = crate::fold::fold_ops(db, &ops).expect("the create folds"); assert!( folded.rejected.is_empty() && folded.applied.len() == owed + 1,"the seeded {table} row did not land: {folded:?}" );}/// The author fact a receipt of `author`'s needs before a file that holds its/// own author's fact applies it — none when the file holds no author yet (it/// parks nobody) or already knows `author`.fn author_fact_if_owed(db: &Driver, author: &str, ms: i64) -> Vec<crate::protocol::types::CottonOp> {let known = |id: &str| { db.get("SELECT 1 AS x FROM cotton_authors WHERE id = ?", &[rusqlite::types::Value::Text(id.to_string())]) .expect("reads") .is_some() };let own = db .get("SELECT author_id FROM _meta", &[]) .expect("reads") .and_then(|row| row.text("author_id").map(str::to_string)) .filter(|id| !id.is_empty());if !own.is_some_and(|id| known(&id)) || known(author) {returnVec::new(); }let workspace = db .get("SELECT workspace_id FROM _meta", &[]) .expect("reads") .and_then(|row| row.text("workspace_id").map(str::to_string)) .expect("the file's workspace");let first_stamp = format!("{:013}:0000:{author}", ms - 1);let fact = serde_json::json!({"id": author,"workspace_id": workspace,"device_id": author,"minted_at": crate::protocol::js::to_iso_string(ms - 1).expect("an instant"),"reason": "first","first_stamp": first_stamp, }); vec![crate::wire::op_of_change( db, &crate::mint::Change::create(crate::fold::AUTHORS_TABLE, author, fact.as_object().cloned().expect("an object"), ), &first_stamp, author, &crate::actor::Actor::device_job(crate::actor::JobName::named("mint-author").expect("a job name")), format!("op-seed-author-{author}"), ) .expect("the author fact's receipt")]}/// The owner projection is local receipt state, never a dangling relationship.pubfn assert_receipt_owners_are_attached(db: &Driver) { assert!(db.get("SELECT 1 FROM _writes_owners o LEFT JOIN _writes w ON w.op_id = o.receipt WHERE w.op_id IS NULL LIMIT 1", &[]).unwrap().is_none());}