-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathActivityRepoTest.kt
More file actions
889 lines (756 loc) · 32.7 KB
/
Copy pathActivityRepoTest.kt
File metadata and controls
889 lines (756 loc) · 32.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
package to.bitkit.repositories
import com.synonym.bitkitcore.Activity
import com.synonym.bitkitcore.ActivityFilter
import com.synonym.bitkitcore.LightningActivity
import com.synonym.bitkitcore.OnchainActivity
import com.synonym.bitkitcore.PaymentType
import com.synonym.bitkitcore.SortDirection
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import org.junit.Before
import org.junit.Test
import org.lightningdevkit.ldknode.PaymentDetails
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.argThat
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.mockito.kotlin.wheneverBlocking
import to.bitkit.data.AppCacheData
import to.bitkit.data.CacheStore
import to.bitkit.data.dto.PendingBoostActivity
import to.bitkit.ext.create
import to.bitkit.services.CoreService
import to.bitkit.test.BaseUnitTest
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.Clock
import kotlin.time.ExperimentalTime
@Suppress("LargeClass")
@OptIn(ExperimentalTime::class)
class ActivityRepoTest : BaseUnitTest() {
private val coreService = mock<CoreService>()
private val lightningRepo = mock<LightningRepo>()
private val blocktankRepo = mock<BlocktankRepo>()
private val transferRepo = mock<TransferRepo>()
private val cacheStore = mock<CacheStore>()
private val clock = mock<Clock>()
private lateinit var sut: ActivityRepo
private val testPaymentDetails = mock<PaymentDetails> {
on { id } doReturn "payment1"
}
private val testActivityV1 = mock<LightningActivity> {
on { id } doReturn "activity1"
}
private val testActivity = mock<Activity.Lightning> {
on { v1 } doReturn testActivityV1
}
private val baseOnchainActivity = OnchainActivity.create(
id = "base_activity_id",
txType = PaymentType.SENT,
txId = "base_tx_id",
value = 1000uL,
fee = 100uL,
address = "bc1test",
timestamp = 1234567890uL,
feeRate = 10uL,
)
@Suppress("LongParameterList")
private fun createOnchainActivity(
id: String = baseOnchainActivity.id,
txId: String = baseOnchainActivity.txId,
value: ULong = baseOnchainActivity.value,
fee: ULong = baseOnchainActivity.fee,
feeRate: ULong = baseOnchainActivity.feeRate,
address: String = baseOnchainActivity.address,
confirmed: Boolean = baseOnchainActivity.confirmed,
timestamp: ULong = baseOnchainActivity.timestamp,
isBoosted: Boolean = baseOnchainActivity.isBoosted,
boostTxIds: List<String> = baseOnchainActivity.boostTxIds,
isTransfer: Boolean = baseOnchainActivity.isTransfer,
doesExist: Boolean = baseOnchainActivity.doesExist,
confirmTimestamp: ULong? = baseOnchainActivity.confirmTimestamp,
channelId: String? = baseOnchainActivity.channelId,
transferTxId: String? = baseOnchainActivity.transferTxId,
contact: String? = baseOnchainActivity.contact,
createdAt: ULong? = baseOnchainActivity.createdAt,
updatedAt: ULong? = baseOnchainActivity.updatedAt,
): Activity.Onchain {
return Activity.Onchain(
v1 = baseOnchainActivity.copy(
id = id,
txId = txId,
value = value,
fee = fee,
feeRate = feeRate,
address = address,
confirmed = confirmed,
timestamp = timestamp,
isBoosted = isBoosted,
boostTxIds = boostTxIds,
isTransfer = isTransfer,
doesExist = doesExist,
confirmTimestamp = confirmTimestamp,
channelId = channelId,
transferTxId = transferTxId,
contact = contact,
createdAt = createdAt,
updatedAt = updatedAt
)
)
}
@Before
fun setUp() {
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData()))
whenever(coreService.activity).thenReturn(mock())
whenever(clock.now()).thenReturn(Clock.System.now())
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
whenever(blocktankRepo.blocktankState).thenReturn(MutableStateFlow(BlocktankState()))
sut = ActivityRepo(
bgDispatcher = testDispatcher,
ioDispatcher = testDispatcher,
coreService = coreService,
lightningRepo = lightningRepo,
blocktankRepo = blocktankRepo,
cacheStore = cacheStore,
transferRepo = transferRepo,
clock = clock,
)
}
private fun setupSyncActivitiesMocks(
cacheData: AppCacheData,
) {
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
wheneverBlocking { lightningRepo.getPayments() }.thenReturn(Result.success(emptyList()))
wheneverBlocking {
coreService.activity.syncLdkNodePaymentsToActivities(
any(),
eq(false),
any()
)
}.thenReturn(Unit)
wheneverBlocking { transferRepo.syncTransferStates() }.thenReturn(Result.success(Unit))
wheneverBlocking { coreService.activity.allPossibleTags() }.thenReturn(emptyList())
}
@Test
fun `syncActivities success flow`() = test {
val payments = listOf(testPaymentDetails)
wheneverBlocking { lightningRepo.getPayments() }.thenReturn(Result.success(payments))
wheneverBlocking { coreService.activity.getActivity(any<String>()) }.thenReturn(null)
wheneverBlocking {
coreService.activity.syncLdkNodePaymentsToActivities(
any<List<PaymentDetails>>(),
any<Boolean>(),
any<Map<String, String>>()
)
}.thenReturn(Unit)
wheneverBlocking { transferRepo.syncTransferStates() }.thenReturn(Result.success(Unit))
wheneverBlocking { coreService.activity.allPossibleTags() }.thenReturn(emptyList())
val result = sut.syncActivities()
assertTrue(result.isSuccess)
verify(lightningRepo).getPayments()
verify(coreService.activity).syncLdkNodePaymentsToActivities(any(), any(), any())
assertFalse(sut.isSyncingLdkNodePayments.value)
}
@Test
fun `syncActivities handles lightningRepo failure`() = test {
val exception = Exception("Lightning repo failed")
wheneverBlocking { lightningRepo.getPayments() }.thenReturn(Result.failure(exception))
val result = sut.syncActivities()
assertTrue(result.isFailure)
assertEquals(exception, result.exceptionOrNull())
assertFalse(sut.isSyncingLdkNodePayments.value)
}
@Test
fun `findActivityByPaymentId returns failure when activity not found after sync`() = test {
val paymentId = "payment123"
wheneverBlocking {
coreService.activity.get(
filter = any(),
txType = any(),
tags = any(),
search = any(),
minDate = any(),
maxDate = any(),
limit = any(),
sortDirection = any()
)
}.thenReturn(emptyList())
wheneverBlocking { lightningRepo.sync() }.thenReturn(Result.success(Unit))
wheneverBlocking { lightningRepo.getPayments() }.thenReturn(Result.success(emptyList()))
val result = sut.findActivityByPaymentId(
paymentHashOrTxId = paymentId,
type = ActivityFilter.LIGHTNING,
txType = PaymentType.RECEIVED
)
assertTrue(result.isFailure)
assertTrue(result.exceptionOrNull() is IllegalStateException)
}
@Test
fun `getActivities returns activities successfully`() = test {
val activities = listOf(testActivity)
wheneverBlocking {
coreService.activity.get(
filter = ActivityFilter.ALL,
txType = PaymentType.RECEIVED,
tags = listOf("tag1"),
search = "search",
minDate = 1000u,
maxDate = 2000u,
limit = 50u,
sortDirection = SortDirection.DESC
)
}.thenReturn(activities)
val result = sut.getActivities(
filter = ActivityFilter.ALL,
txType = PaymentType.RECEIVED,
tags = listOf("tag1"),
search = "search",
minDate = 1000u,
maxDate = 2000u,
limit = 50u,
sortDirection = SortDirection.DESC
)
assertTrue(result.isSuccess)
assertEquals(activities, result.getOrThrow())
}
@Test
fun `getActivity returns activity when found`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(testActivity)
val result = sut.getActivity(activityId)
assertTrue(result.isSuccess)
assertEquals(testActivity, result.getOrThrow())
}
@Test
fun `getActivity returns null when not found`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(null)
val result = sut.getActivity(activityId)
assertTrue(result.isSuccess)
assertNull(result.getOrThrow())
}
@Test
fun `contactActivities filters replaced sent transaction`() = test {
val contactPublicKey = "pubky3rsduhcxpw74snwyct86m38c63j3pq8x4ycqikxg64roik8yw5xg"
val replacedTxId = "replaced_tx_id"
val replacedActivity = createOnchainActivity(
id = "replaced_activity_id",
txId = replacedTxId,
doesExist = false,
contact = contactPublicKey,
)
val replacementActivity = createOnchainActivity(
id = "replacement_activity_id",
txId = "replacement_tx_id",
boostTxIds = listOf(replacedTxId),
contact = contactPublicKey,
)
whenever(coreService.activity.getTxIdsInBoostTxIds()).thenReturn(setOf(replacedTxId))
whenever(
coreService.activity.get(
filter = ActivityFilter.ALL,
txType = null,
tags = null,
search = null,
minDate = null,
maxDate = null,
limit = null,
sortDirection = SortDirection.DESC,
)
).thenReturn(listOf(replacedActivity, replacementActivity))
val result = sut.contactActivities(contactPublicKey)
assertEquals(listOf(replacementActivity), result.getOrThrow())
}
@Test
fun `setContact propagates contact to replacement transaction`() = test {
val contactPublicKey = "pubky3rsduhcxpw74snwyct86m38c63j3pq8x4ycqikxg64roik8yw5xg"
val replacedTxId = "replaced_tx_id"
val replacedActivity = createOnchainActivity(
id = "replaced_activity_id",
txId = replacedTxId,
doesExist = false,
)
val replacementActivity = createOnchainActivity(
id = "replacement_activity_id",
txId = "replacement_tx_id",
boostTxIds = listOf(replacedTxId),
)
whenever(coreService.activity.getActivity(replacedTxId)).thenReturn(null)
whenever(coreService.activity.getOnchainActivityByTxId(replacedTxId)).thenReturn(replacedActivity.v1)
whenever(
coreService.activity.get(
filter = ActivityFilter.ONCHAIN,
txType = null,
tags = null,
search = null,
minDate = null,
maxDate = null,
limit = null,
sortDirection = null,
)
).thenReturn(listOf(replacedActivity, replacementActivity))
val result = sut.setContact(
contactPublicKey = contactPublicKey,
forPaymentId = replacedTxId,
syncLdkPayments = false,
)
assertTrue(result.isSuccess)
verify(coreService.activity).update(
eq(replacedActivity.v1.id),
argThat {
this is Activity.Onchain && v1.contact == contactPublicKey
},
)
verify(coreService.activity).update(
eq(replacementActivity.v1.id),
argThat {
this is Activity.Onchain && v1.contact == contactPublicKey
},
)
}
@Test
fun `updateActivity updates successfully when not deleted`() = test {
val activityId = "activity123"
val cacheData = AppCacheData(deletedActivities = emptyList())
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
wheneverBlocking { coreService.activity.update(activityId, testActivity) }.thenReturn(Unit)
val result = sut.updateActivity(activityId, testActivity)
assertTrue(result.isSuccess)
verify(coreService.activity).update(activityId, testActivity)
}
@Test
fun `updateActivity fails when activity is deleted and forceUpdate is false`() = test {
val activityId = "activity123"
val cacheData = AppCacheData(deletedActivities = listOf(activityId))
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
val result = sut.updateActivity(activityId, testActivity, forceUpdate = false)
assertTrue(result.isFailure)
verify(coreService.activity, never()).update(any(), any())
}
@Test
fun `updateActivity succeeds when activity is deleted but forceUpdate is true`() = test {
val activityId = "activity123"
val cacheData = AppCacheData(deletedActivities = listOf(activityId))
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
wheneverBlocking { coreService.activity.update(activityId, testActivity) }.thenReturn(Unit)
val result = sut.updateActivity(activityId, testActivity, forceUpdate = true)
assertTrue(result.isSuccess)
verify(coreService.activity).update(activityId, testActivity)
}
@Test
fun `replaceActivity updates activity and copies tags`() = test {
val activityId = "activity123"
val activityToDeleteId = "activity456"
val tagsMock = listOf("tag1", "tag2")
val cacheData = AppCacheData(deletedActivities = emptyList())
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
// Mock update for the new activity
wheneverBlocking { coreService.activity.update(activityId, testActivity) }.thenReturn(Unit)
// Mock getActivity to return the new activity (for addTagsToActivity check)
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(testActivity)
// Mock tags retrieval from the old activity
wheneverBlocking { coreService.activity.tags(activityToDeleteId) }.thenReturn(tagsMock)
// Mock tags retrieval from the new activity (should be empty so all tags are considered new)
wheneverBlocking { coreService.activity.tags(activityId) }.thenReturn(emptyList())
// Mock appendTags to add tags to the new activity
wheneverBlocking { coreService.activity.appendTags(activityId, tagsMock) }.thenReturn(Result.success(Unit))
val result = sut.replaceActivity(activityId, activityToDeleteId, testActivity)
assertTrue(result.isSuccess)
// Verify the new activity is updated
verify(coreService.activity).update(activityId, testActivity)
// Verify tags are retrieved from the old activity
verify(coreService.activity).tags(activityToDeleteId)
// Verify tags are added to the new activity
verify(coreService.activity).appendTags(activityId, tagsMock)
// Verify delete is NOT called
verify(coreService.activity, never()).delete(any())
// Verify addActivityToDeletedList is NOT called
verify(cacheStore, never()).addActivityToDeletedList(any())
}
@Test
fun `deleteActivity deletes successfully`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.delete(activityId) }.thenReturn(true)
wheneverBlocking { cacheStore.addActivityToDeletedList(activityId) }.thenReturn(Unit)
val result = sut.deleteActivity(activityId)
assertTrue(result.isSuccess)
verify(coreService.activity).delete(activityId)
verify(cacheStore).addActivityToDeletedList(activityId)
}
@Test
fun `deleteActivity fails when service returns false`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.delete(activityId) }.thenReturn(false)
val result = sut.deleteActivity(activityId)
assertTrue(result.isFailure)
verify(cacheStore, never()).addActivityToDeletedList(any())
}
@Test
fun `insertActivity inserts successfully when not deleted`() = test {
val cacheData = AppCacheData(deletedActivities = emptyList())
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
wheneverBlocking { coreService.activity.insert(testActivity) }.thenReturn(Unit)
val result = sut.insertActivity(testActivity)
assertTrue(result.isSuccess)
verify(coreService.activity).insert(testActivity)
}
@Test
fun `insertActivity fails when activity is deleted`() = test {
val cacheData = AppCacheData(deletedActivities = listOf("activity1"))
whenever(cacheStore.data).thenReturn(flowOf(cacheData))
val result = sut.insertActivity(testActivity)
assertTrue(result.isFailure)
verify(coreService.activity, never()).insert(any())
}
@Test
fun `addTagsToActivity adds new tags successfully`() = test {
val activityId = "activity123"
val existingTags = listOf("tag1", "tag2")
val newTags = listOf("tag2", "tag3", "tag4", "") // tag2 exists, empty string should be filtered
val expectedNewTags = listOf("tag3", "tag4")
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(testActivity)
wheneverBlocking { coreService.activity.tags(activityId) }.thenReturn(existingTags)
wheneverBlocking {
coreService.activity.appendTags(
activityId,
expectedNewTags
)
}.thenReturn(Result.success(Unit))
val result = sut.addTagsToActivity(activityId, newTags)
assertTrue(result.isSuccess)
verify(coreService.activity).appendTags(activityId, expectedNewTags)
}
@Test
fun `addTagsToActivity fails when activity not found`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(null)
val result = sut.addTagsToActivity(activityId, listOf("tag1"))
assertTrue(result.isFailure)
}
@Test
fun `addTagsToActivity does nothing when no new tags`() = test {
val activityId = "activity123"
val existingTags = listOf("tag1", "tag2")
val duplicateTags = listOf("tag1", "tag2", "")
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(testActivity)
wheneverBlocking { coreService.activity.tags(activityId) }.thenReturn(existingTags)
val result = sut.addTagsToActivity(activityId, duplicateTags)
assertTrue(result.isSuccess)
verify(coreService.activity, never()).appendTags(any(), any())
}
@Test
fun `attachTagsToActivity should fail with empty tags`() = test {
val result = sut.addTagsToTransaction(
paymentHashOrTxId = "txId",
type = ActivityFilter.ALL,
txType = PaymentType.SENT,
tags = emptyList()
)
assertTrue(result.isFailure)
}
@Test
fun `attachTagsToActivity should fail with empty paymentHashOrTxId`() = test {
val result = sut.addTagsToTransaction(
paymentHashOrTxId = "",
type = ActivityFilter.ALL,
txType = PaymentType.SENT,
tags = listOf("tag1")
)
assertTrue(result.isFailure)
}
@Test
fun `removeTagsFromActivity removes tags successfully`() = test {
val activityId = "activity123"
val tagsToRemove = listOf("tag1", "tag2")
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(testActivity)
wheneverBlocking { coreService.activity.dropTags(activityId, tagsToRemove) }.thenReturn(Unit)
val result = sut.removeTagsFromActivity(activityId, tagsToRemove)
assertTrue(result.isSuccess)
verify(coreService.activity).dropTags(activityId, tagsToRemove)
}
@Test
fun `removeTagsFromActivity fails when activity not found`() = test {
val activityId = "activity123"
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(null)
val result = sut.removeTagsFromActivity(activityId, listOf("tag1"))
assertTrue(result.isFailure)
}
@Test
fun `getActivityTags returns tags successfully`() = test {
val activityId = "activity123"
val tags = listOf("tag1", "tag2", "tag3")
wheneverBlocking { coreService.activity.tags(activityId) }.thenReturn(tags)
val result = sut.getActivityTags(activityId)
assertTrue(result.isSuccess)
assertEquals(tags, result.getOrThrow())
}
@Test
fun `getAllAvailableTags returns all tags successfully`() = test {
val allTags = listOf("tag1", "tag2", "tag3", "tag4")
wheneverBlocking { coreService.activity.allPossibleTags() }.thenReturn(allTags)
val result = sut.getAllAvailableTags()
assertTrue(result.isSuccess)
assertEquals(allTags, result.getOrThrow())
}
@Test
fun `removeAllActivities removes all activities successfully`() = test {
wheneverBlocking { coreService.activity.removeAll() }.thenReturn(Unit)
val result = sut.removeAllActivities()
assertTrue(result.isSuccess)
verify(coreService.activity).removeAll()
}
@Test
fun `generateTestData generates with validated count`() = test {
wheneverBlocking { coreService.activity.generateRandomTestData(any()) }.thenReturn(Unit)
val result = sut.generateTestData(50)
assertTrue(result.isSuccess)
verify(coreService.activity).generateRandomTestData(50)
}
@Test
fun `generateTestData coerces count to valid range`() = test {
wheneverBlocking { coreService.activity.generateRandomTestData(any()) }.thenReturn(Unit)
val result = sut.generateTestData(1500) // Over limit
assertTrue(result.isSuccess)
verify(coreService.activity).generateRandomTestData(1000) // Should be coerced to max
}
@Test
fun `addActivityToPendingBoost adds to cache`() = test {
val pendingBoost = PendingBoostActivity(
txId = "tx123",
updatedAt = 2000u,
activityToDelete = null
)
wheneverBlocking { cacheStore.addActivityToPendingBoost(pendingBoost) }.thenReturn(Unit)
sut.addActivityToPendingBoost(pendingBoost)
verify(cacheStore).addActivityToPendingBoost(pendingBoost)
}
@Test
fun `boostPendingActivities adds parentTxId to boostTxIds when parentTxId is provided`() = test {
val txId = "tx123"
val parentTxId = "parentTx456"
val activityId = "activity123"
val updatedAt = 2000uL
val existingActivity = createOnchainActivity(
id = activityId,
txId = txId,
updatedAt = 1000uL
)
val pendingBoost = PendingBoostActivity(
txId = txId,
updatedAt = updatedAt,
activityToDelete = null,
parentTxId = parentTxId
)
val cacheData = AppCacheData(pendingBoostActivities = listOf(pendingBoost))
setupSyncActivitiesMocks(cacheData)
wheneverBlocking {
coreService.activity.get(
filter = eq(ActivityFilter.ONCHAIN),
txType = eq(PaymentType.SENT),
tags = anyOrNull(),
search = anyOrNull(),
minDate = anyOrNull(),
maxDate = anyOrNull(),
limit = eq(10u),
sortDirection = anyOrNull()
)
}.thenReturn(listOf(existingActivity))
wheneverBlocking { coreService.activity.update(eq(activityId), any()) }.thenReturn(Unit)
wheneverBlocking { cacheStore.removeActivityFromPendingBoost(pendingBoost) }.thenReturn(Unit)
val result = sut.syncActivities()
assertTrue(result.isSuccess)
// Verify update was called with parentTxId added to empty boostTxIds
verify(coreService.activity).update(
eq(activityId),
argThat { activity ->
activity is Activity.Onchain && activity.v1.boostTxIds == listOf(parentTxId)
}
)
verify(cacheStore).removeActivityFromPendingBoost(pendingBoost)
}
@Test
fun `boostPendingActivities preserves existing boostTxIds when adding parentTxId`() = test {
val txId = "tx123"
val parentTxId = "parentTx456"
val existingBoostTxId = "existingBoost123"
val activityId = "activity123"
val updatedAt = 2000uL
val existingActivity = createOnchainActivity(
id = activityId,
txId = txId,
boostTxIds = listOf(existingBoostTxId),
updatedAt = 1000uL
)
val pendingBoost = PendingBoostActivity(
txId = txId,
updatedAt = updatedAt,
activityToDelete = null,
parentTxId = parentTxId
)
val cacheData = AppCacheData(pendingBoostActivities = listOf(pendingBoost))
setupSyncActivitiesMocks(cacheData)
wheneverBlocking {
coreService.activity.get(
filter = eq(ActivityFilter.ONCHAIN),
txType = eq(PaymentType.SENT),
tags = anyOrNull(),
search = anyOrNull(),
minDate = anyOrNull(),
maxDate = anyOrNull(),
limit = eq(10u),
sortDirection = anyOrNull()
)
}.thenReturn(listOf(existingActivity))
wheneverBlocking { coreService.activity.update(eq(activityId), any()) }.thenReturn(Unit)
wheneverBlocking { cacheStore.removeActivityFromPendingBoost(pendingBoost) }.thenReturn(Unit)
val result = sut.syncActivities()
assertTrue(result.isSuccess)
// Verify update was called with both existing and new parentTxId in boostTxIds
verify(coreService.activity).update(
eq(activityId),
argThat { activity ->
activity is Activity.Onchain &&
activity.v1.boostTxIds.contains(existingBoostTxId) &&
activity.v1.boostTxIds.contains(parentTxId)
}
)
}
@Test
fun `boostPendingActivities does not add parentTxId when parentTxId is null`() = test {
val txId = "tx123"
val existingBoostTxId = "existingBoost123"
val activityId = "activity123"
val updatedAt = 2000uL
val existingActivity = createOnchainActivity(
id = activityId,
txId = txId,
boostTxIds = listOf(existingBoostTxId),
updatedAt = 1000uL
)
val pendingBoost = PendingBoostActivity(
txId = txId,
updatedAt = updatedAt,
activityToDelete = null,
parentTxId = null
)
val cacheData = AppCacheData(pendingBoostActivities = listOf(pendingBoost))
setupSyncActivitiesMocks(cacheData)
wheneverBlocking {
coreService.activity.get(
filter = eq(ActivityFilter.ONCHAIN),
txType = eq(PaymentType.SENT),
tags = anyOrNull(),
search = anyOrNull(),
minDate = anyOrNull(),
maxDate = anyOrNull(),
limit = eq(10u),
sortDirection = anyOrNull()
)
}.thenReturn(listOf(existingActivity))
wheneverBlocking { coreService.activity.update(eq(activityId), any()) }.thenReturn(Unit)
wheneverBlocking { cacheStore.removeActivityFromPendingBoost(pendingBoost) }.thenReturn(Unit)
val result = sut.syncActivities()
assertTrue(result.isSuccess)
// Verify update was called with only existing boostTxIds (no new parentTxId added)
verify(coreService.activity).update(
eq(activityId),
argThat { activity ->
activity is Activity.Onchain &&
activity.v1.boostTxIds == listOf(existingBoostTxId)
}
)
}
@Test
fun `boostPendingActivities calls replaceActivity when activityToDelete is provided`() = test {
val txId = "tx123"
val parentTxId = "parentTx456"
val activityId = "activity123"
val activityToDeleteId = "activity456"
val updatedAt = 2000uL
val existingActivity = createOnchainActivity(
id = activityId,
txId = txId,
updatedAt = 1000uL
)
val pendingBoost = PendingBoostActivity(
txId = txId,
updatedAt = updatedAt,
activityToDelete = activityToDeleteId,
parentTxId = parentTxId
)
val cacheData = AppCacheData(pendingBoostActivities = listOf(pendingBoost))
setupSyncActivitiesMocks(cacheData)
wheneverBlocking {
coreService.activity.get(
filter = eq(ActivityFilter.ONCHAIN),
txType = eq(PaymentType.SENT),
tags = anyOrNull(),
search = anyOrNull(),
minDate = anyOrNull(),
maxDate = anyOrNull(),
limit = eq(10u),
sortDirection = anyOrNull()
)
}.thenReturn(listOf(existingActivity))
val tagsToCopy = listOf("tag1", "tag2")
wheneverBlocking { coreService.activity.update(eq(activityId), any()) }.thenReturn(Unit)
wheneverBlocking { coreService.activity.getActivity(activityId) }.thenReturn(existingActivity)
wheneverBlocking { coreService.activity.tags(activityToDeleteId) }.thenReturn(tagsToCopy)
wheneverBlocking { coreService.activity.tags(activityId) }.thenReturn(emptyList())
wheneverBlocking { coreService.activity.appendTags(activityId, tagsToCopy) }.thenReturn(Result.success(Unit))
wheneverBlocking { cacheStore.removeActivityFromPendingBoost(pendingBoost) }.thenReturn(Unit)
val result = sut.syncActivities()
assertTrue(result.isSuccess)
// Verify replaceActivity was called (indirectly by checking the new activity was updated)
verify(coreService.activity).update(eq(activityId), any())
// Verify tags were copied from old activity to new activity
verify(coreService.activity).tags(activityToDeleteId)
verify(coreService.activity).appendTags(activityId, tagsToCopy)
verify(cacheStore).removeActivityFromPendingBoost(pendingBoost)
}
@Test
fun `boostPendingActivities skips when activity updatedAt is newer than pendingBoost updatedAt`() = test {
val txId = "tx123"
val activityId = "activity123"
val updatedAt = 2000uL
val existingActivity = createOnchainActivity(
id = activityId,
txId = txId,
updatedAt = 3000uL // Newer than pendingBoost.updatedAt
)
val pendingBoost = PendingBoostActivity(
txId = txId,
updatedAt = updatedAt,
activityToDelete = null,
parentTxId = null
)
val cacheData = AppCacheData(pendingBoostActivities = listOf(pendingBoost))
setupSyncActivitiesMocks(cacheData)
wheneverBlocking {
coreService.activity.get(
filter = eq(ActivityFilter.ONCHAIN),
txType = eq(PaymentType.SENT),
tags = anyOrNull(),
search = anyOrNull(),
minDate = anyOrNull(),
maxDate = anyOrNull(),
limit = eq(10u),
sortDirection = anyOrNull()
)
}.thenReturn(listOf(existingActivity))
wheneverBlocking { cacheStore.removeActivityFromPendingBoost(pendingBoost) }.thenReturn(Unit)
val result = sut.syncActivities()
assertTrue(result.isSuccess)
// Verify update was NOT called (activity is newer)
verify(coreService.activity, never()).update(eq(activityId), any())
// Verify pending boost was removed (skipped)
verify(cacheStore).removeActivityFromPendingBoost(pendingBoost)
}
}