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