From bc8df2e592b60c3ae8b3c8ea05ca3b368a4a022e Mon Sep 17 00:00:00 2001 From: "Yohan R." <7434196+groyoh@users.noreply.github.com> Date: Thu, 11 Jun 2026 14:06:47 +0200 Subject: [PATCH 1/2] test(wallets): wait for async ongoing balance refresh ## Context lago-api now refreshes the wallet ongoing balance asynchronously after a balance increase (getlago/lago-api#5631). `Wallets::Balance::IncreaseService` sets `balance_cents`, `credits_balance` and `last_balance_sync_at` in-line, then enqueues `RefreshWalletJob` which sets `ongoing_balance_cents` and `credits_ongoing_balance`. Once `balance_cents` reaches 2000 the ongoing balance is only eventually 2000, so asserting it directly races the async job. ## Description `assert_wallet_attributes_with_updated_balance` now re-fetches the wallet until `ongoing_balance_cents` reaches 2000 before asserting. This is backward compatible with the synchronous behaviour (the poll returns on the first fetch when the ongoing balance is already synced). --- spec/integration/customers/wallets_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/integration/customers/wallets_spec.rb b/spec/integration/customers/wallets_spec.rb index 13adf033..9a30faa7 100644 --- a/spec/integration/customers/wallets_spec.rb +++ b/spec/integration/customers/wallets_spec.rb @@ -51,6 +51,14 @@ def assert_wallet_attributes(wallet, **attributes) end def assert_wallet_attributes_with_updated_balance(wallet, **attributes) + # The balance is synced synchronously when the wallet transaction settles, but the ongoing + # balance is now refreshed by an async job (RefreshWalletJob). Re-fetch until that refresh has + # landed, otherwise the ongoing balance can still be 0 when the balance already reached 2000. + wait_until do + wallet = client.customers.wallets.get(wallet.external_customer_id, wallet.code) + wallet.ongoing_balance_cents == 2000 + end + assert_wallet_attributes( wallet, credits_balance: '10.0', From b4af30a3191ec835023a80f789efc74362556868 Mon Sep 17 00:00:00 2001 From: "Yohan R." <7434196+groyoh@users.noreply.github.com> Date: Thu, 11 Jun 2026 14:15:35 +0200 Subject: [PATCH 2/2] test(wallets): derive ongoing wait value and cover legacy spec ## Context PR review flagged two issues with the async ongoing balance wait: the polling condition hard-coded 2000 while the helper accepts an `ongoing_balance_cents` override, and the legacy `client.wallets` spec (`spec/integration/wallets_spec.rb`) had the same race without the fix. ## Description The wait now derives the expected ongoing balance from the passed attributes (defaulting to 2000) so it stays consistent with the assertions. The same re-fetch wait was applied to the legacy `assert_wallet_attributes_with_updated_balance` helper. --- spec/integration/customers/wallets_spec.rb | 3 ++- spec/integration/wallets_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/integration/customers/wallets_spec.rb b/spec/integration/customers/wallets_spec.rb index 9a30faa7..940e32c8 100644 --- a/spec/integration/customers/wallets_spec.rb +++ b/spec/integration/customers/wallets_spec.rb @@ -54,9 +54,10 @@ def assert_wallet_attributes_with_updated_balance(wallet, **attributes) # The balance is synced synchronously when the wallet transaction settles, but the ongoing # balance is now refreshed by an async job (RefreshWalletJob). Re-fetch until that refresh has # landed, otherwise the ongoing balance can still be 0 when the balance already reached 2000. + 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 == 2000 + wallet.ongoing_balance_cents == expected_ongoing_balance_cents end assert_wallet_attributes( diff --git a/spec/integration/wallets_spec.rb b/spec/integration/wallets_spec.rb index 841aca63..b5f3da32 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) + # The balance is synced synchronously when the wallet transaction settles, but the ongoing + # balance is now refreshed by an async job (RefreshWalletJob). Re-fetch until that refresh has + # landed, otherwise the ongoing balance can still be 0 when the balance already reached 2000. + 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',