use super::*;
use crate::common::test_helpers::create_test_db_pool;
use crate::domain::retention_policy::RetentionBatchPolicy;
use crate::infrastructure::database::entities::geo_restriction_log;
use chrono::Duration;
use sea_orm::{ActiveModelTrait, EntityTrait, Set};
static RESTRICTION_LOCK: std::sync::OnceLock<tokio::sync::Mutex<()>> = std::sync::OnceLock::new();
fn restriction_lock() -> &'static tokio::sync::Mutex<()> {
RESTRICTION_LOCK.get_or_init(|| tokio::sync::Mutex::new(()))
}
async fn insert_log_with_age(days_ago: i64) -> crate::domain::id::Id {
let pool = create_test_db_pool();
let session = pool.get_session("admin").await.expect("get session");
let conn = session.connection().expect("get connection");
let id = crate::domain::id::Id::generate();
let model = geo_restriction_log::ActiveModel {
id: Set(id.into()),
team_id: Set(crate::domain::id::Id::generate().into()),
ip_address: Set("192.168.1.1".to_string()),
country_code: Set(Some("US".to_string())),
restriction_type: Set("country_block".to_string()),
url: Set(None),
reason: Set("retention test".to_string()),
created_at: Set((chrono::Utc::now() - Duration::days(days_ago)).fixed_offset()),
};
model.insert(conn).await.expect("insert log");
id
}
async fn row_exists(id: crate::domain::id::Id) -> bool {
let pool = create_test_db_pool();
let session = pool.get_session("admin").await.expect("session");
let conn = session.connection().expect("connection");
geo_restriction_log::Entity::find_by_id(id)
.one(conn)
.await
.expect("query row")
.is_some()
}
#[tokio::test]
async fn cleanup_expired_deletes_old_keeps_recent() {
let _guard = restriction_lock().lock().await;
let repo = DatabaseGeoRestrictionRepository::new(create_test_db_pool());
let old_id = insert_log_with_age(100).await;
let recent_id = insert_log_with_age(1).await;
let deleted = repo
.cleanup_expired(90, &RetentionBatchPolicy::default())
.await
.expect("cleanup_expired failed");
assert!(
deleted >= 1,
"deleted count should include our old row: {deleted}"
);
assert!(
!row_exists(old_id).await,
"100-day-old row should be deleted"
);
assert!(row_exists(recent_id).await, "1-day-old row should be kept");
}
#[tokio::test]
async fn cleanup_expired_with_zero_retention_removes_fresh_rows() {
let _guard = restriction_lock().lock().await;
let repo = DatabaseGeoRestrictionRepository::new(create_test_db_pool());
let fresh_id = insert_log_with_age(0).await;
let deleted = repo
.cleanup_expired(0, &RetentionBatchPolicy::default())
.await
.expect("cleanup_expired failed");
assert!(
deleted >= 1,
"fresh row should be removable with 0 retention: {deleted}"
);
assert!(
!row_exists(fresh_id).await,
"row should be deleted with 0-day retention"
);
}
#[tokio::test]
async fn cleanup_expired_batches_until_drained() {
let _guard = restriction_lock().lock().await;
let repo = DatabaseGeoRestrictionRepository::new(create_test_db_pool());
let mut old_ids = Vec::new();
for _ in 0..5 {
old_ids.push(insert_log_with_age(100).await);
}
let recent_id = insert_log_with_age(1).await;
let policy = RetentionBatchPolicy {
batch_size: 2,
max_rows_per_cycle: 100_000,
statement_timeout_ms: 60_000,
};
let deleted = repo
.cleanup_expired(90, &policy)
.await
.expect("cleanup_expired failed");
assert!(
deleted >= 5,
"our 5 old rows must be among the deleted: {deleted}"
);
for id in old_ids {
assert!(!row_exists(id).await, "100-day-old row should be deleted");
}
assert!(row_exists(recent_id).await, "1-day-old row must survive");
}
#[tokio::test]
async fn cleanup_expired_caps_at_max_rows_per_cycle() {
let _guard = restriction_lock().lock().await;
let repo = DatabaseGeoRestrictionRepository::new(create_test_db_pool());
for _ in 0..5 {
insert_log_with_age(100).await;
}
let policy = RetentionBatchPolicy {
batch_size: 2,
max_rows_per_cycle: 3,
statement_timeout_ms: 60_000,
};
repo.cleanup_expired(90, &RetentionBatchPolicy::default())
.await
.expect("pre-clean failed");
for _ in 0..5 {
insert_log_with_age(100).await;
}
let deleted = repo
.cleanup_expired(90, &policy)
.await
.expect("cleanup_expired failed");
assert_eq!(
deleted, 3,
"deletion must stop once max_rows_per_cycle is reached"
);
}