| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
pub const STORAGE_RATE_CENTS_PER_GB: i64 = 3; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
pub const BASE_FLOOR_CENTS: i64 = 31; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
pub const WARNING_THRESHOLDS_PCT: &[i16] = &[75, 90, 100]; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub fn monthly_price_cents( |
| 42 |
enforcement_mode: &str, |
| 43 |
storage_gb_cap: Option<u32>, |
| 44 |
key_cap: Option<u32>, |
| 45 |
gb_per_key: Option<u32>, |
| 46 |
) -> i64 { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
let gb: i64 = match enforcement_mode { |
| 53 |
"bulk" => i64::from(storage_gb_cap.unwrap_or(0)), |
| 54 |
"per_key" => { |
| 55 |
let k = i64::from(key_cap.unwrap_or(0)); |
| 56 |
let g = i64::from(gb_per_key.unwrap_or(0)); |
| 57 |
k.saturating_mul(g) |
| 58 |
} |
| 59 |
_ => 0, |
| 60 |
}; |
| 61 |
let raw = gb.saturating_mul(STORAGE_RATE_CENTS_PER_GB); |
| 62 |
raw.max(BASE_FLOOR_CENTS) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
pub fn storage_cap_bytes(storage_gb: u32) -> i64 { |
| 67 |
i64::from(storage_gb) * 1024 * 1024 * 1024 |
| 68 |
} |
| 69 |
|
| 70 |
#[cfg(test)] |
| 71 |
mod tests { |
| 72 |
use super::*; |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn bulk_mode_pricing() { |
| 76 |
|
| 77 |
assert_eq!(monthly_price_cents("bulk", Some(100), None, None), 300); |
| 78 |
|
| 79 |
assert_eq!(monthly_price_cents("bulk", Some(1000), None, None), 3000); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn per_key_mode_pricing() { |
| 84 |
|
| 85 |
assert_eq!(monthly_price_cents("per_key", None, Some(50), Some(2)), 300); |
| 86 |
|
| 87 |
assert_eq!(monthly_price_cents("per_key", None, Some(1000), Some(1)), 3000); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn floor_kicks_in_for_small_accounts() { |
| 92 |
|
| 93 |
assert_eq!(monthly_price_cents("bulk", Some(1), None, None), 31); |
| 94 |
|
| 95 |
assert_eq!(monthly_price_cents("bulk", Some(10), None, None), 31); |
| 96 |
|
| 97 |
assert_eq!(monthly_price_cents("bulk", Some(11), None, None), 33); |
| 98 |
|
| 99 |
assert_eq!(monthly_price_cents("per_key", None, Some(1), Some(1)), 31); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn heavy_workload_pricing() { |
| 104 |
|
| 105 |
assert_eq!(monthly_price_cents("bulk", Some(10_240), None, None), 30_720); |
| 106 |
|
| 107 |
assert_eq!(monthly_price_cents("per_key", None, Some(10_000), Some(1)), 30_000); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn missing_knobs_drop_to_floor() { |
| 112 |
|
| 113 |
assert_eq!(monthly_price_cents("bulk", None, None, None), BASE_FLOOR_CENTS); |
| 114 |
assert_eq!(monthly_price_cents("per_key", None, None, None), BASE_FLOOR_CENTS); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn unknown_mode_drops_to_floor() { |
| 119 |
|
| 120 |
assert_eq!(monthly_price_cents("unknown", Some(100), None, None), BASE_FLOOR_CENTS); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn floor_amount_covers_stripe_fee() { |
| 125 |
|
| 126 |
|
| 127 |
let net = (BASE_FLOOR_CENTS as f64) * 0.971 - 30.0; |
| 128 |
assert!(net >= 0.0, "floor must net ≥ 0 after Stripe fees, got {net}"); |
| 129 |
assert!(net < 1.0, "floor should be tight, not overshoot — got {net}"); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn storage_cap_in_bytes() { |
| 134 |
assert_eq!(storage_cap_bytes(10), 10 * 1024 * 1024 * 1024); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#[test] |
| 140 |
fn pricing_at_u32_max_does_not_panic() { |
| 141 |
|
| 142 |
let p = monthly_price_cents("bulk", Some(u32::MAX), None, None); |
| 143 |
assert!(p > 0, "huge price should be positive, got {p}"); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn per_key_pricing_at_u32_max_saturates_cleanly() { |
| 148 |
|
| 149 |
|
| 150 |
let p = monthly_price_cents("per_key", None, Some(u32::MAX), Some(u32::MAX)); |
| 151 |
assert!(p > 0, "saturated price should still be positive, got {p}"); |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn storage_cap_at_u32_max_fits_in_i64() { |
| 156 |
|
| 157 |
let bytes = storage_cap_bytes(u32::MAX); |
| 158 |
assert!(bytes > 0, "u32::MAX GB should produce a positive i64"); |
| 159 |
assert_eq!(bytes, (u32::MAX as i64) * 1024 * 1024 * 1024); |
| 160 |
} |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn bulk_with_zero_gb_drops_to_floor() { |
| 164 |
|
| 165 |
|
| 166 |
assert_eq!(monthly_price_cents("bulk", Some(0), None, None), BASE_FLOOR_CENTS); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn per_key_one_dimension_zero_drops_to_floor() { |
| 171 |
|
| 172 |
assert_eq!(monthly_price_cents("per_key", None, Some(0), Some(10)), BASE_FLOOR_CENTS); |
| 173 |
assert_eq!(monthly_price_cents("per_key", None, Some(10), Some(0)), BASE_FLOOR_CENTS); |
| 174 |
} |
| 175 |
} |
| 176 |
|