[mock] Update tests

This commit is contained in:
Karlatemp 2023-02-15 00:36:47 +08:00
parent 4d9f6e88f9
commit 3ba53ee4c2
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59

View File

@ -9,11 +9,10 @@
package net.mamoe.mirai.mock.test.mock
import kotlinx.serialization.KSerializer
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.modules.plus
import kotlinx.serialization.serializer
import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.mock.test.MockBotTestBase
@ -32,7 +31,7 @@ internal class MessageSerializationTest : MockBotTestBase() {
get() = Json {
serializersModule = module
useArrayPolymorphism = false
ignoreUnknownKeys = true
ignoreUnknownKeys = false
}
private inline fun <reified T : Any> T.serialize(serializer: KSerializer<T> = module.serializer()): String {
@ -66,6 +65,13 @@ internal class MessageSerializationTest : MockBotTestBase() {
deserialized,
msg
)
if (t is SingleMessage) {
PolymorphicWrapperContent(t)
.serialize(PolymorphicWrapperContent.serializer())
.deserialize(PolymorphicWrapperContent.serializer())
.let { assert1(t, it.message, msg) }
}
}
private fun assert1(t: Any, deserialized: Any, msg: String) {
@ -126,4 +132,44 @@ internal class MessageSerializationTest : MockBotTestBase() {
}
}
@Serializable
data class PolymorphicWrapperImage(
val message: @Polymorphic Image
)
@Serializable
data class PolymorphicWrapperContent(
val message: @Polymorphic SingleMessage
)
@Test
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "UNRESOLVED_REFERENCE")
fun `test serialization for MockImage`() = runTest {
val img = this@MessageSerializationTest.bot.uploadMockImage(
Image.randomImageContent().toExternalResource().toAutoCloseable()
)
PolymorphicWrapperImage(img)
.serialize(PolymorphicWrapperImage.serializer())
.also { println(it) }
.deserialize(PolymorphicWrapperImage.serializer())
}
// https://github.com/mamoe/mirai/pull/2414#issuecomment-1386253123
@Test
fun `test 2414-1386253123`() = runTest {
// event
val img = this@MessageSerializationTest.bot.uploadMockImage(
Image.randomImageContent().toExternalResource().toAutoCloseable()
)
val msg = buildMessageChain {
add("imgUploaded")
add(img)
}
val s = format.encodeToString(msg)
println(s)
println(format.decodeFromString<MessageChain>(s))
testSerialization(msg)
}
}