|
| 1 | +package love.forte.simbot.component.onebot.v11.core |
| 2 | + |
| 3 | +import kotlinx.coroutines.test.runTest |
| 4 | +import love.forte.simbot.common.id.IntID.Companion.ID |
| 5 | +import love.forte.simbot.component.onebot.v11.core.api.OneBotMessageOutgoing |
| 6 | +import love.forte.simbot.component.onebot.v11.core.api.SendMsgApi |
| 7 | +import love.forte.simbot.component.onebot.v11.core.utils.sendMsgApi |
| 8 | +import love.forte.simbot.component.onebot.v11.core.utils.sendTextMsgApi |
| 9 | +import love.forte.simbot.component.onebot.v11.message.resolveToOneBotSegmentList |
| 10 | +import love.forte.simbot.component.onebot.v11.message.segment.OneBotFace |
| 11 | +import love.forte.simbot.component.onebot.v11.message.segment.OneBotReply |
| 12 | +import love.forte.simbot.component.onebot.v11.message.segment.OneBotText |
| 13 | +import love.forte.simbot.message.Face |
| 14 | +import love.forte.simbot.message.MessageIdReference |
| 15 | +import love.forte.simbot.message.buildMessages |
| 16 | +import kotlin.collections.first |
| 17 | +import kotlin.test.Test |
| 18 | +import kotlin.test.assertEquals |
| 19 | +import kotlin.test.assertIs |
| 20 | +import kotlin.test.assertNotNull |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for `OneBotReply` and `MessageReference` for send. |
| 24 | + * |
| 25 | + * @author ForteScarlet |
| 26 | + */ |
| 27 | +class ReplyAndMessageReferenceTests { |
| 28 | + @Test |
| 29 | + fun sendReplyWithSingleSegmentTest() { |
| 30 | + val api = sendMsgApi( |
| 31 | + SendMsgApi.MESSAGE_TYPE_GROUP, |
| 32 | + target = 123.ID, |
| 33 | + message = listOf(OneBotText.create("Hello")), |
| 34 | + reply = 456.ID, |
| 35 | + ) |
| 36 | + |
| 37 | + val body = assertIs<SendMsgApi.Body>(api.body) |
| 38 | + val content = assertIs<OneBotMessageOutgoing.SegmentsValue>(body.message) |
| 39 | + val firstReply = assertIs<OneBotReply>(content.segments.first()) |
| 40 | + |
| 41 | + assertEquals(456.ID, firstReply.id) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun sendReplyWithSegmentsTest() { |
| 46 | + val api = sendMsgApi( |
| 47 | + SendMsgApi.MESSAGE_TYPE_GROUP, |
| 48 | + target = 123.ID, |
| 49 | + message = listOf(OneBotText.create("Hello"), OneBotText.create("Hello"), OneBotFace.create(999.ID)), |
| 50 | + reply = 456.ID, |
| 51 | + ) |
| 52 | + |
| 53 | + val body = assertIs<SendMsgApi.Body>(api.body) |
| 54 | + val content = assertIs<OneBotMessageOutgoing.SegmentsValue>(body.message) |
| 55 | + val firstReply = assertIs<OneBotReply>(content.segments.first()) |
| 56 | + |
| 57 | + assertEquals(456.ID, firstReply.id) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun sendReplyWithTextTest() { |
| 62 | + val api = sendTextMsgApi( |
| 63 | + SendMsgApi.MESSAGE_TYPE_GROUP, |
| 64 | + target = 123.ID, |
| 65 | + text = "666", |
| 66 | + reply = 456.ID, |
| 67 | + ) |
| 68 | + |
| 69 | + val body = assertIs<SendMsgApi.Body>(api.body) |
| 70 | + val content = assertIs<OneBotMessageOutgoing.SegmentsValue>(body.message) |
| 71 | + val firstReply = assertIs<OneBotReply>(content.segments.first()) |
| 72 | + |
| 73 | + assertEquals(456.ID, firstReply.id) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun messagesToSegmentsWithMessageReferenceTest() = runTest { |
| 78 | + val messages = buildMessages { |
| 79 | + +"Hello" |
| 80 | + +MessageIdReference(123.ID) |
| 81 | + +Face(111.ID) |
| 82 | + } |
| 83 | + |
| 84 | + val segments = messages.resolveToOneBotSegmentList(null) |
| 85 | + assertEquals(3, segments.size) |
| 86 | + val reply = assertNotNull( |
| 87 | + segments.firstNotNullOfOrNull { it as? OneBotReply } |
| 88 | + ) |
| 89 | + |
| 90 | + assertEquals(123.ID, reply.id) |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments