2020-06-28 02:08:44 +08:00
|
|
|
/*
|
2020-08-16 23:36:24 +08:00
|
|
|
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
2020-06-28 02:08:44 +08:00
|
|
|
*
|
2020-08-23 17:46:51 +08:00
|
|
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
|
|
|
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
|
2020-06-28 02:08:44 +08:00
|
|
|
*
|
|
|
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.mamoe.mirai.console
|
|
|
|
|
2020-09-01 08:52:24 +08:00
|
|
|
import kotlinx.coroutines.*
|
2020-07-24 20:11:10 +08:00
|
|
|
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
|
2020-08-20 14:26:44 +08:00
|
|
|
import net.mamoe.mirai.console.command.CommandManager
|
2020-08-22 01:36:22 +08:00
|
|
|
import net.mamoe.mirai.console.data.MemoryPluginDataStorage
|
|
|
|
import net.mamoe.mirai.console.data.PluginDataStorage
|
2020-09-12 19:43:51 +08:00
|
|
|
import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader
|
2020-09-12 20:20:41 +08:00
|
|
|
import net.mamoe.mirai.console.plugin.loader.PluginLoader
|
2020-09-12 13:31:34 +08:00
|
|
|
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
2020-08-22 19:43:07 +08:00
|
|
|
import net.mamoe.mirai.console.util.ConsoleInput
|
2020-09-12 13:31:34 +08:00
|
|
|
import net.mamoe.mirai.console.util.ConsoleInternalApi
|
2020-09-20 18:01:02 +08:00
|
|
|
import net.mamoe.mirai.console.util.SemVersion
|
2020-06-28 02:08:44 +08:00
|
|
|
import net.mamoe.mirai.message.data.Message
|
2020-10-24 19:34:16 +08:00
|
|
|
import net.mamoe.mirai.utils.*
|
2020-08-22 19:43:07 +08:00
|
|
|
import java.nio.file.Path
|
2020-06-28 02:08:44 +08:00
|
|
|
import kotlin.coroutines.Continuation
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
import kotlin.coroutines.resume
|
2020-11-25 17:19:52 +08:00
|
|
|
import kotlin.io.path.createTempDirectory
|
2020-06-28 02:08:44 +08:00
|
|
|
import kotlin.test.assertNotNull
|
|
|
|
|
2020-11-25 17:19:52 +08:00
|
|
|
@OptIn(ConsoleInternalApi::class, kotlin.io.path.ExperimentalPathApi::class)
|
2020-06-28 02:08:44 +08:00
|
|
|
fun initTestEnvironment() {
|
2020-07-24 20:11:10 +08:00
|
|
|
object : MiraiConsoleImplementation {
|
2020-11-25 17:19:52 +08:00
|
|
|
override val rootPath: Path = createTempDirectory()
|
2020-08-22 19:43:07 +08:00
|
|
|
|
2020-09-12 13:31:34 +08:00
|
|
|
@ConsoleExperimentalApi
|
2020-08-22 19:43:07 +08:00
|
|
|
override val frontEndDescription: MiraiConsoleFrontEndDescription
|
2020-08-26 22:58:52 +08:00
|
|
|
get() = object : MiraiConsoleFrontEndDescription {
|
|
|
|
override val name: String
|
|
|
|
get() = "Test"
|
|
|
|
override val vendor: String
|
|
|
|
get() = "Test"
|
2020-09-20 18:01:02 +08:00
|
|
|
override val version: SemVersion
|
2020-09-20 18:54:51 +08:00
|
|
|
get() = SemVersion("1.0.0")
|
2020-08-26 22:58:52 +08:00
|
|
|
|
|
|
|
}
|
2020-09-12 20:20:41 +08:00
|
|
|
override val builtInPluginLoaders: List<Lazy<PluginLoader<*, *>>> = listOf(lazy { JvmPluginLoader })
|
2020-08-30 17:31:52 +08:00
|
|
|
override val consoleCommandSender: MiraiConsoleImplementation.ConsoleCommandSenderImpl =
|
|
|
|
object : MiraiConsoleImplementation.ConsoleCommandSenderImpl {
|
|
|
|
override suspend fun sendMessage(message: Message) {
|
|
|
|
println(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun sendMessage(message: String) {
|
|
|
|
println(message)
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 20:20:41 +08:00
|
|
|
override val dataStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage()
|
|
|
|
override val configStorageForJvmPluginLoader: PluginDataStorage = MemoryPluginDataStorage()
|
2020-08-26 21:27:28 +08:00
|
|
|
override val dataStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage()
|
|
|
|
override val configStorageForBuiltIns: PluginDataStorage = MemoryPluginDataStorage()
|
|
|
|
|
2020-08-22 19:43:07 +08:00
|
|
|
override val consoleInput: ConsoleInput = object : ConsoleInput {
|
|
|
|
override suspend fun requestInput(hint: String): String {
|
|
|
|
println(hint)
|
|
|
|
return readLine() ?: error("No stdin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver =
|
2020-12-25 20:45:38 +08:00
|
|
|
LoginSolver.Default!!
|
2020-08-22 19:43:07 +08:00
|
|
|
|
2020-10-26 23:20:10 +08:00
|
|
|
override fun createLogger(identity: String?): MiraiLogger {
|
|
|
|
return PlatformLogger(identity)
|
2020-08-22 19:43:07 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 08:52:24 +08:00
|
|
|
override val coroutineContext: CoroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, throwable ->
|
|
|
|
throwable.printStackTrace()
|
|
|
|
}
|
2020-07-24 20:11:10 +08:00
|
|
|
}.start()
|
2020-08-20 14:26:44 +08:00
|
|
|
CommandManager
|
2020-06-28 02:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
internal object Testing {
|
|
|
|
@Volatile
|
|
|
|
internal var cont: Continuation<Any?>? = null
|
|
|
|
|
|
|
|
@Suppress("UNCHECKED_CAST")
|
2020-10-24 13:14:25 +08:00
|
|
|
suspend fun <R> withTesting(timeout: Long = 50000L, block: suspend () -> Unit): R {
|
2020-06-28 02:08:44 +08:00
|
|
|
@Suppress("RemoveExplicitTypeArguments") // bug
|
|
|
|
return if (timeout != -1L) {
|
|
|
|
withTimeout<R>(timeout) {
|
|
|
|
suspendCancellableCoroutine<R> { ct ->
|
|
|
|
this@Testing.cont = ct as Continuation<Any?>
|
|
|
|
runBlocking { block() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
suspendCancellableCoroutine<R> { ct ->
|
|
|
|
this.cont = ct as Continuation<Any?>
|
|
|
|
runBlocking { block() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun ok(result: Any? = Unit) {
|
|
|
|
val cont = cont
|
|
|
|
assertNotNull(cont)
|
|
|
|
cont.resume(result)
|
|
|
|
}
|
|
|
|
}
|