Sentry to Server / Electron / Android(untested)

This commit is contained in:
loucass003 committed 2026-09-29 23:35:39 +02:00
1 parent 87c9190354
commit a0c90f2355
75 files changed
+1964 -289

No files matched your search

@@ -23,6 +23,7 @@ import dev.slimevr.resets.ResetsManager
import dev.slimevr.resets.ResetsMountingTimeoutBehaviour
import dev.slimevr.resets.ResetsState
import dev.slimevr.routing.BoneRoutingManager
import dev.slimevr.sentry.ErrorReportingManager
import dev.slimevr.serial.SerialServer
import dev.slimevr.skeleton.ComputedSkeleton
import dev.slimevr.skeleton.DEFAULT_SKELETON_STATE
@@ -98,6 +99,11 @@ fun buildTestSkeleton(scope: CoroutineScope): Skeleton {
return skeleton
}
fun buildTestErrorReporting(
scope: CoroutineScope,
globalConfig: GlobalConfig = buildTestAppConfig(scope).globalConfig,
): ErrorReportingManager = ErrorReportingManager.create(scope, globalConfig).also { it.startObserving() }
fun buildTestResetsManager(server: VRServer, settings: Settings, scope: CoroutineScope): ResetsManager {
val context = Context.create(
initialState = ResetsState(
@@ -110,7 +116,7 @@ fun buildTestResetsManager(server: VRServer, settings: Settings, scope: Coroutin
behaviours = listOf(ResetsMountingTimeoutBehaviour()),
name = "TestResetsManager",
)
val resetsManager = ResetsManager(context, server, settings, buildTestSkeleton(scope))
val resetsManager = ResetsManager(context, server, settings, buildTestSkeleton(scope), buildTestErrorReporting(scope))
resetsManager.startObserving()
return resetsManager
}
@@ -181,7 +187,7 @@ fun buildTestHeightCalibration(server: VRServer, userConfig: UserConfig, scope:
behaviours = emptyList(),
name = "HeightCalibration[test]",
)
return HeightCalibrationManager(context, server, userConfig)
return HeightCalibrationManager(context, server, userConfig, buildTestErrorReporting(scope))
}
private object NoopConfigStorage : ConfigStorage {
@@ -243,6 +249,7 @@ abstract class TestAppContext : AppContextProvider {
override val skeleton: Skeleton get() = error("not used in test")
override val config: AppConfig get() = error("not used in test")
override val serialServer: SerialServer get() = error("not used in test")
override val errorReporting: ErrorReportingManager get() = error("not used in test")
override val serverInfos: ServerInfos get() = error("not used in test")
override val firmwareManager: FirmwareManager get() = error("not used in test")
override val vrcConfigManager: VRCConfigManager? = null
@@ -0,0 +1,85 @@
package dev.slimevr.sentry
import solarxr_protocol.rpc.ErrorReportingConsent
import kotlin.test.Test
import kotlin.test.assertEquals
class ConsentGateTest {
private fun gate(capacity: Int = 3): Pair<ConsentGate<Int>, MutableList<Int>> {
val sent = mutableListOf<Int>()
return ConsentGate<Int>(capacity) { sent.add(it) } to sent
}
@Test
fun `undecided holds and sends nothing`() {
val (gate, sent) = gate()
gate.offer(1)
gate.offer(2)
assertEquals(emptyList(), sent)
}
@Test
fun `undecided keeps only the newest items`() {
val (gate, sent) = gate(capacity = 3)
(1..5).forEach(gate::offer)
gate.apply(ErrorReportingConsent.ALLOWED)
assertEquals(listOf(3, 4, 5), sent)
}
@Test
fun `allowed flushes held items in order then passes through`() {
val (gate, sent) = gate()
gate.offer(1)
gate.offer(2)
gate.apply(ErrorReportingConsent.ALLOWED)
gate.offer(3)
assertEquals(listOf(1, 2, 3), sent)
}
@Test
fun `denied clears held items and drops new ones`() {
val (gate, sent) = gate()
gate.offer(1)
gate.apply(ErrorReportingConsent.DENIED)
gate.offer(2)
assertEquals(emptyList(), sent)
}
@Test
fun `allowed then denied stops sending immediately`() {
val (gate, sent) = gate()
gate.apply(ErrorReportingConsent.ALLOWED)
gate.offer(1)
gate.apply(ErrorReportingConsent.DENIED)
gate.offer(2)
assertEquals(listOf(1), sent)
}
@Test
fun `denied then allowed only sends what comes after`() {
val (gate, sent) = gate()
gate.apply(ErrorReportingConsent.DENIED)
gate.offer(1)
gate.apply(ErrorReportingConsent.ALLOWED)
gate.offer(2)
assertEquals(listOf(2), sent)
}
@Test
fun `going back to undecided holds again`() {
val (gate, sent) = gate()
gate.apply(ErrorReportingConsent.ALLOWED)
gate.apply(ErrorReportingConsent.UNDECIDED)
gate.offer(1)
assertEquals(emptyList(), sent)
gate.apply(ErrorReportingConsent.ALLOWED)
assertEquals(listOf(1), sent)
}
}
@@ -0,0 +1,99 @@
package dev.slimevr.solarxr
import dev.slimevr.TestAppContext
import dev.slimevr.buildTestErrorReporting
import dev.slimevr.config.ErrorReportingConfig
import dev.slimevr.config.GlobalConfig
import dev.slimevr.config.GlobalConfigState
import dev.slimevr.config.effectiveConsent
import dev.slimevr.context.Context
import dev.slimevr.sentry.ErrorReportingManager
import dev.slimevr.solarxr.rpc.ErrorReportingSettingsBehaviour
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import solarxr_protocol.MessageBundle
import solarxr_protocol.rpc.ChangeErrorReportingSettingsRequest
import solarxr_protocol.rpc.ErrorReportingConsent
import solarxr_protocol.rpc.ErrorReportingSettingsRequest
import solarxr_protocol.rpc.ErrorReportingSettingsResponse
import kotlin.test.Test
import kotlin.test.assertEquals
import dev.slimevr.config.reduce as reduceConfig
private fun TestScope.buildGlobalConfig(errorReporting: ErrorReportingConfig): GlobalConfig {
val context = Context.create(
initialState = GlobalConfigState(errorReporting = errorReporting),
scope = backgroundScope,
reducer = ::reduceConfig,
name = "GlobalConfig[test]",
)
return GlobalConfig(context).also { it.startObserving() }
}
@OptIn(ExperimentalCoroutinesApi::class)
private fun TestScope.testConn(
id: Int,
globalConfig: GlobalConfig,
errorReporting: ErrorReportingManager,
): Pair<SolarXRBridge, List<ErrorReportingSettingsResponse>> {
val context = Context.create(
initialState = SolarXRBridgeState(dataFeedConfigs = listOf()),
scope = backgroundScope,
reducer = ::reduce,
behaviours = listOf(ErrorReportingSettingsBehaviour(globalConfig, errorReporting)),
name = "SolarXRErrorReportingTest[$id]",
)
val bridge = SolarXRBridge(id = id, context = context, appContext = object : TestAppContext() {})
val responses = mutableListOf<ErrorReportingSettingsResponse>()
bridge.outbound.on<MessageBundle> { bundle ->
bundle.rpcMsgs.orEmpty().mapNotNull { it.message as? ErrorReportingSettingsResponse }.forEach(responses::add)
}.launchIn(backgroundScope)
bridge.startObserving()
runCurrent()
return bridge to responses
}
@OptIn(ExperimentalCoroutinesApi::class)
class ErrorReportingTest {
private val fresh = ErrorReportingConfig(userId = "server-id")
@Test
fun `settings request returns the effective consent, user id and session id`() = runTest {
val globalConfig = buildGlobalConfig(fresh.copy(consent = ErrorReportingConsent.ALLOWED, consentVersion = 1))
val errorReporting = buildTestErrorReporting(backgroundScope, globalConfig)
val (conn, responses) = testConn(1, globalConfig, errorReporting)
conn.rpcDispatcher.emit(ErrorReportingSettingsRequest())
runCurrent()
assertEquals(
listOf(
ErrorReportingSettingsResponse(
consent = ErrorReportingConsent.UNDECIDED,
userId = "server-id",
sessionId = errorReporting.context.state.value.sessionId,
),
),
responses,
)
}
@Test
fun `a change is persisted and sent to every connected gui`() = runTest {
val globalConfig = buildGlobalConfig(fresh)
val errorReporting = buildTestErrorReporting(backgroundScope, globalConfig)
val (first, firstResponses) = testConn(1, globalConfig, errorReporting)
val (_, secondResponses) = testConn(2, globalConfig, errorReporting)
first.rpcDispatcher.emit(ChangeErrorReportingSettingsRequest(consent = ErrorReportingConsent.ALLOWED))
runCurrent()
val stored = globalConfig.context.state.value.errorReporting
assertEquals(ErrorReportingConsent.ALLOWED, stored.consent)
assertEquals(ErrorReportingConsent.ALLOWED, effectiveConsent(stored))
assertEquals(ErrorReportingConsent.ALLOWED, firstResponses.last().consent)
assertEquals(ErrorReportingConsent.ALLOWED, secondResponses.last().consent)
}
}
@@ -0,0 +1,50 @@
package dev.slimevr.solarxr
import dev.slimevr.TestAppContext
import dev.slimevr.buildTestAppConfig
import dev.slimevr.config.GlobalConfigActions
import dev.slimevr.config.UserConfigActions
import dev.slimevr.config.changeConsent
import dev.slimevr.context.Context
import dev.slimevr.solarxr.rpc.SettingsResetBehaviour
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import solarxr_protocol.rpc.ErrorReportingConsent
import solarxr_protocol.rpc.SettingsResetRequest
import kotlin.test.Test
import kotlin.test.assertEquals
@OptIn(ExperimentalCoroutinesApi::class)
class SettingsResetTest {
@Test
fun `reset asks for error reporting consent again and resets the user profile`() = runTest {
val config = buildTestAppConfig(backgroundScope)
val userId = config.globalConfig.context.state.value.errorReporting.userId
config.globalConfig.context.dispatch(
GlobalConfigActions.SetErrorReporting(
changeConsent(config.globalConfig.context.state.value.errorReporting, ErrorReportingConsent.ALLOWED),
),
)
config.userConfig.context.dispatch(UserConfigActions.Update { copy(userHeight = 2f) })
val context = Context.create(
initialState = SolarXRBridgeState(dataFeedConfigs = listOf()),
scope = backgroundScope,
reducer = ::reduce,
behaviours = listOf(SettingsResetBehaviour(config)),
name = "SolarXRSettingsResetTest",
)
val bridge = SolarXRBridge(id = 1, context = context, appContext = object : TestAppContext() {})
bridge.startObserving()
runCurrent()
bridge.rpcDispatcher.emit(SettingsResetRequest())
runCurrent()
val errorReporting = config.globalConfig.context.state.value.errorReporting
assertEquals(ErrorReportingConsent.UNDECIDED, errorReporting.consent)
assertEquals(userId, errorReporting.userId)
assertEquals(1.6f, config.userConfig.context.state.value.data.userHeight)
}
}