Fix test code

This commit is contained in:
loucass003 committed 2026-04-13 13:13:42 +02:00
1 parent 4bf1e706d3
commit 2d6b250539
3 files changed
+41 -9

No files matched your search

@@ -1,9 +1,15 @@
package dev.slimevr
import dev.llelievr.espflashkotlin.FlasherSerialInterface
import dev.slimevr.config.DefaultUserBehaviour
import dev.slimevr.config.UserConfig
import dev.slimevr.config.UserConfigData
import dev.slimevr.config.UserConfigState
import dev.slimevr.context.Context
import dev.slimevr.serial.SerialPortHandle
import dev.slimevr.serial.SerialServer
import kotlinx.coroutines.CoroutineScope
import java.nio.file.Files
fun buildTestSerialServer(scope: CoroutineScope) = SerialServer.create(
openPort = { loc, _, _ -> SerialPortHandle(loc, "Fake $loc", {}, {}) },
@@ -25,3 +31,17 @@ fun buildTestSerialServer(scope: CoroutineScope) = SerialServer.create(
)
fun buildTestVrServer(scope: CoroutineScope): VRServer = VRServer.create(scope)
fun buildTestUserConfig(scope: CoroutineScope): UserConfig {
val tempDir = Files.createTempDirectory("slimevr-test").toFile()
tempDir.deleteOnExit()
val behaviours = listOf(DefaultUserBehaviour)
val context = Context.create(
initialState = UserConfigState(data = UserConfigData(), name = "test"),
scope = scope,
behaviours = behaviours,
)
val userConfig = UserConfig(context, scope = scope, userConfigDir = tempDir)
behaviours.forEach { it.observe(userConfig) }
return userConfig
}
@@ -2,6 +2,7 @@
package dev.slimevr.heightcalibration
import dev.slimevr.buildTestUserConfig
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.Job
@@ -51,8 +52,9 @@ private fun TestScope.launchSession(
controllerFlow: MutableSharedFlow<TrackerSnapshot>,
): Job {
val scope = this
val userConfig = buildTestUserConfig(backgroundScope)
return launch {
runCalibrationSession(context, hmdFlow, controllerFlow, clock = { scope.currentTime * 1_000_000L })
runCalibrationSession(context, userConfig, hmdFlow, controllerFlow, clock = { scope.currentTime * 1_000_000L })
}
}
@@ -290,7 +292,7 @@ class HeightCalibrationSessionTest {
val context = makeContext(this)
val controllerFlow = MutableSharedFlow<TrackerSnapshot>(extraBufferCapacity = 1)
val hmdFlow = MutableSharedFlow<TrackerSnapshot>(extraBufferCapacity = 1)
val job = launch { runCalibrationSession(context, hmdFlow, controllerFlow) }
val job = launch { runCalibrationSession(context, buildTestUserConfig(backgroundScope), hmdFlow, controllerFlow) }
advanceTimeBy(TIMEOUT_MS + 1)
@@ -1,21 +1,31 @@
package dev.slimevr.solarxr
import dev.slimevr.buildTestUserConfig
import dev.slimevr.buildTestVrServer
import dev.slimevr.skeleton.Skeleton
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import solarxr_protocol.MessageBundle
import solarxr_protocol.data_feed.DataFeedConfig
import solarxr_protocol.data_feed.PollDataFeed
import solarxr_protocol.data_feed.StartDataFeed
import kotlin.test.Test
import kotlin.test.assertEquals
private fun testConn(backgroundScope: kotlinx.coroutines.CoroutineScope, onSend: suspend (ByteArray) -> Unit) = SolarXRConnection.create(
buildTestVrServer(backgroundScope),
onSend = onSend,
scope = backgroundScope,
behaviours = listOf(DataFeedInitBehaviour),
)
private fun testConn(backgroundScope: kotlinx.coroutines.CoroutineScope, onSend: suspend (ByteArray) -> Unit): SolarXRBridge {
val server = buildTestVrServer(backgroundScope)
val userConfig = buildTestUserConfig(backgroundScope)
val skeleton = Skeleton.create(backgroundScope, userConfig)
val bridge = SolarXRBridge.create(
id = 1,
serverContext = server,
scope = backgroundScope,
behaviours = listOf(DataFeedInitBehaviour(server, skeleton)),
)
bridge.outbound.on<MessageBundle> { _ -> onSend(ByteArray(0)) }
return bridge
}
private fun config(intervalMs: Int) = DataFeedConfig(minimumTimeSinceLast = intervalMs.toUShort())
@@ -91,4 +101,4 @@ class DataFeedTest {
}
// TODO: need more tests for the content of a datafeed + check if the masks work
}
}