-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWalletViewModelTest.kt
More file actions
338 lines (276 loc) · 11.7 KB
/
WalletViewModelTest.kt
File metadata and controls
338 lines (276 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package to.bitkit.ui
import android.content.Context
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.lightningdevkit.ldknode.PeerDetails
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import to.bitkit.data.SettingsStore
import to.bitkit.ext.of
import to.bitkit.models.BalanceState
import to.bitkit.repositories.BackupRepo
import to.bitkit.repositories.BlocktankRepo
import to.bitkit.repositories.ConnectivityRepo
import to.bitkit.repositories.ConnectivityState
import to.bitkit.repositories.LightningRepo
import to.bitkit.repositories.LightningState
import to.bitkit.repositories.SyncSource
import to.bitkit.repositories.WalletRepo
import to.bitkit.repositories.WalletState
import to.bitkit.services.MigrationService
import to.bitkit.test.BaseUnitTest
import to.bitkit.viewmodels.RestoreState
import to.bitkit.viewmodels.WalletViewModel
@OptIn(ExperimentalCoroutinesApi::class)
class WalletViewModelTest : BaseUnitTest() {
private lateinit var sut: WalletViewModel
private val context = mock<Context>()
private val walletRepo = mock<WalletRepo>()
private val lightningRepo = mock<LightningRepo>()
private val settingsStore = mock<SettingsStore>()
private val backupRepo = mock<BackupRepo>()
private val blocktankRepo = mock<BlocktankRepo>()
private val migrationService = mock<MigrationService>()
private val connectivityRepo = mock<ConnectivityRepo>()
private val lightningState = MutableStateFlow(LightningState())
private val walletState = MutableStateFlow(WalletState())
private val balanceState = MutableStateFlow(BalanceState())
private val isRecoveryMode = MutableStateFlow(false)
private val isOnline = MutableStateFlow(ConnectivityState.CONNECTED)
@Before
fun setUp() = runBlocking {
whenever(context.getString(any())).thenReturn("")
whenever(walletRepo.walletState).thenReturn(walletState)
whenever(lightningRepo.lightningState).thenReturn(lightningState)
whenever(migrationService.isMigrationChecked()).thenReturn(true)
whenever(connectivityRepo.isOnline).thenReturn(isOnline)
sut = WalletViewModel(
context = context,
bgDispatcher = testDispatcher,
walletRepo = walletRepo,
lightningRepo = lightningRepo,
settingsStore = settingsStore,
backupRepo = backupRepo,
blocktankRepo = blocktankRepo,
migrationService = migrationService,
connectivityRepo = connectivityRepo,
)
}
@Test
fun `setInitNodeLifecycleState should call lightningRepo`() = test {
sut.setInitNodeLifecycleState()
verify(lightningRepo).setInitNodeLifecycleState()
}
@Test
fun `refreshState should sync wallet`() = test {
sut.refreshState()
verify(walletRepo).syncNodeAndWallet()
}
@Test
fun `refreshReceiveState should refresh receive state`() = test {
sut.refreshReceiveState()
verify(blocktankRepo).refreshInfo()
verify(lightningRepo).updateGeoBlockState()
verify(walletRepo).refreshBip21()
}
@Test
fun `onPullToRefresh should sync wallet`() = test {
sut.onPullToRefresh()
verify(walletRepo).syncNodeAndWallet(SyncSource.MANUAL)
}
@Test
fun `disconnectPeer should call lightningRepo disconnectPeer`() = test {
val testPeer = PeerDetails.of("nodeId", "host", "9735")
val testError = Exception("Test error")
whenever(lightningRepo.disconnectPeer(testPeer)).thenReturn(Result.failure(testError))
sut.disconnectPeer(testPeer)
verify(lightningRepo).disconnectPeer(testPeer)
}
@Test
fun `wipeWallet should call walletRepo wipeWallet`() = test {
whenever(walletRepo.wipeWallet(walletIndex = 0)).thenReturn(Result.success(Unit))
sut.wipeWallet()
verify(walletRepo).wipeWallet(walletIndex = 0)
}
@Test
fun `createWallet should call walletRepo createWallet`() = test {
whenever(walletRepo.createWallet(anyOrNull())).thenReturn(Result.success(Unit))
sut.createWallet(null)
verify(walletRepo).createWallet(anyOrNull())
}
@Test
fun `createWallet should call setInitNodeLifecycleState`() = test {
whenever(walletRepo.createWallet(anyOrNull())).thenReturn(Result.success(Unit))
sut.createWallet(null)
verify(lightningRepo).setInitNodeLifecycleState()
}
@Test
fun `restoreWallet should call walletRepo restoreWallet`() = test {
whenever(walletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.success(Unit))
sut.restoreWallet("test_mnemonic", null)
verify(walletRepo).restoreWallet(any(), anyOrNull())
}
@Test
fun `restoreWallet should call setInitNodeLifecycleState`() = test {
whenever(walletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.success(Unit))
sut.restoreWallet("test_mnemonic", null)
verify(lightningRepo).setInitNodeLifecycleState()
}
@Test
fun `addTagToSelected should call walletRepo addTagToSelected`() = test {
sut.addTagToSelected("test_tag")
verify(walletRepo).addTagToSelected("test_tag")
}
@Test
fun `removeTag should call walletRepo removeTag`() = test {
sut.removeTag("test_tag")
verify(walletRepo).removeTag("test_tag")
}
@Test
fun `updateBip21Description should call walletRepo updateBip21Description`() = test {
sut.updateBip21Description("test_description")
verify(walletRepo).setBip21Description("test_description")
}
@Test
fun `backup restore should not be triggered when wallet exists while not restoring`() = test {
assertEquals(RestoreState.Initial, sut.restoreState.value)
walletState.value = walletState.value.copy(walletExists = true)
verify(backupRepo, never()).performFullRestoreFromLatestBackup()
}
@Test
fun `onBackupRestoreSuccess should reset restoreState`() = test {
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.success(Unit))
walletState.value = walletState.value.copy(walletExists = true)
sut.restoreWallet("mnemonic", "passphrase")
assertEquals(RestoreState.InProgress.Wallet, sut.restoreState.value)
sut.onRestoreContinue()
assertEquals(RestoreState.Settled, sut.restoreState.value)
}
@Test
fun `onProceedWithoutRestore should exit restore flow`() = test {
val testError = Exception("Test error")
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.failure(testError))
sut.restoreWallet("mnemonic", "passphrase")
walletState.value = walletState.value.copy(walletExists = true)
assertEquals(RestoreState.Completed, sut.restoreState.value)
sut.onProceedWithoutRestore(onDone = {})
advanceUntilIdle()
assertEquals(RestoreState.Settled, sut.restoreState.value)
}
@Test
fun `restore state should transition as expected`() = test {
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.success(Unit))
assertEquals(RestoreState.Initial, sut.restoreState.value)
sut.restoreWallet("mnemonic", "passphrase")
assertEquals(RestoreState.InProgress.Wallet, sut.restoreState.value)
walletState.value = walletState.value.copy(walletExists = true)
assertEquals(RestoreState.Completed, sut.restoreState.value)
sut.onRestoreContinue()
assertEquals(RestoreState.Settled, sut.restoreState.value)
}
@Test
fun `start should call refreshBip21 when restore state is idle`() = test {
// Create fresh mocks for this test
val testWalletRepo: WalletRepo = mock()
val testLightningRepo: LightningRepo = mock()
// Create a wallet state with walletExists = true
val testWalletState = MutableStateFlow(WalletState(walletExists = true))
// Set up mocks BEFORE creating SUT
whenever(testWalletRepo.walletState).thenReturn(testWalletState)
whenever(testWalletRepo.balanceState).thenReturn(balanceState)
whenever(testWalletRepo.walletExists()).thenReturn(true)
whenever(testLightningRepo.lightningState).thenReturn(lightningState)
whenever(testLightningRepo.isRecoveryMode).thenReturn(isRecoveryMode)
whenever(
testLightningRepo.start(
any(),
anyOrNull(),
any(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
any(),
),
).thenReturn(Result.success(Unit))
val testSut = WalletViewModel(
context = context,
bgDispatcher = testDispatcher,
walletRepo = testWalletRepo,
lightningRepo = testLightningRepo,
settingsStore = settingsStore,
backupRepo = backupRepo,
blocktankRepo = blocktankRepo,
migrationService = migrationService,
connectivityRepo = connectivityRepo,
)
assertEquals(RestoreState.Initial, testSut.restoreState.value)
assertEquals(true, testSut.walletExists)
testSut.start()
advanceUntilIdle()
verify(testLightningRepo).start(
any(),
anyOrNull(),
any(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
any(),
)
verify(testWalletRepo).refreshBip21()
}
@Test
fun `start should skip refreshBip21 when restore is in progress`() = test {
// Create fresh mocks for this test
val testWalletRepo: WalletRepo = mock()
val testLightningRepo: LightningRepo = mock()
// Create wallet state with walletExists = true so start() doesn't return early
val testWalletState = MutableStateFlow(WalletState(walletExists = true))
// Set up mocks BEFORE creating SUT
whenever(testWalletRepo.walletState).thenReturn(testWalletState)
whenever(testWalletRepo.balanceState).thenReturn(balanceState)
whenever(testWalletRepo.walletExists()).thenReturn(true)
whenever(testWalletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.success(Unit))
whenever(testLightningRepo.lightningState).thenReturn(lightningState)
whenever(testLightningRepo.isRecoveryMode).thenReturn(isRecoveryMode)
whenever(
testLightningRepo.start(
any(),
anyOrNull(),
any(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
any(),
),
).thenReturn(Result.success(Unit))
val testSut = WalletViewModel(
context = context,
bgDispatcher = testDispatcher,
walletRepo = testWalletRepo,
lightningRepo = testLightningRepo,
settingsStore = settingsStore,
backupRepo = backupRepo,
blocktankRepo = blocktankRepo,
migrationService = migrationService,
connectivityRepo = connectivityRepo,
)
// Trigger restore to put state in non-idle
testSut.restoreWallet("mnemonic", null)
assertEquals(RestoreState.InProgress.Wallet, testSut.restoreState.value)
testSut.start()
advanceUntilIdle()
verify(testWalletRepo, never()).refreshBip21()
}
}