8
8
9
9
import pytest
10
10
from aiohttp import InvalidURL
11
+ from aleph .sdk .exceptions import InsufficientFundsError
12
+ from aleph .sdk .types import TokenType
11
13
from aleph_message .models import Chain , ItemHash
12
14
from aleph_message .models .execution .base import Payment , PaymentType
13
15
from aleph_message .models .execution .environment import (
16
18
HypervisorType ,
17
19
)
18
20
from multidict import CIMultiDict , CIMultiDictProxy
21
+ from typer import Exit
19
22
20
23
from aleph_client .commands import help_strings
21
24
from aleph_client .commands .instance import (
@@ -556,11 +559,18 @@ async def test_delete_instance():
556
559
mock_auth_client_class , mock_auth_client = create_mock_auth_client (mock_account )
557
560
mock_fetch_vm_info = create_mock_fetch_vm_info ()
558
561
mock_vm_client_class , mock_vm_client = create_mock_vm_client ()
562
+ mock_fetch_settings = AsyncMock (
563
+ return_value = {
564
+ "community_wallet_timestamp" : 900000 , # Before creation time
565
+ "community_wallet_address" : "0xcommunity" ,
566
+ }
567
+ )
559
568
560
569
@patch ("aleph_client.commands.instance._load_account" , mock_load_account )
561
570
@patch ("aleph_client.commands.instance.AuthenticatedAlephHttpClient" , mock_auth_client_class )
562
571
@patch ("aleph_client.commands.instance.fetch_vm_info" , mock_fetch_vm_info )
563
572
@patch ("aleph_client.commands.instance.VmClient" , mock_vm_client_class )
573
+ @patch ("aleph_client.commands.instance.fetch_settings" , mock_fetch_settings )
564
574
@patch .object (asyncio , "sleep" , AsyncMock ())
565
575
async def delete_instance ():
566
576
print () # For better display when pytest -v -s
@@ -573,6 +583,46 @@ async def delete_instance():
573
583
await delete_instance ()
574
584
575
585
586
+ @pytest .mark .asyncio
587
+ async def test_delete_instance_with_insufficient_funds ():
588
+
589
+ mock_load_account = create_mock_load_account ()
590
+ mock_account = mock_load_account .return_value
591
+
592
+ # Configure manage_flow to raise InsufficientFundsError
593
+ insufficient_funds_error = InsufficientFundsError (
594
+ token_type = TokenType .GAS , required_funds = 10.0 , available_funds = 5.0
595
+ )
596
+ mock_account .manage_flow = AsyncMock (side_effect = insufficient_funds_error )
597
+
598
+ mock_auth_client_class , mock_auth_client = create_mock_auth_client (mock_account )
599
+ mock_fetch_vm_info = create_mock_fetch_vm_info ()
600
+ mock_vm_client_class , mock_vm_client = create_mock_vm_client ()
601
+ mock_fetch_settings = AsyncMock (
602
+ return_value = {
603
+ "community_wallet_timestamp" : 900000 , # Before creation time
604
+ "community_wallet_address" : "0xcommunity" ,
605
+ }
606
+ )
607
+
608
+ @patch ("aleph_client.commands.instance._load_account" , mock_load_account )
609
+ @patch ("aleph_client.commands.instance.AuthenticatedAlephHttpClient" , mock_auth_client_class )
610
+ @patch ("aleph_client.commands.instance.fetch_vm_info" , mock_fetch_vm_info )
611
+ @patch ("aleph_client.commands.instance.VmClient" , mock_vm_client_class )
612
+ @patch ("aleph_client.commands.instance.fetch_settings" , mock_fetch_settings )
613
+ @patch .object (asyncio , "sleep" , AsyncMock ())
614
+ async def delete_instance_with_insufficient_funds ():
615
+ print () # For better display when pytest -v -s
616
+ with pytest .raises (Exit ): # We expect Exit to be raised
617
+ await delete (FAKE_VM_HASH )
618
+ mock_auth_client .get_message .assert_called_once ()
619
+ mock_vm_client .erase_instance .assert_called_once ()
620
+ mock_account .manage_flow .assert_called_once () # First manage_flow call raises the error
621
+ mock_auth_client .forget .assert_not_called () # This should not be called as we exit early
622
+
623
+ await delete_instance_with_insufficient_funds ()
624
+
625
+
576
626
@pytest .mark .asyncio
577
627
async def test_reboot_instance ():
578
628
mock_load_account = create_mock_load_account ()
0 commit comments