|
| 1 | +package com.getcode.opencode.controllers |
| 2 | + |
| 3 | +import com.getcode.ed25519.Ed25519 |
| 4 | +import com.getcode.opencode.model.accounts.AccountCluster |
| 5 | +import com.getcode.opencode.model.accounts.AccountFilter |
| 6 | +import com.getcode.opencode.model.accounts.AccountInfo |
| 7 | +import com.getcode.opencode.model.accounts.AccountResponse |
| 8 | +import com.getcode.opencode.model.accounts.AccountType |
| 9 | +import com.getcode.opencode.model.core.ID |
| 10 | +import com.getcode.opencode.model.core.errors.GetAccountsError |
| 11 | +import com.getcode.opencode.model.transactions.ExchangeData |
| 12 | +import com.getcode.opencode.repositories.AccountRepository |
| 13 | +import com.getcode.solana.keys.Mint |
| 14 | +import com.getcode.solana.keys.Key32 |
| 15 | +import com.getcode.solana.keys.PublicKey |
| 16 | +import com.getcode.utils.network.NetworkObserverStub |
| 17 | +import io.mockk.mockk |
| 18 | +import kotlinx.coroutines.CoroutineScope |
| 19 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 20 | +import kotlinx.coroutines.test.runTest |
| 21 | +import org.junit.Test |
| 22 | +import kotlin.test.assertEquals |
| 23 | +import kotlin.test.assertFalse |
| 24 | +import kotlin.test.assertIs |
| 25 | +import kotlin.test.assertTrue |
| 26 | + |
| 27 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 28 | +class AccountControllerTest { |
| 29 | + |
| 30 | + private val repository = FakeAccountRepository() |
| 31 | + private val networkObserver = NetworkObserverStub(isConnected = false) |
| 32 | + private val controller = AccountController(repository, networkObserver) |
| 33 | + |
| 34 | + // region hasAccountFor |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `hasAccountFor returns false when no accounts exist`() { |
| 38 | + assertFalse(controller.hasAccountFor(Mint.usdf)) |
| 39 | + } |
| 40 | + |
| 41 | + // endregion |
| 42 | + |
| 43 | + // region createUserAccount |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `createUserAccount delegates to repository`() = runTest { |
| 47 | + val id: ID = listOf(1, 2, 3) |
| 48 | + repository.createUserAccountResult = Result.success(id) |
| 49 | + |
| 50 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 51 | + val result = controller.createUserAccount(cluster, Mint.usdf) |
| 52 | + |
| 53 | + assertTrue(result.isSuccess) |
| 54 | + assertEquals(id, result.getOrThrow()) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `createUserAccount propagates failure`() = runTest { |
| 59 | + val error = RuntimeException("creation failed") |
| 60 | + repository.createUserAccountResult = Result.failure(error) |
| 61 | + |
| 62 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 63 | + val result = controller.createUserAccount(cluster, Mint.usdf) |
| 64 | + |
| 65 | + assertTrue(result.isFailure) |
| 66 | + assertEquals(error, result.exceptionOrNull()) |
| 67 | + } |
| 68 | + |
| 69 | + // endregion |
| 70 | + |
| 71 | + // region getAccounts |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `getAccounts returns accounts from repository`() = runTest { |
| 75 | + val accountInfo = stubAccountInfo(Mint.usdf) |
| 76 | + val response = AccountResponse( |
| 77 | + accounts = mapOf(accountInfo.address to accountInfo) |
| 78 | + ) |
| 79 | + repository.getAccountsResult = Result.success(response) |
| 80 | + |
| 81 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 82 | + val result = controller.getAccounts(cluster, cluster) |
| 83 | + |
| 84 | + assertTrue(result.isSuccess) |
| 85 | + assertEquals(1, result.getOrThrow().accounts.size) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `getAccounts propagates repository failure`() = runTest { |
| 90 | + val error = GetAccountsError.NotFound() |
| 91 | + repository.getAccountsResult = Result.failure(error) |
| 92 | + |
| 93 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 94 | + val result = controller.getAccounts(cluster, cluster) |
| 95 | + |
| 96 | + assertTrue(result.isFailure) |
| 97 | + assertIs<GetAccountsError.NotFound>(result.exceptionOrNull()) |
| 98 | + } |
| 99 | + |
| 100 | + // endregion |
| 101 | + |
| 102 | + // region getAccount |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `getAccount returns single account from repository`() = runTest { |
| 106 | + val accountInfo = stubAccountInfo(Mint.usdf) |
| 107 | + repository.getAccountResult = Result.success(accountInfo) |
| 108 | + |
| 109 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 110 | + val filter = AccountFilter.AccountType(AccountType.Primary) |
| 111 | + val result = controller.getAccount(cluster, cluster, filter) |
| 112 | + |
| 113 | + assertTrue(result.isSuccess) |
| 114 | + assertEquals(accountInfo.mint, result.getOrThrow().mint) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `getAccount propagates repository failure`() = runTest { |
| 119 | + val error = GetAccountsError.NotFound() |
| 120 | + repository.getAccountResult = Result.failure(error) |
| 121 | + |
| 122 | + val cluster = mockk<AccountCluster>(relaxed = true) |
| 123 | + val filter = AccountFilter.AccountType(AccountType.Primary) |
| 124 | + val result = controller.getAccount(cluster, cluster, filter) |
| 125 | + |
| 126 | + assertTrue(result.isFailure) |
| 127 | + assertIs<GetAccountsError.NotFound>(result.exceptionOrNull()) |
| 128 | + } |
| 129 | + |
| 130 | + // endregion |
| 131 | + |
| 132 | + // region helpers |
| 133 | + |
| 134 | + private fun stubAccountInfo(mint: Mint): AccountInfo { |
| 135 | + return AccountInfo( |
| 136 | + address = Key32.mock, |
| 137 | + owner = null, |
| 138 | + authority = null, |
| 139 | + accountType = AccountType.Primary, |
| 140 | + index = 0, |
| 141 | + balanceSource = AccountInfo.BalanceSource.Cache, |
| 142 | + balance = 1000L, |
| 143 | + managementState = AccountInfo.ManagementState.Locked, |
| 144 | + blockchainState = AccountInfo.BlockchainState.Exists, |
| 145 | + claimState = AccountInfo.ClaimState.Unknown, |
| 146 | + originalExchangeData = mockk(relaxed = true), |
| 147 | + mint = mint, |
| 148 | + createdAt = null, |
| 149 | + isGiftCardIssuer = false, |
| 150 | + usdCostBasis = 0.0, |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + // endregion |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Fake [AccountRepository]. MockK cannot return [kotlin.Result] from mocked interfaces. |
| 159 | + */ |
| 160 | +private class FakeAccountRepository : AccountRepository { |
| 161 | + var isValidAccountResult: Result<Boolean> = Result.success(true) |
| 162 | + var createUserAccountResult: Result<ID> = Result.success(emptyList()) |
| 163 | + var getAccountsResult: Result<AccountResponse> = Result.success(AccountResponse(emptyMap())) |
| 164 | + var getAccountResult: Result<AccountInfo> = Result.failure(RuntimeException("not configured")) |
| 165 | + |
| 166 | + override suspend fun isValidAccount(owner: Ed25519.KeyPair): Result<Boolean> = |
| 167 | + isValidAccountResult |
| 168 | + |
| 169 | + override suspend fun createUserAccount( |
| 170 | + scope: CoroutineScope, |
| 171 | + ownerForMint: AccountCluster, |
| 172 | + mint: Mint, |
| 173 | + ): Result<ID> = createUserAccountResult |
| 174 | + |
| 175 | + override suspend fun getAccounts( |
| 176 | + accountOwner: Ed25519.KeyPair, |
| 177 | + requestingOwner: Ed25519.KeyPair, |
| 178 | + filter: AccountFilter?, |
| 179 | + ): Result<AccountResponse> = getAccountsResult |
| 180 | + |
| 181 | + override suspend fun getAccount( |
| 182 | + accountOwner: Ed25519.KeyPair, |
| 183 | + requestingOwner: Ed25519.KeyPair, |
| 184 | + filter: AccountFilter, |
| 185 | + ): Result<AccountInfo> = getAccountResult |
| 186 | +} |
| 187 | + |
0 commit comments