|
| 1 | +package pl.syntaxdevteam.authgatewayx.paper.command |
| 2 | + |
| 3 | +import net.kyori.adventure.text.Component |
| 4 | +import net.kyori.adventure.text.event.ClickEvent |
| 5 | +import org.bukkit.command.CommandSender |
| 6 | +import org.bukkit.command.ConsoleCommandSender |
| 7 | +import org.bukkit.entity.Player |
| 8 | +import java.lang.reflect.Proxy |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertEquals |
| 11 | +import kotlin.test.assertFalse |
| 12 | +import kotlin.test.assertTrue |
| 13 | + |
| 14 | +class AdminHelpCommandControllerTest { |
| 15 | + private val text = AdminHelpCommandText( |
| 16 | + header = Component.text("header {version}"), |
| 17 | + playerSection = Component.text("player"), |
| 18 | + adminSection = Component.text("admin"), |
| 19 | + changePassword = Component.text("change"), |
| 20 | + logout = Component.text("logout"), |
| 21 | + info = Component.text("info"), |
| 22 | + alts = Component.text("alts"), |
| 23 | + setPassword = Component.text("setpassword"), |
| 24 | + migrateStatus = Component.text("status"), |
| 25 | + migrateInspect = Component.text("inspect"), |
| 26 | + migrateRetry = Component.text("retry"), |
| 27 | + migrateRecover = Component.text("recover"), |
| 28 | + hover = Component.text("hover"), |
| 29 | + footer = Component.text("footer"), |
| 30 | + ) |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `player sees only commands allowed by permissions and entries are clickable`() { |
| 34 | + val replies = mutableListOf<Component>() |
| 35 | + val player = sender( |
| 36 | + Player::class.java, |
| 37 | + setOf( |
| 38 | + AdminHelpCommandController.LOGOUT_PERMISSION, |
| 39 | + AdminHelpCommandController.ALTS_PERMISSION, |
| 40 | + ), |
| 41 | + replies, |
| 42 | + ) |
| 43 | + AdminHelpCommandController(text, "1.2.3").show(player) |
| 44 | + |
| 45 | + assertEquals(6, replies.size) |
| 46 | + assertTrue(replies[0].toString().contains("1.2.3")) |
| 47 | + assertEquals(text.playerSection, replies[1]) |
| 48 | + assertEquals(ClickEvent.suggestCommand("/logout"), replies[2].clickEvent()) |
| 49 | + assertEquals(text.adminSection, replies[3]) |
| 50 | + assertEquals(ClickEvent.suggestCommand("/agx alts "), replies[4].clickEvent()) |
| 51 | + assertEquals(text.footer, replies[5]) |
| 52 | + assertFalse(replies.any { it == text.info || it == text.setPassword }) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `console sees console-capable administration commands but not player-only commands`() { |
| 57 | + val replies = mutableListOf<Component>() |
| 58 | + val console = sender(ConsoleCommandSender::class.java, emptySet(), replies) |
| 59 | + AdminHelpCommandController(text, "1.0.0").show(console) |
| 60 | + |
| 61 | + assertEquals(text.adminSection, replies[1]) |
| 62 | + assertTrue(replies.contains(text.info)) |
| 63 | + assertTrue(replies.contains(text.alts)) |
| 64 | + assertTrue(replies.contains(text.migrateStatus)) |
| 65 | + assertTrue(replies.contains(text.migrateInspect)) |
| 66 | + assertTrue(replies.contains(text.migrateRetry)) |
| 67 | + assertTrue(replies.contains(text.migrateRecover)) |
| 68 | + assertFalse(replies.contains(text.changePassword)) |
| 69 | + assertFalse(replies.contains(text.logout)) |
| 70 | + assertFalse(replies.contains(text.setPassword)) |
| 71 | + assertEquals(text.footer, replies.last()) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `player without plugin permissions receives only frame`() { |
| 76 | + val replies = mutableListOf<Component>() |
| 77 | + val player = sender(Player::class.java, emptySet(), replies) |
| 78 | + AdminHelpCommandController(text, "1.0.0").show(player) |
| 79 | + |
| 80 | + assertEquals(2, replies.size) |
| 81 | + assertEquals(text.footer, replies.last()) |
| 82 | + } |
| 83 | + |
| 84 | + private fun <T : CommandSender> sender( |
| 85 | + type: Class<T>, |
| 86 | + permissions: Set<String>, |
| 87 | + replies: MutableList<Component>, |
| 88 | + ): T = Proxy.newProxyInstance(type.classLoader, arrayOf(type)) { _, method, args -> |
| 89 | + when (method.name) { |
| 90 | + "hasPermission" -> (args?.firstOrNull() as? String) in permissions |
| 91 | + "sendMessage" -> { |
| 92 | + args?.filterIsInstance<Component>()?.let(replies::addAll) |
| 93 | + null |
| 94 | + } |
| 95 | + "isOnline" -> true |
| 96 | + "toString" -> "help-test-sender" |
| 97 | + "hashCode" -> 1 |
| 98 | + "equals" -> false |
| 99 | + else -> null |
| 100 | + } |
| 101 | + } as T |
| 102 | +} |
0 commit comments