fix MockNormalMember modify admin (#2420)

* fix MockNormalMember modify admin (untested)

* modify as required

* fix changeOwner (tested)

* update test

* update tests
This commit is contained in:
Eritque arcus 2023-01-17 08:12:31 -05:00 committed by GitHub
parent 56bfc65d82
commit aa84e6d337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 2 deletions

View File

@ -108,10 +108,11 @@ internal class MockGroupImpl(
}
override suspend fun changeOwner(member: NormalMember) {
if (member === owner) return
val oldOwner = owner
val oldPerm = member.permission
member.mock().mockApi.permission = MemberPermission.OWNER
oldOwner.mock().mockApi.permission = MemberPermission.MEMBER
member.mock().mockApi.permission = MemberPermission.OWNER
owner = member
if (member === botAsMember) {

View File

@ -162,7 +162,7 @@ internal class MockNormalMemberImpl(
val newPerm = if (operation) MemberPermission.ADMINISTRATOR else MemberPermission.MEMBER
if (newPerm != permission) {
val oldPerm = permission
mockApi.permission = oldPerm
mockApi.permission = newPerm
MemberPermissionChangeEvent(this, oldPerm, newPerm).broadcast()
}
}

View File

@ -10,10 +10,14 @@
package net.mamoe.mirai.mock.test.mock
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.events.BotGroupPermissionChangeEvent
import net.mamoe.mirai.event.events.MemberPermissionChangeEvent
import net.mamoe.mirai.mock.test.MockBotTestBase
import net.mamoe.mirai.mock.utils.simpleMemberInfo
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertSame
internal class MockMemberTest : MockBotTestBase() {
@Test
@ -21,4 +25,53 @@ internal class MockMemberTest : MockBotTestBase() {
val m = bot.addGroup(111, "aaa").addMember(simpleMemberInfo(222, "bbb", permission = MemberPermission.MEMBER))
assertNotEquals("", m.avatarUrl)
}
@Test
internal fun changeOwner() = runTest {
val group = bot.addGroup(111, "aaa")
val member = group.addMember(simpleMemberInfo(222, "bbb", permission = MemberPermission.MEMBER))
val events = runAndReceiveEventBroadcast {
group.changeOwner(member)
assertSame(member, group.owner)
assertSame(MemberPermission.OWNER, member.permission)
}
assertEquals(2, events.size)
assertIsInstance<MemberPermissionChangeEvent>(events[0]) {
assertSame(member, this.member)
assertSame(MemberPermission.OWNER, new)
assertSame(MemberPermission.MEMBER, origin)
assertSame(group, this.group)
}
assertIsInstance<BotGroupPermissionChangeEvent>(events[1]) {
assertSame(MemberPermission.MEMBER, new)
assertSame(MemberPermission.OWNER, origin)
assertSame(group, this.group)
}
}
@Test
internal fun modifyAdmin() = runTest {
val group = bot.addGroup(111, "aaa")
group.changeOwner(group.botAsMember)
val m = group.addMember(simpleMemberInfo(222, "bbb", permission = MemberPermission.MEMBER))
val events = runAndReceiveEventBroadcast {
m.modifyAdmin(true)
assertEquals(MemberPermission.ADMINISTRATOR, m.permission)
m.modifyAdmin(false)
assertEquals(MemberPermission.MEMBER, m.permission)
}
assertEquals(2, events.size)
assertIsInstance<MemberPermissionChangeEvent>(events[0]) {
assertSame(m, member)
assertSame(MemberPermission.MEMBER, origin)
assertSame(MemberPermission.ADMINISTRATOR, new)
assertSame(group, this.group)
}
assertIsInstance<MemberPermissionChangeEvent>(events[1]) {
assertSame(m, member)
assertSame(MemberPermission.ADMINISTRATOR, origin)
assertSame(MemberPermission.MEMBER, new)
assertSame(group, this.group)
}
}
}