Adds test and some refinements.

This commit is contained in:
Vitor Pamplona
2026-03-15 19:51:39 -04:00
parent 61089fa866
commit 0e476c8988
15 changed files with 128 additions and 28 deletions
@@ -90,6 +90,10 @@ object MessageKSerializer : KSerializer<Message> {
is CountMessage -> {
add(CountResultKSerializer.serializeToElement(value.result))
}
is EoseMessage -> {
add(JsonPrimitive(value.subId))
}
}
}
jsonEncoder.encodeJsonElement(element)
@@ -25,6 +25,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.IRelayClientLis
import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.AuthMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.ClosedMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.CountMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.EoseMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.EventMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.Message
@@ -62,6 +63,7 @@ class RelayLogger(
is OkMessage -> if (debugReceiving) Log.d(logTag, "OK: ${msg.eventId} ${msg.success} ${msg.message}")
is AuthMessage -> if (debugReceiving) Log.d(logTag, "Auth: ${msg.challenge}")
is NotifyMessage -> if (debugReceiving) Log.d(logTag, "Notify: ${msg.message}")
is CountMessage -> if (debugReceiving) Log.d(logTag, "Count: ${msg.result.count} approx: ${msg.result.approximate}")
is ClosedMessage -> Log.w(logTag, "Closed: ${msg.subId} ${msg.message}")
}
}
@@ -26,8 +26,8 @@ class CountResultDeserializer {
companion object {
fun fromJson(jsonObject: JsonNode): CountResult =
CountResult(
count = jsonObject.get("count").asInt(),
approximate = jsonObject.get("approximate").asBoolean(),
count = jsonObject.get("count")?.asInt() ?: 0,
approximate = jsonObject.get("approximate")?.asBoolean() ?: false,
)
}
}
@@ -72,8 +72,8 @@ class MessageSerializer : StdSerializer<Message>(Message::class.java) {
countSerializer.serialize(msg.result, gen, provider)
}
else -> {
null
is EoseMessage -> {
gen.writeString(msg.subId)
}
}
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip01Core.relay
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.queryCountSuspend
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.normalizeRelayUrl
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
class NostrClientQueryCountTest : BaseNostrClientTest() {
val fiatjaf = "wss://pyramid.fiatjaf.com".normalizeRelayUrl()
val utxo = "wss://news.utxo.one".normalizeRelayUrl()
val metadata = Filter(kinds = listOf(0))
val outboxRelays = Filter(kinds = listOf(10002))
@Test
fun testQueryCountSuspend() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result = client.queryCountSuspend(relay = fiatjaf, filter = metadata)
assertTrue((result?.count ?: 0) > 1)
client.disconnect()
appScope.cancel()
}
@Test
fun testQueryCountSuspendAllEvents() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result = client.queryCountSuspend(relay = fiatjaf, filter = Filter())
assertTrue((result?.count ?: 0) > 1)
client.disconnect()
appScope.cancel()
}
@Test
fun testQueryCountSuspendMultipleRelays() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result =
client.queryCountSuspend(
filters =
mapOf(
fiatjaf to listOf(metadata, outboxRelays),
utxo to listOf(metadata, outboxRelays),
),
)
result.forEach { url, result ->
println("${url.url}: ${result.count}")
assertTrue(result.count > 1)
}
client.disconnect()
appScope.cancel()
}
}