Skip to content

Commit 52c1c5c

Browse files
Test Coverage Added
1 parent 21a5067 commit 52c1c5c

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

client/src/test/kotlin/io/github/hosseinkarami_dev/near/rpc/client/ChangesTest.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ class ChangesTest {
5555
val result = response.getResultOrNull<RpcStateChangesInBlockResponse>()
5656
println("Changes Response: $result")
5757

58-
if (response !is RpcResponse.Success)
59-
assertTrue { false }
60-
61-
assertTrue { true }
58+
assertTrue { response is RpcResponse.Success }
6259
}
6360
}

client/src/test/kotlin/io/github/hosseinkarami_dev/near/rpc/client/SendTxTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ class SendTxTest {
4646
))
4747
val result = response.getResultOrNull<RpcTransactionResponse>()
4848
println("send Tx Response: $response")
49-
assertTrue { true }
49+
assertTrue { response is RpcResponse.Failure }
5050
}
5151
}

client/src/test/kotlin/io/github/hosseinkarami_dev/near/rpc/client/StatusTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
1111
import kotlin.test.AfterTest
1212
import kotlin.test.BeforeTest
1313
import kotlin.test.assertNotNull
14+
import kotlin.test.assertTrue
1415

1516
class StatusTest {
1617

@@ -40,7 +41,7 @@ class StatusTest {
4041
fun testStatus() = runTest {
4142
val response = nearClient.status()
4243
val result = response.getResultOrNull<RpcStatusResponse>()
43-
assertNotNull(result)
4444
println("Status response: $result")
45+
assertTrue { response is RpcResponse.Success }
4546
}
4647
}

client/src/test/kotlin/io/github/hosseinkarami_dev/near/rpc/client/TxStatusTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TxStatusTest {
4747
txHash = CryptoHash("5FfisT8c27W2vg3AqFTX5EVKve2xV5ZUHuzr2vxYM6c2"),
4848
))
4949
val result = response.getResultOrNull<RpcTransactionResponse>()
50-
println("Experimental Tx Status Response: $response")
50+
println("Experimental Tx Status Response: $result")
5151
assertTrue { response is RpcResponse.Success }
5252
}
5353
}

client/src/test/kotlin/io/github/hosseinkarami_dev/near/rpc/client/ValidatorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ class ValidatorTest {
4848
val response = nearClient.validators(RpcValidatorRequest.Latest)
4949
val result = response.getResultOrNull<RpcValidatorResponse>()
5050
println("Validator Response: $result")
51-
assertNotNull(result)
51+
assertNotNull(response is RpcResponse.Success)
5252
}
5353
}

generator/src/main/kotlin/io/github/hosseinkarami_dev/near/rpc/generator/SerializerGenerator.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,37 @@ object SerializerGenerator {
3939
val clsName = info.className
4040
val serializerName = "${clsName}Serializer"
4141

42-
// derive discriminator candidate field names from the sealed-info itself
42+
// --- smarter discriminator candidate selection ---
43+
// 1) If *every* variant contains a serialized property named "name" -> prioritize it
44+
// 2) Otherwise collect candidates among properties that are string-like, enum-like (Name / *Name) or simple token types
45+
// exclude obvious message field unless it's name-like
4346
val discCandidates: List<String> = run {
44-
val freq = mutableMapOf<String, Int>()
4547
val nVariants = info.variants.size
48+
49+
val hasNameFieldInAll = info.variants.all { v -> v.props.any { it.serialName == "name" } }
50+
if (hasNameFieldInAll) return@run listOf("name")
51+
52+
val freq = mutableMapOf<String, Int>()
53+
val stringTypes = setOf("String", "kotlin.String")
54+
4655
for (v in info.variants) {
4756
for (p in v.props) {
48-
val t = sanitizeType(p.type)
49-
// consider only string-like properties as possible discriminators
50-
if (t.equals("String", ignoreCase = true) || t.equals("kotlin.String", ignoreCase = true)) {
57+
val raw = sanitizeType(p.type)
58+
59+
val isString = stringTypes.any { it.equals(raw, ignoreCase = true) }
60+
val isNameLike = raw.equals("Name", ignoreCase = true) || raw.endsWith("Name")
61+
val isSimpleToken = raw.matches(Regex("^[A-Za-z_][A-Za-z0-9_]*$")) && !raw.contains('.') && !raw.contains('<') && raw.length <= 60
62+
63+
if (isString || isNameLike || isSimpleToken) {
64+
// avoid picking generic "message" text as discriminator unless it actually looks name-like
65+
if (p.serialName.equals("message", ignoreCase = true) && !isNameLike) continue
5166
freq[p.serialName] = (freq[p.serialName] ?: 0) + 1
5267
}
5368
}
5469
}
70+
5571
if (freq.isEmpty()) emptyList()
5672
else {
57-
// threshold: appear in at least half of variants
5873
val threshold = (nVariants + 1) / 2
5974
freq.filter { it.value >= threshold }.keys.toList()
6075
}
@@ -549,4 +564,4 @@ object SerializerGenerator {
549564
if (s.startsWith("`") && s.endsWith("`") && s.length > 1) s = s.substring(1, s.length - 1)
550565
return s.trim()
551566
}
552-
}
567+
}

0 commit comments

Comments
 (0)