profile_event_builder.rs (2395B)
1 #[path = "../src/test_fixtures.rs"] 2 mod test_fixtures; 3 4 use nostr::{Keys as RadrootsNostrKeys, SecretKey as RadrootsNostrSecretKey}; 5 use radroots_event::profile::AuthoredProfile; 6 use radroots_nostr::event::Timestamp as RadrootsNostrTimestamp; 7 use radroots_nostr::event::build_profile as build_profile_event; 8 9 #[test] 10 fn typed_profile_builder_preserves_the_strict_replacement_snapshot() { 11 let keys = RadrootsNostrKeys::new( 12 RadrootsNostrSecretKey::from_hex(test_fixtures::FIXTURE_ALICE_SECRET_KEY_HEX).unwrap(), 13 ); 14 let created_at = RadrootsNostrTimestamp::from_secs(1_784_347_200); 15 let profile = AuthoredProfile::new("Alice") 16 .unwrap() 17 .with_display_name("Alice's Orchard") 18 .with_about("Tree fruit") 19 .with_bot(false); 20 21 let event = build_profile_event(&profile) 22 .unwrap() 23 .custom_created_at(created_at) 24 .sign_with_keys(&keys) 25 .unwrap(); 26 27 assert_eq!(event.kind.as_u16(), 0); 28 assert_eq!(event.created_at, created_at); 29 assert!(event.tags.is_empty()); 30 assert_eq!( 31 event.content, 32 r#"{"name":"Alice","display_name":"Alice's Orchard","about":"Tree fruit","bot":false}"# 33 ); 34 assert!(event.verify().is_ok()); 35 } 36 37 #[test] 38 fn typed_profile_builder_finalizes_the_same_plan_for_an_external_signer() { 39 let keys = RadrootsNostrKeys::new( 40 RadrootsNostrSecretKey::from_hex(test_fixtures::FIXTURE_ALICE_SECRET_KEY_HEX).unwrap(), 41 ); 42 let created_at = RadrootsNostrTimestamp::from_secs(1_784_347_200); 43 let profile = AuthoredProfile::new("Alice").unwrap().with_bot(false); 44 let request = build_profile_event(&profile) 45 .unwrap() 46 .custom_created_at(created_at) 47 .into_external_signing_request(keys.public_key()) 48 .expect("typed external request"); 49 let plan = request.authored_plan().expect("typed request plan").clone(); 50 assert_eq!( 51 plan.body().contract().contract_id().as_str(), 52 "radroots.profile.metadata.v1" 53 ); 54 assert_eq!(plan.created_at(), created_at.as_secs()); 55 56 let unsigned: nostr::UnsignedEvent = 57 serde_json::from_value(serde_json::to_value(&request).unwrap()).unwrap(); 58 let event = unsigned.sign_with_keys(&keys).unwrap(); 59 let completed = request.complete(event).expect("exact typed completion"); 60 assert_eq!(completed.id.to_hex(), plan.expected_event_id().to_hex()); 61 }