[console] feat: login use auto login account configuration (#2209)

* feat: other account configuration

* add: test
This commit is contained in:
cssxsh 2022-09-02 07:50:08 +08:00 committed by GitHub
parent 7b11ed509e
commit e00f592b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -45,7 +45,30 @@ internal open class LoginCommandImpl : SimpleCommand(
protocol: BotConfiguration.MiraiProtocol? = null,
) {
fun BotConfiguration.setup(protocol: BotConfiguration.MiraiProtocol?): BotConfiguration {
if (protocol != null) this.protocol = protocol
val config = DataScope.get<AutoLoginConfig>()
val account = config.accounts.firstOrNull { it.account == id.toString() }
if (account != null) {
account.configuration[AutoLoginConfig.Account.ConfigurationKey.protocol]?.let { protocol ->
try {
this.protocol = BotConfiguration.MiraiProtocol.valueOf(protocol.toString())
} catch (_: Throwable) {
//
}
}
account.configuration[AutoLoginConfig.Account.ConfigurationKey.heartbeatStrategy]?.let { heartStrate ->
try {
this.heartbeatStrategy = BotConfiguration.HeartbeatStrategy.valueOf(heartStrate.toString())
} catch (_: Throwable) {
//
}
}
account.configuration[AutoLoginConfig.Account.ConfigurationKey.device]?.let { device ->
fileBasedDeviceInfo(device.toString())
}
}
if (protocol != null) {
this.protocol = protocol
}
return this
}

View File

@ -21,11 +21,13 @@ import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind
import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.md5
import net.mamoe.mirai.utils.toUHexString
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@OptIn(ExperimentalCommandDescriptors::class)
internal class LoginCommandTest : AbstractCommandTest() {
@ -107,6 +109,41 @@ internal class LoginCommandTest : AbstractCommandTest() {
assertContentEquals(myPwd.md5(), account.passwordMd5)
assertEquals(myId, account.id)
}
@Test
fun `login with saved configuration`() = runTest {
val myId = 123L
val myPwd = "password001"
dataScope.set(AutoLoginConfig().apply {
accounts.add(
Account(
account = myId.toString(),
password = Account.Password(PasswordKind.MD5, myPwd.md5().toUHexString("")),
configuration = mapOf(
Account.ConfigurationKey.protocol to BotConfiguration.MiraiProtocol.ANDROID_PAD.name,
Account.ConfigurationKey.device to "device.new.json",
Account.ConfigurationKey.heartbeatStrategy to BotConfiguration.HeartbeatStrategy.REGISTER.name
)
)
)
})
val bot = awaitDeferred<QQAndroidBot> { cont ->
val command = object : LoginCommandImpl() {
override suspend fun doLogin(bot: Bot) {
cont.complete(bot as QQAndroidBot)
}
}
command.register(true)
command.execute(consoleSender, "$myId")
}
val configuration = bot.configuration
assertEquals(BotConfiguration.MiraiProtocol.ANDROID_PAD, configuration.protocol)
assertEquals(BotConfiguration.HeartbeatStrategy.REGISTER, configuration.heartbeatStrategy)
assertNotNull(configuration.deviceInfo)
}
}
@BuilderInference