lib

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

mod.rs (3284B)


      1 #![forbid(unsafe_code)]
      2 
      3 pub mod decode;
      4 pub mod encode;
      5 
      6 #[cfg(test)]
      7 mod tests {
      8     use crate::error::EventEncodeError;
      9     use crate::resource_cap::encode::resource_harvest_cap_build_tags;
     10     use radroots_core::{Decimal, Quantity, Unit};
     11     use radroots_event::farm::resource_area::ResourceAreaRef;
     12     use radroots_event::farm::resource_cap::{ResourceHarvestCap, ResourceHarvestProduct};
     13 
     14     #[test]
     15     fn resource_harvest_cap_tags_include_required_fields() {
     16         let cap = ResourceHarvestCap {
     17             d_tag: "DAAAAAAAAAAAAAAAAAAAAA".to_string(),
     18             resource_area: ResourceAreaRef {
     19                 pubkey: "area_pubkey".to_string(),
     20                 d_tag: "AAAAAAAAAAAAAAAAAAAAAw".to_string(),
     21             },
     22             product: ResourceHarvestProduct {
     23                 key: "nutmeg".to_string(),
     24                 category: Some("spice".to_string()),
     25             },
     26             start: 1735689600,
     27             end: 1767225600,
     28             cap_quantity: Quantity::try_new(Decimal::from(100000u32), Unit::MassG).unwrap(),
     29             display_amount: None,
     30             display_unit: None,
     31             display_label: None,
     32             tags: Some(vec!["community".to_string()]),
     33         };
     34 
     35         let tags = resource_harvest_cap_build_tags(&cap).expect("tags");
     36         assert!(
     37             tags.iter()
     38                 .any(|tag| tag.first().map(|v| v.as_str()) == Some("d"))
     39         );
     40         assert!(
     41             tags.iter()
     42                 .any(|tag| tag.first().map(|v| v.as_str()) == Some("a"))
     43         );
     44         assert!(
     45             tags.iter()
     46                 .any(|tag| tag.first().map(|v| v.as_str()) == Some("key"))
     47         );
     48         assert!(
     49             tags.iter()
     50                 .any(|tag| tag.first().map(|v| v.as_str()) == Some("start"))
     51         );
     52         assert!(
     53             tags.iter()
     54                 .any(|tag| tag.first().map(|v| v.as_str()) == Some("end"))
     55         );
     56     }
     57 
     58     #[test]
     59     fn resource_harvest_cap_build_tags_rejects_invalid_d_tags() {
     60         let mut cap = ResourceHarvestCap {
     61             d_tag: "DAAAAAAAAAAAAAAAAAAAAA".to_string(),
     62             resource_area: ResourceAreaRef {
     63                 pubkey: "area_pubkey".to_string(),
     64                 d_tag: "AAAAAAAAAAAAAAAAAAAAAw".to_string(),
     65             },
     66             product: ResourceHarvestProduct {
     67                 key: "nutmeg".to_string(),
     68                 category: Some("spice".to_string()),
     69             },
     70             start: 1735689600,
     71             end: 1767225600,
     72             cap_quantity: Quantity::try_new(Decimal::from(100000u32), Unit::MassG).unwrap(),
     73             display_amount: None,
     74             display_unit: None,
     75             display_label: None,
     76             tags: None,
     77         };
     78 
     79         cap.resource_area.d_tag = "invalid".to_string();
     80         let err = resource_harvest_cap_build_tags(&cap).expect_err("invalid resource area d_tag");
     81         assert!(matches!(
     82             err,
     83             EventEncodeError::InvalidField("resource_area.d_tag")
     84         ));
     85 
     86         cap.resource_area.d_tag = "AAAAAAAAAAAAAAAAAAAAAw".to_string();
     87         cap.d_tag = "invalid".to_string();
     88         let err = resource_harvest_cap_build_tags(&cap).expect_err("invalid cap d_tag");
     89         assert!(matches!(err, EventEncodeError::InvalidField("d_tag")));
     90     }
     91 }