Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Mar 23, 2021
1 parent 8907933 commit 62371f1
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 62 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ jobs:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

- name: Gradle (Testing)
# TODO: Add jsIrTest when #4 gets fixed
run: gradle --stacktrace --info core:jvmTest
run: gradle --stacktrace --info jvmTest jsTest
release:
name: Publish artifacts
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions .idea/csv-plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ public abstract class AbstractLavakord internal constructor(
private val nodesMap = mutableMapOf<String, Node>()
protected val linksMap: MutableMap<Long, Link> = mutableMapOf()

internal val json = kotlinx.serialization.json.Json {
serializersModule = RoutePlannerModule
classDiscriminator = "class"
}


internal val restClient = HttpClient(httpClientEngine) {
install(JsonFeature) {
val json = kotlinx.serialization.json.Json {
serializersModule = RoutePlannerModule
classDiscriminator = "class"
}

serializer = KotlinxSerializer(json)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.json.Json
import mu.KotlinLogging

internal val LOG = KotlinLogging.logger {}

internal suspend inline fun <reified T> Node.get(noinline urlBuilder: URLBuilder.() -> Unit): T =
restClient.get(buildUrl(urlBuilder).build()) { addHeader(this@get); accept(ContentType.Application.JavaScript) }

Expand All @@ -36,3 +35,9 @@ private val Node.restClient: HttpClient
val lavakord = this.lavakord as? AbstractLavakord ?: error("Only supported on default implementation")
return lavakord.restClient
}

internal val Node.json: Json
get() {
val lavakord = this.lavakord as? AbstractLavakord ?: error("Only supported on default implementation")
return lavakord.json
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import dev.schlaubi.lavakord.audio.Node
import io.ktor.http.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement

/**
* Retrieves the current address status of the route planner api. Can be null if no Route planner is set
Expand All @@ -24,7 +26,9 @@ public suspend fun Link.addressStatusOrNull(): RoutePlannerStatus<out RoutePlann
*/
public suspend fun Node.addressStatusOrNull(): RoutePlannerStatus<out RoutePlannerStatus.Data>? {
return try {
get { path("/routeplanner/status") }
val response = get<JsonElement> { path("/routeplanner/status") }
// Due to a bug in ktor kx.ser doesn't get the correct info on K/JS and fails
json.decodeFromJsonElement<RoutePlannerStatus<out RoutePlannerStatus.Data>>(response)
} catch (e: SerializationException) {
if (e.message?.endsWith("{}") == true) { // {} means no route planer is not set
return null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package dev.schlaubi.lavakord.rest

import dev.schlaubi.lavakord.rest.RoutePlannerStatus.Data
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Polymorphic
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic

Expand All @@ -13,6 +18,8 @@ internal val RoutePlannerModule = SerializersModule {
subclass(NanoIpRoutePlanner::class, NanoIpRoutePlanner.serializer())
subclass(RotatingNanoIpRoutePlanner::class, RotatingNanoIpRoutePlanner.serializer())
}

contextual(RoutePlannerStatus.Data::class, RoutePlannerStatus.Data.DummySerializer)
}

/**
Expand All @@ -25,7 +32,7 @@ internal val RoutePlannerModule = SerializersModule {
*/
@Serializable
@Polymorphic
public sealed class RoutePlannerStatus<T : Data> {
public sealed class RoutePlannerStatus<T : RoutePlannerStatus.Data> {

public abstract val details: Data

Expand Down Expand Up @@ -59,10 +66,24 @@ public sealed class RoutePlannerStatus<T : Data> {
* Representation of route planner data.
*/
@Suppress("KDocMissingDocumentation") // lavalink doesnt provide
@Serializable(with = Data.DummySerializer::class)
public interface Data {
public val ipBlock: IpBlock
public val failingAddresses: List<FailingAddress>

public object DummySerializer : KSerializer<Data> {
override fun deserialize(decoder: Decoder): Data {
TODO("Not yet implemented")
}

override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("X", PrimitiveKind.SHORT)

override fun serialize(encoder: Encoder, value: Data) {
TODO("Not yet implemented")
}

}

@Serializable
public data class IpBlock(
val type: String,
Expand All @@ -85,7 +106,7 @@ public sealed class RoutePlannerStatus<T : Data> {
@Serializable
@SerialName("RotatingIpRoutePlanner")
public data class RotatingIpRoutePlanner(override val details: Data) :
RoutePlannerStatus<RotatingNanoIpRoutePlanner.Data>() {
RoutePlannerStatus<RotatingIpRoutePlanner.Data>() {
/**
* @property rotateIndex The number of rotations which happened since the restart of Lavalink
* @property ipIndex The current offset in the block
Expand All @@ -109,7 +130,7 @@ public data class RotatingIpRoutePlanner(override val details: Data) :
@Serializable
@SerialName("NanoIpRoutePlanner")
public data class NanoIpRoutePlanner(override val details: Data) :
RoutePlannerStatus<RotatingNanoIpRoutePlanner.Data>() {
RoutePlannerStatus<NanoIpRoutePlanner.Data>() {
/**
* @property currentAddressIndex The current offset in the ip block
*/
Expand Down
6 changes: 3 additions & 3 deletions core/src/commonTest/kotlin/RetryTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dev.schlaubi.lavakord.audio.retry.LinearRetry
import kotlinx.coroutines.launch
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
Expand All @@ -11,7 +11,7 @@ import kotlin.time.seconds

class RetryTest {

//@JsName("testRetryMaxFail")
@JsName("testRetryMaxFail")
@Test
fun `check whether retry exits correctly after exceeding max`() {
val retry = LinearRetry(1.seconds, 5.seconds, 1)
Expand All @@ -23,7 +23,7 @@ class RetryTest {
}
}

//@JsName("testNeverExceedsMaxBackoff")
@JsName("testNeverExceedsMaxBackoff")
@Test
fun `check linear retry never exceeds max backoff`() {
val max = 5
Expand Down
16 changes: 8 additions & 8 deletions core/src/commonTest/kotlin/json/CommandsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package json

import dev.schlaubi.lavakord.audio.internal.GatewayPayload
import json.src.*
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test

class CommandsTest {

//@JsName("testPlayCommand")
@JsName("testPlayCommand")
@Test
fun `test play command serialization`() {
test<GatewayPayload.PlayCommand>(PLAY_COMMAND) {
Expand All @@ -19,46 +19,46 @@ class CommandsTest {
}
}

//@JsName("testStopCommand")
@JsName("testStopCommand")
@Test
fun `test stop command serialization`() {
test<GatewayPayload.StopCommand>(STOP_COMMAND)
}


//@JsName("testPauseCommand")
@JsName("testPauseCommand")
@Test
fun `test pause command serialization`() {
test<GatewayPayload.PauseCommand>(PAUSE_COMMAND) {
pause shouldBe true
}
}

//@JsName("testSeekCommand")
@JsName("testSeekCommand")
@Test
fun `test seek command serialization`() {
test<GatewayPayload.SeekCommand>(SEEK_COMMAND) {
position shouldBe 60000
}
}

//@JsName("testVolumeCommand")
@JsName("testVolumeCommand")
@Test
fun `test volume command serialization`() {
test<GatewayPayload.VolumeCommand>(VOLUME_COMMAND) {
volume shouldBe 125
}
}

//@JsName("testEqCommand")
@JsName("testEqCommand")
@Test
fun `test equalizer command serialization`() {
test<GatewayPayload.EqualizerCommand>(EQUALIZER_COMMAND) {
bands shouldBe listOf(GatewayPayload.EqualizerCommand.Band(0, 0.2F))
}
}

//@JsName("testDestroyCommand")
@JsName("testDestroyCommand")
@Test
fun `test destroy command serialization`() {
test<GatewayPayload.DestroyCommand>(DESTROY_COMMAND)
Expand Down
8 changes: 4 additions & 4 deletions core/src/commonTest/kotlin/json/EventsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package json

import dev.schlaubi.lavakord.audio.internal.GatewayPayload
import json.src.*
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertNotNull

class EventsTest {

//@JsName("testPlayerUpdateEvent")
@JsName("testPlayerUpdateEvent")
@Test
fun `test player update event serialization`() {
test<GatewayPayload.PlayerUpdateEvent>(PLAYER_UPDATE_EVENT) {
state shouldBe GatewayPayload.PlayerUpdateEvent.State(1500467109, 1500467109)
}
}

//@JsName("testStatsEvent")
@JsName("testStatsEvent")
@Test
fun `test stats event serialization`() {
fun GatewayPayload.StatsEvent.validateBasic() {
Expand Down Expand Up @@ -52,7 +52,7 @@ class EventsTest {
}
}

//@JsName("testEmittedEvent")
@JsName("testEmittedEvent")
@Test
fun `test emitted event serialization`() {
fun GatewayPayload.EmittedEvent.validateBasic(type: String) {
Expand Down
9 changes: 4 additions & 5 deletions core/src/commonTest/kotlin/json/RoutePlannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dev.schlaubi.lavakord.rest.RoutePlannerStatus
import json.src.NANO_IP_ROUTE_PLANNER
import json.src.ROTATING_IP_ROUTE_PLANNER
import json.src.ROTATING_NANO_IP_ROUTE_PLANNER
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertTrue

Expand All @@ -27,7 +27,7 @@ fun List<RoutePlannerStatus.Data.FailingAddress>.validate() {

class RoutePlannerTest {

//@JsName("testRotatingNanoIpRoutePlanner")
@JsName("testRotatingNanoIpRoutePlanner")
@Test
fun `test rotating nano ip route planner`() {
test<RotatingNanoIpRoutePlanner>(ROTATING_NANO_IP_ROUTE_PLANNER) {
Expand All @@ -41,8 +41,7 @@ class RoutePlannerTest {
}
}


//@JsName("testRotatingIpRoutePlanner")
@JsName("testRotatingIpRoutePlanner")
@Test
fun `test rotating ip route planner`() {
test<RotatingIpRoutePlanner>(ROTATING_IP_ROUTE_PLANNER) {
Expand All @@ -57,7 +56,7 @@ class RoutePlannerTest {
}
}

//@JsName("testNanoIpRoutePlanner")
@JsName("testNanoIpRoutePlanner")
@Test
fun `test nano ip route planner`() {
test<NanoIpRoutePlanner>(NANO_IP_ROUTE_PLANNER) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/commonTest/kotlin/json/TrackParsingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package json
import Tests.runBlocking
import dev.schlaubi.lavakord.audio.player.Track
import json.src.TRACK
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.time.seconds

class TrackParsingTest {
//@JsName("testNeverGonnaGiveYouUp")
@JsName("testNeverGonnaGiveYouUp")
@Test
fun `NEVVA GONNA GIVE YOU UP`() {
Tests.runBlocking {
Expand Down
10 changes: 5 additions & 5 deletions core/src/commonTest/kotlin/json/TrackRestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package json

import dev.schlaubi.lavakord.rest.TrackResponse
import json.src.*
//import kotlin.js.JsName
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertTrue

Expand All @@ -23,7 +23,7 @@ val neverGonnaGiveYouUp = TrackResponse.PartialTrack(
class TrackRestTest {


//@JsName("testSingleTrack")
@JsName("testSingleTrack")
@Test
fun `test single track loaded`() {
testRest<TrackResponse, TrackResponse>(TRACK_LOADED) {
Expand All @@ -32,7 +32,7 @@ class TrackRestTest {
}
}

//@JsName("testPlaylistAndSearchResult")
@JsName("testPlaylistAndSearchResult")
@Test
fun `test playlist and searchResult loaded`() {
fun TrackResponse.validateTracks() {
Expand All @@ -54,7 +54,7 @@ class TrackRestTest {
}
}

//@JsName("testNoResults")
@JsName("testNoResults")
@Test
fun `test no results found`() {
testRest<TrackResponse, TrackResponse>(NO_MATCHES) {
Expand All @@ -63,7 +63,7 @@ class TrackRestTest {
}
}

//@JsName("testLoadFailed")
@JsName("testLoadFailed")
@Test
fun `test load failed`() {
testRest<TrackResponse, TrackResponse>(LOAD_FAILED) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonTest/kotlin/json/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal inline fun <reified T : GatewayPayload> test(input: String, crossinline
)

fun MockRequestHandleScope.respondJson(json: String) =
respond(json, HttpStatusCode.OK, headersOf("Content-Type" to listOf("application/json")))
respond(json, HttpStatusCode.OK, headersOf("Content-Type" to listOf(ContentType.Application.Json.toString())))


@JvmName("testRoutePlannerStatus")
Expand Down
Loading

0 comments on commit 62371f1

Please sign in to comment.