TogetherViewModelTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/together/TogetherViewModelTest.kt
1@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)23package eu.friedcutlet.yokan.ui.screens.together45import eu.friedcutlet.yokan.database.DbSyncPreference6import eu.friedcutlet.yokan.database.InMemoryYokanDatabase7import eu.friedcutlet.yokan.database.emptySnapshot8import eu.friedcutlet.yokan.domain.ProfileId9import eu.friedcutlet.yokan.services.http.ProfileTestEngine10import eu.friedcutlet.yokan.services.together.HubSession11import eu.friedcutlet.yokan.services.together.ProfileTogetherSession12import eu.friedcutlet.yokan.ui.ViewModelTestHost13import kotlin.test.AfterTest14import kotlin.test.Test15import kotlin.test.assertEquals16import kotlin.test.assertNull17import kotlinx.coroutines.CoroutineScope18import kotlinx.coroutines.awaitCancellation19import kotlinx.coroutines.test.runCurrent20import kotlinx.coroutines.test.runTest21import org.jetbrains.compose.resources.getString22import yokan.composeapp.generated.resources.Res23import yokan.composeapp.generated.resources.together_login_to_join2425class TogetherViewModelTest {26 private val host = ViewModelTestHost()2728 @AfterTest29 fun tearDown() = host.close()3031 @Test32 fun joinConnectsTheRoomAndLeaveClearsIt() = runTest {33 val engine = ProfileTestEngine(host.dispatcher) { awaitCancellation() }34 val together = together(backgroundScope) { engine }35 val vm = host.create { TogetherViewModel(together, { Hub }, host.dispatcher) }3637 vm.join("abc")38 runCurrent()3940 assertEquals("ABC", vm.state.value.client?.code)41 assertEquals("ABC", vm.state.value.room?.code)42 assertNull(vm.state.value.message)4344 vm.leave()45 runCurrent()4647 assertNull(together.client.value)48 assertNull(vm.state.value.room)49 assertEquals(1, engine.closeCalls)50 }5152 @Test53 fun joinWithoutHubAsksToLogInAndDismissClearsTheMessage() = runTest {54 val together = together(backgroundScope) { error("No hub, no socket") }55 val vm = host.create { TogetherViewModel(together, { null }, host.dispatcher) }5657 vm.join("ABC")58 runCurrent()5960 assertEquals(getString(Res.string.together_login_to_join), vm.state.value.message)61 assertNull(together.client.value)6263 vm.dismissMessage()6465 assertNull(vm.state.value.message)66 }6768 @Test69 fun joinFailureBecomesAMessage() = runTest {70 val together = together(backgroundScope) { error("Hub unreachable") }71 val vm = host.create { TogetherViewModel(together, { Hub }, host.dispatcher) }7273 vm.join("ABC")74 runCurrent()7576 assertEquals("Hub unreachable", vm.state.value.message)77 assertNull(vm.state.value.room)78 }7980 private fun together(scope: CoroutineScope, engine: (ProfileId) -> ProfileTestEngine) =81 ProfileTogetherSession(ProfileId.Default, database(), scope, engine)8283 private fun database() = InMemoryYokanDatabase(emptySnapshot()).also { it.upsertSyncPreference(Preference) }8485 private companion object {86 val Preference = DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "owner-token", deviceId = "owner-device")87 val Hub = HubSession("https://hub.test", "owner-token", "owner-device")88 }89}90