diff --git a/mirai-core-mock/src/internal/contact/MockGroupImpl.kt b/mirai-core-mock/src/internal/contact/MockGroupImpl.kt index 257670ee0..75a18d32a 100644 --- a/mirai-core-mock/src/internal/contact/MockGroupImpl.kt +++ b/mirai-core-mock/src/internal/contact/MockGroupImpl.kt @@ -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) { diff --git a/mirai-core-mock/src/internal/contact/MockNormalMemberImpl.kt b/mirai-core-mock/src/internal/contact/MockNormalMemberImpl.kt index 7d505df5a..0ec295bae 100644 --- a/mirai-core-mock/src/internal/contact/MockNormalMemberImpl.kt +++ b/mirai-core-mock/src/internal/contact/MockNormalMemberImpl.kt @@ -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() } } diff --git a/mirai-core-mock/test/mock/MockMemberTest.kt b/mirai-core-mock/test/mock/MockMemberTest.kt index 653d534a6..4fdd7d422 100644 --- a/mirai-core-mock/test/mock/MockMemberTest.kt +++ b/mirai-core-mock/test/mock/MockMemberTest.kt @@ -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(events[0]) { + assertSame(member, this.member) + assertSame(MemberPermission.OWNER, new) + assertSame(MemberPermission.MEMBER, origin) + assertSame(group, this.group) + } + assertIsInstance(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(events[0]) { + assertSame(m, member) + assertSame(MemberPermission.MEMBER, origin) + assertSame(MemberPermission.ADMINISTRATOR, new) + assertSame(group, this.group) + } + assertIsInstance(events[1]) { + assertSame(m, member) + assertSame(MemberPermission.ADMINISTRATOR, origin) + assertSame(MemberPermission.MEMBER, new) + assertSame(group, this.group) + } + } } \ No newline at end of file