Skip to content

Commit

Permalink
[core] adapt test
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Aug 30, 2023
1 parent 272fe60 commit a01035a
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ internal class FriendNoticeProcessor(
}

override suspend fun NoticePipelineContext.processImpl(data: Structmsg.StructMsg) {
val systemMsg = data.msg ?: return
if (attributes[NewContact.SYSTEM_MSG_TYPE] != 0) return
markAsConsumed()

if (data.msgTime <= bot.syncController.latestMsgNewFriendTime) return
if (!bot.syncController.syncNewFriend(data.msgSeq, data.msgTime)) return // duplicate

val systemMsg = data.msg ?: return

collected += NewFriendRequestEvent(
bot,
data.msgSeq,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import net.mamoe.mirai.utils.*
* To add breakpoint, see [NoticeProcessorPipelineImpl.process]
*/
internal abstract class AbstractNoticeProcessorTest : AbstractCommonNHTest(), GroupExtensions {
var mockTime = currentTimeSeconds()

init {
setSystemProp("mirai.network.notice.pipeline.log.full", "true")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.contact.GroupImpl
import net.mamoe.mirai.internal.network.components.NoticeProcessorPipelineImpl
import net.mamoe.mirai.internal.network.protocol.data.proto.Structmsg
import net.mamoe.mirai.internal.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.internal.test.runBlockingUnit
import net.mamoe.mirai.utils.buildTypeSafeMap
import net.mamoe.mirai.utils.currentTimeSeconds
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

internal class BotInvitedJoinTest : AbstractNoticeProcessorTest() {
@Test
fun `invited join`() = runBlockingUnit {
suspend fun runTest() = use {

suspend fun runTest() = use(
attributes = buildTypeSafeMap { set(NewContact.SYSTEM_MSG_TYPE, 1) }
) {
mockTime += 1000
Structmsg.StructMsg(
version = 1,
msgType = 2,
msgSeq = 1630,
msgTime = 1630,
msgSeq = mockTime * 1000,
msgTime = mockTime,
reqUin = 1230,
msg = Structmsg.SystemMsg(
subType = 1,
Expand Down Expand Up @@ -81,6 +86,7 @@ internal class BotInvitedJoinTest : AbstractNoticeProcessorTest() {
c2cInviteJoinGroupFlag = 1,
),
)

}

setBot(1230003)
Expand All @@ -93,7 +99,7 @@ internal class BotInvitedJoinTest : AbstractNoticeProcessorTest() {
assertEquals("user1", invitorNick)
assertEquals(2230203, groupId)
assertEquals("testtest", groupName)
assertEquals(1630, eventId)
assertEquals(mockTime * 1000, eventId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.events.MemberJoinEvent
import net.mamoe.mirai.event.events.MemberJoinRequestEvent
import net.mamoe.mirai.internal.network.protocol.data.proto.Structmsg
import net.mamoe.mirai.internal.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.internal.test.runBlockingUnit
import net.mamoe.mirai.utils.buildTypeSafeMap
import net.mamoe.mirai.utils.currentTimeSeconds
import kotlin.test.*

internal class MemberJoinTest : AbstractNoticeProcessorTest() {

@Test
fun `member actively request join`() = runBlockingUnit {
suspend fun runTest() = use {
suspend fun runTest() = use(
attributes = buildTypeSafeMap { set(NewContact.SYSTEM_MSG_TYPE, 1) }
) {
mockTime += 1000
Structmsg.StructMsg(
version = 1,
msgType = 2,
msgSeq = 16300,
msgTime = 1630,
msgSeq = mockTime * 1000,
msgTime = mockTime,
reqUin = 1230001,
msg = Structmsg.SystemMsg(
subType = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright 2019-2023 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/dev/LICENSE
*/

package net.mamoe.mirai.internal.notice.processors

import net.mamoe.mirai.event.events.NewFriendRequestEvent
import net.mamoe.mirai.internal.network.protocol.data.proto.Structmsg
import net.mamoe.mirai.internal.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.internal.test.runBlockingUnit
import net.mamoe.mirai.utils.buildTypeSafeMap
import net.mamoe.mirai.utils.currentTimeSeconds
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

internal class NewFriendRequestTest : AbstractNoticeProcessorTest() {
@Test
fun `new friend request from search test`() = runBlockingUnit {

setBot(114514)

use(attributes = buildTypeSafeMap { set(NewContact.SYSTEM_MSG_TYPE, 0) }) {
mockTime += 1000
Structmsg.StructMsg(
version = 1,
msgType = 2,
msgSeq = mockTime * 1000,
msgTime = mockTime,
reqUin = 123456,
unreadFlag = 0,
msg = Structmsg.SystemMsg(
subType = 1,
msgTitle = "好友申请",
msgDescribe = "请求加为好友",
msgAdditional = "我是颠佬",
msgSource = "QQ群",
msgDecided = "",
srcId = 3004,
subSrcId = 2,
groupCode = 1,
actionUin = 0,
groupMsgType = 0,
groupInviterRole = 0,
friendInfo = Structmsg.FriendInfo(
msgJointFriend = "1 个共同好友",
msgBlacklist = "设为黑名单后你将拒绝对方,并不再接收此人请求。"
),
groupInfo = null,
actorUin = 0,
msgActorDescribe = "",
msgAdditionalList = "",
relation = 0,
reqsubtype = 0,
cloneUin = 0,
discussUin = 0,
eimGroupId = 0,
msgInviteExtinfo = null,
msgPayGroupExtinfo = null,
sourceFlag = 1,
gameNick = byteArrayOf(),
gameMsg = byteArrayOf(),
groupFlagext3 = 0,
groupOwnerUin = 0,
doubtFlag = 0,
warningTips = byteArrayOf(),
nameMore = byteArrayOf(),
reqUinFaceid = 21762,
reqUinNick = "颠佬",
groupName = "",
actionUinNick = "",
msgQna = "",
msgDetail = "",
groupExtFlag = 0,
actorUinNick = "",
picUrl = "",
cloneUinNick = "",
reqUinBusinessCard = "",
eimGroupIdName = "",
reqUinPreRemark = "",
actionUinQqNick = "",
actionUinRemark = "",
reqUinGender = 255,
reqUinAge = 0,
c2cInviteJoinGroupFlag = 0,
cardSwitch = 0,
actions = listOf(
Structmsg.SystemMsgAction(
name = "同意",
result = "已同意",
action = 1,
actionInfo = Structmsg.SystemMsgActionInfo(
type = 2,
groupCode = 0,
sig = byteArrayOf(),
msg = "",
groupId = 0,
remark = "",
blacklist = false,
addFrdSNInfo = null
),
detailName = "同意"
),
Structmsg.SystemMsgAction(
name = "拒绝",
result = "已拒绝",
action = 1,
actionInfo = Structmsg.SystemMsgActionInfo(
type = 3,
groupCode = 0,
sig = byteArrayOf(),
msg = "",
groupId = 0,
remark = "",
blacklist = false,
addFrdSNInfo = null
),
detailName = "拒绝"
),
)
)
)

}.run {
assertEquals(1, size, toString())
val event = single()
assertIs<NewFriendRequestEvent>(event)

assertEquals(123456, event.fromId)
assertEquals("颠佬", event.fromNick)
}
}@Test
fun `new friend request test`() = runBlockingUnit {

setBot(114514)

use(attributes = buildTypeSafeMap { set(NewContact.SYSTEM_MSG_TYPE, 0) }) {
mockTime += 1000
Structmsg.StructMsg(
version = 1,
msgType = 1,
msgSeq = mockTime * 1000,
msgTime = mockTime,
reqUin = 654321,
unreadFlag = 0,
msg = Structmsg.SystemMsg(
subType = 1,
msgTitle = "好友申请",
msgDescribe = "请求加为好友1",
msgAdditional = "",
msgSource = "QQ群-%group_name%",
msgDecided = "",
srcId = 6,
subSrcId = 2,
groupCode = 111111,
actionUin = 0,
groupMsgType = 0,
groupInviterRole = 0,
friendInfo = Structmsg.FriendInfo(
msgJointFriend = "2 个共同好友",
msgBlacklist = "设为黑名单后你将拒绝对方,并不再接收此人请求。"
),
groupInfo = Structmsg.GroupInfo(
groupAuthType = 0,
displayAction = 0,
msgAlert = "",
msgDetailAlert = "",
msgOtherAdminDone = "",
appPrivilegeFlag = 67633344
),
actorUin = 0,
msgActorDescribe = "",
msgAdditionalList = "",
relation = 0,
reqsubtype = 0,
cloneUin = 0,
discussUin = 0,
eimGroupId = 0,
msgInviteExtinfo = null,
msgPayGroupExtinfo = null,
sourceFlag = 1,
gameNick = byteArrayOf(),
gameMsg = byteArrayOf(),
groupFlagext3 = 0,
groupOwnerUin = 0,
doubtFlag = 0,
warningTips = byteArrayOf(),
nameMore = byteArrayOf(),
reqUinFaceid = 0,
reqUinNick = "颠佬2",
groupName = "%group_name%",
actionUinNick = "",
msgQna = "",
msgDetail = "",
groupExtFlag = 1076036672,
actorUinNick = "",
picUrl = "",
cloneUinNick = "",
reqUinBusinessCard = "",
eimGroupIdName = "",
reqUinPreRemark = "",
actionUinQqNick = "",
actionUinRemark = "",
reqUinGender = 0,
reqUinAge = 0,
c2cInviteJoinGroupFlag = 0,
cardSwitch = 0,
actions = listOf(
Structmsg.SystemMsgAction(
name = "同意",
result = "已同意",
action = 1,
actionInfo = Structmsg.SystemMsgActionInfo(
type = 2,
groupCode = 0,
sig = byteArrayOf(),
msg = "",
groupId = 0,
remark = "",
blacklist = false,
addFrdSNInfo = null
),
detailName = "同意"
),
Structmsg.SystemMsgAction(
name = "拒绝",
result = "已拒绝",
action = 1,
actionInfo = Structmsg.SystemMsgActionInfo(
type = 3,
groupCode = 0,
sig = byteArrayOf(),
msg = "",
groupId = 0,
remark = "",
blacklist = false,
addFrdSNInfo = null
),
detailName = "拒绝"
),
)
)
)

}.run {
assertEquals(1, size, toString())
val event = single()
assertIs<NewFriendRequestEvent>(event)

assertEquals(654321, event.fromId)
assertEquals("颠佬2", event.fromNick)
assertEquals(111111, event.fromGroupId)
}
}
}

0 comments on commit a01035a

Please sign in to comment.