|
| 1 | +package to.bitkit.data.backup |
| 2 | + |
| 3 | +import kotlinx.coroutines.runBlocking |
| 4 | +import org.junit.Before |
| 5 | +import org.junit.Test |
| 6 | +import org.mockito.kotlin.any |
| 7 | +import org.mockito.kotlin.mock |
| 8 | +import org.mockito.kotlin.never |
| 9 | +import org.mockito.kotlin.verify |
| 10 | +import org.mockito.kotlin.whenever |
| 11 | +import to.bitkit.data.keychain.Keychain |
| 12 | +import to.bitkit.test.BaseUnitTest |
| 13 | +import kotlin.test.assertFalse |
| 14 | +import kotlin.test.assertTrue |
| 15 | + |
| 16 | +class VssBackupClientTest : BaseUnitTest() { |
| 17 | + |
| 18 | + private lateinit var sut: VssBackupClient |
| 19 | + |
| 20 | + private val vssStoreIdProvider = mock<VssStoreIdProvider>() |
| 21 | + private val keychain = mock<Keychain>() |
| 22 | + |
| 23 | + @Before |
| 24 | + fun setUp() = runBlocking { |
| 25 | + sut = VssBackupClient( |
| 26 | + ioDispatcher = testDispatcher, |
| 27 | + vssStoreIdProvider = vssStoreIdProvider, |
| 28 | + keychain = keychain, |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `setup returns false when mnemonic is not available`() = test { |
| 34 | + whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(null) |
| 35 | + |
| 36 | + val result = sut.setup() |
| 37 | + |
| 38 | + assertFalse(result) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `setup does not call vssStoreIdProvider when mnemonic is not available`() = test { |
| 43 | + whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(null) |
| 44 | + |
| 45 | + sut.setup() |
| 46 | + |
| 47 | + verify(vssStoreIdProvider, never()).getVssStoreId(any()) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `setup checks mnemonic before proceeding with vss initialization`() = test { |
| 52 | + val testMnemonic = "abandon abandon abandon abandon abandon abandon " + |
| 53 | + "abandon abandon abandon abandon abandon about" |
| 54 | + whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(testMnemonic) |
| 55 | + whenever(vssStoreIdProvider.getVssStoreId(any())).thenReturn("test-store-id") |
| 56 | + |
| 57 | + // Setup will fail on native VSS calls, but we verify we passed the mnemonic check |
| 58 | + runCatching { sut.setup() } |
| 59 | + |
| 60 | + verify(vssStoreIdProvider).getVssStoreId(any()) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `setup can be called multiple times when mnemonic not available`() = test { |
| 65 | + whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(null) |
| 66 | + |
| 67 | + // Multiple calls should all return false without crashing |
| 68 | + assertFalse(sut.setup()) |
| 69 | + assertFalse(sut.setup()) |
| 70 | + assertFalse(sut.setup()) |
| 71 | + } |
| 72 | +} |
0 commit comments