CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Agent (push) Successful in 5m57s
81 lines
3.0 KiB
Rust
81 lines
3.0 KiB
Rust
//! The month's peak of extra targets goes to the registry when it rises and
|
|
//! once a day besides; a new month starts again (t72). Runs against a live
|
|
//! Mongo (`TEST_MONGODB_URI`) and skips cleanly without one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
use chrono::{Duration, TimeZone, Utc};
|
|
use compliance_agent::database::DatabasePool;
|
|
use compliance_agent::metering::{assign_billing, charge, record_extra_peak};
|
|
use compliance_core::models::{OnboardedTarget, PlanLimits, RunCharge, TargetType};
|
|
use mongodb::bson::doc;
|
|
|
|
const TENANT: &str = "extra-peak";
|
|
|
|
#[tokio::test]
|
|
async fn the_peak_is_sent_when_it_rises_and_once_a_day() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"extrapeak_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP extra_peak: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let db = pool.for_tenant_id(TENANT).await.unwrap();
|
|
// Arrange: three targets on a plan that includes one, on 6 Nov.
|
|
let mut ids = Vec::new();
|
|
for days in [3, 2, 1] {
|
|
let mut target = OnboardedTarget::new(format!("t{days}"), TargetType::FirmwareRtos);
|
|
target.created_at = Utc::now() - Duration::days(days);
|
|
let res = db.onboarded_targets().insert_one(&target).await.unwrap();
|
|
ids.push(res.inserted_id.as_object_id().unwrap());
|
|
}
|
|
assign_billing(&db, Some(1)).await.unwrap();
|
|
let nov6 = Utc.with_ymd_and_hms(2026, 11, 6, 9, 0, 0).single().unwrap();
|
|
|
|
// Act / Assert: two extra targets, sent once that day.
|
|
assert_eq!(record_extra_peak(&db, nov6).await.unwrap(), Some(2));
|
|
assert_eq!(record_extra_peak(&db, nov6).await.unwrap(), None);
|
|
|
|
// One extra target is archived: the month's peak stays two.
|
|
db.onboarded_targets()
|
|
.update_one(
|
|
doc! { "_id": ids[2] },
|
|
doc! { "$set": { "archived_at": mongodb::bson::DateTime::now() } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assign_billing(&db, Some(1)).await.unwrap();
|
|
assert_eq!(record_extra_peak(&db, nov6).await.unwrap(), None);
|
|
assert_eq!(
|
|
record_extra_peak(&db, nov6 + Duration::days(1))
|
|
.await
|
|
.unwrap(),
|
|
Some(2),
|
|
"re-sent once the next day"
|
|
);
|
|
|
|
// A new month starts from the targets extra now.
|
|
let dec1 = Utc.with_ymd_and_hms(2026, 12, 1, 9, 0, 0).single().unwrap();
|
|
assert_eq!(record_extra_peak(&db, dec1).await.unwrap(), Some(1));
|
|
|
|
// The month document the peak created still counts the quota.
|
|
let plan = PlanLimits {
|
|
monthly_pruefungen: Some(1),
|
|
prueftag_cap: None,
|
|
};
|
|
assert_eq!(
|
|
charge(&db, "t", plan, dec1).await.unwrap().charge,
|
|
RunCharge::Quota
|
|
);
|
|
assert_eq!(
|
|
charge(&db, "t", plan, dec1).await.unwrap().charge,
|
|
RunCharge::Prueftag
|
|
);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|