Skip to content

Commit

Permalink
First successful build
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Jul 1, 2023
1 parent 5cd514a commit 56d332e
Show file tree
Hide file tree
Showing 15 changed files with 1,970 additions and 2,218 deletions.
4 changes: 0 additions & 4 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,6 @@ public abstract class dev/kord/core/builder/kord/BaseKordBuilder {
public final fun getHttpClient ()Lio/ktor/client/HttpClient;
public final fun getStackTraceRecovery ()Z
public final fun getToken ()Ljava/lang/String;
public final fun requestHandler (Lkotlin/jvm/functions/Function1;)V
public final fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
public final fun setDefaultDispatcher (Lkotlinx/coroutines/CoroutineDispatcher;)V
public final fun setDefaultStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)V
Expand Down Expand Up @@ -2134,13 +2133,10 @@ public abstract class dev/kord/core/builder/kord/RestOnlyBuilder {
public final fun build ()Ldev/kord/core/Kord;
public abstract fun getApplicationId ()Ldev/kord/common/entity/Snowflake;
public final fun getDefaultDispatcher ()Lkotlinx/coroutines/CoroutineDispatcher;
protected final fun getHandlerBuilder ()Lkotlin/jvm/functions/Function1;
public final fun getHttpClient ()Lio/ktor/client/HttpClient;
protected abstract fun getToken ()Ljava/lang/String;
public final fun requestHandler (Lkotlin/jvm/functions/Function1;)V
public abstract fun setApplicationId (Ldev/kord/common/entity/Snowflake;)V
public final fun setDefaultDispatcher (Lkotlinx/coroutines/CoroutineDispatcher;)V
protected final fun setHandlerBuilder (Lkotlin/jvm/functions/Function1;)V
public final fun setHttpClient (Lio/ktor/client/HttpClient;)V
}

Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kotlin {

api(libs.kord.cache.api)
api(libs.kord.cache.map)
api(libs.ktor.client.mock)
}
}
jvmTest {
Expand Down
5 changes: 3 additions & 2 deletions core/live-tests/src/commonTest/kotlin/TestToken.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import dev.kord.core.builder.kord.getBotIdFromToken
import dev.kord.core.cache.registerKordData
import dev.kord.core.gateway.DefaultMasterGateway
import dev.kord.core.gateway.handler.DefaultGatewayEventInterceptor
import dev.kord.core.regression.CrashingHandler
import dev.kord.core.regression.FakeGateway
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.gateway.builder.Shards
import dev.kord.rest.service.RestClient
import dev.kord.test.getEnv
import io.ktor.client.*
import io.ktor.client.engine.mock.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow

Expand All @@ -31,7 +32,7 @@ suspend inline fun withKord(block: (kord: Kord) -> Unit) {
resources,
MapDataCache().also { it.registerKordData() },
DefaultMasterGateway(mapOf(0 to FakeGateway)),
RestClient(CrashingHandler(resources.httpClient, resources.token)),
RestClient(HttpClient(MockEngine)),
getBotIdFromToken(token),
MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE),
Dispatchers.Default,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
package dev.kord.core.regression

import dev.kord.cache.api.put
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Snowflake
import dev.kord.core.cache.data.ChannelData
import dev.kord.core.withKord
import dev.kord.gateway.Command
import dev.kord.gateway.Event
import dev.kord.gateway.Gateway
import dev.kord.gateway.GatewayConfiguration
import dev.kord.rest.request.JsonRequest
import dev.kord.rest.request.MultipartRequest
import dev.kord.rest.request.Request
import dev.kord.rest.route.Route
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.content.*
import io.ktor.http.*
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.time.Duration


Expand Down Expand Up @@ -64,63 +44,3 @@ object FakeGateway : Gateway {

override val coroutineContext: CoroutineContext = SupervisorJob() + EmptyCoroutineContext
}

class CrashingHandler(private val client: HttpClient, override val token: String) : RequestHandler {
override suspend fun <B : Any, R> handle(request: Request<B, R>): R {
if (request.route != Route.CurrentUserGet) throw IllegalStateException("shouldn't do a request")
val response = client.request {
method = request.route.method
headers.appendAll(request.headers)

url {
url.takeFrom(request.baseUrl)
encodedPath += request.path
parameters.appendAll(request.parameters)
}


request.body?.let {
@Suppress("UNCHECKED_CAST")
when (request) {
is MultipartRequest<*, *> -> {
headers.append(
"payload_json",
parser.encodeToString(it.strategy as SerializationStrategy<Any>, it.body)
)
setBody(MultiPartFormDataContent(request.data))
}

is JsonRequest<*, *> -> {
val json = parser.encodeToString(it.strategy as SerializationStrategy<Any>, it.body)
setBody(TextContent(json, ContentType.Application.Json))
}
}
}
}

return request.route.mapper.deserialize(parser, response.bodyAsText())
}
}

class CacheMissingRegressions {

@Test
@JsName("test1")
fun `if data not in cache explode`() = runTest {
withKord { kord ->
val id = 5uL
assertFailsWith<IllegalStateException> { kord.getChannel(Snowflake(id)) }
}
}

@Test
@JsName("test2")
fun `if data in cache don't fetch from rest`() = runTest {
withKord { kord ->
val id = Snowflake(5uL)
kord.cache.put(ChannelData(id, ChannelType.GuildText))

kord.getChannel(id)
}
}
}
5 changes: 4 additions & 1 deletion core/src/commonMain/kotlin/builder/kord/KordBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import dev.kord.gateway.ratelimit.IdentifyRateLimiter
import dev.kord.rest.json.response.BotGatewayResponse
import dev.kord.rest.ratelimit.ExclusionRequestRateLimiter
import dev.kord.rest.request.*
import dev.kord.rest.route.Routes
import dev.kord.rest.service.RestClient
import io.ktor.client.*
import io.ktor.client.plugins.resources.*
import io.ktor.client.request.*
import io.ktor.client.request.get
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.http.HttpHeaders.Authorization
Expand Down Expand Up @@ -169,7 +172,7 @@ public abstract class BaseKordBuilder internal constructor(public val token: Str
* Requests the gateway info for the bot, or throws a [KordInitializationException] when something went wrong.
*/
private suspend fun HttpClient.getGatewayInfo(): BotGatewayResponse {
val response = get("${Route.baseUrl}${Route.GatewayBotGet.path}") {
val response = get(Routes.Gateway.Bot) {
header(UserAgent, KordConstants.USER_AGENT)
header(Authorization, "Bot $token")
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/builder/kord/RestOnlyBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class RestOnlyBuilder {
EntitySupplyStrategy.rest,
)

val rest = RestClient(httpClient)
val rest = RestClient(httpClient!!)

return Kord(
resources = resources,
Expand Down
3 changes: 1 addition & 2 deletions core/src/commonTest/kotlin/live/AbstractLiveEntityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import dev.kord.gateway.Event
import dev.kord.gateway.Gateway
import dev.kord.gateway.GatewayConfiguration
import dev.kord.gateway.builder.Shards
import dev.kord.rest.request.KtorRequestHandler
import dev.kord.rest.service.RestClient
import io.ktor.client.*
import kotlinx.atomicfu.atomic
Expand Down Expand Up @@ -97,7 +96,7 @@ abstract class AbstractLiveEntityTest<LIVE : AbstractLiveKordEntity> {
resources = ClientResources("token", Snowflake(0u), Shards(1), maxConcurrency = 1, HttpClient(), EntitySupplyStrategy.cache),
cache = DataCache.none(),
DefaultMasterGateway(mapOf(0 to gateway)),
RestClient(KtorRequestHandler(token = "token")),
RestClient(HttpClient()),
randomId(),
MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE),
Dispatchers.Default,
Expand Down
3 changes: 1 addition & 2 deletions core/src/commonTest/kotlin/performance/KordEventDropTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import dev.kord.core.on
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.gateway.*
import dev.kord.gateway.builder.Shards
import dev.kord.rest.request.KtorRequestHandler
import dev.kord.rest.service.RestClient
import io.ktor.client.*
import kotlinx.atomicfu.atomic
Expand Down Expand Up @@ -55,7 +54,7 @@ class KordEventDropTest {
resources = ClientResources("token", Snowflake(0u), Shards(1), maxConcurrency = 1, HttpClient(), EntitySupplyStrategy.cache),
cache = DataCache.none(),
DefaultMasterGateway(mapOf(0 to SpammyGateway)),
RestClient(KtorRequestHandler("token", clock = Clock.System)),
RestClient(HttpClient()),
Snowflake("420"),
MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE),
Dispatchers.Default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package dev.kord.core.supplier

import dev.kord.common.annotation.KordUnsafe
import dev.kord.common.entity.Snowflake
import dev.kord.common.http.HttpEngine
import dev.kord.core.ClientResources
import dev.kord.core.Kord
import dev.kord.core.cache.KordCacheBuilder
import dev.kord.core.gateway.DefaultMasterGateway
import dev.kord.core.gateway.handler.DefaultGatewayEventInterceptor
import dev.kord.gateway.Gateway
import dev.kord.gateway.builder.Shards
import dev.kord.rest.request.KtorRequestHandler
import dev.kord.rest.service.RestClient
import io.ktor.client.*
import kotlinx.coroutines.Dispatchers
Expand All @@ -29,7 +29,7 @@ internal class CacheEntitySupplierTest {
ClientResources("", Snowflake(0u), Shards(0), maxConcurrency = 1, HttpClient(), EntitySupplyStrategy.cache),
KordCacheBuilder().build(),
DefaultMasterGateway(mapOf(0 to Gateway.none())),
RestClient(KtorRequestHandler("")),
RestClient(HttpClient()),
Snowflake(0u),
MutableSharedFlow(),
Dispatchers.Default,
Expand Down
Loading

0 comments on commit 56d332e

Please sign in to comment.