Skip to content

Commit

Permalink
Add test for format=nodeId and shortChannelId
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohit Kumar committed Aug 19, 2023
1 parent 6f6807c commit 9f6f5fa
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 55 deletions.
70 changes: 54 additions & 16 deletions src/nativeTest/kotlin/commands/FindRouteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,55 @@ import kotlin.test.*

@OptIn(ExperimentalCli::class)
class FindRouteCommandTest {
private fun runTest(eclairClient: IEclairClientBuilder): DummyResultWriter {
private fun runTest(eclairClient: IEclairClientBuilder, format: String? = null): DummyResultWriter {
val resultWriter = DummyResultWriter()
val command = FindRouteCommand(resultWriter, eclairClient)
val parser = ArgParser("test")
parser.subcommands(command)
parser.parse(
arrayOf(
"findroute",
"-p",
"password",
"--invoice",
"lnbcrt10n1pjduajwpp5s6dsm9vk3q0ntxeq2zd6d4jz8mv8wau75dugud5puc3lltwp68esdqsd3shgetnw33k7er9sp5rye5z7eccrg7kx9jj6u24q2aumgl09e0e894w6hdceyk60g7a2hsmqz9gxqrrsscqp79q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgq9fktzq8fpyey9js0x85t6s5mtcwqzmmd4ql9cjq04f4tunlysje894mdcmhjwkewrk5wn2ylv3da64pda7tj04s3m90en5t6p7yyglgpue2lzz",
"--format",
"full"
)
val arguments = mutableListOf(
"findroute",
"-p",
"password",
"--invoice",
"lnbcrt10n1pjduajwpp5s6dsm9vk3q0ntxeq2zd6d4jz8mv8wau75dugud5puc3lltwp68esdqsd3shgetnw33k7er9sp5rye5z7eccrg7kx9jj6u24q2aumgl09e0e894w6hdceyk60g7a2hsmqz9gxqrrsscqp79q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgq9fktzq8fpyey9js0x85t6s5mtcwqzmmd4ql9cjq04f4tunlysje894mdcmhjwkewrk5wn2ylv3da64pda7tj04s3m90en5t6p7yyglgpue2lzz"
)
format?.let { arguments.addAll(listOf("--format", it)) }
parser.parse(arguments.toTypedArray())
return resultWriter
}

@Test
fun `successful request`() {
val resultWriter = runTest(DummyEclairClient(findrouteResponse = DummyEclairClient.validFindRouteResponse))
fun `successful request via nodeId`() {
val resultWriter = runTest(DummyEclairClient(), "nodeId")
assertNull(resultWriter.lastError)
assertNotNull(resultWriter.lastResult)
val format = Json { ignoreUnknownKeys = true }
assertEquals(
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validFindRouteResponse),
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseNodeId),
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
)
}

@Test
fun `successful request via shortChannelId`() {
val resultWriter = runTest(DummyEclairClient(), "shortChannelId")
assertNull(resultWriter.lastError)
assertNotNull(resultWriter.lastResult)
val format = Json { ignoreUnknownKeys = true }
assertEquals(
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseShortChannelId),
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
)
}

@Test
fun `successful request via full`() {
val resultWriter = runTest(DummyEclairClient(), "full")
assertNull(resultWriter.lastError)
assertNotNull(resultWriter.lastResult)
val format = Json { ignoreUnknownKeys = true }
assertEquals(
format.decodeFromString(FindRouteResponse.serializer(), DummyEclairClient.validRouteResponseFull),
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!),
)
}
Expand All @@ -53,8 +75,24 @@ class FindRouteCommandTest {
}

@Test
fun `serialization error`() {
val resultWriter = runTest(DummyEclairClient(findrouteResponse = "{invalidJson}"))
fun `serialization error via nodeId`() {
val resultWriter = runTest(DummyEclairClient(findrouteResponseNodeId = "{invalidJson}"), "nodeId")
assertNull(resultWriter.lastResult)
assertNotNull(resultWriter.lastError)
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))
}

@Test
fun `serialization error via shortChannelId`() {
val resultWriter = runTest(DummyEclairClient(findrouteResponseShortChannelId = "{invalidJson}"), "shortChannelId")
assertNull(resultWriter.lastResult)
assertNotNull(resultWriter.lastError)
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))
}

@Test
fun `serialization error via full`() {
val resultWriter = runTest(DummyEclairClient(findrouteResponseFull = "{invalidJson}"), "full")
assertNull(resultWriter.lastResult)
assertNotNull(resultWriter.lastError)
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed"))
Expand Down
113 changes: 74 additions & 39 deletions src/nativeTest/kotlin/mocks/EclairClientMocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ class DummyEclairClient(
private val getinvoiceResponse: String = validGetInvoiceResponse,
private val listinvoicesResponse: String = validListInvoicesResponse,
private val listpendinginvoicesResponse: String = validListPendingInvoicesResponse,
private val findrouteResponse: String = validFindRouteResponse,
private val findroutetonodeResponse: String = validFindRouteToNodeResponse,
private val findroutebetweennodesResponse: String = validFindRouteBetweenNodesResponse,
private val findrouteResponseNodeId: String = validRouteResponseNodeId,
private val findrouteResponseShortChannelId: String = validRouteResponseShortChannelId,
private val findrouteResponseFull: String = validRouteResponseFull,
private val findroutetonodeResponseNodeId: String = validRouteResponseNodeId,
private val findroutetonodeResponseShortChannelId: String = validRouteResponseShortChannelId,
private val findroutetonodeResponseFull: String = validRouteResponseFull,
private val findroutebetweennodesResponseNodeId: String = validRouteResponseNodeId,
private val findroutebetweennodesResponseShortChannelId: String = validRouteResponseShortChannelId,
private val findroutebetweennodesResponseFull: String = validRouteResponseFull,
) : IEclairClient, IEclairClientBuilder {
override fun build(apiHost: String, apiPassword: String): IEclairClient = this
override suspend fun getInfo(): Either<ApiError, String> = Either.Right(getInfoResponse)
override suspend fun connect(target: ConnectionTarget): Either<ApiError, String> = Either.Right(validConnectResponse)
override suspend fun connect(target: ConnectionTarget): Either<ApiError, String> =
Either.Right(validConnectResponse)

override suspend fun rbfopen(
channelId: String,
targetFeerateSatByte: Int,
Expand Down Expand Up @@ -104,7 +112,8 @@ class DummyEclairClient(
paymentPreimage: String?
): Either<ApiError, String> = Either.Right(createInvoiceResponse)

override suspend fun deleteinvoice(paymentHash: String): Either<ApiError, String> = Either.Right(deleteInvoiceResponse)
override suspend fun deleteinvoice(paymentHash: String): Either<ApiError, String> =
Either.Right(deleteInvoiceResponse)

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

Expand Down Expand Up @@ -181,7 +190,14 @@ class DummyEclairClient(
maxFeeMsat: Int?,
includeLocalChannelCost: Boolean?,
pathFindingExperimentName: String?
): Either<ApiError, String> = Either.Right(findrouteResponse)
): Either<ApiError, String> {
return when (format) {
"nodeId" -> Either.Right(findrouteResponseNodeId)
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
"full" -> Either.Right(findrouteResponseFull)
else -> Either.Right(findrouteResponseNodeId)
}
}

override suspend fun findroutetonode(
nodeId: String,
Expand All @@ -192,7 +208,14 @@ class DummyEclairClient(
maxFeeMsat: Int?,
includeLocalChannelCost: Boolean?,
pathFindingExperimentName: String?
): Either<ApiError, String> = Either.Right(findroutetonodeResponse)
): Either<ApiError, String> {
return when (format) {
"nodeId" -> Either.Right(findrouteResponseNodeId)
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
"full" -> Either.Right(findrouteResponseFull)
else -> Either.Right(findrouteResponseNodeId)
}
}

override suspend fun findroutebetweennodes(
sourceNodeId: String,
Expand All @@ -204,7 +227,14 @@ class DummyEclairClient(
maxFeeMsat: Int?,
includeLocalChannelCost: Boolean?,
pathFindingExperimentName: String?
): Either<ApiError, String> = Either.Right(findroutebetweennodesResponse)
): Either<ApiError, String> {
return when (format) {
"nodeId" -> Either.Right(findrouteResponseNodeId)
"shortChannelId" -> Either.Right(findrouteResponseShortChannelId)
"full" -> Either.Right(findrouteResponseFull)
else -> Either.Right(findrouteResponseNodeId)
}
}

companion object {
val validGetInfoResponse =
Expand Down Expand Up @@ -422,7 +452,8 @@ class DummyEclairClient(
},
"routingInfo": []
}"""
val validDeleteInvoiceResponse = "deleted invoice 6f0864735283ca95eaf9c50ef77893f55ee3dd11cb90710cbbfb73f018798a68"
val validDeleteInvoiceResponse =
"deleted invoice 6f0864735283ca95eaf9c50ef77893f55ee3dd11cb90710cbbfb73f018798a68"
val validParseInvoiceResponse = """{
"prefix": "lnbcrt",
"timestamp": 1643718891,
Expand Down Expand Up @@ -793,7 +824,31 @@ class DummyEclairClient(
"routingInfo": []
}
]"""
val validFindRouteResponse = """{
val validRouteResponseNodeId = """{
"routes": [
{
"amount": 5000,
"nodeIds": [
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
]
}
]
}"""
val validRouteResponseShortChannelId = """{
"routes": [
{
"amount": 5000,
"shortChannelIds": [
"11203x1x0",
"11203x7x5",
"11205x3x3"
]
}
]
}"""
val validRouteResponseFull = """{
"type": "types.FindRouteResponse",
"routes": [
{
Expand Down Expand Up @@ -904,30 +959,6 @@ class DummyEclairClient(
]
}
"""
val validFindRouteToNodeResponse = """{
"routes": [
{
"amount": 5000,
"nodeIds": [
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
]
}
]
}"""
val validFindRouteBetweenNodesResponse = """{
"routes": [
{
"amount": 5000,
"nodeIds": [
"036d65409c41ab7380a43448f257809e7496b52bf92057c09c4f300cbd61c50d96",
"03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f",
"03d06758583bb5154774a6eb221b1276c9e82d65bbaceca806d90e20c108f4b1c7"
]
}
]
}"""
}
}

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

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

override suspend fun getreceivedinfo(paymentHash: String?, invoice: String?): Either<ApiError, String> = Either.Left(error)
override suspend fun getreceivedinfo(paymentHash: String?, invoice: String?): Either<ApiError, String> =
Either.Left(error)

override suspend fun listreceivedpayments(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
override suspend fun listreceivedpayments(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
Either.Left(error)

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

override suspend fun listinvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
override suspend fun listinvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
Either.Left(error)

override suspend fun listpendinginvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> = Either.Left(error)
override suspend fun listpendinginvoices(from: Int?, to: Int?, count: Int?, skip: Int?): Either<ApiError, String> =
Either.Left(error)

override suspend fun findroute(
invoice: String,
Expand Down

0 comments on commit 9f6f5fa

Please sign in to comment.