From 09e12f2da58350adb9f00da1923116600e5b4e46 Mon Sep 17 00:00:00 2001 From: Mario Diniz Date: Thu, 11 Jun 2026 16:25:15 -0300 Subject: [PATCH] test(wallets): wait for async ongoing balance refresh ## Context Six wallet integration tests (`#update`, `#get`, `#get_all` on both `spec/integration/wallets_spec.rb` and `spec/integration/customers/ wallets_spec.rb`) intermittently fail in CI with `expected: "10.0", got: "0.0"` on `credits_ongoing_balance`. `balance_cents` is synced synchronously when the wallet transaction settles, but `ongoing_balance_cents` / `credits_ongoing_balance` are refreshed by an async job (RefreshWalletJob). The existing `wait_for_balance_update` only polls until `balance_cents == 2000`, so the assertion can run before the ongoing refresh has landed and the wallet still reports `0.0`. ## Description Re-fetches the wallet inside `assert_wallet_attributes_with_updated_ balance` and waits until `ongoing_balance_cents` matches the expected value before asserting. The expected value is read from the optional `ongoing_balance_cents` override (default 2000), so tests that override it continue to drive the wait correctly. --- spec/integration/customers/wallets_spec.rb | 9 +++++++++ spec/integration/wallets_spec.rb | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/spec/integration/customers/wallets_spec.rb b/spec/integration/customers/wallets_spec.rb index 13adf033..d46f6974 100644 --- a/spec/integration/customers/wallets_spec.rb +++ b/spec/integration/customers/wallets_spec.rb @@ -51,6 +51,15 @@ def assert_wallet_attributes(wallet, **attributes) end def assert_wallet_attributes_with_updated_balance(wallet, **attributes) + # balance_cents is synced synchronously when the wallet transaction settles, but + # ongoing_balance_cents is refreshed by an async job (RefreshWalletJob), so re-fetch + # until the ongoing refresh has landed before asserting. + expected_ongoing_balance_cents = attributes.fetch(:ongoing_balance_cents, 2000) + wait_until do + wallet = client.customers.wallets.get(wallet.external_customer_id, wallet.code) + wallet.ongoing_balance_cents == expected_ongoing_balance_cents + end + assert_wallet_attributes( wallet, credits_balance: '10.0', diff --git a/spec/integration/wallets_spec.rb b/spec/integration/wallets_spec.rb index 841aca63..0b83aaca 100644 --- a/spec/integration/wallets_spec.rb +++ b/spec/integration/wallets_spec.rb @@ -49,6 +49,15 @@ def assert_wallet_attributes(wallet, **attributes) end def assert_wallet_attributes_with_updated_balance(wallet, **attributes) + # balance_cents is synced synchronously when the wallet transaction settles, but + # ongoing_balance_cents is refreshed by an async job (RefreshWalletJob), so re-fetch + # until the ongoing refresh has landed before asserting. + expected_ongoing_balance_cents = attributes.fetch(:ongoing_balance_cents, 2000) + wait_until do + wallet = client.wallets.get(wallet.lago_id) + wallet.ongoing_balance_cents == expected_ongoing_balance_cents + end + assert_wallet_attributes( wallet, credits_balance: '10.0',