Merge pull request #3197 from davotoula/fix/relay-log-diagnostics

Make relay failure logs diagnosable (exception class on null message, correct NIP-11 error label)
This commit is contained in:
Vitor Pamplona
2026-06-12 13:42:31 -04:00
committed by GitHub
4 changed files with 192 additions and 7 deletions
@@ -0,0 +1,124 @@
/*
* 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.client.single.basic
import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.RelayConnectionListener
import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocket
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebSocketListener
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebsocketBuilder
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class BasicRelayClientTest {
private class FakeWebSocket : WebSocket {
override fun needsReconnect() = false
override fun connect() {}
override fun disconnect() {}
override fun send(msg: String) = true
}
private class FakeWebsocketBuilder : WebsocketBuilder {
var capturedListener: WebSocketListener? = null
override fun build(
url: NormalizedRelayUrl,
out: WebSocketListener,
): WebSocket {
capturedListener = out
return FakeWebSocket()
}
}
private class RecordingConnectionListener : RelayConnectionListener {
val cannotConnectMessages = mutableListOf<String>()
override fun onCannotConnect(
relay: IRelayClient,
errorMessage: String,
) {
cannotConnectMessages.add(errorMessage)
}
}
private class MessagelessException : Exception()
private data class Harness(
val socketListener: WebSocketListener,
val connectionListener: RecordingConnectionListener,
)
private fun connectAndCapture(): Harness {
val builder = FakeWebsocketBuilder()
val listener = RecordingConnectionListener()
val client =
BasicRelayClient(
NormalizedRelayUrl("wss://relay.example.com/"),
builder,
listener,
)
client.connect()
val socketListener = builder.capturedListener
assertNotNull(socketListener)
return Harness(socketListener, listener)
}
@Test
fun onFailureWithNullMessageReportsExceptionClassName() {
val (socket, listener) = connectAndCapture()
socket.onFailure(MessagelessException(), null, null)
assertEquals(
listOf("WebSocket Failure: MessagelessException"),
listener.cannotConnectMessages,
)
}
@Test
fun onFailureWithMessageKeepsExistingFormat() {
val (socket, listener) = connectAndCapture()
socket.onFailure(Exception("Connection reset"), null, null)
assertEquals(
listOf("WebSocket Failure: Connection reset"),
listener.cannotConnectMessages,
)
}
@Test
fun serverMisconfiguredWithNullMessageReportsExceptionClassName() {
val (socket, listener) = connectAndCapture()
socket.onFailure(MessagelessException(), 200, "OK")
assertEquals(
listOf("Server Misconfigured. Response: 200 OK. Exception: MessagelessException"),
listener.cannotConnectMessages,
)
}
}