Skip to content

Commit 9f6f5fa

Browse files
author
Mohit Kumar
committed
Add test for format=nodeId and shortChannelId
1 parent 6f6807c commit 9f6f5fa

File tree

2 files changed

+128
-55
lines changed

2 files changed

+128
-55
lines changed

src/nativeTest/kotlin/commands/FindRouteTest.kt

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,55 @@ import kotlin.test.*
1313

1414
@OptIn(ExperimentalCli::class)
1515
class FindRouteCommandTest {
16-
private fun runTest(eclairClient: IEclairClientBuilder): DummyResultWriter {
16+
private fun runTest(eclairClient: IEclairClientBuilder, format: String? = null): DummyResultWriter {
1717
val resultWriter = DummyResultWriter()
1818
val command = FindRouteCommand(resultWriter, eclairClient)
1919
val parser = ArgParser("test")
2020
parser.subcommands(command)
21-
parser.parse(
22-
arrayOf(
23-
"findroute",
24-
"-p",
25-
"password",
26-
"--invoice",
27-
"lnbcrt10n1pjduajwpp5s6dsm9vk3q0ntxeq2zd6d4jz8mv8wau75dugud5puc3lltwp68esdqsd3shgetnw33k7er9sp5rye5z7eccrg7kx9jj6u24q2aumgl09e0e894w6hdceyk60g7a2hsmqz9gxqrrsscqp79q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgq9fktzq8fpyey9js0x85t6s5mtcwqzmmd4ql9cjq04f4tunlysje894mdcmhjwkewrk5wn2ylv3da64pda7tj04s3m90en5t6p7yyglgpue2lzz",
28-
"--format",
29-
"full"
30-
)
21+
val arguments = mutableListOf(
22+
"findroute",
23+
"-p",
24+
"password",
25+
"--invoice",
26+
"lnbcrt10n1pjduajwpp5s6dsm9vk3q0ntxeq2zd6d4jz8mv8wau75dugud5puc3lltwp68esdqsd3shgetnw33k7er9sp5rye5z7eccrg7kx9jj6u24q2aumgl09e0e894w6hdceyk60g7a2hsmqz9gxqrrsscqp79q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgq9fktzq8fpyey9js0x85t6s5mtcwqzmmd4ql9cjq04f4tunlysje894mdcmhjwkewrk5wn2ylv3da64pda7tj04s3m90en5t6p7yyglgpue2lzz"
3127
)
28+
format?.let { arguments.addAll(listOf("--format", it)) }
29+
parser.parse(arguments.toTypedArray())
3230
return resultWriter
3331
}
3432

3533
@Test
36-
fun `successful request`() {
37-
val resultWriter = runTest(DummyEclairClient(findrouteResponse = DummyEclairClient.validFindRouteResponse))
34+
fun `successful request via nodeId`() {
35+
val resultWriter = runTest(DummyEclairClient(), "nodeId")
3836
assertNull(resultWriter.lastError)
3937
assertNotNull(resultWriter.lastResult)
4038
val format = Json { ignoreUnknownKeys = true }
4139
assertEquals(
42-
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validFindRouteResponse),
40+
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseNodeId),
41+
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
42+
)
43+
}
44+
45+
@Test
46+
fun `successful request via shortChannelId`() {
47+
val resultWriter = runTest(DummyEclairClient(), "shortChannelId")
48+
assertNull(resultWriter.lastError)
49+
assertNotNull(resultWriter.lastResult)
50+
val format = Json { ignoreUnknownKeys = true }
51+
assertEquals(
52+
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseShortChannelId),
53+
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
54+
)
55+
}
56+
57+
@Test
58+
fun `successful request via full`() {
59+
val resultWriter = runTest(DummyEclairClient(), "full")
60+
assertNull(resultWriter.lastError)
61+
assertNotNull(resultWriter.lastResult)
62+
val format = Json { ignoreUnknownKeys = true }
63+
assertEquals(
64+
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseFull),
4365
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
4466
)
4567
}
@@ -53,8 +75,24 @@ class FindRouteCommandTest {
5375
}
5476

5577
@Test
56-
fun `serialization error`() {
57-
val resultWriter = runTest(DummyEclairClient(findrouteResponse = "{invalidJson}"))
78+
fun `serialization error via nodeId`() {
79+
val resultWriter = runTest(DummyEclairClient(findrouteResponseNodeId = "{invalidJson}"), "nodeId")
80+
assertNull(resultWriter.lastResult)
81+
assertNotNull(resultWriter.lastError)
82+
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))
83+
}
84+
85+
@Test
86+
fun `serialization error via shortChannelId`() {
87+
val resultWriter = runTest(DummyEclairClient(findrouteResponseShortChannelId = "{invalidJson}"), "shortChannelId")
88+
assertNull(resultWriter.lastResult)
89+
assertNotNull(resultWriter.lastError)
90+
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))
91+
}
92+
93+
@Test
94+
fun `serialization error via full`() {
95+
val resultWriter = runTest(DummyEclairClient(findrouteResponseFull = "{invalidJson}"), "full")
5896
assertNull(resultWriter.lastResult)
5997
assertNotNull(resultWriter.lastError)
6098
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))

src/nativeTest/kotlin/mocks/EclairClientMocks.kt

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ class DummyEclairClient(
3333
private val getinvoiceResponse: String = validGetInvoiceResponse,
3434
private val listinvoicesResponse: String = validListInvoicesResponse,
3535
private val listpendinginvoicesResponse: String = validListPendingInvoicesResponse,
36-
private val findrouteResponse: String = validFindRouteResponse,
37-
private val findroutetonodeResponse: String = validFindRouteToNodeResponse,
38-
private val findroutebetweennodesResponse: String = validFindRouteBetweenNodesResponse,
36+
private val findrouteResponseNodeId: String = validRouteResponseNodeId,
37+
private val findrouteResponseShortChannelId: String = validRouteResponseShortChannelId,
38+
private val findrouteResponseFull: String = validRouteResponseFull,
39+
private val findroutetonodeResponseNodeId: String = validRouteResponseNodeId,
40+
private val findroutetonodeResponseShortChannelId: String = validRouteResponseShortChannelId,
41+
private val findroutetonodeResponseFull: String = validRouteResponseFull,
42+
private val findroutebetweennodesResponseNodeId: String = validRouteResponseNodeId,
43+
private val findroutebetweennodesResponseShortChannelId: String = validRouteResponseShortChannelId,
44+
private val findroutebetweennodesResponseFull: String = validRouteResponseFull,
3945
) : IEclairClient, IEclairClientBuilder {
4046
override fun build(apiHost: String, apiPassword: String): IEclairClient = this
4147
override suspend fun getInfo(): Either<ApiError, String> = Either.Right(getInfoResponse)
42-
override suspend fun connect(target: ConnectionTarget): Either<ApiError, String> = Either.Right(validConnectResponse)
48+
override suspend fun connect(target: ConnectionTarget): Either<ApiError, String> =
49+
Either.Right(validConnectResponse)
50+
4351
override suspend fun rbfopen(
4452
channelId: String,
4553
targetFeerateSatByte: Int,
@@ -104,7 +112,8 @@ class DummyEclairClient(
104112
paymentPreimage: String?
105113
): Either<ApiError, String> = Either.Right(createInvoiceResponse)
106114

107-
override suspend fun deleteinvoice(paymentHash: String): Either<ApiError, String> = Either.Right(deleteInvoiceResponse)
115+
override suspend fun deleteinvoice(paymentHash: String): Either<ApiError, String> =
116+
Either.Right(deleteInvoiceResponse)
108117

109118
override suspend fun parseinvoice(invoice: String): Either<ApiError, String> = Either.Right(parseInvoiceResponse)
110119

@@ -181,7 +190,14 @@ class DummyEclairClient(
181190
maxFeeMsat: Int?,
182191
includeLocalChannelCost: Boolean?,
183192
pathFindingExperimentName: String?
184-
): Either<ApiError, String> = Either.Right(findrouteResponse)
193+
): Either<ApiError, String> {
194+
return when (format) {
195+
"nodeId" -> Either.Right(findrouteResponseNodeId)
196+
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
197+
"full" -> Either.Right(findrouteResponseFull)
198+
else -> Either.Right(findrouteResponseNodeId)
199+
}
200+
}
185201

186202
override suspend fun findroutetonode(
187203
nodeId: String,
@@ -192,7 +208,14 @@ class DummyEclairClient(
192208
maxFeeMsat: Int?,
193209
includeLocalChannelCost: Boolean?,
194210
pathFindingExperimentName: String?
195-
): Either<ApiError, String> = Either.Right(findroutetonodeResponse)
211+
): Either<ApiError, String> {
212+
return when (format) {
213+
"nodeId" -> Either.Right(findrouteResponseNodeId)
214+
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
215+
"full" -> Either.Right(findrouteResponseFull)
216+
else -> Either.Right(findrouteResponseNodeId)
217+
}
218+
}
196219

197220
override suspend fun findroutebetweennodes(
198221
sourceNodeId: String,
@@ -204,7 +227,14 @@ class DummyEclairClient(
204227
maxFeeMsat: Int?,
205228
includeLocalChannelCost: Boolean?,
206229
pathFindingExperimentName: String?
207-
): Either<ApiError, String> = Either.Right(findroutebetweennodesResponse)
230+
): Either<ApiError, String> {
231+
return when (format) {
232+
"nodeId" -> Either.Right(findrouteResponseNodeId)
233+
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
234+
"full" -> Either.Right(findrouteResponseFull)
235+
else -> Either.Right(findrouteResponseNodeId)
236+
}
237+
}
208238

209239
companion object {
210240
val validGetInfoResponse =
@@ -422,7 +452,8 @@ class DummyEclairClient(
422452
},
423453
"routingInfo": []
424454
}"""
425-
val validDeleteInvoiceResponse = "deleted invoice 6f0864735283ca95eaf9c50ef77893f55ee3dd11cb90710cbbfb73f018798a68"
455+
val validDeleteInvoiceResponse =
456+
"deleted invoice 6f0864735283ca95eaf9c50ef77893f55ee3dd11cb90710cbbfb73f018798a68"
426457
val validParseInvoiceResponse = """{
427458
"prefix": "lnbcrt",
428459
"timestamp": 1643718891,
@@ -793,7 +824,31 @@ class DummyEclairClient(
793824
"routingInfo": []
794825
}
795826
]"""
796-
val validFindRouteResponse = """{
827+
val validRouteResponseNodeId = """{
828+
"routes": [
829+
{
830+
"amount": 5000,
831+
"nodeIds": [
832+
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
833+
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
834+
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
835+
]
836+
}
837+
]
838+
}"""
839+
val validRouteResponseShortChannelId = """{
840+
"routes": [
841+
{
842+
"amount": 5000,
843+
"shortChannelIds": [
844+
"11203x1x0",
845+
"11203x7x5",
846+
"11205x3x3"
847+
]
848+
}
849+
]
850+
}"""
851+
val validRouteResponseFull = """{
797852
"type": "types.FindRouteResponse",
798853
"routes": [
799854
{
@@ -904,30 +959,6 @@ class DummyEclairClient(
904959
]
905960
}
906961
"""
907-
val validFindRouteToNodeResponse = """{
908-
"routes": [
909-
{
910-
"amount": 5000,
911-
"nodeIds": [
912-
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
913-
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
914-
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
915-
]
916-
}
917-
]
918-
}"""
919-
val validFindRouteBetweenNodesResponse = """{
920-
"routes": [
921-
{
922-
"amount": 5000,
923-
"nodeIds": [
924-
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
925-
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
926-
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
927-
]
928-
}
929-
]
930-
}"""
931962
}
932963
}
933964

@@ -1037,17 +1068,21 @@ class FailingEclairClient(private val error: ApiError) : IEclairClient, IEclairC
10371068
externalId: String?
10381069
): Either<ApiError, String> = Either.Left(error)
10391070

1040-
override suspend fun getsentinfo(paymentHash: String, id: String?): Either<ApiError, String> = Either.Left(error)
1071+
override suspend fun getsentinfo(paymentHash: String, id: String?): Either<ApiError, String> = Either.Left(error)
10411072

1042-
override suspend fun getreceivedinfo(paymentHash: String?, invoice: String?): Either<ApiError, String> = Either.Left(error)
1073+
override suspend fun getreceivedinfo(paymentHash: String?, invoice: String?): Either<ApiError, String> =
1074+
Either.Left(error)
10431075

1044-
override suspend fun listreceivedpayments(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
1076+
override suspend fun listreceivedpayments(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
1077+
Either.Left(error)
10451078

10461079
override suspend fun getinvoice(paymentHash: String): Either<ApiError, String> = Either.Left(error)
10471080

1048-
override suspend fun listinvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
1081+
override suspend fun listinvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
1082+
Either.Left(error)
10491083

1050-
override suspend fun listpendinginvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
1084+
override suspend fun listpendinginvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
1085+
Either.Left(error)
10511086

10521087
override suspend fun findroute(
10531088
invoice: String,

0 commit comments

Comments
 (0)