@@ -51,6 +51,7 @@ import to.bitkit.services.LightningService
5151import to.bitkit.services.LnurlService
5252import to.bitkit.services.LspNotificationsService
5353import to.bitkit.test.BaseUnitTest
54+ import to.bitkit.utils.UrlValidator
5455import kotlin.test.assertEquals
5556import kotlin.test.assertFalse
5657import kotlin.test.assertNotNull
@@ -72,6 +73,7 @@ class LightningRepoTest : BaseUnitTest() {
7273 private val lnurlService = mock<LnurlService >()
7374 private val connectivityRepo = mock<ConnectivityRepo >()
7475 private val vssBackupClientLdk = mock<VssBackupClientLdk >()
76+ private val urlValidator = UrlValidator { Result .success(Unit ) }
7577
7678 @Before
7779 fun setUp () = runBlocking {
@@ -94,6 +96,7 @@ class LightningRepoTest : BaseUnitTest() {
9496 preActivityMetadataRepo = preActivityMetadataRepo,
9597 connectivityRepo = connectivityRepo,
9698 vssBackupClientLdk = vssBackupClientLdk,
99+ urlValidator = urlValidator,
97100 )
98101 }
99102
@@ -498,6 +501,78 @@ class LightningRepoTest : BaseUnitTest() {
498501 assertTrue(result.isFailure)
499502 }
500503
504+ @Test
505+ fun `restartWithRgsServer should setup with new rgs server` () = test {
506+ startNodeForTesting()
507+ val customRgsUrl = " https://rgs.example.com/snapshot"
508+ whenever(lightningService.node).thenReturn(null )
509+ whenever(lightningService.stop()).thenReturn(Unit )
510+
511+ val result = sut.restartWithRgsServer(customRgsUrl)
512+
513+ assertTrue(result.isSuccess)
514+ val inOrder = inOrder(lightningService)
515+ inOrder.verify(lightningService).stop()
516+ inOrder.verify(lightningService).setup(any(), isNull(), eq(customRgsUrl), anyOrNull(), anyOrNull())
517+ inOrder.verify(lightningService).start(anyOrNull(), any())
518+ assertEquals(NodeLifecycleState .Running , sut.lightningState.value.nodeLifecycleState)
519+ }
520+
521+ @Test
522+ fun `restartWithRgsServer should handle stop failure` () = test {
523+ startNodeForTesting()
524+ whenever(lightningService.stop()).thenThrow(RuntimeException (" Stop failed" ))
525+
526+ val result = sut.restartWithRgsServer(" https://rgs.example.com/snapshot" )
527+
528+ assertTrue(result.isFailure)
529+ }
530+
531+ @Test
532+ fun `restartWithRgsServer should handle start failure and recover` () = test {
533+ startNodeForTesting()
534+ whenever(lightningService.node).thenReturn(null )
535+ whenever(lightningService.stop()).thenReturn(Unit )
536+ whenever(lightningService.setup(any(), isNull(), eq(" https://bad.rgs/snapshot" ), anyOrNull(), anyOrNull()))
537+ .thenThrow(RuntimeException (" Failed to start node" ))
538+
539+ val result = sut.restartWithRgsServer(" https://bad.rgs/snapshot" )
540+
541+ assertTrue(result.isFailure)
542+ }
543+
544+ @Test
545+ fun `restartWithRgsServer should fail when url is unreachable` () = test {
546+ val failingValidator = UrlValidator { Result .failure(Exception (" DNS resolution failed" )) }
547+ val sutWithFailingValidator = LightningRepo (
548+ bgDispatcher = testDispatcher,
549+ lightningService = lightningService,
550+ settingsStore = settingsStore,
551+ coreService = coreService,
552+ lspNotificationsService = lspNotificationsService,
553+ firebaseMessaging = firebaseMessaging,
554+ keychain = keychain,
555+ lnurlService = lnurlService,
556+ cacheStore = cacheStore,
557+ preActivityMetadataRepo = preActivityMetadataRepo,
558+ connectivityRepo = connectivityRepo,
559+ vssBackupClientLdk = vssBackupClientLdk,
560+ urlValidator = failingValidator,
561+ )
562+ sutWithFailingValidator.setInitNodeLifecycleState()
563+ whenever(lightningService.node).thenReturn(mock())
564+ whenever(lightningService.sync()).thenReturn(Unit )
565+ val blocktank = mock<BlocktankService >()
566+ whenever(coreService.blocktank).thenReturn(blocktank)
567+ whenever(blocktank.info(any())).thenReturn(null )
568+ sutWithFailingValidator.start()
569+
570+ val result = sutWithFailingValidator.restartWithRgsServer(" https://rapidsync.lightningdevkit/snapshot" )
571+
572+ assertTrue(result.isFailure)
573+ assertEquals(" DNS resolution failed" , result.exceptionOrNull()?.message)
574+ }
575+
501576 @Test
502577 fun `getFeeRateForSpeed should use provided feeRates` () = test {
503578 val mockFeeRates = mock<FeeRates >()
0 commit comments