|
| 1 | +package org.jetbrains.kotlinconf.storage |
| 2 | + |
| 3 | +import com.russhwolf.settings.ExperimentalSettingsApi |
| 4 | +import com.russhwolf.settings.MapSettings |
| 5 | +import com.russhwolf.settings.ObservableSettings |
| 6 | +import com.russhwolf.settings.observable.makeObservable |
| 7 | +import com.russhwolf.settings.set |
| 8 | +import kotlinx.coroutines.flow.first |
| 9 | +import kotlinx.coroutines.test.runTest |
| 10 | +import kotlinx.serialization.json.Json |
| 11 | +import org.jetbrains.kotlinconf.NotificationSettings |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.assertEquals |
| 14 | +import kotlin.test.assertNull |
| 15 | +import kotlin.test.assertTrue |
| 16 | + |
| 17 | +class MultiplatformSettingsStorageMigrationTest { |
| 18 | + |
| 19 | + @OptIn(ExperimentalSettingsApi::class) |
| 20 | + private fun inMemorySettings(): ObservableSettings = MapSettings().makeObservable() |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates a storage object with data matching the older, 2025 storage version. |
| 24 | + */ |
| 25 | + private fun get2025Settings() = inMemorySettings().apply { |
| 26 | + this["storageVersion"] = MultiplatformSettingsStorage.V2025 |
| 27 | + this["newsCache"] = "{\"dummy\":\"value\"}" |
| 28 | + this["notificationSettings"] = """ |
| 29 | + { |
| 30 | + "sessionReminders": true, |
| 31 | + "scheduleUpdates": false, |
| 32 | + "jetBrainsNews": true, |
| 33 | + "kotlinConfNews": "true" |
| 34 | + } |
| 35 | + """.trimIndent() |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun migration_2025_to_2026_updates_version() { |
| 40 | + val settings = get2025Settings() |
| 41 | + val storage = MultiplatformSettingsStorage(settings) |
| 42 | + |
| 43 | + // Run migrations |
| 44 | + storage.ensureCurrentVersion() |
| 45 | + |
| 46 | + // Storage version should be successfully updated to the current version |
| 47 | + assertEquals(MultiplatformSettingsStorage.CURRENT_STORAGE_VERSION, settings.getInt("storageVersion", 0)) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun migration_2026_to_2026_removes_news_cache() { |
| 52 | + val settings = get2025Settings() |
| 53 | + val storage = MultiplatformSettingsStorage(settings) |
| 54 | + |
| 55 | + // Run migrations |
| 56 | + storage.ensureCurrentVersion() |
| 57 | + |
| 58 | + // The newsCache key should be removed by the migration |
| 59 | + assertNull(settings.getStringOrNull("newsCache")) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun migration_2026_to_2026_migrates_notification_settings() = runTest { |
| 64 | + val settings = get2025Settings() |
| 65 | + val storage = MultiplatformSettingsStorage(settings) |
| 66 | + |
| 67 | + // Run migrations |
| 68 | + storage.ensureCurrentVersion() |
| 69 | + |
| 70 | + // Notification settings should still be readable with the current version |
| 71 | + val notiSettings = storage.getNotificationSettings().first() |
| 72 | + assertTrue(notiSettings != null) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun unknown_storage_version_triggers_destructive_upgrade_and_clears_all_keys() { |
| 77 | + val settings = inMemorySettings() |
| 78 | + settings["userId2025"] = "user-123" |
| 79 | + settings["newsCache"] = "legacy-data" |
| 80 | + settings["notificationSettings"] = "{\"sessionReminders\":false,\"scheduleUpdates\":true}" |
| 81 | + val storage = MultiplatformSettingsStorage(settings) |
| 82 | + |
| 83 | + // Run migrations |
| 84 | + storage.ensureCurrentVersion() |
| 85 | + |
| 86 | + // Only the storageVersion should remain and be set to the current version |
| 87 | + assertEquals(setOf("storageVersion"), settings.keys) |
| 88 | + assertEquals(MultiplatformSettingsStorage.CURRENT_STORAGE_VERSION, settings.getInt("storageVersion", 0)) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun unknown_migration_path_triggers_destructive_upgrade() { |
| 93 | + val settings = inMemorySettings() |
| 94 | + settings["storageVersion"] = 2024_000 // A version we don't have a migration for |
| 95 | + settings["favorites"] = "[\"S1\",\"S2\"]" |
| 96 | + val storage = MultiplatformSettingsStorage(settings) |
| 97 | + |
| 98 | + // Run migrations |
| 99 | + storage.ensureCurrentVersion() |
| 100 | + |
| 101 | + // Only the storageVersion should remain and be set to the current version |
| 102 | + assertEquals(setOf("storageVersion"), settings.keys) |
| 103 | + assertEquals(MultiplatformSettingsStorage.CURRENT_STORAGE_VERSION, settings.getInt("storageVersion", 0)) |
| 104 | + } |
| 105 | +} |
0 commit comments