use crate::db::Database;
use crate::db::repository::usage_ledger::UsageLedgerRepository;
use crate::usage::data::{DashboardData, Period};
#[tokio::test]
async fn model_rows_and_header_sum_the_ledger_cost() {
let home = tempfile::tempdir().expect("tempdir");
crate::config::profile::with_home_override_async(home.path().to_path_buf(), async {
let db = Database::connect_in_memory().await.expect("in-memory DB");
db.run_migrations().await.expect("migrations");
let repo = UsageLedgerRepository::new(db.pool().clone());
repo.record("s1", "zai", "glm-5.3-flash", 10_000_000, 0.30, None)
.await
.unwrap();
repo.record("s1", "zai", "glm-5.3-flash", 4_540_704, 0.2246, None)
.await
.unwrap();
let d = DashboardData::fetch(db.pool(), Period::AllTime)
.await
.expect("dashboard fetch");
let model_sum: f64 = d.models.iter().map(|m| m.cost).sum();
assert!(
(model_sum - 0.5246).abs() < 1e-9,
"model rows must sum the ledger, got {model_sum}"
);
assert!(
(d.summary.total_cost - 0.5246).abs() < 1e-9,
"header must match the model rows, got {}",
d.summary.total_cost
);
})
.await;
}