package eu.friedcutlet.yokan.database.sync import eu.friedcutlet.yokan.database.DbCookie import eu.friedcutlet.yokan.database.DbChapterReleasePreference import eu.friedcutlet.yokan.database.DbLocalFolder import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.DbSyncPreference import eu.friedcutlet.yokan.database.DbUpdateError import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.emptySnapshot import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals import kotlin.test.assertTrue class DeviceLocalSettingsTest { private val deviceLocal = SyncPaths.excluded(SyncPaths.KIND_SETTINGS) private val thisDevice = DbSettings( id = 1, downloadLocation = "/storage/emulated/0/Yokan", jrePath = "/usr/lib/jvm", themeIsDark = false, ) /** Another device's settings with every device-local field set away from [thisDevice]. */ private val otherDevice = DbSettings( id = 2, syncIdentityVersion = 1, localFolders = listOf("/mnt/usb"), namedLocalFolders = listOf(DbLocalFolder(name = "USB", path = "/mnt/usb")), downloadLocalFolderName = "USB", askDownloadDestination = false, downloadLocation = "C:/Yokan", autoBackupLocation = "C:/Backups", themeSeedImagePath = "C:/wallpaper.png", androidProxyServer = "127.0.0.1:1080", cloudflareBypassProxyUrl = "http://127.0.0.1:8191", jrePath = "C:/jdk", extensionServerPath = "C:/server.jar", btServerAddress = "10.0.0.2", btServerPort = 8090, desktopCloseToTray = true, desktopLaunchAtLogin = true, cookiesList = listOf(DbCookie(host = "example.org", cookie = "session=1")), metadataServerToken = "secret", features = eu.friedcutlet.yokan.database.DbFeaturePreferences(home = eu.friedcutlet.yokan.database.DbHomePreferences(tmdbReadAccessToken = "device-secret", sourceFeeds = listOf(eu.friedcutlet.yokan.database.DbHomeSourceFeed(42))), externalDownloads = eu.friedcutlet.yokan.database.DbExternalDownloads(eu.friedcutlet.yokan.database.PreferredDownloader.Aria2, "https://aria.example.org/jsonrpc", "device-rpc-secret", "/downloads")), updateErrors = listOf(DbUpdateError(mangaId = 1, name = "Title", error = "Host not found")), enableLogs = true, enableFileLogging = true, developerMode = true, customHubServers = listOf("http://192.168.1.5:8080"), chapterReleasePreferences = listOf(DbChapterReleasePreference(scanlatorPriority = listOf("group b"), skipDuplicateReleases = true)), automaticReaderLayout = true, readerHoldToScroll = true, einkMode = true, einkPreset = eu.friedcutlet.yokan.database.DbEinkPreset.KoboSage, einkRefreshEveryPages = 1, einkOptimizeDownloads = true, automaticReaderLayouts = listOf(eu.friedcutlet.yokan.database.DbAutomaticReaderLayout(1)), themeIsDark = true, ) private fun DbSettings.fields(): JsonObject = SyncPaths.json.encodeToJsonElement(DbSettings.serializer(), this).jsonObject @Test fun restoreKeepsEveryDeviceLocalFieldFromCurrentDevice() { val here = thisDevice.fields() val there = otherDevice.fields() for (key in deviceLocal) assertNotEquals(here[key], there[key], "fixture must differ on $key") val restored = preserveDeviceLocalSettings(current = thisDevice, incoming = otherDevice).fields() for (key in deviceLocal) assertEquals(here[key], restored[key], "restore overwrote device-local $key") assertEquals(there["themeIsDark"], restored["themeIsDark"], "shared settings still come from the backup") } @Test fun syncOutboxExcludesTheSameDeviceLocalFields() { val hub = DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "tok", syncOn = true, hubEpoch = 1) val db = InMemoryYokanDatabase(emptySnapshot(thisDevice).copy(syncPreferences = listOf(hub))) db.updateSettings(otherDevice) val paths = db.outbox() .filter { it.kind == SyncPaths.KIND_SETTINGS } .map { it.path.substringBefore('.') } assertTrue("themeIsDark" in paths, "shared settings are still recorded") assertTrue(paths.none { it in deviceLocal }, "device-local fields leaked into the outbox: $paths") } @Test fun hubPullKeepsDeviceLocalFieldsAnOlderClientPushed() { val here = thisDevice.fields() val legacyHubDoc = otherDevice.fields() val pulled = SyncPaths.applyIncoming(SyncPaths.KIND_SETTINGS, here, legacyHubDoc, SyncPaths.SETTINGS_ID) for (key in deviceLocal - "id") assertEquals(here[key], pulled[key], "hub pull overwrote device-local $key") assertEquals(legacyHubDoc["themeIsDark"], pulled["themeIsDark"], "shared settings still come from the hub") } }