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
@@ -89,7 +89,7 @@ abstract class BasicRelaySetupInfoModel : ViewModel() {
private fun loadCounts() {
_countResults.value = emptyMap()
val client = Amethyst.instance.client
val client = account.client
val relayList = _relays.value
if (relayList.isEmpty()) return
@@ -69,9 +69,9 @@ fun RelayEventCountRow(
val countText =
if (entry.approximate) {
"~${countToHumanReadable(entry.count, entry.label)}"
"~${countToHumanReadable(entry.count, stringRes(entry.label))}"
} else {
countToHumanReadable(entry.count, entry.label)
countToHumanReadable(entry.count, stringRes(entry.label))
}
Row(
@@ -29,13 +29,13 @@ data class RelayCountResult(
) {
@Immutable
data class CountEntry(
val label: String,
val label: Int,
val count: Int,
val approximate: Boolean = false,
)
}
data class CountFilter(
val label: String,
val label: Int,
val filter: Filter,
)
@@ -21,6 +21,7 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.dm
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
@@ -39,7 +40,7 @@ class DMRelayListViewModel : BasicRelaySetupInfoModel() {
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = "events",
label = R.string.dms,
filter =
Filter(
kinds = listOf(GiftWrapEvent.KIND, PrivateDmEvent.KIND),
@@ -21,10 +21,13 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.indexer
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent
@Stable
class IndexerRelayListViewModel : BasicRelaySetupInfoModel() {
@@ -39,12 +42,12 @@ class IndexerRelayListViewModel : BasicRelaySetupInfoModel() {
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = "kind 0",
filter = Filter(kinds = listOf(0)),
label = R.string.profiles,
filter = Filter(kinds = listOf(MetadataEvent.KIND)),
),
CountFilter(
label = "kind 10002",
filter = Filter(kinds = listOf(10002)),
label = R.string.relay_settings_lower,
filter = Filter(kinds = listOf(AdvertisedRelayListEvent.KIND)),
),
)
}
@@ -21,6 +21,7 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.nip37
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
@@ -39,7 +40,7 @@ class PrivateOutboxRelayListViewModel : BasicRelaySetupInfoModel() {
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = "events",
label = R.string.events,
filter = Filter(authors = listOf(account.pubKey)),
),
)
@@ -24,6 +24,7 @@ import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.service.replace
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
@@ -136,7 +137,7 @@ class Nip65RelayListViewModel : ViewModel() {
RelayCountResult(
listOf(
RelayCountResult.CountEntry(
label = "events",
label = R.string.events,
count = result.count,
approximate = result.approximate,
),
@@ -155,7 +156,7 @@ class Nip65RelayListViewModel : ViewModel() {
RelayCountResult(
listOf(
RelayCountResult.CountEntry(
label = "events",
label = R.string.events,
count = result.count,
approximate = result.approximate,
),
@@ -22,8 +22,6 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.proxy
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Stable
@@ -35,12 +33,4 @@ class ProxyRelayListViewModel : BasicRelaySetupInfoModel() {
override suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>) {
account.saveProxyRelayList(urlList)
}
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = "events",
filter = Filter(),
),
)
}
@@ -21,6 +21,7 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.search
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
@@ -39,7 +40,7 @@ class SearchRelayListViewModel : BasicRelaySetupInfoModel() {
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = "events",
label = R.string.events,
filter = Filter(),
),
)
+4
View File
@@ -1802,4 +1802,8 @@
<string name="uptime">%1$d%% uptime</string>
<string name="namecoin_settings">Namecoin Settings</string>
<string name="ots_explorer_settings">Bitcoin Explorer (OTS)</string>
<string name="events">events</string>
<string name="dms">DMs</string>
<string name="profiles">profiles</string>
<string name="relay_settings_lower">relay settings</string>
</resources>
@@ -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()
}
}