use crate::core::errors::ComplianceError;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RuntimeMetrics {
pub current_mau: u64,
pub monthly_revenue: f64,
pub has_authorizing_key: bool,
}
impl RuntimeMetrics {
pub fn free_tier() -> Self {
Self {
current_mau: 0,
monthly_revenue: 0.0,
has_authorizing_key: false,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct ComplianceMonitor {
pub mau_threshold: u64,
pub revenue_threshold_usd: f64,
pub enforcement_strict: bool,
}
impl ComplianceMonitor {
pub fn new() -> Self {
Self {
mau_threshold: 50_000,
revenue_threshold_usd: 10_000.0,
enforcement_strict: false,
}
}
pub fn assert_compliance(&self, metrics: &RuntimeMetrics) -> Result<(), ComplianceError> {
let crossed_trigger = metrics.current_mau > self.mau_threshold
|| metrics.monthly_revenue > self.revenue_threshold_usd;
if crossed_trigger && !metrics.has_authorizing_key {
return Err(ComplianceError::ThresholdExceeded);
}
Ok(())
}
}
impl Default for ComplianceMonitor {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn free_tier_is_compliant() {
let monitor = ComplianceMonitor::new();
assert!(monitor
.assert_compliance(&RuntimeMetrics::free_tier())
.is_ok());
}
#[test]
fn crossing_mau_without_key_fails() {
let monitor = ComplianceMonitor::new();
let metrics = RuntimeMetrics {
current_mau: 50_001,
monthly_revenue: 0.0,
has_authorizing_key: false,
};
assert_eq!(
monitor.assert_compliance(&metrics),
Err(ComplianceError::ThresholdExceeded)
);
}
#[test]
fn crossing_revenue_with_key_is_allowed() {
let monitor = ComplianceMonitor::new();
let metrics = RuntimeMetrics {
current_mau: 1_000_000,
monthly_revenue: 250_000.0,
has_authorizing_key: true,
};
assert!(monitor.assert_compliance(&metrics).is_ok());
}
#[test]
fn exactly_at_threshold_is_compliant() {
let monitor = ComplianceMonitor::new();
let metrics = RuntimeMetrics {
current_mau: 50_000,
monthly_revenue: 10_000.0,
has_authorizing_key: false,
};
assert!(monitor.assert_compliance(&metrics).is_ok());
}
}