diff --git a/crates/db/tests/ai_credits_reaper.rs b/crates/db/tests/ai_credits_reaper.rs index deb6ed3..4afdcd4 100644 --- a/crates/db/tests/ai_credits_reaper.rs +++ b/crates/db/tests/ai_credits_reaper.rs @@ -36,6 +36,20 @@ async fn expired_hold_is_swept_and_credits_return_to_available() { AiCreditsRepository::ensure_wallet(&pool, user_id).await.unwrap(); + // This test's sweep query below is intentionally global (it mirrors the + // real cron reaper, which sweeps every wallet, not just one) - in a real + // deployment the reaper runs continuously so nothing stays 'held' past + // its expiry for long. Against a persistent test database re-run many + // times over a session (not wiped between CI runs), other tests' + // reservations that were never captured/released can accumulate and go + // stale, which would make the exact-match assertion below flaky. Sweep + // those first so this test starts from the same clean slate a real + // continuously-running reaper would maintain. + sqlx::query("DELETE FROM ai_reservation_holds WHERE status = 'held' AND expires_at < NOW()") + .execute(&pool) + .await + .unwrap(); + let hold = AiCreditsRepository::try_reserve_credits(&pool, user_id, "help_answer", 4, None, None) .await .expect("reserve");