From 85690b3002a7529ef15eb7a554931b224e102eab Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Sivakumar Date: Thu, 13 Aug 2026 22:01:24 +0530 Subject: [PATCH] fix(test): make ai_credits_reaper.rs resilient to accumulated stale holds The sweep query is intentionally global (matches the real cron reaper), but against a persistent, never-reset test database re-run many times over a debugging session, other tests' un-captured/ un-released reservations eventually go stale and get picked up alongside this test's own hold, breaking the exact-match assertion (observed: 6 expired holds instead of 1). Clear pre-existing stale 'held' rows first, matching the clean slate a continuously-running production reaper would actually maintain. Co-Authored-By: Claude Sonnet 5 --- crates/db/tests/ai_credits_reaper.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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");