package eu.friedcutlet.yokan.services.sync import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.DbSyncPreference import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.SyncServerProvider import eu.friedcutlet.yokan.database.emptySnapshot import eu.friedcutlet.yokan.services.backup.BackupOptions import kotlin.test.Test import kotlin.test.assertEquals import kotlinx.coroutines.test.runTest class HybridSyncServiceTest { @Test fun routesEveryCallToTheSelectedProvider() = runTest { val cases = listOf( DbSyncPreference(syncId = 1, server = "https://hub.test") to "hub", DbSyncPreference(syncId = 1, server = cloudServerUri("google-drive")) to "cloud", DbSyncPreference(syncId = 1, server = "https://m.test", serverProvider = SyncServerProvider.Mangayomi) to "mangayomi", DbSyncPreference(syncId = 1, server = "https://sync.test", serverProvider = SyncServerProvider.SyncYomi) to "syncyomi", // A state-sync provider owns its calls even if a cloud:// server string was left behind. DbSyncPreference(syncId = 1, server = cloudServerUri("google-drive"), serverProvider = SyncServerProvider.GoogleDrive) to "gdrive", ) for ((preference, expected) in cases) { val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(preference))) val hub = Recorder("hub"); val cloud = CloudRecorder(); val mangayomi = Recorder("mangayomi") val syncYomi = Recorder("syncyomi"); val drive = Recorder("gdrive") val calls = listOf(hub, cloud, mangayomi, syncYomi, drive) val hybrid = HybridSyncService(hub, cloud, db, mangayomiSync = mangayomi, syncYomiSync = syncYomi, googleDriveSync = drive) hybrid.sync(1, DbSettings()); hybrid.pushChanges(1); hybrid.pullChanges(1); hybrid.status(1); hybrid.logout(1) assertEquals(List(5) { expected }, calls.flatMap { it.calls }, "routing for $expected") } } @Test fun stateSyncProvidersOwnLoginAndBrowserSignIn() = runTest { for ((provider, expected) in listOf(SyncServerProvider.SyncYomi to "syncyomi", SyncServerProvider.GoogleDrive to "gdrive")) { val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(DbSyncPreference(syncId = 1, serverProvider = provider)))) val hub = Recorder("hub"); val cloud = CloudRecorder(); val syncYomi = Recorder("syncyomi"); val drive = Recorder("gdrive") val hybrid = HybridSyncService(hub, cloud, db, syncYomiSync = syncYomi, googleDriveSync = drive) hybrid.login(1, "cloud://google-drive", "", "secret") hybrid.beginBrowserLogin(1, "client") assertEquals(listOf(expected, expected), listOf(hub, cloud, syncYomi, drive).flatMap { it.calls }) } } @Test fun missingStateSyncServiceReportsUnavailable() = runTest { val db = InMemoryYokanDatabase(emptySnapshot().copy(syncPreferences = listOf(DbSyncPreference(syncId = 1, serverProvider = SyncServerProvider.GoogleDrive)))) val hybrid = HybridSyncService(Recorder("hub"), CloudRecorder(), db) assertEquals(listOf("Google Drive sync is unavailable."), hybrid.sync(1, DbSettings()).errors) } private open class Recorder(private val name: String) : SyncService { val calls = mutableListOf() private fun record() { calls += name } override suspend fun login(syncId: Long, server: String, email: String, password: String): SyncLoginResult { record(); return SyncLoginResult(true, name) } override fun beginBrowserLogin(syncId: Long, server: String): BrowserLoginAttempt? { record(); return null } override suspend fun logout(syncId: Long) = record() override suspend fun sync(syncId: Long, settings: DbSettings): SyncResult { record(); return SyncResult(0, 0, false) } override suspend fun pushChanges(syncId: Long): SyncResult { record(); return SyncResult(0, 0, false) } override suspend fun pullChanges(syncId: Long): SyncResult { record(); return SyncResult(0, 0, false) } override fun status(syncId: Long): SyncConnectionStatus { record(); return SyncConnectionStatus(true, null, name, null) } } private class CloudRecorder : Recorder("cloud"), CloudBackupSyncService { override fun providers(): List = emptyList() override fun isCloudPreference(syncPreference: DbSyncPreference?): Boolean = syncPreference?.server?.startsWith(CloudServerPrefix) == true override suspend fun connect(syncId: Long, providerId: String, accountLabel: String?, accessToken: String) = SyncLoginResult(true, "") override suspend fun uploadBackup(syncId: Long, options: BackupOptions): CloudBackupResult = error("unused") override suspend fun restoreLatestBackup(syncId: Long): CloudBackupRestoreResult = error("unused") } }