seal.rs (3533B)
1 use radroots_event::envelope::kind::{KIND_MESSAGE, KIND_SEAL}; 2 use radroots_event::social::seal::Seal; 3 4 mod common; 5 6 use common::{AUTHOR, EVENT_ID, EVENT_SIG}; 7 use radroots_event_codec::decode::EventParseError; 8 use radroots_event_codec::decode::seal::{data_from_event, parsed_from_event, seal_from_parts}; 9 use radroots_event_codec::encode::EventEncodeError; 10 use radroots_event_codec::encode::seal::{seal_build_tags, to_wire_parts, to_wire_parts_with_kind}; 11 12 #[test] 13 fn seal_to_wire_parts_requires_content() { 14 let seal = Seal { 15 content: " ".to_string(), 16 }; 17 18 let err = to_wire_parts(&seal).unwrap_err(); 19 assert!(matches!( 20 err, 21 EventEncodeError::EmptyRequiredField("content") 22 )); 23 } 24 25 #[test] 26 fn seal_to_wire_parts_sets_kind_and_content() { 27 let seal = Seal { 28 content: "payload".to_string(), 29 }; 30 31 let parts = to_wire_parts(&seal).unwrap(); 32 assert_eq!(parts.kind, KIND_SEAL); 33 assert_eq!(parts.content, "payload"); 34 assert!(parts.tags.is_empty()); 35 } 36 37 #[test] 38 fn seal_from_parts_rejects_wrong_kind() { 39 let err = seal_from_parts(KIND_MESSAGE, &[], "payload").unwrap_err(); 40 assert!(matches!( 41 err, 42 EventParseError::InvalidKind { 43 expected: "13", 44 got: KIND_MESSAGE 45 } 46 )); 47 } 48 49 #[test] 50 fn seal_from_parts_requires_empty_tags() { 51 let err = seal_from_parts( 52 KIND_SEAL, 53 &[vec!["p".to_string(), "x".to_string()]], 54 "payload", 55 ) 56 .unwrap_err(); 57 assert!(matches!(err, EventParseError::InvalidTag("tags"))); 58 } 59 60 #[test] 61 fn seal_from_parts_requires_content() { 62 let err = seal_from_parts(KIND_SEAL, &[], " ").unwrap_err(); 63 assert!(matches!(err, EventParseError::InvalidTag("content"))); 64 } 65 66 #[test] 67 fn seal_metadata_and_index_from_event_roundtrip() { 68 let metadata = data_from_event( 69 EVENT_ID.to_string(), 70 AUTHOR.to_string(), 71 14, 72 KIND_SEAL, 73 "payload".to_string(), 74 Vec::new(), 75 ) 76 .unwrap(); 77 assert_eq!(metadata.id, EVENT_ID); 78 assert_eq!(metadata.author, AUTHOR); 79 assert_eq!(metadata.published_at, 14); 80 assert_eq!(metadata.kind, KIND_SEAL); 81 assert_eq!(metadata.data.content, "payload"); 82 83 let index = parsed_from_event( 84 EVENT_ID.to_string(), 85 AUTHOR.to_string(), 86 14, 87 KIND_SEAL, 88 "payload".to_string(), 89 Vec::new(), 90 EVENT_SIG.to_string(), 91 ) 92 .unwrap(); 93 assert_eq!(index.event.kind_u32(), KIND_SEAL); 94 assert_eq!(index.event.signature_hex(), EVENT_SIG); 95 assert_eq!(index.data.data.content, "payload"); 96 } 97 98 #[test] 99 fn seal_index_from_event_propagates_parse_errors() { 100 let err = parsed_from_event( 101 "id".to_string(), 102 "author".to_string(), 103 14, 104 KIND_MESSAGE, 105 "payload".to_string(), 106 Vec::new(), 107 "sig".to_string(), 108 ) 109 .unwrap_err(); 110 assert!(matches!( 111 err, 112 EventParseError::InvalidKind { 113 expected: "13", 114 got: KIND_MESSAGE 115 } 116 )); 117 } 118 119 #[test] 120 fn seal_build_tags_and_kind_validation_cover_paths() { 121 let seal = Seal { 122 content: "payload".to_string(), 123 }; 124 assert!(seal_build_tags(&seal).unwrap().is_empty()); 125 126 let parts = to_wire_parts_with_kind(&seal, KIND_SEAL).unwrap(); 127 assert_eq!(parts.kind, KIND_SEAL); 128 assert_eq!(parts.content, "payload"); 129 130 let err = to_wire_parts_with_kind(&seal, KIND_MESSAGE).unwrap_err(); 131 assert!(matches!(err, EventEncodeError::InvalidKind(KIND_MESSAGE))); 132 }