diff --git a/scalabot-app/src/main/kotlin/AppConfigs.kt b/scalabot-app/src/main/kotlin/AppConfigs.kt index cb4a8ce..be9359a 100644 --- a/scalabot-app/src/main/kotlin/AppConfigs.kt +++ b/scalabot-app/src/main/kotlin/AppConfigs.kt @@ -32,7 +32,14 @@ internal data class BotAccount( val name: String, val token: String, val creatorId: Long = -1 -) +) { + + val id + // 不要想着每次获取都要从 token 里取出有性能损耗. + // 由于 Gson 解析方式, 如果不这么做, 会出现 token 设置前 id 初始化完成, 就只有"0"了, + // 虽然能过单元测试, 但实际使用过程是不能正常用的. + get() = token.substringBefore(":").toLong() +} /** * 机器人配置. diff --git a/scalabot-app/src/test/kotlin/AppConfigTest.kt b/scalabot-app/src/test/kotlin/AppConfigTest.kt new file mode 100644 index 0000000..c61b52e --- /dev/null +++ b/scalabot-app/src/test/kotlin/AppConfigTest.kt @@ -0,0 +1,31 @@ +package net.lamgc.scalabot + +import com.google.gson.Gson +import java.util.* +import kotlin.math.abs +import kotlin.test.Test +import kotlin.test.assertEquals + +class BotAccountTest { + + @Test + fun deserializerTest() { + val accountId = abs(Random().nextInt()).toLong() + val creatorId = abs(Random().nextInt()).toLong() + val botAccount = Gson().fromJson( + """ + { + "name": "TestBot", + "token": "${accountId}:AAHErDroUTznQsOd_oZPJ6cQEj4Z5mGHO10", + "creatorId": $creatorId + } + """.trimIndent(), BotAccount::class.java + ) + assertEquals("TestBot", botAccount.name) + assertEquals("${accountId}:AAHErDroUTznQsOd_oZPJ6cQEj4Z5mGHO10", botAccount.token) + assertEquals(accountId, botAccount.id, "Botaccount ID does not match expectations.") + assertEquals(creatorId, botAccount.creatorId) + } + +} +