//! The holder list against the table specs. Every value is invented //! ([nothing-real-ever-goes-in-cotton-s-public-tree]). use serde_json::json; use super::*; use crate::schema::table_specs::TABLE_SPECS; fn listed(table: &str, column: &str) -> bool { MEDIA_KEY_HOLDERS.iter().any(|(t, c, _)| *t == table && *c == column) || NOT_MEDIA_KEYS.iter().any(|(t, c, _)| *t == table && *c == column) } /// RULE: Every key-shaped column of every table spec (key, *_key, path, *_path, a JSON media) is on MEDIA_KEY_HOLDERS or on NOT_MEDIA_KEYS with what it holds instead, so a column a media writer fills with an object key is never missed by erasure. #[test] fn every_media_key_column_is_on_media_key_holders() { let mut missing = Vec::new(); for spec in TABLE_SPECS { for (column, codec) in spec.columns { if is_key_shaped(column, *codec) && !listed(spec.name, column) { missing.push(format!("{}.{column}", spec.name)); } } } assert!(missing.is_empty(), "key-shaped columns on neither list: {missing:?}"); } // A signed member claim names a public signing key, never an object to fetch // or erase; classifying it as a media holder would give it the wrong lifecycle. #[test] fn a_member_person_claim_names_a_signer_not_a_media_object() { assert!(NOT_MEDIA_KEYS.iter().any(|(table, column, _)| *table == "cotton_member_person_claims" && *column == "person_key" )); assert!(!MEDIA_KEY_HOLDERS.iter().any(|(table, column, _)| *table == "cotton_member_person_claims" && *column == "person_key" )); } /// RULE: Every entry of MEDIA_KEY_HOLDERS and NOT_MEDIA_KEYS names a column a table spec declares, and no column is on both lists. #[test] fn every_holder_entry_names_a_declared_column() { let declared = |table: &str, column: &str| { TABLE_SPECS .iter() .any(|spec| spec.name == table && spec.columns.iter().any(|(name, _)| *name == column)) }; for (table, column, _) in MEDIA_KEY_HOLDERS { assert!(declared(table, column), "{table}.{column} is on MEDIA_KEY_HOLDERS but no spec declares it"); assert!( !NOT_MEDIA_KEYS.iter().any(|(t, c, _)| t == table && c == column), "{table}.{column} is on both lists" ); } for (table, column, _) in NOT_MEDIA_KEYS { assert!(declared(table, column), "{table}.{column} is on NOT_MEDIA_KEYS but no spec declares it"); } } #[test] fn an_items_column_names_each_entry_key_as_text_or_value() { let value = json!([{"key": "ws/messages/t/a.jpg", "content_type": "image/jpeg"}, {"content_type": "x"}, {"key": " "}]); assert_eq!(keys_in_value(KeyShape::Items, &value), vec!["ws/messages/t/a.jpg".to_string()]); let text = json!(value.to_string()); assert_eq!(keys_in_value(KeyShape::Items, &text), vec!["ws/messages/t/a.jpg".to_string()]); assert!(keys_in_value(KeyShape::Items, &json!("not json")).is_empty()); assert_eq!(keys_in_value(KeyShape::Text, &json!("ws/photos/a.jpg")), vec!["ws/photos/a.jpg".to_string()]); assert!(keys_in_value(KeyShape::Text, &json!(null)).is_empty()); } /// RULE: a voice note's playback rendition is the row's own object, so erasure, unlink and the mover see its playbackKey too. #[test] fn a_voice_notes_rendition_is_named_by_its_row() { let media = serde_json::json!([{ "key": "w/messages/t/a.ogg", "contentType": "audio/ogg", "playbackKey": "w/messages/t/b.caf", "playbackContentType": "audio/x-caf" }]); assert_eq!( keys_in_value(KeyShape::Items, &media), vec!["w/messages/t/a.ogg".to_string(), "w/messages/t/b.caf".to_string()] ); }