listing.rs (13238B)
1 use radroots_domain::{ListingOptionId, listing::*}; 2 3 #[path = "support/listing_media.rs"] 4 mod media; 5 6 #[path = "support/listing_time.rs"] 7 mod time; 8 9 #[path = "support/agreement.rs"] 10 mod agreement; 11 12 #[path = "support/causal.rs"] 13 mod causal; 14 #[path = "support/commerce.rs"] 15 mod commerce; 16 17 #[test] 18 fn public_text_has_shared_unicode_bounds_and_revision_data_rejects_inverted_time() { 19 use radroots_domain::{EnvelopeEventId, PublicKey, UnixSeconds}; 20 let title = "🥕".repeat(160); 21 let summary = "é".repeat(280); 22 let body = " ".repeat(16_384); 23 let value = ListingText::new(&title, &summary, &body).unwrap(); 24 #[cfg(feature = "serde")] 25 assert_eq!( 26 serde_json::to_value(&value).unwrap(), 27 serde_json::json!({"title":title,"summary":summary,"description_markdown":body}) 28 ); 29 assert!(!format!("{value:?}").contains('🥕')); 30 for (title, summary, body) in [ 31 ("🥕".repeat(161), "summary".into(), String::new()), 32 ("title".into(), "é".repeat(281), String::new()), 33 ("title".into(), "summary".into(), " ".repeat(16_385)), 34 ] { 35 assert_eq!( 36 ListingText::new(&title, &summary, &body).unwrap_err(), 37 ListingError::InvalidText 38 ); 39 } 40 let terms = ListingTerms::new( 41 ListingState::Active, 42 product(), 43 ListingOptionId::from_bytes([1; 32]), 44 vec![option(1, SaleUnit::measured(MeasurementUnit::Each), "1", "1", "10").unwrap()], 45 fulfillment(), 46 None, 47 vec![], 48 ) 49 .unwrap(); 50 let key = 51 PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") 52 .unwrap(); 53 // These are structural public data, without a signed/installed claim. 54 assert_eq!( 55 Listing::new( 56 ListingId::from_bytes([1; 16]), 57 key, 58 EnvelopeEventId::from_bytes([2; 32]), 59 UnixSeconds::new(99).unwrap(), 60 UnixSeconds::new(100).unwrap(), 61 value, 62 terms 63 ) 64 .unwrap_err(), 65 ListingError::InvalidTime 66 ); 67 } 68 69 fn product() -> Product { 70 Product::new( 71 "Carrots", 72 "Public description", 73 None, 74 Some("Example region"), 75 ) 76 .unwrap() 77 } 78 fn fulfillment() -> PublicFulfillment { 79 PublicFulfillment::new( 80 vec![FulfillmentMode::Pickup], 81 "Example region", 82 LeadSeconds::new(0).unwrap(), 83 ) 84 .unwrap() 85 } 86 fn option( 87 n: u8, 88 unit: SaleUnit, 89 increment: &str, 90 minimum: &str, 91 amount: &str, 92 ) -> Result<ListingOption, ListingError> { 93 ListingOption::quote_required( 94 ListingOptionId::from_bytes([n; 32]), 95 "One bunch", 96 unit, 97 increment.parse().unwrap(), 98 minimum.parse().unwrap(), 99 Supply::Available { 100 amount: amount.parse().unwrap(), 101 }, 102 LeadSeconds::new(0).unwrap(), 103 ) 104 } 105 106 #[test] 107 fn spare_caller_capacity_preserves_exact_terms_and_default_selection() { 108 let mut modes = Vec::with_capacity(4096); 109 modes.push(FulfillmentMode::Pickup); 110 let mut options = Vec::with_capacity(4096); 111 options.push( 112 option( 113 1, 114 SaleUnit::package_unspecified("bunch").unwrap(), 115 "1", 116 "1", 117 "10", 118 ) 119 .unwrap(), 120 ); 121 options.push( 122 option( 123 2, 124 SaleUnit::measured(MeasurementUnit::Kilogram), 125 "0.25", 126 "0.5", 127 "10", 128 ) 129 .unwrap(), 130 ); 131 let compact = options.clone(); 132 let expanded = ListingTerms::new( 133 ListingState::Active, 134 product(), 135 ListingOptionId::from_bytes([2; 32]), 136 options, 137 PublicFulfillment::new(modes, "Example region", LeadSeconds::new(0).unwrap()).unwrap(), 138 None, 139 vec![], 140 ) 141 .unwrap(); 142 let expected = ListingTerms::new( 143 ListingState::Active, 144 product(), 145 ListingOptionId::from_bytes([2; 32]), 146 compact, 147 fulfillment(), 148 None, 149 vec![], 150 ) 151 .unwrap(); 152 assert_eq!(expanded, expected); 153 assert_eq!( 154 expanded.default_option().id(), 155 ListingOptionId::from_bytes([2; 32]) 156 ); 157 #[cfg(feature = "serde")] 158 assert_eq!( 159 serde_json::to_vec(&expanded).unwrap(), 160 serde_json::to_vec(&expected).unwrap() 161 ); 162 assert_eq!(expanded.clone(), expected); 163 } 164 165 #[test] 166 fn identifiers_durations_and_public_data_preserve_exact_cross_host_values() { 167 let id = ListingId::from_bytes([0xAB; 16]); 168 assert_eq!(id.to_string(), "ab".repeat(16)); 169 assert_eq!( 170 id.to_string().parse::<ListingId>().unwrap().as_bytes(), 171 &[0xAB; 16] 172 ); 173 for bad in [ 174 "ab".repeat(15), 175 "ab".repeat(17), 176 "AB".repeat(16), 177 "0g".repeat(16), 178 "é".repeat(16), 179 ] { 180 assert_eq!( 181 bad.parse::<ListingId>().unwrap_err(), 182 ListingError::InvalidIdentifier 183 ); 184 } 185 assert_eq!( 186 LeadSeconds::new(i64::MAX as u64).unwrap().get(), 187 i64::MAX as u64 188 ); 189 assert_eq!( 190 LeadSeconds::new(i64::MAX as u64 + 1).unwrap_err(), 191 ListingError::InvalidDuration 192 ); 193 let name = "🥕".repeat(160); 194 let value = Product::new(&name, "\u{0001}", None, None).unwrap(); 195 assert_eq!(value.name(), name); 196 assert!(!format!("{value:?}").contains('🥕')); 197 assert!(Product::new(&(name + "x"), "", None, None).is_err()); 198 assert!(Product::new("", "", None, None).is_err()); 199 assert!(Product::new("x", &"x".repeat(4097), None, None).is_err()); 200 assert!(Product::new("x", "", None, Some(&"x".repeat(161))).is_err()); 201 assert!(SaleUnit::package_unspecified("").is_err()); 202 assert!(SaleUnit::package_unspecified(&"x".repeat(81)).is_err()); 203 assert!(PublicFulfillment::new(vec![], "", LeadSeconds::new(0).unwrap()).is_err()); 204 assert!( 205 PublicFulfillment::new( 206 vec![FulfillmentMode::Pickup; 2], 207 "", 208 LeadSeconds::new(0).unwrap() 209 ) 210 .is_err() 211 ); 212 assert!( 213 PublicFulfillment::new( 214 vec![FulfillmentMode::Pickup; 3], 215 "", 216 LeadSeconds::new(0).unwrap() 217 ) 218 .is_err() 219 ); 220 assert!( 221 PublicFulfillment::new( 222 vec![FulfillmentMode::Pickup], 223 &"x".repeat(161), 224 LeadSeconds::new(0).unwrap() 225 ) 226 .is_err() 227 ); 228 assert!( 229 PublicFulfillment::new( 230 vec![FulfillmentMode::Pickup, FulfillmentMode::Delivery], 231 "", 232 LeadSeconds::new(0).unwrap() 233 ) 234 .is_ok() 235 ); 236 } 237 238 #[test] 239 fn exact_increments_and_units_never_invent_fractional_packages_or_conversion() { 240 for unit in [ 241 MeasurementUnit::Gram, 242 MeasurementUnit::Kilogram, 243 MeasurementUnit::Millilitre, 244 MeasurementUnit::Litre, 245 ] { 246 let value = option(1, SaleUnit::measured(unit), "0.25", "0.5", "1.5").unwrap(); 247 assert_eq!(value.unit().label(), unit.symbol()); 248 assert!(!value.unit().integral()); 249 assert_eq!(value.price(), &Price::QuoteRequired); 250 assert!(option(1, SaleUnit::measured(unit), "0.25", "0.3", "1.5").is_err()); 251 } 252 for unit in [ 253 SaleUnit::measured(MeasurementUnit::Each), 254 SaleUnit::package_unspecified("bunch").unwrap(), 255 ] { 256 assert!(unit.integral()); 257 for (inc, min, amount) in [ 258 ("0.5", "1", "10"), 259 ("1", "1.5", "10"), 260 ("1", "1", "0.5"), 261 ("2", "1", "10"), 262 ] { 263 assert_eq!( 264 option(1, unit.clone(), inc, min, amount).unwrap_err(), 265 ListingError::InvalidQuantity 266 ); 267 } 268 let available = option(1, unit, "1", "2", "0").unwrap(); 269 assert_eq!(available.id(), ListingOptionId::from_bytes([1; 32])); 270 } 271 let unknown = ListingOption::quote_required( 272 ListingOptionId::from_bytes([2; 32]), 273 "Measured", 274 SaleUnit::measured(MeasurementUnit::Kilogram), 275 "0.000001".parse().unwrap(), 276 "0.000002".parse().unwrap(), 277 Supply::Unknown, 278 LeadSeconds::new(0).unwrap(), 279 ) 280 .unwrap(); 281 assert_eq!(unknown.label(), "Measured"); 282 } 283 284 #[test] 285 fn selected_amounts_validate_minimum_and_increment_without_using_advertised_stock() { 286 let measured = option( 287 1, 288 SaleUnit::measured(MeasurementUnit::Kilogram), 289 "0.25", 290 "0.5", 291 "0", 292 ) 293 .unwrap(); 294 assert_eq!(measured.minimum().to_string(), "0.5"); 295 assert_eq!(measured.increment().to_string(), "0.25"); 296 for amount in ["0.5", "0.75", "10.25", "999999999999"] { 297 measured.validate_amount(amount.parse().unwrap()).unwrap(); 298 } 299 for amount in ["0.25", "0.6", "0.500001"] { 300 assert_eq!( 301 measured.validate_amount(amount.parse().unwrap()), 302 Err(ListingError::InvalidQuantity) 303 ); 304 } 305 for unit in [ 306 SaleUnit::measured(MeasurementUnit::Each), 307 SaleUnit::package_unspecified("bag").unwrap(), 308 ] { 309 let value = option(2, unit, "2", "4", "0").unwrap(); 310 for amount in ["4", "6", "100"] { 311 value.validate_amount(amount.parse().unwrap()).unwrap(); 312 } 313 for amount in ["1", "3", "4.5", "5"] { 314 assert_eq!( 315 value.validate_amount(amount.parse().unwrap()), 316 Err(ListingError::InvalidQuantity) 317 ); 318 } 319 } 320 } 321 322 #[test] 323 fn selected_amounts_preserve_extreme_exact_scales_and_shared_constructor_rules() { 324 let tiny = option( 325 1, 326 SaleUnit::measured(MeasurementUnit::Gram), 327 "0.000001", 328 "0.000002", 329 "0", 330 ) 331 .unwrap(); 332 for amount in ["0.000002", "1", "999999999999"] { 333 tiny.validate_amount(amount.parse().unwrap()).unwrap(); 334 } 335 assert_eq!( 336 tiny.validate_amount("0.000001".parse().unwrap()), 337 Err(ListingError::InvalidQuantity) 338 ); 339 let large = option( 340 2, 341 SaleUnit::measured(MeasurementUnit::Litre), 342 "999999999999", 343 "999999999999", 344 "0", 345 ) 346 .unwrap(); 347 large 348 .validate_amount("999999999999".parse().unwrap()) 349 .unwrap(); 350 assert_eq!( 351 large.validate_amount("1".parse().unwrap()), 352 Err(ListingError::InvalidQuantity) 353 ); 354 } 355 356 #[test] 357 fn bounded_unique_options_preserve_exact_default_without_fallback() { 358 let make = |n| { 359 option( 360 n, 361 SaleUnit::package_unspecified("bunch").unwrap(), 362 "1", 363 "1", 364 "10", 365 ) 366 .unwrap() 367 }; 368 let terms = |choices, default| { 369 ListingTerms::new( 370 ListingState::Active, 371 product(), 372 ListingOptionId::from_bytes([default; 32]), 373 choices, 374 fulfillment(), 375 None, 376 vec![], 377 ) 378 }; 379 assert!(terms(vec![], 1).is_err()); 380 assert!(terms(vec![make(1), make(1)], 1).is_err()); 381 assert!(terms(vec![make(1)], 2).is_err()); 382 assert!(terms((1..=17).map(make).collect(), 1).is_err()); 383 let valid = terms((1..=16).map(make).collect(), 16).unwrap(); 384 assert_eq!( 385 valid.default_option().id(), 386 ListingOptionId::from_bytes([16; 32]) 387 ); 388 assert_eq!(valid.options().len(), 16); 389 assert_eq!(valid.fulfillment().area(), "Example region"); 390 assert_eq!(valid.state(), ListingState::Active); 391 assert!(!format!("{valid:?}").contains("Carrots")); 392 } 393 394 #[cfg(feature = "serde")] 395 #[test] 396 fn serialized_values_have_exact_string_numbers_and_no_private_or_derived_fields() { 397 let id = ListingId::from_bytes([0xAB; 16]); 398 let raw = serde_json::to_string(&id).unwrap(); 399 assert_eq!(serde_json::from_str::<ListingId>(&raw).unwrap(), id); 400 assert!(serde_json::from_str::<ListingId>("null").is_err()); 401 assert!(serde_json::from_str::<ListingId>(&format!("\"{}\"", "AB".repeat(16))).is_err()); 402 assert_eq!( 403 serde_json::to_string(&LeadSeconds::new(0).unwrap()).unwrap(), 404 "\"0\"" 405 ); 406 for bad in ["0", "\"00\"", "\"-1\"", "null"] { 407 assert!(serde_json::from_str::<LeadSeconds>(bad).is_err()); 408 } 409 let choice = option( 410 1, 411 SaleUnit::package_unspecified("bunch").unwrap(), 412 "1", 413 "1", 414 "10", 415 ) 416 .unwrap(); 417 let terms = ListingTerms::new( 418 ListingState::Active, 419 product(), 420 choice.id(), 421 vec![choice], 422 fulfillment(), 423 None, 424 vec![], 425 ) 426 .unwrap(); 427 let value = serde_json::to_value(terms).unwrap(); 428 assert_eq!( 429 value["options"][0]["unit"], 430 serde_json::json!({"type":"package","label":"bunch","contents":{"type":"unspecified"}}) 431 ); 432 assert_eq!(value["options"][0]["increment"], "1"); 433 assert_eq!( 434 value["options"][0]["price"], 435 serde_json::json!({"type":"quote_required"}) 436 ); 437 assert_eq!( 438 value["options"][0]["supply"], 439 serde_json::json!({"type":"available","amount":"10"}) 440 ); 441 assert!(value.get("default_index").is_none()); 442 assert_eq!(value["media"], serde_json::json!([])); 443 assert_eq!(value.as_object().unwrap().len(), 7); 444 }