Flightlist steps + unit tests

This commit is contained in:
loucass003 committed 2026-07-29 04:02:25 +02:00
1 parent 6562d41721
commit 05e8faeb34
9 files changed
+521 -18

No files matched your search

@@ -12,6 +12,7 @@ import dev.slimevr.config.UserConfig
import dev.slimevr.config.UserConfigData
import dev.slimevr.config.UserConfigState
import dev.slimevr.context.Context
import dev.slimevr.device.DeviceOrigin
import dev.slimevr.firmware.FirmwareManager
import dev.slimevr.heightcalibration.HeightCalibrationActions
import dev.slimevr.heightcalibration.HeightCalibrationManager
@@ -27,19 +28,30 @@ import dev.slimevr.resets.ResetsState
import dev.slimevr.serial.FlashingHandler
import dev.slimevr.serial.SerialPortHandle
import dev.slimevr.serial.SerialServer
import dev.slimevr.skeleton.ComputedSkeleton
import dev.slimevr.skeleton.DEFAULT_SKELETON_STATE
import dev.slimevr.skeleton.ProportionsBehaviour
import dev.slimevr.skeleton.Skeleton
import dev.slimevr.skeleton.buildBones
import dev.slimevr.tapdetection.TapDetectionManager
import dev.slimevr.tracker.Tracker
import dev.slimevr.tracker.TrackerBasicBehaviour
import dev.slimevr.tracker.TrackerState
import dev.slimevr.trackingchecklist.TrackingChecklist
import dev.slimevr.udp.UdpServer
import dev.slimevr.vmc.VMCManager
import dev.slimevr.vrchat.VRCConfigManager
import dev.slimevr.vrcosc.VRCOSCManager
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.datatypes.MagnetometerStatus
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.datatypes.hardware_info.ImuType
import solarxr_protocol.rpc.UserHeightCalibrationStatus
import kotlin.time.Duration
fun buildTestSerialServer(scope: CoroutineScope) = SerialServer.create(
openPort = { loc, _, _ -> SerialPortHandle(loc, "Fake $loc", {}, {}) },
@@ -87,7 +99,7 @@ fun buildTestSkeleton(scope: CoroutineScope): Skeleton {
behaviours = listOf(ProportionsBehaviour(buildTestUserConfig(scope))),
name = "TestSkeleton",
)
val skeleton = Skeleton(context, MutableStateFlow(buildBones(context.state.value)))
val skeleton = Skeleton(context, MutableStateFlow(ComputedSkeleton(bones = buildBones(context.state.value), frameTime = Duration.ZERO)))
skeleton.startObserving()
return skeleton
}
@@ -108,6 +120,51 @@ fun buildTestResetsManager(server: VRServer, settings: Settings, scope: Coroutin
return resetsManager
}
fun buildTestTracker(
scope: CoroutineScope,
appContext: AppContextProvider,
settings: Settings,
id: Int,
bodyPart: BodyPart? = null,
status: TrackerStatus = TrackerStatus.DISCONNECTED,
origin: DeviceOrigin = DeviceOrigin.UDP,
sensorType: ImuType? = ImuType.BNO085,
position: Vector3? = null,
completedRestCalibration: Boolean? = true,
): Tracker {
val state = TrackerState(
id = id,
hardwareId = "test-$id",
name = "Tracker $id",
restOrientation = Quaternion.IDENTITY,
rawRotation = Quaternion.IDENTITY,
rotation = Quaternion.IDENTITY,
rawAcceleration = Vector3.NULL,
acceleration = Vector3.NULL,
rawMagnetometer = Vector3.NULL,
bodyPart = bodyPart,
mountingOrientation = Quaternion.IDENTITY,
origin = origin,
deviceId = 0,
customName = null,
sensorType = sensorType,
position = position,
tps = 0u,
imuTemp = null,
status = status,
completedRestCalibration = completedRestCalibration,
magStatus = MagnetometerStatus.NOT_SUPPORTED,
sessionCalibration = null,
)
val context = Context.create(
initialState = state,
scope = scope,
behaviours = listOf(TrackerBasicBehaviour()),
name = "TestTracker[$id]",
)
return Tracker(context, appContext, settings)
}
fun buildTestSettings(scope: CoroutineScope): Settings {
val initialState = SettingsState(data = SettingsConfigState(), name = "test")
val context = Context.create<SettingsState, dev.slimevr.config.SettingsActions>(
@@ -0,0 +1,251 @@
package dev.slimevr.trackingchecklist
import dev.slimevr.TestAppContext
import dev.slimevr.VRServer
import dev.slimevr.VRServerActions
import dev.slimevr.buildTestResetsManager
import dev.slimevr.buildTestSettings
import dev.slimevr.buildTestTracker
import dev.slimevr.buildTestVrServer
import dev.slimevr.config.Settings
import dev.slimevr.device.DeviceOrigin
import dev.slimevr.networkprofile.NetworkInfo
import dev.slimevr.networkprofile.NetworkProfileActions
import dev.slimevr.networkprofile.NetworkProfileManager
import dev.slimevr.resets.FOOT_PARTS
import dev.slimevr.resets.ResetsActions
import dev.slimevr.resets.ResetsManager
import dev.slimevr.tracker.Tracker
import dev.slimevr.tracker.TrackerActions
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.datatypes.hardware_info.ImuType
import solarxr_protocol.rpc.ResetType
import solarxr_protocol.rpc.TrackingChecklistStep
import solarxr_protocol.rpc.TrackingChecklistStepId
import kotlin.test.Test
import kotlin.test.assertEquals
@OptIn(ExperimentalCoroutinesApi::class)
class TrackingChecklistTest {
// Registers every step behaviour together, matching production wiring, so the tests also cover
// that the behaviours coexist without clobbering each other's steps.
private class Harness(scope: TestScope, networkSupported: Boolean = true) {
val server: VRServer = buildTestVrServer(scope.backgroundScope)
val settings: Settings = buildTestSettings(scope.backgroundScope)
val resetsManager: ResetsManager = buildTestResetsManager(server, settings, scope.backgroundScope)
val networkProfileManager: NetworkProfileManager = NetworkProfileManager.create(scope.backgroundScope, isSupported = networkSupported)
private val appContext = object : TestAppContext() {
override val server = this@Harness.server
override val resetsManager = this@Harness.resetsManager
}
private val checklist = TrackingChecklist.create(scope.backgroundScope)
private var nextId = 1
init {
checklist.context.behaviours.addAll(
listOf(
HMDCheckBehaviour(server),
TrackerRestCheckBehaviour(server),
TrackerErrorCheckBehaviour(server),
FullResetCheckBehaviour(server, resetsManager),
MountingCalibrationCheckBehaviour(server, resetsManager, settings),
FeetMountingCalibrationCheckBehaviour(server, resetsManager, settings),
NetworkProfileCheckBehaviour(networkProfileManager),
),
)
checklist.context.observeAll(checklist)
}
fun addTracker(
bodyPart: BodyPart?,
status: TrackerStatus = TrackerStatus.OK,
origin: DeviceOrigin = DeviceOrigin.UDP,
sensorType: ImuType? = ImuType.BNO085,
position: Vector3? = null,
completedRestCalibration: Boolean? = true,
): Tracker {
val id = nextId++
val tracker = buildTestTracker(
server.context.scope,
appContext,
settings,
id = id,
bodyPart = bodyPart,
status = status,
origin = origin,
sensorType = sensorType,
position = position,
completedRestCalibration = completedRestCalibration,
)
server.context.dispatch(VRServerActions.NewTracker(id, tracker))
return tracker
}
fun step(id: TrackingChecklistStepId): TrackingChecklistStep = checklist.context.state.value.steps[id] ?: error("unknown step $id")
}
@Test
fun `FULL_RESET is disabled with no trackers`() = runTest {
val h = Harness(this)
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.FULL_RESET).enabled)
assertEquals(true, h.step(TrackingChecklistStepId.FULL_RESET).valid)
}
@Test
fun `FULL_RESET flags a connected assigned tracker and clears on full reset`() = runTest {
val h = Harness(this)
val tracker = h.addTracker(BodyPart.CHEST)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FULL_RESET).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.FULL_RESET).valid)
h.resetsManager.context.dispatch(ResetsActions.EndReset(ResetType.FULL))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FULL_RESET).valid)
// Reassigning the tracker flags it again
tracker.context.dispatch(TrackerActions.Update { copy(bodyPart = BodyPart.HIP) })
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.FULL_RESET).valid)
}
@Test
fun `FULL_RESET flags a tracker that reconnects`() = runTest {
val h = Harness(this)
val tracker = h.addTracker(BodyPart.CHEST)
runCurrent()
h.resetsManager.context.dispatch(ResetsActions.EndReset(ResetType.FULL))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FULL_RESET).valid)
// Disconnect then reconnect
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.DISCONNECTED))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FULL_RESET).valid)
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.FULL_RESET).valid)
}
@Test
fun `MOUNTING_CALIBRATION is enabled with an IMU tracker and valid after a mounting reset`() = runTest {
val h = Harness(this)
h.addTracker(BodyPart.CHEST)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.MOUNTING_CALIBRATION).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.MOUNTING_CALIBRATION).valid)
h.resetsManager.context.dispatch(ResetsActions.EndReset(ResetType.MOUNTING, bodyParts = null))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.MOUNTING_CALIBRATION).valid)
}
@Test
fun `FEET_MOUNTING_CALIBRATION enables with a foot tracker and validates after a feet mounting reset`() = runTest {
val h = Harness(this)
h.addTracker(BodyPart.LEFT_FOOT)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION).valid)
h.resetsManager.context.dispatch(ResetsActions.EndReset(ResetType.MOUNTING, bodyParts = FOOT_PARTS.toList()))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION).valid)
}
@Test
fun `FEET_MOUNTING_CALIBRATION is disabled without a foot tracker`() = runTest {
val h = Harness(this)
h.addTracker(BodyPart.CHEST)
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION).enabled)
}
@Test
fun `UNASSIGNED_HMD is invalid until the HMD is assigned to the head`() = runTest {
val h = Harness(this)
// An HMD is a DRIVER-origin tracker with a computed position
val hmd = h.addTracker(bodyPart = null, origin = DeviceOrigin.DRIVER, sensorType = null, position = Vector3.NULL)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.UNASSIGNED_HMD).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.UNASSIGNED_HMD).valid)
hmd.context.dispatch(TrackerActions.Update { copy(bodyPart = BodyPart.HEAD) })
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.UNASSIGNED_HMD).valid)
}
@Test
fun `UNASSIGNED_HMD is disabled without a driver tracker`() = runTest {
val h = Harness(this)
h.addTracker(BodyPart.CHEST)
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.UNASSIGNED_HMD).enabled)
}
@Test
fun `TRACKER_ERROR is invalid while an assigned tracker is in error`() = runTest {
val h = Harness(this)
val tracker = h.addTracker(BodyPart.CHEST, status = TrackerStatus.ERROR)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.TRACKER_ERROR).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.TRACKER_ERROR).valid)
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.TRACKER_ERROR).valid)
}
@Test
fun `TRACKERS_REST_CALIBRATION is invalid until an uncalibrated tracker finishes rest calibration`() = runTest {
val h = Harness(this)
val tracker = h.addTracker(BodyPart.CHEST, completedRestCalibration = false)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.TRACKERS_REST_CALIBRATION).enabled)
assertEquals(false, h.step(TrackingChecklistStepId.TRACKERS_REST_CALIBRATION).valid)
tracker.context.dispatch(TrackerActions.Update { copy(completedRestCalibration = true) })
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.TRACKERS_REST_CALIBRATION).valid)
}
@Test
fun `NETWORK_PROFILE_PUBLIC is invalid while a public network is present`() = runTest {
val h = Harness(this)
runCurrent()
assertEquals(true, h.step(TrackingChecklistStepId.NETWORK_PROFILE_PUBLIC).enabled)
assertEquals(true, h.step(TrackingChecklistStepId.NETWORK_PROFILE_PUBLIC).valid)
h.networkProfileManager.context.dispatch(
NetworkProfileActions.UpdateNetworks(listOf(NetworkInfo("wlan0", null, null, null, null))),
)
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.NETWORK_PROFILE_PUBLIC).valid)
}
@Test
fun `NETWORK_PROFILE_PUBLIC is disabled when unsupported`() = runTest {
val h = Harness(this, networkSupported = false)
runCurrent()
assertEquals(false, h.step(TrackingChecklistStepId.NETWORK_PROFILE_PUBLIC).enabled)
}
}