1+ package com.reown.walletkit
2+
3+ import com.reown.walletkit.client.Wallet
4+ import com.reown.walletkit.client.toWallet
5+ import com.reown.walletkit.use_cases.ExecuteChainAbstractionUseCase
6+ import io.mockk.coEvery
7+ import io.mockk.mockk
8+ import junit.framework.TestCase.assertEquals
9+ import kotlinx.coroutines.async
10+ import kotlinx.coroutines.test.runTest
11+ import org.junit.Before
12+ import org.junit.Test
13+ import uniffi.uniffi_yttrium.ChainAbstractionClient
14+ import uniffi.yttrium.Amount
15+ import uniffi.yttrium.ExecuteDetails
16+ import uniffi.yttrium.FeeEstimatedTransaction
17+ import uniffi.yttrium.FundingMetadata
18+ import uniffi.yttrium.InitialTransactionMetadata
19+ import uniffi.yttrium.Metadata
20+ import uniffi.yttrium.PrepareDetailedResponseSuccess
21+ import uniffi.yttrium.PrepareResponseAvailable
22+ import uniffi.yttrium.TransactionFee
23+ import uniffi.yttrium.TxnDetails
24+ import uniffi.yttrium.UiFields
25+ import kotlin.coroutines.resume
26+ import kotlin.coroutines.suspendCoroutine
27+
28+ class ExecuteChainAbstractionUseCaseTest {
29+ private lateinit var chainAbstractionClient: ChainAbstractionClient
30+ private lateinit var useCase: ExecuteChainAbstractionUseCase
31+ private val feeEstimatedTransactionMetadata = FeeEstimatedTransaction (
32+ from = " from" ,
33+ to = " to" ,
34+ value = " value" ,
35+ input = " data" ,
36+ nonce = " nonce" ,
37+ gasLimit = " gas" ,
38+ chainId = " 1" ,
39+ maxPriorityFeePerGas = " 11" ,
40+ maxFeePerGas = " 33"
41+ )
42+ private val txDetails = TxnDetails (
43+ transaction = feeEstimatedTransactionMetadata,
44+ fee = TransactionFee (Amount (" 11" , " 18" , 2u , " 22" , " 1222" ), Amount (" 11" , " 18" , 2u , " 22" , " 1222" )),
45+ transactionHashToSign = " hash" ,
46+ )
47+ private var prepareAvailable: Wallet .Model .PrepareSuccess .Available = PrepareDetailedResponseSuccess .Available (
48+ UiFields (
49+ route = listOf (txDetails),
50+ initial = txDetails,
51+ bridge = listOf (TransactionFee (Amount (" 11" , " 18" , 2u , " 22" , " 1222" ), Amount (" 11" , " 18" , 2u , " 22" , " 1222" ))),
52+ localTotal = Amount (" 11" , " 18" , 2u , " 22" , " 1222" ),
53+ localBridgeTotal = Amount (" 11" , " 18" , 2u , " 22" , " 1222" ),
54+ localRouteTotal = Amount (" 11" , " 18" , 2u , " 22" , " 1222" ),
55+ routeResponse = PrepareResponseAvailable (
56+ orchestrationId = " 123" ,
57+ initialTransaction = PrepareChainAbstractionUseCaseTest .transaction,
58+ metadata = Metadata (
59+ fundingFrom = listOf (FundingMetadata (chainId = " 1" , tokenContract = " token" , symbol = " s" , amount = " 11" , decimals = 18u , bridgingFee = " 0" )),
60+ initialTransaction = InitialTransactionMetadata (transferTo = " aa" , amount = " 11" , tokenContract = " cc" , symbol = " s" , decimals = 18u ),
61+ checkIn = 11u
62+ ),
63+ transactions = listOf (PrepareChainAbstractionUseCaseTest .transaction)
64+ )
65+ )
66+
67+ ).v1.toWallet()
68+
69+ @Before
70+ fun setup () {
71+ chainAbstractionClient = mockk()
72+ useCase = ExecuteChainAbstractionUseCase (chainAbstractionClient)
73+ }
74+
75+ @Test
76+ fun invokeSuccessfulExecution () = runTest {
77+ // Given
78+ val signedRouteTxs = listOf (" signedTx1" , " signedTx2" )
79+ val initSignedTx = " initTx"
80+ val executeDetails = ExecuteDetails (initialTxnReceipt = " receipt" , initialTxnHash = " initTx" )
81+
82+ coEvery { chainAbstractionClient.execute(any(), signedRouteTxs, initSignedTx) } returns executeDetails
83+
84+ // When
85+ val result = async {
86+ suspendCoroutine { continuation ->
87+ useCase(prepareAvailable, signedRouteTxs, initSignedTx,
88+ onSuccess = {
89+ continuation.resume(it.initialTxHash)
90+ }, onError = {
91+ continuation.resume(" " )
92+ })
93+ }
94+ }.await()
95+
96+ // Then
97+ assertEquals(" initTx" , result)
98+ }
99+
100+ @Test
101+ fun invokeClientExecutionThrowsException () = runTest {
102+ // Given
103+ val signedRouteTxs = listOf (" signedTx1" , " signedTx2" )
104+ val initSignedTx = " initTx"
105+ val exception = Exception (" Test error" )
106+
107+ coEvery { chainAbstractionClient.execute(any(), signedRouteTxs, initSignedTx) } throws exception
108+
109+
110+ // When
111+ val result = async {
112+ suspendCoroutine { continuation ->
113+ useCase(prepareAvailable, signedRouteTxs, initSignedTx,
114+ onSuccess = {
115+ continuation.resume(" " )
116+ }, onError = {
117+ continuation.resume(it.throwable.message)
118+ })
119+ }
120+ }.await()
121+
122+ // Then
123+ assertEquals(" Test error" , result)
124+ }
125+ }
0 commit comments