-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLightningNodeServiceTest.kt
More file actions
237 lines (200 loc) · 8.55 KB
/
LightningNodeServiceTest.kt
File metadata and controls
237 lines (200 loc) · 8.55 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
package to.bitkit.androidServices
import android.Manifest
import android.app.Activity
import android.app.Application
import android.app.Notification
import android.app.NotificationManager
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.firebase.messaging.FirebaseMessaging
import dagger.hilt.android.testing.BindValue
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import dagger.hilt.android.testing.UninstallModules
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.lightningdevkit.ldknode.Event
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows
import org.robolectric.annotation.Config
import to.bitkit.App
import to.bitkit.CurrentActivity
import to.bitkit.R
import to.bitkit.data.AppCacheData
import to.bitkit.data.CacheStore
import to.bitkit.di.DbModule
import to.bitkit.di.DispatchersModule
import to.bitkit.di.ViewModelModule
import to.bitkit.domain.commands.NotifyPaymentReceived
import to.bitkit.domain.commands.NotifyPaymentReceivedHandler
import to.bitkit.models.NewTransactionSheetDetails
import to.bitkit.models.NewTransactionSheetDirection
import to.bitkit.models.NewTransactionSheetType
import to.bitkit.models.NotificationDetails
import to.bitkit.repositories.LightningRepo
import to.bitkit.repositories.WalletRepo
import to.bitkit.services.NodeEventHandler
import to.bitkit.test.BaseUnitTest
import to.bitkit.ui.shared.toast.ToastQueueManager
@HiltAndroidTest
@UninstallModules(DispatchersModule::class, DbModule::class, ViewModelModule::class)
@Config(application = HiltTestApplication::class, sdk = [34]) // Pin Robolectric to an SDK that supports Java 17
@RunWith(RobolectricTestRunner::class)
class LightningNodeServiceTest : BaseUnitTest() {
@get:Rule(order = 1)
var hiltRule = HiltAndroidRule(this)
@BindValue
val firebaseMessaging = mock<FirebaseMessaging>()
@BindValue
val toastManagerProvider = mock<(CoroutineScope) -> ToastQueueManager>()
@BindValue
val lightningRepo = mock<LightningRepo>()
@BindValue
val walletRepo = mock<WalletRepo>()
@BindValue
val notifyPaymentReceivedHandler = mock<NotifyPaymentReceivedHandler>()
@BindValue
val cacheStore = mock<CacheStore>()
private var capturedHandler: NodeEventHandler? = null
private val cacheData = MutableSharedFlow<AppCacheData>(replay = 1)
private val context = ApplicationProvider.getApplicationContext<Context>()
@Before
fun setUp() = runBlocking {
hiltRule.inject()
lightningRepo.stub {
on {
start(
any(),
anyOrNull(),
any(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
any(),
)
} doAnswer {
capturedHandler = it.getArgument(5) as? NodeEventHandler
Result.success(Unit)
}
}
whenever(lightningRepo.stop()).thenReturn(Result.success(Unit))
// Set up CacheStore mock
whenever(cacheStore.data).thenReturn(cacheData)
// Mock NotifyPaymentReceivedHandler to return ShowNotification result
val sheet = NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
paymentHashOrTxId = "test_hash",
sats = 100L,
)
val notification = NotificationDetails(
title = context.getString(R.string.notification__received__title),
body = "Received ₿ 100 ($0.10)",
)
whenever(notifyPaymentReceivedHandler.invoke(any()))
.thenReturn(Result.success(NotifyPaymentReceived.Result.ShowNotification(sheet, notification)))
// Grant permissions for notifications
val app = context as Application
Shadows.shadowOf(app).grantPermissions(Manifest.permission.POST_NOTIFICATIONS)
// Reset App.currentActivity to simulate background state
App.currentActivity = CurrentActivity()
}
@After
fun tearDown() {
App.currentActivity = null
}
@Test
fun `payment received in background shows notification`() = test {
val controller = Robolectric.buildService(LightningNodeService::class.java)
controller.create().startCommand(0, 0)
testScheduler.advanceUntilIdle()
assertNotNull("Event handler should be captured", capturedHandler)
val event = Event.PaymentReceived(
paymentId = "payment_id",
paymentHash = "test_hash",
amountMsat = 100000u,
customRecords = emptyList()
)
capturedHandler?.invoke(event)
testScheduler.advanceUntilIdle()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val shadows = Shadows.shadowOf(notificationManager)
val paymentNotification = shadows.allNotifications.find {
it.extras.getString(Notification.EXTRA_TITLE) == context.getString(R.string.notification__received__title)
}
assertNotNull("Payment notification should be present", paymentNotification)
val expected = NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
paymentHashOrTxId = "test_hash",
sats = 100L,
)
verify(cacheStore).setBackgroundReceive(expected)
}
@Test
fun `payment received in foreground does nothing`() = test {
// Simulate foreground by setting App.currentActivity.value via lifecycle callback
val mockActivity: Activity = mock()
App.currentActivity?.onActivityStarted(mockActivity)
val controller = Robolectric.buildService(LightningNodeService::class.java)
controller.create().startCommand(0, 0)
testScheduler.advanceUntilIdle()
val event = Event.PaymentReceived(
paymentId = "payment_id_fg",
paymentHash = "test_hash_fg",
amountMsat = 200000u,
customRecords = emptyList()
)
capturedHandler?.invoke(event)
testScheduler.advanceUntilIdle()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val shadows = Shadows.shadowOf(notificationManager)
val paymentNotification = shadows.allNotifications.find {
it.extras.getString(Notification.EXTRA_TITLE) == context.getString(R.string.notification__received__title)
}
assertNull("Payment notification should NOT be present in foreground", paymentNotification)
verify(cacheStore, never()).setBackgroundReceive(any())
}
@Test
fun `notification uses content from use case result`() = test {
val controller = Robolectric.buildService(LightningNodeService::class.java)
controller.create().startCommand(0, 0)
testScheduler.advanceUntilIdle()
val event = Event.PaymentReceived(
paymentId = "payment_id",
paymentHash = "test_hash",
amountMsat = 100000u,
customRecords = emptyList()
)
capturedHandler?.invoke(event)
testScheduler.advanceUntilIdle()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val shadows = Shadows.shadowOf(notificationManager)
val paymentNotification = shadows.allNotifications.find {
it.extras.getString(Notification.EXTRA_TITLE) == context.getString(R.string.notification__received__title)
}
assertNotNull("Payment notification should be present", paymentNotification)
val body = paymentNotification?.extras?.getString(Notification.EXTRA_TEXT)
assertEquals($$"Received ₿ 100 ($0.10)", body)
}
}