Yokan / rebuild
Course 1 8624561

HybridSyncServiceTest.kt

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

test / fixture74 lines
1package eu.friedcutlet.yokan.services.sync23import eu.friedcutlet.yokan.database.DbSettings4import 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.BackupOptions9import kotlin.test.Test10import kotlin.test.assertEquals11import kotlinx.coroutines.test.runTest1213class HybridSyncServiceTest {14    @Test15    fun routesEveryCallToTheSelectedProvider() = runTest {16        val cases = listOf(17            DbSyncPreference(syncId = 1, server = "https://hub.test") to "hub",18            DbSyncPreference(syncId = 1, server = cloudServerUri("google-drive")) to "cloud",19            DbSyncPreference(syncId = 1, server = "https://m.test", serverProvider = SyncServerProvider.Mangayomi) to "mangayomi",20            DbSyncPreference(syncId = 1, server = "https://sync.test", serverProvider = SyncServerProvider.SyncYomi) to "syncyomi",21            // A state-sync provider owns its calls even if a cloud:// server string was left behind.22            DbSyncPreference(syncId = 1, server = cloudServerUri("google-drive"), serverProvider = SyncServerProvider.GoogleDrive) to "gdrive",23        )24        for ((preference, expected) in cases) {25            val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(preference)))26            val hub = Recorder("hub"); val cloud = CloudRecorder(); val mangayomi = Recorder("mangayomi")27            val syncYomi = Recorder("syncyomi"); val drive = Recorder("gdrive")28            val calls = listOf(hub, cloud, mangayomi, syncYomi, drive)29            val hybrid = HybridSyncService(hub, cloud, db, mangayomiSync = mangayomi, syncYomiSync = syncYomi, googleDriveSync = drive)3031            hybrid.sync(1, DbSettings()); hybrid.pushChanges(1); hybrid.pullChanges(1); hybrid.status(1); hybrid.logout(1)32            assertEquals(List(5) { expected }, calls.flatMap { it.calls }, "routing for $expected")33        }34    }3536    @Test37    fun stateSyncProvidersOwnLoginAndBrowserSignIn() = runTest {38        for ((provider, expected) in listOf(SyncServerProvider.SyncYomi to "syncyomi", SyncServerProvider.GoogleDrive to "gdrive")) {39            val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(DbSyncPreference(syncId = 1, serverProvider = provider))))40            val hub = Recorder("hub"); val cloud = CloudRecorder(); val syncYomi = Recorder("syncyomi"); val drive = Recorder("gdrive")41            val hybrid = HybridSyncService(hub, cloud, db, syncYomiSync = syncYomi, googleDriveSync = drive)42            hybrid.login(1, "cloud://google-drive", "", "secret")43            hybrid.beginBrowserLogin(1, "client")44            assertEquals(listOf(expected, expected), listOf(hub, cloud, syncYomi, drive).flatMap { it.calls })45        }46    }4748    @Test49    fun missingStateSyncServiceReportsUnavailable() = runTest {50        val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(DbSyncPreference(syncId = 1, serverProvider = SyncServerProvider.GoogleDrive))))51        val hybrid = HybridSyncService(Recorder("hub"), CloudRecorder(), db)52        assertEquals(listOf("Google Drive sync is unavailable."), hybrid.sync(1, DbSettings()).errors)53    }5455    private open class Recorder(private val name: String) : SyncService {56        val calls = mutableListOf<String>()57        private fun record() { calls += name }58        override suspend fun login(syncId: Long, server: String, email: String, password: String): SyncLoginResult { record(); return SyncLoginResult(true, name) }59        override fun beginBrowserLogin(syncId: Long, server: String): BrowserLoginAttempt? { record(); return null }60        override suspend fun logout(syncId: Long) = record()61        override suspend fun sync(syncId: Long, settings: DbSettings): SyncResult { record(); return SyncResult(0, 0, false) }62        override suspend fun pushChanges(syncId: Long): SyncResult { record(); return SyncResult(0, 0, false) }63        override suspend fun pullChanges(syncId: Long): SyncResult { record(); return SyncResult(0, 0, false) }64        override fun status(syncId: Long): SyncConnectionStatus { record(); return SyncConnectionStatus(true, null, name, null) }65    }6667    private class CloudRecorder : Recorder("cloud"), CloudBackupSyncService {68        override fun providers(): List<CloudProviderDescriptor> = emptyList()69        override fun isCloudPreference(syncPreference: DbSyncPreference?): Boolean = syncPreference?.server?.startsWith(CloudServerPrefix) == true70        override suspend fun connect(syncId: Long, providerId: String, accountLabel: String?, accessToken: String) = SyncLoginResult(true, "")71        override suspend fun uploadBackup(syncId: Long, options: BackupOptions): CloudBackupResult = error("unused")72        override suspend fun restoreLatestBackup(syncId: Long): CloudBackupRestoreResult = error("unused")73    }74}75

Search chapters, reference and source filenames.