Add tests to timeout feature (closes #4)
This commit is contained in:
parent
c299406d69
commit
8560c3d82e
|
@ -9,7 +9,6 @@ import moe.kageru.kagebot.config.RawTimeoutFeature
|
|||
import moe.kageru.kagebot.persistence.Dao
|
||||
import org.javacord.api.entity.permission.Role
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
import java.lang.IllegalArgumentException
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
|
||||
|
@ -18,11 +17,17 @@ class TimeoutFeature(raw: RawTimeoutFeature) : MessageFeature {
|
|||
?: throw IllegalArgumentException("No timeout role defined")
|
||||
|
||||
override fun handle(message: MessageCreateEvent) {
|
||||
val (_, target, time) = message.readableMessageContent.split(' ', limit = 3).apply {
|
||||
if (size != 3) {
|
||||
val (target, time) = message.readableMessageContent.split(' ', limit = 3).let {
|
||||
if (it.size != 3) {
|
||||
message.channel.sendMessage("Error: expected “<command> <user> <time>”. If the name contains spaces, please use the user ID instead.")
|
||||
return
|
||||
}
|
||||
val time = it[2].toLongOrNull()
|
||||
if (time == null) {
|
||||
message.channel.sendMessage("Error: malformed time")
|
||||
return
|
||||
}
|
||||
Pair(it[1], time)
|
||||
}
|
||||
findUser(target)?.let { user ->
|
||||
val oldRoles = user.getRoles(Config.server)
|
||||
|
@ -32,7 +37,7 @@ class TimeoutFeature(raw: RawTimeoutFeature) : MessageFeature {
|
|||
role.id
|
||||
}
|
||||
user.addRole(timeoutRole)
|
||||
val releaseTime = Instant.now().plus(Duration.ofMinutes(time.toLong())).epochSecond
|
||||
val releaseTime = Instant.now().plus(Duration.ofMinutes(time)).epochSecond
|
||||
Dao.saveTimeout(releaseTime, listOf(user.id) + oldRoles)
|
||||
Log.info("Removed roles ${oldRoles.joinToString()} from user ${user.discriminatedName}")
|
||||
} ?: message.channel.sendMessage("Could not find user $target. Consider using the user ID.")
|
||||
|
|
|
@ -14,6 +14,7 @@ import moe.kageru.kagebot.config.RawConfig
|
|||
import org.javacord.api.DiscordApi
|
||||
import org.javacord.api.entity.channel.ServerTextChannel
|
||||
import org.javacord.api.entity.message.embed.EmbedBuilder
|
||||
import org.javacord.api.entity.permission.Role
|
||||
import org.javacord.api.entity.user.User
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
import org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl
|
||||
|
@ -21,6 +22,14 @@ import java.io.File
|
|||
import java.util.*
|
||||
|
||||
object TestUtil {
|
||||
val TIMEOUT_ROLE = mockk<Role> {
|
||||
every { id } returns 123
|
||||
}
|
||||
val TEST_ROLE = mockk<Role> {
|
||||
every { id } returns 1
|
||||
every { isManaged } returns false
|
||||
}
|
||||
|
||||
fun mockMessage(
|
||||
content: String,
|
||||
replies: MutableList<String> = mutableListOf(),
|
||||
|
@ -58,7 +67,7 @@ object TestUtil {
|
|||
sentEmbeds: MutableList<EmbedBuilder> = mutableListOf(),
|
||||
sentMessages: MutableList<String> = mutableListOf()
|
||||
) {
|
||||
val channel = mockk<Optional<ServerTextChannel>> {
|
||||
val channel = mockk<Optional<ServerTextChannel>>(relaxed = true) {
|
||||
every { isPresent } returns true
|
||||
every { get() } returns mockk {
|
||||
every { sendMessage(capture(sentEmbeds)) } returns mockk {
|
||||
|
@ -73,8 +82,11 @@ object TestUtil {
|
|||
every { icon.ifPresent(any()) } just Runs
|
||||
every { getTextChannelById(any<String>()) } returns channel
|
||||
every { getTextChannelsByName(any()) } returns listOf(channel.get())
|
||||
every { getRolesByNameIgnoreCase("testrole") } returns listOf(mockk {
|
||||
every { id } returns 1
|
||||
every { getRolesByNameIgnoreCase("testrole") } returns listOf(TEST_ROLE)
|
||||
every { getRolesByNameIgnoreCase("timeout") } returns listOf(TIMEOUT_ROLE)
|
||||
every { getMembersByName(any()) } returns listOf(mockk(relaxed = true) {
|
||||
every { id } returns 123
|
||||
every { getRoles(any()) } returns listOf(TEST_ROLE)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package moe.kageru.kagebot.features
|
||||
|
||||
import io.kotlintest.matchers.string.shouldContain
|
||||
import io.kotlintest.shouldBe
|
||||
import io.kotlintest.specs.StringSpec
|
||||
import moe.kageru.kagebot.Kagebot.process
|
||||
import moe.kageru.kagebot.TestUtil
|
||||
import moe.kageru.kagebot.TestUtil.TEST_ROLE
|
||||
import moe.kageru.kagebot.persistence.Dao
|
||||
|
||||
class TimeoutFeatureTest : StringSpec({
|
||||
TestUtil.prepareTestEnvironment()
|
||||
"should remove and store roles" {
|
||||
TestUtil.mockMessage("!timeout kageru 99999999").process()
|
||||
Dao.getAllTimeouts().let {
|
||||
it.size shouldBe 1
|
||||
val user = Dao.deleteTimeout(it.first())
|
||||
user shouldBe arrayOf(123, TEST_ROLE.id)
|
||||
}
|
||||
}
|
||||
"should return error for invalid input" {
|
||||
val replies = mutableListOf<String>()
|
||||
TestUtil.mockMessage("!timeout kageruWithoutATime", replies = replies).process()
|
||||
replies.size shouldBe 1
|
||||
replies[0] shouldContain "Error"
|
||||
}
|
||||
"should catch malformed time" {
|
||||
val replies = mutableListOf<String>()
|
||||
TestUtil.mockMessage("!timeout kageru this is not a time", replies = replies).process()
|
||||
replies.size shouldBe 1
|
||||
replies[0] shouldContain "Error"
|
||||
}
|
||||
})
|
|
@ -17,6 +17,9 @@ content = [
|
|||
"Second paragraph heading", "Second paragraph content"
|
||||
]
|
||||
|
||||
[feature.timeout]
|
||||
role = "timeout"
|
||||
|
||||
[[command]]
|
||||
trigger = "!debug"
|
||||
feature = "debug"
|
||||
|
@ -24,3 +27,7 @@ feature = "debug"
|
|||
[[command]]
|
||||
trigger = "!welcome"
|
||||
feature = "welcome"
|
||||
|
||||
[[command]]
|
||||
trigger = "!timeout"
|
||||
feature = "timeout"
|
Loading…
Reference in New Issue
Block a user