From c93d6aa608e7dc9238069b3b36dd8cc4b67a4ee7 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 30 May 2021 22:51:26 +0800 Subject: [PATCH] Add ForwardRefineTest --- .../kotlin/message/LongMessageInternal.kt | 4 + .../kotlin/message/RefinableMessage.kt | 2 +- .../kotlin/message/data/ForwardRefineTest.kt | 75 +++++++++++++++++++ .../kotlin/message/data/MessageRefineTest.kt | 6 -- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 mirai-core/src/jvmTest/kotlin/message/data/ForwardRefineTest.kt diff --git a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt index d67c45506..54c9fe5e9 100644 --- a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt +++ b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt @@ -38,6 +38,10 @@ internal data class LongMessageInternal internal constructor(override val conten internal data class ForwardMessageInternal( override val content: String, val resId: String?, + /** + * null means top-level. + * not null means nested and need [ForwardMessageInternal.MsgTransmits] in [RefineContext] + */ val fileName: String?, ) : AbstractServiceMessage(), RefinableMessage { diff --git a/mirai-core/src/commonMain/kotlin/message/RefinableMessage.kt b/mirai-core/src/commonMain/kotlin/message/RefinableMessage.kt index 44d269dcd..5851f665f 100644 --- a/mirai-core/src/commonMain/kotlin/message/RefinableMessage.kt +++ b/mirai-core/src/commonMain/kotlin/message/RefinableMessage.kt @@ -121,7 +121,7 @@ internal object EmptyRefineContext : RefineContext { @Suppress("UNCHECKED_CAST") internal class SimpleRefineContext( - private val delegate: MutableMap, Any> + private val delegate: MutableMap, Any> = mutableMapOf() ) : MutableRefineContext { override fun contains(key: RefineContextKey<*>): Boolean = delegate.containsKey(key) diff --git a/mirai-core/src/jvmTest/kotlin/message/data/ForwardRefineTest.kt b/mirai-core/src/jvmTest/kotlin/message/data/ForwardRefineTest.kt new file mode 100644 index 000000000..a1679b0e4 --- /dev/null +++ b/mirai-core/src/jvmTest/kotlin/message/data/ForwardRefineTest.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.internal.message.data + +import net.mamoe.mirai.Bot +import net.mamoe.mirai.internal.AbstractTestWithMiraiImpl +import net.mamoe.mirai.internal.MockBot +import net.mamoe.mirai.internal.message.DeepMessageRefiner.refineDeep +import net.mamoe.mirai.internal.message.ForwardMessageInternal +import net.mamoe.mirai.internal.message.SimpleRefineContext +import net.mamoe.mirai.internal.test.runBlockingUnit +import net.mamoe.mirai.internal.utils._miraiContentToString +import net.mamoe.mirai.message.data.* +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +internal class ForwardRefineTest : AbstractTestWithMiraiImpl() { + private val bot = MockBot() + + companion object { + private const val content = """ + + 群聊的聊天记录 A:@B ‘ A:@B ’
查看转发消息
+ """ + private const val resId = "ZnAsFPOuieB0CJt7HJ6zC1Rq0MRqfso44OAvY99wqePeYYcGr03NI6UXB24QqefR" + private val nodes = listOf(ForwardMessage.Node(1, 1, "sender", AtAll)) + } + + override suspend fun downloadForwardMessage(bot: Bot, resourceId: String): List { + assertEquals(resId, resourceId) + return nodes + } + + @Test + fun `can refine ForwardMessageInternal deep`() = runBlockingUnit { + val internal = ForwardMessageInternal(content, resId, null) + val expectedOrigin = + SimpleServiceMessage(internal.serviceId, content) // we do not expose ForwardMessageInternal + val refine = internal.toMessageChain().refineDeep(bot, SimpleRefineContext().apply { + set(ForwardMessageInternal.MsgTransmits, mapOf()) + }) + println(refine.size) + println(refine.first()::class) + println(refine._miraiContentToString()) + assertTrue { refine.first() is MessageOrigin } + assertTrue { refine.drop(1).first() is ForwardMessage } + assertEquals( + ForwardMessage( + preview = listOf("A:@B ‘", "A:@B ’"), + title = "群聊的聊天记录", + brief = "[聊天记录]", + source = "聊天记录", + summary = "查看转发消息", + nodeList = nodes + ), + refine[ForwardMessage] + ) + assertEquals( + expectedOrigin, + refine[MessageOrigin]!!.origin + ) + assertEquals( + MessageOrigin(expectedOrigin, resId, MessageOriginKind.FORWARD), + refine[MessageOrigin] + ) + } +} \ No newline at end of file diff --git a/mirai-core/src/jvmTest/kotlin/message/data/MessageRefineTest.kt b/mirai-core/src/jvmTest/kotlin/message/data/MessageRefineTest.kt index 9d037cb27..914cd64c3 100644 --- a/mirai-core/src/jvmTest/kotlin/message/data/MessageRefineTest.kt +++ b/mirai-core/src/jvmTest/kotlin/message/data/MessageRefineTest.kt @@ -96,12 +96,6 @@ private fun RefinableMessage0( } } -private object MiraiImplForRefineTest : MiraiImpl() { - override suspend fun downloadForwardMessage(bot: Bot, resourceId: String): List { - return super.downloadForwardMessage(bot, resourceId) - } -} - internal class MessageRefineTest : AbstractTestWithMiraiImpl() { @Test