DeviceLocalSettingsTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/database/sync/DeviceLocalSettingsTest.kt
1package eu.friedcutlet.yokan.database.sync23import eu.friedcutlet.yokan.database.DbCookie4import eu.friedcutlet.yokan.database.DbChapterReleasePreference5import eu.friedcutlet.yokan.database.DbLocalFolder6import eu.friedcutlet.yokan.database.DbSettings7import eu.friedcutlet.yokan.database.DbSyncPreference8import eu.friedcutlet.yokan.database.DbUpdateError9import eu.friedcutlet.yokan.database.InMemoryYokanDatabase10import eu.friedcutlet.yokan.database.emptySnapshot11import kotlinx.serialization.json.JsonObject12import kotlinx.serialization.json.jsonObject13import kotlin.test.Test14import kotlin.test.assertEquals15import kotlin.test.assertNotEquals16import kotlin.test.assertTrue1718class DeviceLocalSettingsTest {19 private val deviceLocal = SyncPaths.excluded(SyncPaths.KIND_SETTINGS)2021 private val thisDevice = DbSettings(22 id = 1,23 downloadLocation = "/storage/emulated/0/Yokan",24 jrePath = "/usr/lib/jvm",25 themeIsDark = false,26 )2728 /** Another device's settings with every device-local field set away from [thisDevice]. */29 private val otherDevice = DbSettings(30 id = 2,31 syncIdentityVersion = 1,32 localFolders = listOf("/mnt/usb"),33 namedLocalFolders = listOf(DbLocalFolder(name = "USB", path = "/mnt/usb")),34 downloadLocalFolderName = "USB",35 askDownloadDestination = false,36 downloadLocation = "C:/Yokan",37 autoBackupLocation = "C:/Backups",38 themeSeedImagePath = "C:/wallpaper.png",39 androidProxyServer = "127.0.0.1:1080",40 cloudflareBypassProxyUrl = "http://127.0.0.1:8191",41 jrePath = "C:/jdk",42 extensionServerPath = "C:/server.jar",43 btServerAddress = "10.0.0.2",44 btServerPort = 8090,45 desktopCloseToTray = true,46 desktopLaunchAtLogin = true,47 cookiesList = listOf(DbCookie(host = "example.org", cookie = "session=1")),48 metadataServerToken = "secret",49 features = eu.friedcutlet.yokan.database.DbFeaturePreferences(home = eu.friedcutlet.yokan.database.DbHomePreferences(tmdbReadAccessToken = "device-secret",50 sourceFeeds = listOf(eu.friedcutlet.yokan.database.DbHomeSourceFeed(42))),51 externalDownloads = eu.friedcutlet.yokan.database.DbExternalDownloads(eu.friedcutlet.yokan.database.PreferredDownloader.Aria2, "https://aria.example.org/jsonrpc", "device-rpc-secret", "/downloads")),52 updateErrors = listOf(DbUpdateError(mangaId = 1, name = "Title", error = "Host not found")),53 enableLogs = true,54 enableFileLogging = true,55 developerMode = true,56 customHubServers = listOf("http://192.168.1.5:8080"),57 chapterReleasePreferences = listOf(DbChapterReleasePreference(scanlatorPriority = listOf("group b"), skipDuplicateReleases = true)),58 automaticReaderLayout = true,59 readerHoldToScroll = true,60 einkMode = true,61 einkPreset = eu.friedcutlet.yokan.database.DbEinkPreset.KoboSage,62 einkRefreshEveryPages = 1,63 einkOptimizeDownloads = true,64 automaticReaderLayouts = listOf(eu.friedcutlet.yokan.database.DbAutomaticReaderLayout(1)),65 themeIsDark = true,66 )6768 private fun DbSettings.fields(): JsonObject =69 SyncPaths.json.encodeToJsonElement(DbSettings.serializer(), this).jsonObject7071 @Test72 fun restoreKeepsEveryDeviceLocalFieldFromCurrentDevice() {73 val here = thisDevice.fields()74 val there = otherDevice.fields()75 for (key in deviceLocal) assertNotEquals(here[key], there[key], "fixture must differ on $key")7677 val restored = preserveDeviceLocalSettings(current = thisDevice, incoming = otherDevice).fields()7879 for (key in deviceLocal) assertEquals(here[key], restored[key], "restore overwrote device-local $key")80 assertEquals(there["themeIsDark"], restored["themeIsDark"], "shared settings still come from the backup")81 }8283 @Test84 fun syncOutboxExcludesTheSameDeviceLocalFields() {85 val hub = DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "tok", syncOn = true, hubEpoch = 1)86 val db = InMemoryYokanDatabase(emptySnapshot(thisDevice).copy(syncPreferences = listOf(hub)))8788 db.updateSettings(otherDevice)8990 val paths = db.outbox()91 .filter { it.kind == SyncPaths.KIND_SETTINGS }92 .map { it.path.substringBefore('.') }93 assertTrue("themeIsDark" in paths, "shared settings are still recorded")94 assertTrue(paths.none { it in deviceLocal }, "device-local fields leaked into the outbox: $paths")95 }9697 @Test98 fun hubPullKeepsDeviceLocalFieldsAnOlderClientPushed() {99 val here = thisDevice.fields()100 val legacyHubDoc = otherDevice.fields()101102 val pulled = SyncPaths.applyIncoming(SyncPaths.KIND_SETTINGS, here, legacyHubDoc, SyncPaths.SETTINGS_ID)103104 for (key in deviceLocal - "id") assertEquals(here[key], pulled[key], "hub pull overwrote device-local $key")105 assertEquals(legacyHubDoc["themeIsDark"], pulled["themeIsDark"], "shared settings still come from the hub")106 }107}108