pricing.rs (8980B)
1 use radroots_domain::{ 2 Decimal, DiscountBps, FixedPrice, MAX_PRICED_LINES, Money, MoneyError, PRICING_PROFILE, 3 PriceTier, PricingError, checked_order_total, 4 }; 5 use std::error::Error; 6 7 fn money(code: &str, value: &str, exponent: u8) -> Money { 8 Money::from_parts(code, value, exponent).unwrap() 9 } 10 fn cad(value: &str) -> Money { 11 money("CAD", value, 2) 12 } 13 fn price(rate: &str, basis: &str, discount: u16, tiers: Vec<PriceTier>) -> FixedPrice { 14 FixedPrice::new( 15 cad(rate), 16 basis.parse().unwrap(), 17 DiscountBps::new(discount).unwrap(), 18 tiers, 19 ) 20 .unwrap() 21 } 22 fn tier(from: &str, rate: &str) -> PriceTier { 23 PriceTier::new(from.parse().unwrap(), cad(rate)) 24 } 25 26 #[test] 27 fn independent_fraction_decimal_and_bigint_vectors_execute_the_rust_engine() { 28 let fixture: serde_json::Value = 29 serde_json::from_str(include_str!("fixtures/net_line_v1.json")).unwrap(); 30 assert_eq!(fixture["schema_version"], 1); 31 assert_eq!(fixture["profile"], PRICING_PROFILE); 32 let vectors = fixture["vectors"].as_array().unwrap(); 33 assert_eq!(vectors.len(), 26); 34 for row in vectors { 35 let input = price( 36 row["rate_minor"].as_str().unwrap(), 37 row["basis"].as_str().unwrap(), 38 u16::try_from(row["discount_bps"].as_u64().unwrap()).unwrap(), 39 vec![], 40 ); 41 let result = input.line(row["quantity"].as_str().unwrap().parse().unwrap()); 42 if row["expected_error"].is_null() { 43 let line = result.unwrap(); 44 assert_eq!( 45 line.total(), 46 cad(row["expected_minor"].as_str().unwrap()), 47 "{}", 48 row["id"] 49 ); 50 assert_eq!(line.rate(), input.rate()); 51 assert_eq!(line.basis(), input.basis()); 52 assert_eq!(line.discount_bps(), input.discount_bps()); 53 assert_eq!(line.selected_tier_from(), None); 54 } else { 55 assert_eq!(row["expected_error"], "minor_out_of_range"); 56 assert_eq!( 57 result, 58 Err(PricingError::Money(MoneyError::MinorOutOfRange)), 59 "{}", 60 row["id"] 61 ); 62 } 63 } 64 } 65 66 #[test] 67 fn discounts_are_bounded_and_explicit_free_prices_are_not_quotes() { 68 for value in [0, 1, 9999, 10_000] { 69 assert_eq!(DiscountBps::new(value).unwrap().get(), value); 70 } 71 for value in [10_001, u16::MAX] { 72 assert_eq!(DiscountBps::new(value), Err(PricingError::InvalidDiscount)); 73 } 74 assert_eq!(DiscountBps::NONE.get(), 0); 75 assert_eq!(DiscountBps::FULL.get(), 10_000); 76 for input in [ 77 price("0", "1", 0, vec![]), 78 price("325", "1", 10_000, vec![]), 79 ] { 80 assert_eq!(input.line("1".parse().unwrap()).unwrap().total(), cad("0")); 81 } 82 } 83 84 #[test] 85 fn one_inclusive_tier_is_selected_without_sorting_or_combining_discounts() { 86 let input = price("500", "1", 1000, vec![tier("2", "400"), tier("5", "300")]); 87 for (amount, rate, selected, total) in [ 88 ("1", "500", None, "450"), 89 ("1.999999", "500", None, "900"), 90 ("2", "400", Some("2"), "720"), 91 ("4.999999", "400", Some("2"), "1800"), 92 ("5", "300", Some("5"), "1350"), 93 ("7.5", "300", Some("5"), "2025"), 94 ] { 95 let line = input.line(amount.parse().unwrap()).unwrap(); 96 assert_eq!(line.rate(), cad(rate)); 97 assert_eq!( 98 line.selected_tier_from(), 99 selected.map(|v| v.parse().unwrap()) 100 ); 101 assert_eq!(line.total(), cad(total)); 102 assert_eq!(line.basis().amount(), Decimal::ONE); 103 assert_eq!(line.discount_bps().get(), 1000); 104 } 105 let explicit_zero = price("500", "1", 0, vec![tier("0", "100")]); 106 let line = explicit_zero.line("0.000001".parse().unwrap()).unwrap(); 107 assert_eq!(line.selected_tier_from(), Some(Decimal::ZERO)); 108 assert_eq!(line.rate(), cad("100")); 109 } 110 111 #[test] 112 fn tier_admission_checks_numeric_order_currency_and_count_without_guessing() { 113 for tiers in [ 114 vec![tier("2", "100"), tier("1", "90")], 115 vec![tier("1", "100"), tier("1", "90")], 116 vec![tier("0", "100"), tier("0", "90")], 117 ] { 118 assert_eq!( 119 FixedPrice::new(cad("200"), "1".parse().unwrap(), DiscountBps::NONE, tiers), 120 Err(PricingError::InvalidTierOrder) 121 ); 122 } 123 for rate in [money("USD", "100", 2), money("JPY", "100", 0)] { 124 assert_eq!( 125 FixedPrice::new( 126 cad("200"), 127 "1".parse().unwrap(), 128 DiscountBps::NONE, 129 vec![PriceTier::new(Decimal::ONE, rate)] 130 ), 131 Err(PricingError::Money(MoneyError::CurrencyMismatch)) 132 ); 133 } 134 let tiers = (0..=8).map(|n| tier(&n.to_string(), "100")).collect(); 135 assert_eq!( 136 FixedPrice::new(cad("200"), "1".parse().unwrap(), DiscountBps::NONE, tiers), 137 Err(PricingError::TooManyTiers) 138 ); 139 assert_eq!(FixedPrice::MAX_TIERS, 8); 140 let mut tiers = Vec::with_capacity(100_000); 141 tiers.extend((0..8).map(|n| tier(&n.to_string(), "100"))); 142 let input = price("200", "100", 0, tiers); 143 assert_eq!(input.tiers().len(), 8); 144 assert_eq!(input.tiers()[7].from().to_string(), "7"); 145 assert_eq!(input.tiers()[7].rate(), cad("100")); 146 assert_eq!( 147 input.line("100".parse().unwrap()).unwrap().total(), 148 cad("100") 149 ); 150 let across_scales = price("200", "1", 0, vec![tier("0.5", "150"), tier("2", "100")]); 151 assert_eq!( 152 across_scales.line("2".parse().unwrap()).unwrap().total(), 153 cad("200") 154 ); 155 } 156 157 #[test] 158 fn line_rounding_precedes_the_checked_aggregate_and_fulfillment_charge() { 159 let one_half = price("1", "1", 0, vec![]) 160 .line("0.5".parse().unwrap()) 161 .unwrap() 162 .total(); 163 assert_eq!(one_half, cad("0")); 164 assert_eq!( 165 checked_order_total(&[one_half, one_half], cad("5")).unwrap(), 166 cad("5") 167 ); 168 let combined = price("1", "1", 0, vec![]) 169 .line("1".parse().unwrap()) 170 .unwrap() 171 .total(); 172 assert_eq!(combined, cad("1")); 173 assert_eq!( 174 checked_order_total(&[combined], cad("5")).unwrap(), 175 cad("6") 176 ); 177 let zero = cad("0"); 178 assert_eq!( 179 checked_order_total(&[], zero), 180 Err(PricingError::InvalidLineCount) 181 ); 182 assert_eq!( 183 checked_order_total(&[zero; 33], zero), 184 Err(PricingError::InvalidLineCount) 185 ); 186 assert_eq!(MAX_PRICED_LINES, 32); 187 assert_eq!( 188 checked_order_total(&[cad("1"); 32], cad("7")).unwrap(), 189 cad("39") 190 ); 191 let maximum = cad("999999999999999"); 192 assert_eq!(checked_order_total(&[maximum], zero).unwrap(), maximum); 193 assert_eq!( 194 checked_order_total(&[maximum], cad("1")), 195 Err(PricingError::Money(MoneyError::MinorOutOfRange)) 196 ); 197 assert_eq!( 198 checked_order_total(&[maximum, maximum], zero), 199 Err(PricingError::Money(MoneyError::MinorOutOfRange)) 200 ); 201 for other in [money("USD", "0", 2), money("JPY", "0", 0)] { 202 assert_eq!( 203 checked_order_total(&[zero], other), 204 Err(PricingError::Money(MoneyError::CurrencyMismatch)) 205 ); 206 assert_eq!( 207 checked_order_total(&[other], zero), 208 Err(PricingError::Money(MoneyError::CurrencyMismatch)) 209 ); 210 } 211 } 212 213 #[test] 214 fn private_price_diagnostics_are_redacted_and_failures_are_input_free() { 215 let input = price("928174615", "1", 1234, vec![tier("3", "928174614")]); 216 let line = input.line("3".parse().unwrap()).unwrap(); 217 for debug in [ 218 format!("{input:?}"), 219 format!("{line:?}"), 220 format!("{:?}", input.discount_bps()), 221 format!("{:?}", input.tiers()[0]), 222 ] { 223 assert!(!debug.contains("92817461")); 224 assert!(!debug.contains("CAD")); 225 assert!(!debug.contains("1234")); 226 } 227 for error in [ 228 PricingError::InvalidDiscount, 229 PricingError::TooManyTiers, 230 PricingError::InvalidTierOrder, 231 PricingError::InvalidLineCount, 232 PricingError::ArithmeticOutOfRange, 233 MoneyError::CurrencyMismatch.into(), 234 ] { 235 let text = error.to_string(); 236 assert!(!text.is_empty()); 237 assert!(!text.contains("92817461")); 238 assert!(!text.contains("CAD")); 239 } 240 assert!(PricingError::InvalidDiscount.source().is_none()); 241 assert!( 242 PricingError::Money(MoneyError::CurrencyMismatch) 243 .source() 244 .is_some() 245 ); 246 } 247 248 #[cfg(feature = "serde")] 249 #[test] 250 fn explicit_serialization_retains_exact_basis_discount_and_tier_fields() { 251 let input = price("325", "0.5", 125, vec![tier("2.5", "299")]); 252 let expected = serde_json::json!({"rate":{"currency":"CAD","minor":"325","exponent":2},"basis":"0.5","discount_bps":125,"tiers":[{"from":"2.5","rate":{"currency":"CAD","minor":"299","exponent":2}}]}); 253 assert_eq!(serde_json::to_value(input).unwrap(), expected); 254 }