change from runBlocking to runTest where appropriate

This commit is contained in:
davotoula
2025-10-14 20:34:44 +02:00
parent 50adf82d90
commit 5d6e7d1921
9 changed files with 294 additions and 310 deletions

View File

@@ -28,7 +28,7 @@ import io.mockk.impl.annotations.SpyK
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
@@ -54,7 +54,7 @@ class SplitterTest {
@Test
fun testSplit() =
runBlocking {
runTest {
val vitor = mySplitBuilder.addItem("Vitor")
assertEquals(1f, mySplitBuilder.items[vitor].percentage, 0.01f)

View File

@@ -117,6 +117,7 @@ kotlin {
commonTest {
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotlinx.coroutines.test)
}
}
@@ -148,6 +149,7 @@ kotlin {
dependsOn(commonTest.get())
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotlinx.coroutines.test)
}
}
@@ -201,6 +203,7 @@ kotlin {
implementation(libs.androidx.core)
implementation(libs.androidx.junit)
implementation(libs.androidx.espresso.core)
implementation(libs.kotlinx.coroutines.test)
}
}

View File

@@ -31,7 +31,7 @@ import com.vitorpamplona.quartz.nip19Bech32.entities.NEmbed
import com.vitorpamplona.quartz.utils.Hex
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -39,18 +39,17 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class NIP19EmbedTests {
@Test
fun testEmbedKind1Event() {
fun testEmbedKind1Event() =
runTest {
val signer =
NostrSignerInternal(
KeyPair(Hex.decode("e8e7197ccc53c9ed4cf9b1c8dce085475fa1ffdd71f2c14e44fe23d0bdf77598")),
)
val textNote: TextNoteEvent =
runBlocking {
val textNote =
signer.sign(
TextNoteEvent.build("I like this. It could solve the ninvite problem in #1062, and it seems like it could be applied very broadly to limit the spread of events that shouldn't stand on their own or need to be private. The one question I have is how long are these embeds? If it's 50 lines of text, that breaks the human readable (or at least parseable) requirement of kind 1s. Also, encoding json in a tlv is silly, we should at least use the tlv to reduce the payload size."),
)
}
assertNotNull(textNote)
@@ -66,16 +65,14 @@ class NIP19EmbedTests {
}
@Test
fun testVisionPrescriptionEmbedEvent() {
fun testVisionPrescriptionEmbedEvent() =
runTest {
val signer =
NostrSignerInternal(
KeyPair(Hex.decode("e8e7197ccc53c9ed4cf9b1c8dce085475fa1ffdd71f2c14e44fe23d0bdf77598")),
)
val eyeglassesPrescriptionEvent =
runBlocking {
signer.sign(FhirResourceEvent.build(visionPrescriptionFhir))
}
val eyeglassesPrescriptionEvent = signer.sign(FhirResourceEvent.build(visionPrescriptionFhir))
assertNotNull(eyeglassesPrescriptionEvent)
@@ -92,16 +89,14 @@ class NIP19EmbedTests {
}
@Test
fun testVisionPrescriptionBundleEmbedEvent() {
fun testVisionPrescriptionBundleEmbedEvent() =
runTest {
val signer =
NostrSignerInternal(
KeyPair(Hex.decode("e8e7197ccc53c9ed4cf9b1c8dce085475fa1ffdd71f2c14e44fe23d0bdf77598")),
)
val eyeglassesPrescriptionEvent =
runBlocking {
signer.sign(FhirResourceEvent.build(visionPrescriptionBundle))
}
val eyeglassesPrescriptionEvent = signer.sign(FhirResourceEvent.build(visionPrescriptionBundle))
assertNotNull(eyeglassesPrescriptionEvent)
@@ -118,16 +113,14 @@ class NIP19EmbedTests {
}
@Test
fun testVisionPrescriptionBundle2EmbedEvent() {
fun testVisionPrescriptionBundle2EmbedEvent() =
runTest {
val signer =
NostrSignerInternal(
KeyPair(decodePrivateKeyAsHexOrNull("nsec1arn3jlxv20y76n8ek8ydecy9ga06rl7aw8evznjylc3ap00hwkvqx4vvy6")!!.hexToByteArray()),
)
val eyeglassesPrescriptionEvent =
runBlocking {
signer.sign(FhirResourceEvent.build(visionPrescriptionBundle2))
}
val eyeglassesPrescriptionEvent = signer.sign(FhirResourceEvent.build(visionPrescriptionBundle2))
assertNotNull(eyeglassesPrescriptionEvent)

View File

@@ -27,7 +27,7 @@ import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Ignore
import org.junit.Test
@@ -65,7 +65,7 @@ internal class Nip46Test {
@Test
fun signEncoder() =
runBlocking {
runTest {
val expected = BunkerRequestSign(event = dummyEvent)
val actual = encodeDecodeEvent(expected)
@@ -76,7 +76,7 @@ internal class Nip46Test {
@Test
fun connectEncoder() =
runBlocking {
runTest {
val expected = BunkerRequestConnect(remoteKey = remoteKey.pubKey)
val actual = encodeDecodeEvent(expected)
@@ -86,7 +86,7 @@ internal class Nip46Test {
@Test
fun pingEncoder() =
runBlocking {
runTest {
val expected = BunkerRequestPing()
val actual = encodeDecodeEvent(expected)
@@ -96,7 +96,7 @@ internal class Nip46Test {
@Test
fun getPubkeyEncoder() =
runBlocking {
runTest {
val expected = BunkerRequestGetPublicKey()
val actual = encodeDecodeEvent(expected)
@@ -106,7 +106,7 @@ internal class Nip46Test {
@Test
fun getRelaysEncoder() =
runBlocking {
runTest {
val expected = BunkerRequestGetRelays()
val actual = encodeDecodeEvent(expected)
@@ -116,7 +116,7 @@ internal class Nip46Test {
@Test
fun testNip04Encrypt() =
runBlocking {
runTest {
val expected = BunkerRequestNip04Encrypt(pubKey = peer.pubKey, message = "Test")
val actual = encodeDecodeEvent(expected)
@@ -128,7 +128,7 @@ internal class Nip46Test {
@Test
fun testNip44Encrypt() =
runBlocking {
runTest {
val expected = BunkerRequestNip44Encrypt(pubKey = peer.pubKey, message = "Test")
val actual = encodeDecodeEvent(expected)
@@ -140,7 +140,7 @@ internal class Nip46Test {
@Test
fun testNip04Decrypt() =
runBlocking {
runTest {
val expected = BunkerRequestNip04Decrypt(pubKey = peer.pubKey, ciphertext = "Test")
val actual = encodeDecodeEvent(expected)
@@ -152,7 +152,7 @@ internal class Nip46Test {
@Test
fun testNip44Decrypt() =
runBlocking {
runTest {
val expected = BunkerRequestNip44Decrypt(pubKey = peer.pubKey, ciphertext = "Test")
val actual = encodeDecodeEvent(expected)
@@ -166,7 +166,7 @@ internal class Nip46Test {
@Test
fun testAckResponse() =
runBlocking {
runTest {
val expected = BunkerResponseAck()
val actual = encodeDecodeEvent(expected)
@@ -177,7 +177,7 @@ internal class Nip46Test {
@Test
fun testPongResponse() =
runBlocking {
runTest {
val expected = BunkerResponsePong()
val actual = encodeDecodeEvent(expected)
@@ -188,7 +188,7 @@ internal class Nip46Test {
@Test
fun testErrorResponse() =
runBlocking {
runTest {
val expected = BunkerResponseError(error = "Error")
val actual = encodeDecodeEvent(expected)
@@ -199,7 +199,7 @@ internal class Nip46Test {
@Test
fun testEventResponse() =
runBlocking {
runTest {
val expected = BunkerResponseEvent(event = dummyEventSigned)
val actual = encodeDecodeEvent(expected)
@@ -211,7 +211,7 @@ internal class Nip46Test {
@Test
fun testPubkeyResponse() =
runBlocking {
runTest {
val expected = BunkerResponsePublicKey(pubkey = peer.pubKey)
val actual = encodeDecodeEvent(expected)
@@ -223,7 +223,7 @@ internal class Nip46Test {
@Test
fun testRelaysResponse() =
runBlocking {
runTest {
val expected = BunkerResponseGetRelays(relays = mapOf("url" to ReadWrite(true, false)))
val actual = encodeDecodeEvent(expected)
@@ -236,7 +236,7 @@ internal class Nip46Test {
@Test
@Ignore("Impossible to recreate the class since there are no hints on the json")
fun testDecryptResponse() =
runBlocking {
runTest {
val expected = BunkerResponseDecrypt(plaintext = "Test")
val actual = encodeDecodeEvent(expected)
@@ -249,7 +249,7 @@ internal class Nip46Test {
@Test
@Ignore("Impossible to recreate the class since there are no hints on the json")
fun testEncryptResponse() =
runBlocking {
runTest {
val expected = BunkerResponseEncrypt(ciphertext = "Test")
val actual = encodeDecodeEvent(expected)

View File

@@ -31,14 +31,15 @@ import com.vitorpamplona.quartz.nip57Zaps.PrivateZapEncryption.Companion.createE
import com.vitorpamplona.quartz.utils.Hex
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.fail
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class PrivateZapTests {
@Test
fun testPollZap() {
fun testPollZap() =
runTest {
val poll =
OptimizedJsonMapper.fromJson(
"""{
@@ -91,7 +92,6 @@ class PrivateZapTests {
)
val privateZapRequest =
runBlocking {
LnZapRequestEvent.create(
zappedEvent = poll,
relays = setOf(RelayUrlNormalizer.normalize("wss://relay.damus.io/")),
@@ -101,7 +101,6 @@ class PrivateZapTests {
zapType = LnZapEvent.ZapType.PRIVATE,
toUserPubHex = null,
)
}
val recepientPK = privateZapRequest.zappedAuthor().firstOrNull()
val recepientPost = privateZapRequest.zappedPost().firstOrNull()
@@ -122,7 +121,8 @@ class PrivateZapTests {
}
@Test
fun testKind1PrivateZap() {
fun testKind1PrivateZap() =
runTest {
val textNote =
Event.fromJson(
"""{
@@ -151,7 +151,6 @@ class PrivateZapTests {
)
val privateZapRequest =
runBlocking {
LnZapRequestEvent.create(
zappedEvent = textNote,
relays =
@@ -166,7 +165,6 @@ class PrivateZapTests {
zapType = LnZapEvent.ZapType.PRIVATE,
toUserPubHex = null,
)
}
val recepientPK = privateZapRequest.zappedAuthor().firstOrNull()
val recepientPost = privateZapRequest.zappedPost().firstOrNull()

View File

@@ -34,7 +34,7 @@ import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
import com.vitorpamplona.quartz.utils.Hex
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
@@ -47,7 +47,7 @@ import org.junit.runner.RunWith
class GiftWrapEventTest {
@Test()
fun testNip17Utils() =
runBlocking {
runTest {
val sender = NostrSignerInternal(KeyPair())
val receiver = NostrSignerInternal(KeyPair())
val message = "Hola, que tal?"
@@ -88,7 +88,7 @@ class GiftWrapEventTest {
@Test()
fun testNip17UtilsForGroups() =
runBlocking {
runTest {
val sender = NostrSignerInternal(KeyPair())
val receiver1 = NostrSignerInternal(KeyPair())
val receiver2 = NostrSignerInternal(KeyPair())
@@ -142,7 +142,7 @@ class GiftWrapEventTest {
@Test()
fun testInternalsSimpleMessage() =
runBlocking {
runTest {
val sender = NostrSignerInternal(KeyPair())
val receiver = NostrSignerInternal(KeyPair())
@@ -259,7 +259,7 @@ class GiftWrapEventTest {
@Test()
fun testInternalsGroupMessage() =
runBlocking {
runTest {
val sender = NostrSignerInternal(KeyPair())
val receiverA = NostrSignerInternal(KeyPair())
val receiverB = NostrSignerInternal(KeyPair())
@@ -446,7 +446,8 @@ class GiftWrapEventTest {
}
@Test
fun testCaseFromAmethyst1() {
fun testCaseFromAmethyst1() =
runTest {
val json =
"""
{
@@ -465,18 +466,16 @@ class GiftWrapEventTest {
}
""".trimIndent()
val rumor: Event =
runBlocking {
val privateKey = "de6152a85a0dea3b09a08a6f8139a314d498a7b52f7e5c28858b64270abd4c70"
unwrapUnsealRumor(json, privateKey)
}
val rumor = unwrapUnsealRumor(json, privateKey)
assertNotNull(rumor)
assertEquals("Hola, que tal?", rumor.content)
}
@Test
fun testCaseFromAmethyst2() {
fun testCaseFromAmethyst2() =
runTest {
val json =
"""
{
@@ -497,17 +496,15 @@ class GiftWrapEventTest {
val privateKey = "409ff7654141eaa16cd2161fe5bd127aeaef71f270c67587474b78998a8e3533"
val rumor: Event =
runBlocking {
unwrapUnsealRumor(json, privateKey)
}
val rumor = unwrapUnsealRumor(json, privateKey)
assertNotNull(rumor)
assertEquals("Hola, que tal?", rumor.content)
}
@Test
fun testDecryptFromCoracle() {
fun testDecryptFromCoracle() =
runTest {
val json =
"""
{
@@ -531,17 +528,15 @@ class GiftWrapEventTest {
val privateKey = "09e0051fdf5fdd9dd7a54713583006442cbdbf87bdcdab1a402f26e527d56771"
val rumor: Event =
runBlocking {
unwrapUnsealRumor(json, privateKey)
}
val rumor: Event = unwrapUnsealRumor(json, privateKey)
assertNotNull(rumor)
assertEquals("test", rumor.content)
}
@Test
fun testFromCoracle2() {
fun testFromCoracle2() =
runTest {
val json =
"""
{
@@ -562,10 +557,7 @@ class GiftWrapEventTest {
val privateKey = "09e0051fdf5fdd9dd7a54713583006442cbdbf87bdcdab1a402f26e527d56771"
val rumor =
runBlocking {
unwrapUnsealRumor(json, privateKey)
}
val rumor = unwrapUnsealRumor(json, privateKey)
assertEquals("asdfasdfasdf", rumor.content)
assertEquals(1690659269L, rumor.createdAt)
@@ -576,7 +568,8 @@ class GiftWrapEventTest {
}
@Test
fun testFromCoracle3() {
fun testFromCoracle3() =
runTest {
val json =
"""
{
@@ -597,10 +590,7 @@ class GiftWrapEventTest {
val privateKey = "7dd22cafc512c0bc363a259f6dcda515b13ae3351066d7976fd0bb79cbd0d700"
val rumor =
runBlocking {
unwrapUnsealRumor(json, privateKey)
}
val rumor = unwrapUnsealRumor(json, privateKey)
assertEquals("8d1a56008d4e31dae2fb8bef36b3efea519eff75f57033107e2aa16702466ef2", rumor.id)
assertEquals("Howdy", rumor.content)
@@ -659,7 +649,7 @@ class GiftWrapEventTest {
wrap.checkSignature()
val event =
runBlocking {
runTest {
wrap.unwrapThrowing(receiversPrivateKey)
}

View File

@@ -20,7 +20,7 @@
*/
package com.vitorpamplona.quartz.nip05DnsIdentifiers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@@ -36,7 +36,7 @@ class Nip05Test {
@Test
fun `test with matching case on user name`() =
runBlocking {
runTest {
// Set-up
val userNameToTest = ALL_UPPER_CASE_USER_NAME
val expectedPubKey = "ca29c211f1c72d5b6622268ff43d2288ea2b2cb5b9aa196ff9f1704fc914b71b"
@@ -51,7 +51,7 @@ class Nip05Test {
@Test
fun `test with NOT matching case on user name`() =
runBlocking {
runTest {
// Set-up
val expectedPubKey = "ca29c211f1c72d5b6622268ff43d2288ea2b2cb5b9aa196ff9f1704fc914b71b"
val nostrJson = "{ \"names\": { \"$ALL_UPPER_CASE_USER_NAME\": \"$expectedPubKey\" }}"

View File

@@ -23,7 +23,7 @@ package com.vitorpamplona.quartz.nip51Lists.bookmarkList
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip51Lists.bookmarkList.tags.EventBookmark
import com.vitorpamplona.quartz.utils.nsecToKeyPair
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@@ -33,7 +33,7 @@ class BookmarkListEventTest {
@Test
fun publicTagsPreservedWhenRemovingFromPrivateBookmarks() =
runBlocking {
runTest {
// Create a test event bookmark
val testEventId = "a".repeat(64)
val testBookmark = EventBookmark(testEventId)
@@ -84,7 +84,7 @@ class BookmarkListEventTest {
@Test
fun publicTagsRemovedWhenRemovingFromPublicBookmarks() =
runBlocking {
runTest {
// Create a test event bookmark
val testEventId = "b".repeat(64)
val testBookmark = EventBookmark(testEventId)

View File

@@ -30,14 +30,14 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
class NostrClientSendAndWaitTest : BaseNostrClientTest() {
@Test
fun testSendAndWaitForResponse() =
runBlocking {
runTest {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)