//! The declaration's own rules. The ONE reader that acts on it is tested where //! it lives, in `outbox_tests.rs` beside this file. The server's `_ops` log was //! the second, and it went with the log. use super::*; use serde_json::json; fn patch(value: serde_json::Value) -> Map { value.as_object().cloned().expect("an object") } #[test] fn a_reading_is_a_declared_table_naming_a_declared_column() { assert!(is_presence_patch( "cotton_devices", Some(&patch(json!({ "sampled_at": "2026-09-11T00:00:00.000Z" }))) )); // A declared table, but the patch is the file's most consequential fact: // who was admitted and who was cut off is never a reading. assert!(!is_presence_patch( "cotton_devices", Some(&patch(json!({ "revoked_at": "2026-09-11T00:00:00.000Z" }))) )); assert!(is_presence_patch( "cotton_sessions", Some(&patch(json!({ "listened_at": "2026-09-11T00:00:00.000Z" }))) )); // A declared table, but the patch is all history. assert!(!is_presence_patch( "cotton_sessions", Some(&patch(json!({ "summary": "shipped the thing" }))) )); // An undeclared table is never a reading, whatever it carries. assert!(!is_presence_patch( "cotton_todos", Some(&patch(json!({ "seen_at": "2026-09-11T00:00:00.000Z" }))) )); } #[test] fn a_delete_is_never_a_reading() { // The `samples-retention` sweep deletes a month-old sample, and that op has // to reach every device — superseding it would leave the row on every phone // for ever. assert!(!is_presence_patch("cotton_devices", None)); assert!(!supersedes( "cotton_devices", None, "hlc-2", Some(&patch(json!({ "sampled_at": "a" }))), "hlc-1" )); } #[test] fn an_identical_stamp_supersedes_its_predecessor() { let reading = json!({ "workspace_id": "ws", "load1": 0.4, "cpu_count": 8, "mem_free_pct": 40, "sampled_at": "2026-09-11T00:00:30.000Z", "updated_at": "2026-09-11T00:00:30.000Z", }); let older = json!({ "workspace_id": "ws", "load1": 0.2, "cpu_count": 8, "mem_free_pct": 60, "sampled_at": "2026-09-11T00:00:00.000Z", "updated_at": "2026-09-11T00:00:00.000Z", }); assert!(supersedes( "cotton_devices", Some(&patch(reading)), "hlc-2", Some(&patch(older)), "hlc-1" )); } #[test] fn a_stamp_never_supersedes_the_rows_birth() { // An insert carries `created_at`; no later stamp does. The subset test is // what keeps the birth in the log, so a replay from zero still creates the // row before it updates it. let insert = patch(json!({ "workspace_id": "ws", "sampled_at": "2026-09-11T00:00:00.000Z", "created_at": "2026-09-11T00:00:00.000Z", "updated_at": "2026-09-11T00:00:00.000Z", })); let stamp = patch(json!({ "workspace_id": "ws", "sampled_at": "2026-09-11T00:00:30.000Z", "updated_at": "2026-09-11T00:00:30.000Z", })); assert!(!supersedes( "cotton_devices", Some(&stamp), "hlc-2", Some(&insert), "hlc-1" )); } #[test] fn a_stamp_never_supersedes_an_op_carrying_a_column_it_does_not() { // `stamp_heartbeat` writes `account` the first time it sees a session and // `title` whenever one is sent. A plain `listened_at` stamp standing in for // one of those would drop the column from a replay. let titled = patch(json!({ "listened_at": "2026-09-11T00:00:00.000Z", "title": "lane C", "updated_at": "2026-09-11T00:00:00.000Z", })); let plain = patch(json!({ "listened_at": "2026-09-11T00:00:30.000Z", "updated_at": "2026-09-11T00:00:30.000Z", })); assert!(!supersedes( "cotton_sessions", Some(&plain), "hlc-2", Some(&titled), "hlc-1" )); // The other way round it is allowed: the titled op carries everything the // plain one did. assert!(supersedes( "cotton_sessions", Some(&titled), "hlc-2", Some(&plain), "hlc-1" )); } #[test] fn an_op_older_by_hlc_supersedes_nothing() { // The fold is per-column last-write-wins by hlc. An op that arrives later // in `server_seq` but is OLDER in hlc lost the fold; removing the winner // would make a replay from zero land somewhere the file never was. let a = patch(json!({ "sampled_at": "2026-09-11T00:00:00.000Z" })); let b = patch(json!({ "sampled_at": "2026-09-11T00:00:30.000Z" })); assert!(!supersedes("cotton_devices", Some(&b), "hlc-1", Some(&a), "hlc-2")); assert!(!supersedes("cotton_devices", Some(&b), "hlc-1", Some(&a), "hlc-1")); } #[test] fn every_declared_column_exists_on_its_table() { // A typo here is a column that never supersedes anything and a guard that // reads as green — so the declaration is held against the schema itself. for (table, columns) in PRESENCE_COLUMNS { let spec = crate::schema::specs::spec_for(table) .unwrap_or_else(|| panic!("{table} is not a table this file has")); for column in *columns { assert!( spec.columns.iter().any(|(name, _)| name == column), "{table} has no column {column}" ); } } }