| 1 |
|
| 2 |
use super::*; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn percentage_discount_50() { |
| 6 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 50), 500); |
| 7 |
} |
| 8 |
|
| 9 |
#[test] |
| 10 |
fn percentage_discount_100() { |
| 11 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 100), 0); |
| 12 |
} |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn percentage_discount_10() { |
| 16 |
|
| 17 |
assert_eq!(apply_discount(999, DiscountType::Percentage, 10), 900); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn fixed_discount() { |
| 22 |
assert_eq!(apply_discount(1000, DiscountType::Fixed, 300), 700); |
| 23 |
} |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn fixed_discount_exceeds_price() { |
| 27 |
assert_eq!(apply_discount(100, DiscountType::Fixed, 500), 0); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[test] |
| 33 |
fn percentage_discount_0() { |
| 34 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 0), 1000); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn percentage_discount_over_100() { |
| 39 |
|
| 40 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 150), 0); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn percentage_discount_1_percent() { |
| 45 |
|
| 46 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 1), 990); |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn percentage_discount_99_percent() { |
| 51 |
|
| 52 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, 99), 10); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn percentage_discount_rounding() { |
| 57 |
|
| 58 |
assert_eq!(apply_discount(1, DiscountType::Percentage, 50), 1); |
| 59 |
|
| 60 |
assert_eq!(apply_discount(3, DiscountType::Percentage, 33), 3); |
| 61 |
|
| 62 |
assert_eq!(apply_discount(199, DiscountType::Percentage, 50), 100); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[test] |
| 68 |
fn fixed_discount_exact_price() { |
| 69 |
assert_eq!(apply_discount(500, DiscountType::Fixed, 500), 0); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn fixed_discount_zero_value() { |
| 74 |
assert_eq!(apply_discount(1000, DiscountType::Fixed, 0), 1000); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn fixed_discount_one_cent() { |
| 79 |
assert_eq!(apply_discount(1000, DiscountType::Fixed, 1), 999); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[test] |
| 85 |
fn zero_price_percentage() { |
| 86 |
assert_eq!(apply_discount(0, DiscountType::Percentage, 50), 0); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn zero_price_fixed() { |
| 91 |
assert_eq!(apply_discount(0, DiscountType::Fixed, 100), 0); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[test] |
| 97 |
fn negative_discount_value_percentage() { |
| 98 |
|
| 99 |
assert_eq!(apply_discount(1000, DiscountType::Percentage, -50), 1000); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn negative_discount_value_fixed() { |
| 104 |
|
| 105 |
assert_eq!(apply_discount(1000, DiscountType::Fixed, -500), 1000); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn negative_price_percentage() { |
| 110 |
|
| 111 |
|
| 112 |
assert_eq!(apply_discount(-1000, DiscountType::Percentage, 50), 0); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn negative_price_fixed() { |
| 117 |
|
| 118 |
assert_eq!(apply_discount(-1000, DiscountType::Fixed, 500), 0); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#[test] |
| 124 |
fn large_price_percentage_no_overflow() { |
| 125 |
|
| 126 |
|
| 127 |
let price = i32::MAX; |
| 128 |
let result = apply_discount(price, DiscountType::Percentage, 50); |
| 129 |
assert_eq!(result, 1_073_741_824); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#[test] |
| 135 |
fn adversarial_percentage_max_price_max_percentage() { |
| 136 |
|
| 137 |
let result = apply_discount(i32::MAX, DiscountType::Percentage, 100); |
| 138 |
assert_eq!(result, 0, "100% discount on any price should be 0"); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn adversarial_percentage_max_price_99_percent() { |
| 143 |
let result = apply_discount(i32::MAX, DiscountType::Percentage, 99); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
assert_eq!(result, 21_474_837); |
| 148 |
assert!(result > 0, "99% discount should leave some remaining"); |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn adversarial_fixed_max_price_max_discount() { |
| 153 |
let result = apply_discount(i32::MAX, DiscountType::Fixed, i32::MAX); |
| 154 |
assert_eq!(result, 0); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn adversarial_both_negative() { |
| 159 |
|
| 160 |
let result = apply_discount(-100, DiscountType::Fixed, -100); |
| 161 |
|
| 162 |
assert_eq!(result, 0); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn adversarial_percentage_discount_exactly_50_odd_price() { |
| 167 |
|
| 168 |
assert_eq!(apply_discount(1, DiscountType::Percentage, 50), 1); |
| 169 |
|
| 170 |
assert_eq!(apply_discount(3, DiscountType::Percentage, 50), 2); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn adversarial_apply_discount_invariant() { |
| 175 |
|
| 176 |
|
| 177 |
for price in [1, 50, 100, 999, 10000, 1_000_000] { |
| 178 |
for pct in [0, 1, 10, 25, 33, 50, 75, 99, 100] { |
| 179 |
let result = apply_discount(price, DiscountType::Percentage, pct); |
| 180 |
assert!( |
| 181 |
result >= 0 && result <= price, |
| 182 |
"Invariant violated: price={price}, pct={pct}, result={result}" |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn adversarial_fixed_discount_invariant() { |
| 190 |
|
| 191 |
for price in [1, 50, 100, 999, 10000] { |
| 192 |
for discount in [0, 1, 50, 100, 999, 10000, 999_999] { |
| 193 |
let result = apply_discount(price, DiscountType::Fixed, discount); |
| 194 |
assert!( |
| 195 |
result >= 0 && result <= price, |
| 196 |
"Invariant violated: price={price}, discount={discount}, result={result}" |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
proptest::proptest! { |
| 205 |
#[test] |
| 206 |
fn prop_percentage_discount_in_range(price in 0..=1_000_000i32, pct in 0..=100i32) { |
| 207 |
let result = apply_discount(price, DiscountType::Percentage, pct); |
| 208 |
proptest::prop_assert!(result >= 0, "Result {} should be >= 0", result); |
| 209 |
proptest::prop_assert!(result <= price, "Result {} should be <= price {}", result, price); |
| 210 |
} |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn prop_fixed_discount_in_range(price in 0..=1_000_000i32, discount in 0..=1_000_000i32) { |
| 214 |
let result = apply_discount(price, DiscountType::Fixed, discount); |
| 215 |
proptest::prop_assert!(result >= 0, "Result {} should be >= 0", result); |
| 216 |
proptest::prop_assert!(result <= price, "Result {} should be <= price {}", result, price); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn prop_100_percent_discount_is_zero(price in 0..=1_000_000i32) { |
| 221 |
proptest::prop_assert_eq!(apply_discount(price, DiscountType::Percentage, 100), 0); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn prop_0_percent_discount_is_identity(price in 0..=1_000_000i32) { |
| 226 |
proptest::prop_assert_eq!(apply_discount(price, DiscountType::Percentage, 0), price); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
proptest::proptest! { |
| 243 |
|
| 244 |
|
| 245 |
#[test] |
| 246 |
fn prop_removing_a_fixed_discount_restores_the_price_exactly( |
| 247 |
price in 0..=1_000_000i32, |
| 248 |
discount in 0..=1_000_000i32, |
| 249 |
) { |
| 250 |
proptest::prop_assume!(discount <= price); |
| 251 |
let discounted = apply_discount(price, DiscountType::Fixed, discount); |
| 252 |
proptest::prop_assert_eq!(discounted + discount, price); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
#[test] |
| 260 |
fn prop_a_fixed_discount_over_the_price_destroys_it( |
| 261 |
price in 0..=1_000_000i32, |
| 262 |
excess in 0..=1_000_000i32, |
| 263 |
) { |
| 264 |
let discount = price.saturating_add(excess); |
| 265 |
proptest::prop_assert_eq!(apply_discount(price, DiscountType::Fixed, discount), 0); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[test] |
| 281 |
fn prop_a_percentage_discount_loses_exactly_the_rounding_remainder( |
| 282 |
price in 0..=1_000_000i32, |
| 283 |
pct in 0..=100i32, |
| 284 |
) { |
| 285 |
let discounted = apply_discount(price, DiscountType::Percentage, pct); |
| 286 |
let remainder = i64::from(discounted) * 100 - i64::from(price) * i64::from(100 - pct); |
| 287 |
proptest::prop_assert!( |
| 288 |
(0..100).contains(&remainder), |
| 289 |
"price={} pct={} discounted={} left remainder {}, outside [0, 100)", |
| 290 |
price, pct, discounted, remainder, |
| 291 |
); |
| 292 |
proptest::prop_assert_eq!( |
| 293 |
remainder, |
| 294 |
(i64::from(price) * i64::from(pct)) % 100, |
| 295 |
"the remainder is not the one integer division dropped", |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
#[test] |
| 303 |
fn prop_removing_a_percentage_discount_is_exact_when_it_divides_evenly( |
| 304 |
hundreds in 0..=10_000i32, |
| 305 |
pct in 0..=99i32, |
| 306 |
) { |
| 307 |
let price = hundreds * 100; |
| 308 |
let discounted = apply_discount(price, DiscountType::Percentage, pct); |
| 309 |
|
| 310 |
proptest::prop_assert_eq!( |
| 311 |
i64::from(discounted) * 100 / i64::from(100 - pct), |
| 312 |
i64::from(price), |
| 313 |
); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
#[test] |
| 322 |
fn removing_a_full_discount_is_impossible_by_construction() { |
| 323 |
for price in [0, 1, 99, 100, 101, 999, 1_000_000] { |
| 324 |
assert_eq!(apply_discount(price, DiscountType::Percentage, 100), 0); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
fn unscoped_discount_promo(max_uses: Option<i32>) -> ValidatedPromo { |
| 332 |
ValidatedPromo { |
| 333 |
code: DbPromoCode { |
| 334 |
id: PromoCodeId::new(), |
| 335 |
creator_id: UserId::new(), |
| 336 |
code: "SAVE10".to_string(), |
| 337 |
code_purpose: CodePurpose::Discount, |
| 338 |
discount_type: Some(DiscountType::Percentage), |
| 339 |
discount_value: Some(10), |
| 340 |
min_price_cents: 0, |
| 341 |
trial_days: None, |
| 342 |
item_id: None, |
| 343 |
project_id: None, |
| 344 |
tier_id: None, |
| 345 |
max_uses, |
| 346 |
use_count: 0, |
| 347 |
expires_at: None, |
| 348 |
starts_at: None, |
| 349 |
created_at: chrono::Utc::now(), |
| 350 |
is_platform_wide: false, |
| 351 |
}, |
| 352 |
is_platform_wide: false, |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
#[test] |
| 357 |
fn single_use_code_discounts_every_eligible_cart_line() { |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
let promo = unscoped_discount_promo(Some(1)); |
| 364 |
for base in [1000, 2000, 4999] { |
| 365 |
let result = apply_promo_to_item(&promo, ItemId::new(), ProjectId::new(), base).unwrap(); |
| 366 |
let PromoApplication::Apply(applied) = result else { |
| 367 |
panic!("expected Apply for an eligible cart line at base {base}"); |
| 368 |
}; |
| 369 |
assert_eq!(applied.price_cents, base - base / 10); |
| 370 |
|
| 371 |
assert_eq!(applied.funding, DiscountFunding::CreatorFunded); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
assert_eq!(promo.code.use_count, 0); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
fn platform_fixed_credit(cents: i32) -> ValidatedPromo { |
| 382 |
ValidatedPromo { |
| 383 |
code: DbPromoCode { |
| 384 |
id: PromoCodeId::new(), |
| 385 |
creator_id: UserId::new(), |
| 386 |
code: "FANPLUS".to_string(), |
| 387 |
code_purpose: CodePurpose::Discount, |
| 388 |
discount_type: Some(DiscountType::Fixed), |
| 389 |
discount_value: Some(cents), |
| 390 |
min_price_cents: 0, |
| 391 |
trial_days: None, |
| 392 |
item_id: None, |
| 393 |
project_id: None, |
| 394 |
tier_id: None, |
| 395 |
max_uses: None, |
| 396 |
use_count: 0, |
| 397 |
expires_at: None, |
| 398 |
starts_at: None, |
| 399 |
created_at: chrono::Utc::now(), |
| 400 |
is_platform_wide: true, |
| 401 |
}, |
| 402 |
is_platform_wide: true, |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn platform_fixed_credit_budget_is_face_value() { |
| 408 |
assert_eq!( |
| 409 |
platform_fixed_credit(500).platform_credit_budget_cents(), |
| 410 |
Some(500) |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn seller_and_percentage_codes_have_no_credit_budget() { |
| 416 |
|
| 417 |
assert_eq!( |
| 418 |
unscoped_discount_promo(None).platform_credit_budget_cents(), |
| 419 |
None |
| 420 |
); |
| 421 |
|
| 422 |
|
| 423 |
let mut pct = platform_fixed_credit(500); |
| 424 |
pct.code.discount_type = Some(DiscountType::Percentage); |
| 425 |
pct.code.discount_value = Some(20); |
| 426 |
assert_eq!(pct.platform_credit_budget_cents(), None); |
| 427 |
} |
| 428 |
|
| 429 |
#[test] |
| 430 |
fn platform_fixed_credit_spent_once_across_cart() { |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
let promo = platform_fixed_credit(500); |
| 435 |
let mut budget = promo.platform_credit_budget_cents(); |
| 436 |
assert_eq!(budget, Some(500)); |
| 437 |
|
| 438 |
let mut total_credit = 0i64; |
| 439 |
let mut total_buyer_paid = 0i64; |
| 440 |
for _ in 0..3 { |
| 441 |
let PromoApplication::Apply(applied) = |
| 442 |
apply_promo_to_item(&promo, ItemId::new(), ProjectId::new(), 1000).unwrap() |
| 443 |
else { |
| 444 |
panic!("expected Apply for an eligible platform-credit line"); |
| 445 |
}; |
| 446 |
let (final_price, credit) = cap_line_to_credit_budget(applied, &mut budget); |
| 447 |
total_credit += credit; |
| 448 |
total_buyer_paid += i64::from(final_price); |
| 449 |
} |
| 450 |
assert_eq!( |
| 451 |
total_credit, 500, |
| 452 |
"MNW reimburses the seller exactly the face value, once" |
| 453 |
); |
| 454 |
assert_eq!( |
| 455 |
total_buyer_paid, |
| 456 |
3000 - 500, |
| 457 |
"buyer gets the $5 credit exactly once" |
| 458 |
); |
| 459 |
assert_eq!(budget, Some(0), "balance fully spent"); |
| 460 |
} |
| 461 |
|
| 462 |
#[test] |
| 463 |
fn platform_fixed_credit_carries_balance_across_cheap_lines() { |
| 464 |
|
| 465 |
|
| 466 |
let promo = platform_fixed_credit(500); |
| 467 |
let mut budget = promo.platform_credit_budget_cents(); |
| 468 |
let mut total_credit = 0i64; |
| 469 |
for _ in 0..2 { |
| 470 |
let PromoApplication::Apply(applied) = |
| 471 |
apply_promo_to_item(&promo, ItemId::new(), ProjectId::new(), 100).unwrap() |
| 472 |
else { |
| 473 |
panic!("expected Apply"); |
| 474 |
}; |
| 475 |
let (final_price, credit) = cap_line_to_credit_budget(applied, &mut budget); |
| 476 |
assert_eq!(final_price, 0, "a $1 item is fully covered by the credit"); |
| 477 |
total_credit += credit; |
| 478 |
} |
| 479 |
assert_eq!(total_credit, 200); |
| 480 |
assert_eq!( |
| 481 |
budget, |
| 482 |
Some(300), |
| 483 |
"unspent balance carries to the rest of the cart" |
| 484 |
); |
| 485 |
} |
| 486 |
|