listing.rs (7981B)
1 #![cfg(feature = "listing")] 2 use radroots_domain::{ListingOptionId, PublicKey, UnixSeconds, listing::*}; 3 use radroots_protocol::{ 4 listing::*, 5 nip01::{self, WireError}, 6 }; 7 8 fn key() -> PublicKey { 9 PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap() 10 } 11 fn input(state: ListingState, body: &str) -> PublishInput { 12 let option = ListingOption::quote_required( 13 ListingOptionId::from_bytes([2; 32]), 14 "One bunch", 15 SaleUnit::package_unspecified("bunch").unwrap(), 16 "1".parse().unwrap(), 17 "1".parse().unwrap(), 18 Supply::Available { 19 amount: "10".parse().unwrap(), 20 }, 21 LeadSeconds::new(0).unwrap(), 22 ) 23 .unwrap(); 24 let terms = ListingTerms::new( 25 state, 26 Product::new( 27 "Carrots", 28 "Public description", 29 None, 30 Some("Example region"), 31 ) 32 .unwrap(), 33 option.id(), 34 vec![option], 35 PublicFulfillment::new( 36 vec![FulfillmentMode::Pickup], 37 "Example region", 38 LeadSeconds::new(0).unwrap(), 39 ) 40 .unwrap(), 41 None, 42 vec![], 43 ) 44 .unwrap(); 45 PublishInput::new( 46 ListingId::from_bytes([1; 16]), 47 "Carrot bunch", 48 "One bunch; quote required", 49 body, 50 UnixSeconds::new(100).unwrap(), 51 terms, 52 ) 53 .unwrap() 54 } 55 fn event(input: &PublishInput) -> ListingEvent { 56 ListingEvent::new(input, key(), UnixSeconds::new(101).unwrap()).unwrap() 57 } 58 fn tag<'a>(event: &'a ListingEvent, name: &str) -> &'a [String] { 59 let fields = event.fields(); 60 let mut selected = fields.tags.iter().filter(|t| t[0] == name); 61 let value = selected.next().unwrap(); 62 assert!(selected.next().is_none()); 63 value 64 } 65 66 #[test] 67 fn assigned_listing_profile_is_canonical_and_quote_required_has_no_fabricated_price() { 68 let captured = input(ListingState::Active, "Ask for terms."); 69 let value = event(&captured); 70 assert_eq!(value.fields().kind, 30402); 71 assert_eq!(tag(&value, "d"), ["d", &"01".repeat(16)]); 72 assert_eq!( 73 tag(&value, "radroots:price_unit"), 74 ["radroots:price_unit", "bunch"] 75 ); 76 assert_eq!(tag(&value, "status"), ["status", "active"]); 77 assert_eq!(tag(&value, "published_at"), ["published_at", "100"]); 78 assert!(!value.fields().tags.iter().any(|t| t[0] == "price")); 79 // Independently written golden profile fixes ordering and exact scalar 80 // representation; this does not claim independent signature qualification. 81 let golden = concat!( 82 "{\"authority\":null,\"default_option\":\"", 83 "0202020202020202020202020202020202020202020202020202020202020202", 84 "\",\"fulfillment\":{\"area\":\"Example region\",\"lead_seconds\":\"0\",\"modes\":[\"pickup\"]},\"media\":[],\"options\":[{\"id\":\"", 85 "0202020202020202020202020202020202020202020202020202020202020202", 86 "\",\"increment\":\"1\",\"label\":\"One bunch\",\"lead_seconds\":\"0\",\"minimum\":\"1\",\"price\":{\"type\":\"quote_required\"},\"supply\":{\"amount\":\"10\",\"type\":\"available\"},\"unit\":{\"contents\":{\"type\":\"unspecified\"},\"label\":\"bunch\",\"type\":\"package\"}}],\"product\":{\"description\":\"Public description\",\"name\":\"Carrots\",\"origin_summary\":\"Example region\",\"producer\":null},\"profile\":\"radroots.listing\",\"state\":\"active\",\"version\":1}" 87 ); 88 assert_eq!(tag(&value, "radroots:listing")[1], golden); 89 let serialized = serde_json::to_value(&captured).unwrap(); 90 assert_eq!( 91 serialized["profile"], 92 serde_json::from_str::<serde_json::Value>(golden).unwrap() 93 ); 94 assert!(serialized.get("terms").is_none()); 95 assert_eq!(serialized.as_object().unwrap().len(), 6); 96 assert_eq!(captured.id().to_string(), "01".repeat(16)); 97 assert_eq!(captured.published_at().get(), 100); 98 assert!(!format!("{captured:?} {value:?}").contains("Ask for terms")); 99 } 100 101 #[test] 102 fn inactive_revisions_keep_the_original_coordinate_without_a_false_sold_status() { 103 let active = event(&input(ListingState::Active, "Original terms.")); 104 for state in [ListingState::Paused, ListingState::Withdrawn] { 105 let revised = event(&input(state, "Original terms.")); 106 assert_eq!(revised.fields().kind, active.fields().kind); 107 assert_eq!(tag(&revised, "d"), tag(&active, "d")); 108 assert_ne!(revised.event_id(), active.event_id()); 109 assert!(revised.fields().content.contains("unavailable")); 110 assert!(revised.fields().content.ends_with("Original terms.")); 111 assert!(tag(&revised, "summary")[1].contains("unavailable")); 112 assert!( 113 !revised 114 .fields() 115 .tags 116 .iter() 117 .any(|t| t[0] == "status" || t[0] == "price") 118 ); 119 } 120 } 121 122 #[test] 123 fn measured_choice_and_opaque_metadata_supply_no_pool_or_endpoint_authority() { 124 use radroots_domain::{AuthorityRef, ExecutorId, Generation}; 125 let choice = ListingOption::quote_required( 126 ListingOptionId::from_bytes([3; 32]), 127 "Measured herbs", 128 SaleUnit::measured(MeasurementUnit::Kilogram), 129 "0.25".parse().unwrap(), 130 "0.5".parse().unwrap(), 131 Supply::Unknown, 132 LeadSeconds::new(60).unwrap(), 133 ) 134 .unwrap(); 135 let terms = ListingTerms::new( 136 ListingState::Active, 137 Product::new("Herbs", "Public description", Some(key()), None).unwrap(), 138 choice.id(), 139 vec![choice], 140 PublicFulfillment::new( 141 vec![FulfillmentMode::Pickup, FulfillmentMode::Delivery], 142 "Example region", 143 LeadSeconds::new(60).unwrap(), 144 ) 145 .unwrap(), 146 Some(AuthorityRef::new( 147 ExecutorId::from_bytes([4; 32]), 148 Generation::new(2).unwrap(), 149 )), 150 vec![], 151 ) 152 .unwrap(); 153 let captured = PublishInput::new( 154 ListingId::from_bytes([1; 16]), 155 "Herbs", 156 "Half kilogram; quote required", 157 "", 158 UnixSeconds::new(100).unwrap(), 159 terms, 160 ) 161 .unwrap(); 162 let value = event(&captured); 163 assert_eq!( 164 tag(&value, "radroots:price_unit"), 165 ["radroots:price_unit", "kg"] 166 ); 167 let profile: serde_json::Value = 168 serde_json::from_str(&tag(&value, "radroots:listing")[1]).unwrap(); 169 assert_eq!( 170 profile["authority"], 171 serde_json::json!({"executor_id":"04".repeat(32),"generation":"2"}) 172 ); 173 assert_eq!(profile["product"]["producer"], key().to_hex()); 174 assert_eq!( 175 profile["options"][0]["unit"], 176 serde_json::json!({"type":"measured","unit":"kg"}) 177 ); 178 assert_eq!( 179 profile["options"][0]["supply"], 180 serde_json::json!({"type":"unknown"}) 181 ); 182 assert_eq!(profile["options"][0]["lead_seconds"], "60"); 183 assert_eq!(profile["authority"].as_object().unwrap().len(), 2); 184 } 185 186 #[test] 187 fn complete_signed_event_limit_counts_escaping_and_fixed_signature_framing() { 188 let empty = event(&input(ListingState::Active, "")); 189 let overhead = nip01::signed_json_len(&empty.fields(), LISTING_EVENT_MAX_BYTES).unwrap(); 190 let free = LISTING_EVENT_MAX_BYTES - overhead; 191 let body = "\u{0001}".repeat(free / 6) + &"a".repeat(free % 6); 192 assert!(body.chars().count() <= 16_384); 193 let exact = event(&input(ListingState::Active, &body)); 194 assert_eq!( 195 nip01::signed_json_len(&exact.fields(), LISTING_EVENT_MAX_BYTES).unwrap(), 196 LISTING_EVENT_MAX_BYTES 197 ); 198 assert!(matches!( 199 ListingEvent::new( 200 &input(ListingState::Active, &(body + "a")), 201 key(), 202 UnixSeconds::new(101).unwrap() 203 ), 204 Err(WireError::LimitExceeded) 205 )); 206 assert!(matches!( 207 ListingEvent::new( 208 &input(ListingState::Active, ""), 209 key(), 210 UnixSeconds::new(99).unwrap() 211 ), 212 Err(WireError::InvalidInput) 213 )); 214 }