//! The history field registry — one declaration per versioned kind. //! //! The gates here are the ones a hole would otherwise hide: a kind missing from //! the registry reads on screen as "this record has no history" even though the //! trigger has been writing rows for it all along, and a column that no longer //! exists reads as a field nobody ever changes. Both are silent, so both are //! held here rather than noticed later. //! //! The TypeScript read `TABLE_SPECS` and `VERSIONED_TABLES` out of //! `@cottondb/store`. Both are Rust now — `cotton_store::schema::specs` and //! `cotton_store::schema::versioned::VERSIONED_TABLES` — so this file reads the //! constants themselves, with no regex over a file that could move out from //! under it. An empty list still fails loudly: a declaration that stopped //! declaring would otherwise pass every case here. use std::collections::HashSet; use cotton_store::schema::specs::columns_of; use super::*; use crate::constants::VersionRecordKind; // ── The store's declaration ───────────────────────────────────────────────── // // It was `packages/store/src/schema/versions.ts`, read by path and parsed by // hand, until step 5i deleted that package. The declaration moved into the // store crate rather than going with it (R18): it is // `cotton_store::schema::versioned::VERSIONED_TABLES` now. The five tests // below are unchanged, because what they were ever really for is the // cross-check between that declaration and the trigger SQL in `schema::ddl` — // and both halves are still here. #[derive(Debug)] struct VersionedTable { table: String, kind: String, /// Columns the trigger replaces with `has_` — the secret never /// reaches the snapshot. redact: Vec, /// Columns the trigger leaves out of the snapshot entirely. omit: Vec, } fn versioned_tables() -> Vec { let entries: Vec = cotton_store::schema::versioned::VERSIONED_TABLES .iter() .map(|entry| VersionedTable { table: entry.table.to_string(), kind: entry.kind.to_string(), redact: entry.redact.iter().map(|c| (*c).to_string()).collect(), omit: entry.omit.iter().map(|c| (*c).to_string()).collect(), }) .collect(); assert!( !entries.is_empty(), "VERSIONED_TABLES is empty — the declaration has stopped declaring" ); entries } fn versioned(table: &str) -> Option { versioned_tables().into_iter().find(|v| v.table == table) } // ── HISTORY_KINDS ──────────────────────────────────────────────────────────── #[test] fn names_the_table_the_trigger_for_that_kind_writes_from() { // `journal` is a cotton_notes row whose kind column says journal — the one // kind whose name is not its table's. for kind in VersionRecordKind::ALL { let table = history_kind(kind).table; assert!( versioned(table).is_some(), "{kind} -> {table} is not a versioned table" ); } } #[test] fn only_names_columns_the_snapshot_really_carries() { for kind in VersionRecordKind::ALL { let spec = history_kind(kind); let entry = versioned(spec.table).expect("a versioned table"); let mut columns: HashSet = columns_of(spec.table) .unwrap_or_else(|| panic!("{} has no TABLE_SPEC", spec.table)) .iter() .map(|(name, _)| (*name).to_string()) .collect(); // A redacted column reaches the snapshot as `has_` and never as // itself — the trigger replaces the secret with the fact that it was set. for redacted in &entry.redact { columns.insert(format!("has_{redacted}")); } for field in spec.fields { assert!( columns.contains(field.column), "{kind}.{} is not a column of {}", field.column, spec.table ); } } } #[test] fn never_shows_a_column_the_trigger_omits_from_the_snapshot() { for kind in VersionRecordKind::ALL { let spec = history_kind(kind); let omitted = versioned(spec.table).expect("a versioned table").omit; for field in spec.fields { assert!( !omitted.contains(&field.column.to_string()), "{kind}.{} is omitted from the snapshot", field.column ); } } } #[test] fn never_offers_to_restore_a_column_the_trigger_redacts() { for kind in VersionRecordKind::ALL { let spec = history_kind(kind); let redacted = versioned(spec.table).expect("a versioned table").redact; for column in history_restore_columns(kind) { assert!( !redacted.contains(&(*column).to_string()), "{kind}.{column} is redacted and cannot be restored" ); } } } #[test] fn restores_only_columns_it_also_shows() { for kind in VersionRecordKind::ALL { let shown = history_columns(kind); for column in history_restore_columns(kind) { assert!( shown.contains(column), "{kind}.{column} is restored but not shown" ); } } } #[test] fn gives_every_kind_at_least_one_field_to_draw() { for kind in VersionRecordKind::ALL { assert!(!history_columns(kind).is_empty(), "{kind}"); } } #[test] fn names_no_column_twice_within_one_kind() { for kind in VersionRecordKind::ALL { let columns = history_columns(kind); let unique: HashSet<&&str> = columns.iter().collect(); assert_eq!(unique.len(), columns.len(), "{kind}"); } } /// RULE: every field history draws has a reader's name — the definition's, /// or a line of set `history` the kit ships (a hand label is built at run time /// as `fields..`, so `line_keys` cannot see it; a missing line /// would show the person `history.fields…` instead of a word). #[test] fn labels_every_field_and_never_with_the_raw_column_name() { for kind in VersionRecordKind::ALL { for field in history_fields(kind) { let label = field.label(&[]); assert!(!label.trim().is_empty(), "{kind}.{}", field.column); assert!( !label.contains('_') && !label.starts_with("history."), "{kind}.{} is labelled with a raw column name", field.column ); } } } #[test] fn writes_a_reason_never_a_bare_false_for_a_kind_that_cannot_restore() { for kind in VersionRecordKind::ALL { match history_kind(kind).restore { Restore::Columns(columns) => { assert!(!columns.is_empty(), "{kind}"); assert!(can_restore_kind(kind), "{kind}"); assert_eq!(restore_refusal(kind), None, "{kind}"); } Restore::Refusal => { let block = restore_refusal(kind).expect("a refusal"); assert!(block.words(&[]).len() > 20, "{kind}"); assert!(!can_restore_kind(kind), "{kind}"); assert_eq!(history_restore_columns(kind), &[] as &[&str], "{kind}"); } } } } #[test] fn restores_a_notes_body_and_title() { assert!(history_restore_columns(VersionRecordKind::Note).contains(&"body")); assert!(history_restore_columns(VersionRecordKind::Journal).contains(&"body")); assert!(history_restore_columns(VersionRecordKind::Todo).contains(&"title")); } #[test] fn shows_a_documents_redacted_password_marker_but_cannot_restore_it() { assert!(history_columns(VersionRecordKind::Document).contains(&"has_password")); assert!(!history_restore_columns(VersionRecordKind::Document).contains(&"has_password")); } // ── Node vectors ───────────────────────────────────────────────────────────── /// One `historyField` probe: the kind, the column asked for, and the label /// and form the TypeScript answered with (or nothing, for a column the kind /// does not draw). type FieldProbe = ( VersionRecordKind, &'static str, Option<(&'static str, HistoryFieldForm)>, ); /// `historyField` probes, read out of Node. #[test] fn one_field_is_looked_up_by_kind_and_column() { let vectors: &[FieldProbe] = &[ ( VersionRecordKind::Todo, "title", Some(("Title", HistoryFieldForm::Text)), ), (VersionRecordKind::Todo, "nope", None), ( VersionRecordKind::Document, "has_password", Some(("Password set", HistoryFieldForm::Flag)), ), ( VersionRecordKind::Memory, "title", Some(("Sentence", HistoryFieldForm::Prose)), ), ( VersionRecordKind::Person, "birth_year", Some(("Birth year", HistoryFieldForm::Number)), ), ( VersionRecordKind::Journal, "journal_date", Some(("Day", HistoryFieldForm::Day)), ), ]; for (kind, column, expected) in vectors { let got = history_field(*kind, column).map(|f| (f.label(&[]), f.form)); assert_eq!(got.as_ref().map(|(label, form)| (label.as_str(), *form)), *expected, "{kind}.{column}"); } } /// The store lane's declaration, as this reader sees it today. Not a claim /// about the store — a claim that the READER still works: if this goes empty /// or loses `redact`, every gate above quietly stops testing anything. #[test] fn the_declaration_still_says_what_these_tests_read_it_for() { // 17 since `cotton_orders` left the list with the Stores tool (11 Oct 2026). let tables = versioned_tables(); assert_eq!(tables.len(), 17); assert_eq!( versioned("cotton_documents") .expect("documents are versioned") .redact, ["password"] ); assert_eq!( versioned("cotton_todos").expect("todos are versioned").kind, "todo" ); // Nothing is omitted from any snapshot today; the gate stays for the day // something is. assert!(tables.iter().all(|entry| entry.omit.is_empty())); } #[test] fn message_history_never_restores_a_send_attempt() { // Restoring a queued or confirmed snapshot would either send twice or invent a receipt. assert!(!can_restore_kind(VersionRecordKind::Message)); assert!(history_restore_columns(VersionRecordKind::Message).is_empty()); assert!(restore_refusal(VersionRecordKind::Message).is_some()); } /// RULE: a column a kind's built-in definition names is labelled and typed by /// the definition, never by a hand label here — one name for one column, so a /// rewording of the definition reaches the history screen too (lane T7, /// 22 Oct 2026: stop hand-listing tables). #[test] fn a_column_the_definition_names_takes_the_definitions_name() { let definitions = crate::definitions::builtin_table_definitions(); for kind in VersionRecordKind::ALL { let Some(definition) = definitions.iter().find(|definition| definition.id == kind.as_str()) else { continue; }; for spec in history_kind(kind).fields { let named = definition .columns .iter() .find(|column| column.key == spec.column) .and_then(|column| column.name.as_deref()); if let Some(name) = named { assert!(!spec.hand, "{kind}.{}: the definition names it {name:?}; drop the hand label", spec.column); assert_eq!(history_field(kind, spec.column).map(|field| field.label(&[])), Some(name.to_string()), "{kind}.{}", spec.column); } else { assert!(spec.hand, "{kind}.{}: no definition names it, so it needs a hand label", spec.column); } } } } /// RULE: a hand label is a row of the `lines` table, so a rewording reaches /// the history screen without a build (contract 3). #[test] fn a_line_row_rewords_a_hand_label() { use crate::rules::shell_lines::LineRow; let rows = [LineRow { set: "history".into(), line: "fields.document.has_password".into(), words: "Locked".into() }]; let field = history_field(VersionRecordKind::Document, "has_password").expect("documents draw their password flag"); assert_eq!(field.label(&rows), "Locked"); assert_eq!(field.label(&[]), "Password set"); }