//! SyncKit developer billing: pricing formula and constants. //! //! Two modes: //! //! bulk — price = storage_gb_cap × $0.03 //! per_key — price = key_cap × gb_per_key × $0.03 //! //! Both are pure GB-based pricing. Egress and ingress are absorbed by the //! storage rate's ~2× margin against Cloudflare R2 ($0.015/GB) where SyncKit //! blobs are hosted. //! //! Invoices are floored at a Stripe-fee-cover threshold so we never lose money //! on a transaction. See `BASE_FLOOR_CENTS` for the math. /// Storage rate in cents per GB per month. Calibrated to ~2× R2 cost /// ($0.015/GB storage, $0 egress on R2). The 2× margin spread absorbs any /// ingress/egress cost variance, so we don't need a separate egress price. pub const STORAGE_RATE_CENTS_PER_GB: i64 = 3; /// Stripe-fee-cover floor in cents. Stripe charges 2.9% + $0.30 per /// successful charge. We pick the smallest invoice `F` (cents) such that the /// remainder after Stripe fees is non-negative: /// /// F × (1 − 0.029) − 30 ≥ 0 ⇒ F ≥ 30 / 0.971 ⇒ F ≥ 30.9¢ /// /// Round up to 31¢. At the floor, MNW nets ~$0 — covered, not profitable. pub const BASE_FLOOR_CENTS: i64 = 31; /// Warning thresholds (percent of storage cap). Matches CHECK constraint on /// `sync_app_usage_current.last_warning_pct`. Only storage is enforced, so /// these thresholds apply to storage usage only. pub const WARNING_THRESHOLDS_PCT: &[i16] = &[75, 90, 100]; /// Compute the monthly Stripe invoice amount in cents for a given knob set. /// /// In bulk mode: `storage_gb_cap` is set, others are `None`. /// In per_key mode: `key_cap` and `gb_per_key` are set, `storage_gb_cap` is `None`. /// /// Floors at `BASE_FLOOR_CENTS` so we never invoice below the Stripe-fee /// break-even amount. pub fn monthly_price_cents( enforcement_mode: &str, storage_gb_cap: Option, key_cap: Option, gb_per_key: Option, ) -> i64 { // Pure integer-cents arithmetic. The rate is a whole number of cents and // the caps are whole GB, so there is no fractional money to round; the old // `(gb as f64 * 3.0).ceil()` was an unnecessary trip through f64. Saturating // multiplies keep absurd admin-set caps from overflowing i64 instead of // wrapping to a negative invoice. let gb: i64 = match enforcement_mode { "bulk" => i64::from(storage_gb_cap.unwrap_or(0)), "per_key" => { let k = i64::from(key_cap.unwrap_or(0)); let g = i64::from(gb_per_key.unwrap_or(0)); k.saturating_mul(g) } _ => 0, }; let raw = gb.saturating_mul(STORAGE_RATE_CENTS_PER_GB); raw.max(BASE_FLOOR_CENTS) } /// Storage cap in bytes for the given GB cap. pub fn storage_cap_bytes(storage_gb: u32) -> i64 { i64::from(storage_gb) * 1024 * 1024 * 1024 } #[cfg(test)] mod tests { use super::*; #[test] fn bulk_mode_pricing() { // 100 GB bulk → 100 × 3 = 300 cents. assert_eq!(monthly_price_cents("bulk", Some(100), None, None), 300); // 1000 GB → $30. assert_eq!(monthly_price_cents("bulk", Some(1000), None, None), 3000); } #[test] fn per_key_mode_pricing() { // 50 keys × 2 GB = 100 GB equivalent → 300 cents. Matches 100 GB bulk. assert_eq!(monthly_price_cents("per_key", None, Some(50), Some(2)), 300); // 1000 keys × 1 GB → $30. assert_eq!(monthly_price_cents("per_key", None, Some(1000), Some(1)), 3000); } #[test] fn floor_kicks_in_for_small_accounts() { // 1 GB bulk → 3¢ raw, floored to 31¢. assert_eq!(monthly_price_cents("bulk", Some(1), None, None), 31); // 10 GB → 30¢, also floored to 31¢ (one cent short). assert_eq!(monthly_price_cents("bulk", Some(10), None, None), 31); // 11 GB → 33¢, above floor. assert_eq!(monthly_price_cents("bulk", Some(11), None, None), 33); // 1 key × 1 GB → 3¢ raw, floored. assert_eq!(monthly_price_cents("per_key", None, Some(1), Some(1)), 31); } #[test] fn heavy_workload_pricing() { // 10 TB bulk → 10240 × 3 = 30720¢ = $307.20. assert_eq!(monthly_price_cents("bulk", Some(10_240), None, None), 30_720); // 10k keys × 1 GB → same. assert_eq!(monthly_price_cents("per_key", None, Some(10_000), Some(1)), 30_000); } #[test] fn missing_knobs_drop_to_floor() { // Mode is set but no knobs provided — should hit the floor. assert_eq!(monthly_price_cents("bulk", None, None, None), BASE_FLOOR_CENTS); assert_eq!(monthly_price_cents("per_key", None, None, None), BASE_FLOOR_CENTS); } #[test] fn unknown_mode_drops_to_floor() { // Defensive: an unrecognized mode shouldn't blow up; it lands at the floor. assert_eq!(monthly_price_cents("unknown", Some(100), None, None), BASE_FLOOR_CENTS); } #[test] fn floor_amount_covers_stripe_fee() { // 31¢ × 0.971 = 30.10¢, minus 30¢ fixed fee = 0.10¢ net. Verifies the // documented math: the floor covers Stripe's fee with ~0 margin. let net = (BASE_FLOOR_CENTS as f64) * 0.971 - 30.0; assert!(net >= 0.0, "floor must net ≥ 0 after Stripe fees, got {net}"); assert!(net < 1.0, "floor should be tight, not overshoot — got {net}"); } #[test] fn storage_cap_in_bytes() { assert_eq!(storage_cap_bytes(10), 10 * 1024 * 1024 * 1024); } // ── Edge cases (test-fuzz) ── #[test] fn pricing_at_u32_max_does_not_panic() { // u32::MAX GB × 3¢ ≈ 1.3e10 cents, fits in i64. The cast must not panic. let p = monthly_price_cents("bulk", Some(u32::MAX), None, None); assert!(p > 0, "huge price should be positive, got {p}"); } #[test] fn per_key_pricing_at_u32_max_saturates_cleanly() { // u32::MAX × u32::MAX overflows f64 precision but Rust's f64-as-i64 cast // saturates at i64::MAX rather than UB. Must not panic. let p = monthly_price_cents("per_key", None, Some(u32::MAX), Some(u32::MAX)); assert!(p > 0, "saturated price should still be positive, got {p}"); } #[test] fn storage_cap_at_u32_max_fits_in_i64() { // u32::MAX × 2^30 = ~4.6e18, well under i64::MAX (~9.2e18). let bytes = storage_cap_bytes(u32::MAX); assert!(bytes > 0, "u32::MAX GB should produce a positive i64"); assert_eq!(bytes, (u32::MAX as i64) * 1024 * 1024 * 1024); } #[test] fn bulk_with_zero_gb_drops_to_floor() { // Defensive: validate_knobs rejects gb=0 at the route layer, but the // pure function should still produce the floor rather than 0. assert_eq!(monthly_price_cents("bulk", Some(0), None, None), BASE_FLOOR_CENTS); } #[test] fn per_key_one_dimension_zero_drops_to_floor() { // If only one of key_cap/gb_per_key is 0, the product is 0 → floor. assert_eq!(monthly_price_cents("per_key", None, Some(0), Some(10)), BASE_FLOOR_CENTS); assert_eq!(monthly_price_cents("per_key", None, Some(10), Some(0)), BASE_FLOOR_CENTS); } }