Yokan / rebuild
Course 1 8624561

SyncSettingsViewModelTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/sync/SyncSettingsViewModelTest.kt

test / fixture82 lines
1package eu.friedcutlet.yokan.ui.screens.settings.sync23import eu.friedcutlet.yokan.data.DefaultYokanRepository4import eu.friedcutlet.yokan.data.SettingsStore5import eu.friedcutlet.yokan.database.DbSettings6import eu.friedcutlet.yokan.database.DbSyncPreference7import eu.friedcutlet.yokan.database.InMemoryYokanDatabase8import eu.friedcutlet.yokan.services.backup.BackupOptions9import eu.friedcutlet.yokan.services.http.HttpService10import eu.friedcutlet.yokan.services.notifications.NoNotifications11import eu.friedcutlet.yokan.services.sync.CloudBackupResult12import eu.friedcutlet.yokan.services.sync.CloudBackupRestoreResult13import eu.friedcutlet.yokan.services.sync.CloudBackupSyncService14import eu.friedcutlet.yokan.services.sync.CloudProviderDescriptor15import eu.friedcutlet.yokan.services.sync.SyncConnectionStatus16import eu.friedcutlet.yokan.services.sync.SyncLoginResult17import eu.friedcutlet.yokan.services.sync.SyncResult18import eu.friedcutlet.yokan.services.sync.SyncService19import eu.friedcutlet.yokan.ui.ViewModelTestHost20import kotlinx.coroutines.CompletableDeferred21import kotlinx.coroutines.ExperimentalCoroutinesApi22import kotlinx.coroutines.test.runCurrent23import kotlinx.coroutines.test.runTest24import kotlin.test.AfterTest25import kotlin.test.Test26import kotlin.test.assertEquals27import kotlin.test.assertFalse28import kotlin.test.assertNull29import kotlin.test.assertTrue3031@OptIn(ExperimentalCoroutinesApi::class)32class SyncSettingsViewModelTest {33    private val host = ViewModelTestHost()34    private val dispatcher = host.dispatcher3536    @AfterTest fun after() = host.close()3738    private open class GatedSync : SyncService {39        val logins = mutableListOf<String>()40        val result = CompletableDeferred<SyncLoginResult>()4142        override suspend fun login(syncId: Long, server: String, email: String, password: String): SyncLoginResult {43            logins += email44            return result.await()45        }46        override suspend fun logout(syncId: Long) = Unit47        override suspend fun sync(syncId: Long, settings: DbSettings) = SyncResult(0, 0, false)48        override suspend fun pushChanges(syncId: Long) = SyncResult(0, 0, false)49        override suspend fun pullChanges(syncId: Long) = SyncResult(0, 0, false)50        override fun status(syncId: Long) = SyncConnectionStatus(false, null, null, null)51    }5253    private class IdleCloud : GatedSync(), CloudBackupSyncService {54        override fun providers() = emptyList<CloudProviderDescriptor>()55        override fun isCloudPreference(syncPreference: DbSyncPreference?) = false56        override suspend fun connect(syncId: Long, providerId: String, accountLabel: String?, accessToken: String) = error("unused")57        override suspend fun uploadBackup(syncId: Long, options: BackupOptions): CloudBackupResult = error("unused")58        override suspend fun restoreLatestBackup(syncId: Long): CloudBackupRestoreResult = error("unused")59    }6061    @Test fun signInIsExclusiveAndReportsTheServiceMessageWhenItEnds() = runTest(dispatcher) {62        val database = InMemoryYokanDatabase()63        val settings = SettingsStore(DefaultYokanRepository(database), backgroundScope) {}64        val sync = GatedSync()65        val http = HttpService { error("offline") }66        val vm = host.create { SyncSettingsViewModel(sync, IdleCloud(), NoNotifications, database, settings, http) }6768        vm.login(1, "https://hub.example", "first@example.com", "secret")69        vm.login(1, "https://hub.example", "second@example.com", "secret")70        runCurrent()7172        assertTrue(vm.state.value.operationInProgress)73        assertEquals(listOf("first@example.com"), sync.logins)74        assertNull(vm.state.value.syncMessage)7576        sync.result.complete(SyncLoginResult(success = false, message = "Wrong password"))77        runCurrent()7879        assertFalse(vm.state.value.operationInProgress)80        assertEquals("Wrong password", vm.state.value.syncMessage)81    }82}83

Search chapters, reference and source filenames.