lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

profile_event_builder.rs (4673B)


      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 }
     62 
     63 #[test]
     64 fn profile_complete_edit_external_signing_request_completes_exact_preserved_replacement() {
     65     use nostr::JsonUtil;
     66     use radroots_event::{
     67         profile::edit::{ProfileEdit, ProfileFieldChange},
     68         wire::Nip01EventWire,
     69     };
     70     use radroots_event_codec::{
     71         admission::profile::verify_and_admit_profile_event, authoring::AuthoredEventPlan,
     72     };
     73     use radroots_nostr::event::ExternalSigningRequest;
     74     let keys = RadrootsNostrKeys::new(
     75         RadrootsNostrSecretKey::from_hex(test_fixtures::FIXTURE_ALICE_SECRET_KEY_HEX).unwrap(),
     76     );
     77     let signed = nostr::EventBuilder::new(
     78         nostr::Kind::Metadata,
     79         r#"{"name":"farm","website":"https://farm.example","custom":{"x":1}}"#,
     80     )
     81     .custom_created_at(RadrootsNostrTimestamp::from_secs(100))
     82     .sign_with_keys(&keys)
     83     .unwrap();
     84     let baseline = verify_and_admit_profile_event(
     85         Nip01EventWire::parse_json(&signed.as_json())
     86             .unwrap()
     87             .into_envelope()
     88             .unwrap(),
     89     )
     90     .unwrap();
     91     let plan = AuthoredEventPlan::from_profile_edit(
     92         &baseline,
     93         &baseline,
     94         &ProfileEdit {
     95             display_name: ProfileFieldChange::Set("Updated farm".into()),
     96             ..Default::default()
     97         },
     98         101,
     99         keys.public_key().to_hex(),
    100     )
    101     .unwrap();
    102     let request = ExternalSigningRequest::from_authored_plan(plan.clone()).unwrap();
    103     assert_eq!(request.authored_plan(), Some(&plan));
    104     let unsigned: nostr::UnsignedEvent =
    105         serde_json::from_value(serde_json::to_value(&request).unwrap()).unwrap();
    106     let completed = request
    107         .complete(unsigned.sign_with_keys(&keys).unwrap())
    108         .unwrap();
    109     assert_eq!(completed.id.to_hex(), plan.expected_event_id().to_hex());
    110     let admitted = verify_and_admit_profile_event(
    111         Nip01EventWire::parse_json(&completed.as_json())
    112             .unwrap()
    113             .into_envelope()
    114             .unwrap(),
    115     )
    116     .unwrap();
    117     assert_eq!(admitted.metadata().display_name(), Some("Updated farm"));
    118     assert_eq!(
    119         admitted.metadata().raw_fields()["website"],
    120         "https://farm.example"
    121     );
    122     assert_eq!(
    123         admitted.metadata().raw_fields()["custom"],
    124         serde_json::json!({"x":1})
    125     );
    126 }