CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 5s
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Successful in 4m54s
CI / Deploy Dashboard (push) Successful in 6m27s
CI / Deploy Agent (push) Successful in 8m36s
151 lines
5.7 KiB
Rust
151 lines
5.7 KiB
Rust
//! The oldest active targets are the plan's included ones; the rest are
|
|
//! extra, and an extra one moves up when an included one is archived (t72).
|
|
//! Runs against a live Mongo (`TEST_MONGODB_URI`) and skips cleanly without
|
|
//! one.
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
use chrono::{Duration, Utc};
|
|
use compliance_agent::database::{Database, DatabasePool};
|
|
use compliance_agent::metering::assign_billing;
|
|
use compliance_core::models::{OnboardedTarget, TargetBilling, TargetType};
|
|
use mongodb::bson::{doc, oid::ObjectId};
|
|
|
|
const TENANT: &str = "target-billing";
|
|
|
|
async fn billing(db: &Database, id: ObjectId) -> TargetBilling {
|
|
db.onboarded_targets()
|
|
.find_one(doc! { "_id": id })
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.billing
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_oldest_targets_are_included_and_an_extra_one_moves_up() {
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!(
|
|
"billing_{}",
|
|
&uuid::Uuid::new_v4().simple().to_string()[..10]
|
|
);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP target_billing: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let db = pool.for_tenant_id(TENANT).await.unwrap();
|
|
|
|
// Arrange: three targets, created a day apart.
|
|
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());
|
|
}
|
|
let (oldest, middle, newest) = (ids[0], ids[1], ids[2]);
|
|
|
|
// Act: a plan with two included targets.
|
|
let changes = assign_billing(&db, Some(2)).await.unwrap();
|
|
|
|
// Assert: only the newest is extra, and it is the one change.
|
|
assert_eq!(changes, vec![(newest, TargetBilling::Extra)]);
|
|
assert_eq!(billing(&db, oldest).await, TargetBilling::Included);
|
|
assert_eq!(billing(&db, middle).await, TargetBilling::Included);
|
|
assert_eq!(billing(&db, newest).await, TargetBilling::Extra);
|
|
assert!(
|
|
assign_billing(&db, Some(2)).await.unwrap().is_empty(),
|
|
"nothing changes a second time"
|
|
);
|
|
|
|
// Act: the oldest is archived.
|
|
db.onboarded_targets()
|
|
.update_one(
|
|
doc! { "_id": oldest },
|
|
doc! { "$set": { "archived_at": mongodb::bson::DateTime::now() } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assign_billing(&db, Some(2)).await.unwrap();
|
|
|
|
// Assert: the extra one moved up.
|
|
assert_eq!(billing(&db, newest).await, TargetBilling::Included);
|
|
|
|
// Act / Assert: a plan without a limit includes every target.
|
|
assign_billing(&db, Some(0)).await.unwrap();
|
|
assert_eq!(billing(&db, middle).await, TargetBilling::Extra);
|
|
assign_billing(&db, None).await.unwrap();
|
|
assert_eq!(billing(&db, middle).await, TargetBilling::Included);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_swap_moves_both_and_holds_the_one_swapped_in_until_month_end() {
|
|
use compliance_agent::metering::swap;
|
|
use compliance_core::models::SwapRefusal;
|
|
|
|
let uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let prefix = format!("swap_{}", &uuid::Uuid::new_v4().simple().to_string()[..10]);
|
|
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
|
|
eprintln!("SKIP target_billing swap: no MongoDB reachable at {uri}");
|
|
return;
|
|
};
|
|
let db = pool.for_tenant_id(TENANT).await.unwrap();
|
|
// Arrange: three targets on a plan with two included: the newest is extra.
|
|
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());
|
|
}
|
|
let (oldest, middle, newest) = (ids[0], ids[1], ids[2]);
|
|
assign_billing(&db, Some(2)).await.unwrap();
|
|
let nov6 = chrono::TimeZone::with_ymd_and_hms(&Utc, 2026, 11, 6, 9, 0, 0)
|
|
.single()
|
|
.unwrap();
|
|
|
|
// Act: swap the newest in for the oldest.
|
|
let swapped = swap(&db, newest, oldest, true, nov6).await.unwrap();
|
|
assign_billing(&db, Some(2)).await.unwrap();
|
|
|
|
// Assert: they changed places; the count of extra targets is the same.
|
|
assert_eq!(swapped, Some(Ok(())));
|
|
assert_eq!(billing(&db, newest).await, TargetBilling::Included);
|
|
assert_eq!(billing(&db, oldest).await, TargetBilling::Extra);
|
|
assert_eq!(billing(&db, middle).await, TargetBilling::Included);
|
|
|
|
// The one swapped in stays included this month; another may go out.
|
|
assert_eq!(
|
|
swap(&db, oldest, newest, true, nov6).await.unwrap(),
|
|
Some(Err(SwapRefusal::Locked))
|
|
);
|
|
assert_eq!(
|
|
swap(&db, oldest, middle, true, nov6).await.unwrap(),
|
|
Some(Ok(()))
|
|
);
|
|
assign_billing(&db, Some(2)).await.unwrap();
|
|
assert_eq!(billing(&db, middle).await, TargetBilling::Extra);
|
|
|
|
// From December, the target swapped in on 6 Nov may go out again.
|
|
let dec1 = chrono::TimeZone::with_ymd_and_hms(&Utc, 2026, 12, 1, 9, 0, 0)
|
|
.single()
|
|
.unwrap();
|
|
assert_eq!(
|
|
swap(&db, middle, newest, true, dec1).await.unwrap(),
|
|
Some(Ok(()))
|
|
);
|
|
assert_eq!(
|
|
swap(&db, middle, ObjectId::new(), true, dec1)
|
|
.await
|
|
.unwrap(),
|
|
None,
|
|
"an unknown target"
|
|
);
|
|
|
|
pool.drop_tenant(TENANT).await.ok();
|
|
}
|