Yokan / rebuild
Course 1 8624561

StateSyncSupportTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/sync/StateSyncSupportTest.kt

test / fixture59 lines
1package eu.friedcutlet.yokan.services.sync23import eu.friedcutlet.yokan.database.DbManga4import eu.friedcutlet.yokan.database.DbSyncPreference5import eu.friedcutlet.yokan.database.InMemoryYokanDatabase6import eu.friedcutlet.yokan.database.SyncServerProvider7import eu.friedcutlet.yokan.database.emptySnapshot8import eu.friedcutlet.yokan.services.backup.BackupFormat9import eu.friedcutlet.yokan.services.backup.BackupOptions10import kotlinx.coroutines.CompletableDeferred11import kotlinx.coroutines.awaitCancellation12import kotlinx.coroutines.launch13import kotlinx.coroutines.sync.Mutex14import kotlinx.coroutines.test.runTest15import kotlin.test.Test16import kotlin.test.assertEquals17import kotlin.test.assertNull18import kotlin.test.assertTrue1920class StateSyncSupportTest {21    @Test22    fun guardedRestoreRefusingChangedAccountBecomesAccountChangedFailure() = runTest {23        val remote = backup(InMemoryYokanDatabase(emptySnapshot().copy(mangas = listOf(DbManga(id = 7, name = "Remote", link = "/remote", source = "src", favorite = true)))))24            .exportBackup(BackupOptions(format = BackupFormat.Tachiyomi, compressionLevel = 0)).bytes25        val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(Captured.copy(authToken = "key-2"))))2627        val result = runStateSync(SyncOperationMonitor(), Mutex(), SyncOperationStage.Syncing, "Test") {28            backup(db).mergeRemoteState(remote, Captured, "Test")29            SyncResult(0, 0, false, emptyList())30        }3132        assertEquals(listOf(SyncAccountChangedMessage), result.errors)33        assertTrue(db.mangas().none { it.name == "Remote" })34    }3536    @Test37    fun cancellingTheCallerPropagatesInsteadOfReturningAResult() = runTest {38        val entered = CompletableDeferred<Unit>()39        var result: SyncResult? = null40        val caller = launch {41            result = runStateSync(SyncOperationMonitor(), Mutex(), SyncOperationStage.Syncing, "Test") {42                entered.complete(Unit)43                awaitCancellation()44            }45        }46        entered.await()47        caller.cancel()48        caller.join()4950        assertTrue(caller.isCancelled)51        assertNull(result)52    }5354    private companion object {55        val Captured = DbSyncPreference(56            syncId = 1, server = "https://sync.test", authToken = "key-1", syncOn = true, serverProvider = SyncServerProvider.SyncYomi,57        )58    }59}60

Search chapters, reference and source filenames.