1616from aleph_client .commands .files import upload
1717
1818from .mocks import FAKE_STORE_HASH , FAKE_STORE_HASH_PUBLISHER
19+ from .test_instance import create_mock_client
1920
2021runner = CliRunner ()
2122
@@ -244,41 +245,49 @@ def test_account_sign_bytes(env_files):
244245 assert result .stdout .startswith ("\n Signature:" )
245246
246247
247- def test_account_balance (mocker , env_files , mock_voucher_service ):
248+ def test_account_balance (mocker , env_files , mock_voucher_service , mock_get_balances ):
249+ """
250+ This test verifies that the account balance command correctly displays balance and voucher information.
251+ """
252+
248253 settings .CONFIG_FILE = env_files [1 ]
249- balance_response = {
250- "address" : "0xCAfEcAfeCAfECaFeCaFecaFecaFECafECafeCaFe" ,
251- "balance" : 24853 ,
252- "details" : {"AVAX" : 4000 , "BASE" : 10000 , "ETH" : 10853 },
253- "locked_amount" : 4663.334518051392 ,
254- "available_amount" : 20189.665481948608 ,
255- }
254+ mock_client_class , mock_client = create_mock_client (None , None , mock_get_balances = mock_get_balances )
256255
257- mocker .patch ("aleph_client.commands.account.get_balance" , return_value = balance_response )
258- # TODO: used the mocked_client fixture instead (also need to move get_balance to the SDK)
259- mock_client = mocker .AsyncMock ()
260256 mock_client .voucher = mock_voucher_service
261- mocker .patch ("aleph_client.commands.account.AuthenticatedAlephHttpClient.__aenter__" , return_value = mock_client )
257+
258+ # Replace both client types with our mock implementation
259+ mocker .patch ("aleph_client.commands.account.AlephHttpClient" , mock_client_class )
260+ mocker .patch ("aleph_client.commands.account.AuthenticatedAlephHttpClient" , mock_client_class )
262261
263262 result = runner .invoke (
264263 app , ["account" , "balance" , "--address" , "0xCAfEcAfeCAfECaFeCaFecaFecaFECafECafeCaFe" , "--chain" , "ETH" ]
265264 )
265+
266266 assert result .exit_code == 0
267267 assert result .stdout .startswith ("╭─ Account Infos" )
268268 assert "Available: 20189.67" in result .stdout
269269 assert "Vouchers:" in result .stdout
270270 assert "EVM Test Voucher" in result .stdout
271271
272272
273- def test_account_balance_error (mocker , env_files ):
273+ def test_account_balance_error (mocker , env_files , mock_voucher_empty ):
274274 """Test error handling in the account balance command when API returns an error."""
275275 settings .CONFIG_FILE = env_files [1 ]
276276
277- # Mock get_balance to raise an exception - simulating a non-200 response
278- mock_get_balance = mocker .patch (
279- "aleph_client.commands.account.get_balance" ,
280- side_effect = Exception ("Failed to retrieve balance for address test. Status code: 404" ),
277+ mock_client_class = MagicMock ()
278+ mock_client = MagicMock ()
279+ mock_client .__aenter__ .return_value = mock_client
280+ mock_client .__aexit__ .return_value = None
281+ mock_client .get_balances = AsyncMock (
282+ side_effect = Exception (
283+ "Failed to retrieve balance for address 0xCAfEcAfeCAfECaFeCaFecaFecaFECafECafeCaFe. Status code: 404"
284+ )
281285 )
286+ mock_client .voucher = mock_voucher_empty
287+ mock_client_class .return_value = mock_client
288+
289+ mocker .patch ("aleph_client.commands.account.AlephHttpClient" , mock_client_class )
290+ mocker .patch ("aleph_client.commands.account.AuthenticatedAlephHttpClient" , mock_client_class )
282291
283292 # Test with an address directly
284293 result = runner .invoke (
@@ -288,7 +297,6 @@ def test_account_balance_error(mocker, env_files):
288297 # The command should run without crashing but report the error
289298 assert result .exit_code == 0
290299 assert "Failed to retrieve balance for address" in result .stdout
291- mock_get_balance .assert_called_once ()
292300
293301
294302def test_account_vouchers_display (mocker , env_files , mock_voucher_service ):
0 commit comments