Yokan / rebuild
Course 1 8624561

SyncPathsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/database/sync/SyncPathsTest.kt

test / fixture150 lines
1package eu.friedcutlet.yokan.database.sync23import eu.friedcutlet.yokan.database.DbChapter4import eu.friedcutlet.yokan.database.DbChapterReleasePreference5import eu.friedcutlet.yokan.database.DbManga6import eu.friedcutlet.yokan.database.DbOutboxOp7import eu.friedcutlet.yokan.database.DbPersonalReaderMode8import eu.friedcutlet.yokan.database.DbReaderMode9import eu.friedcutlet.yokan.database.DbSettings10import eu.friedcutlet.yokan.database.DbSourcePreference11import kotlinx.serialization.json.JsonObject12import kotlinx.serialization.json.JsonPrimitive13import kotlinx.serialization.json.buildJsonObject14import kotlinx.serialization.json.jsonObject15import kotlinx.serialization.json.jsonPrimitive16import kotlinx.serialization.json.put17import kotlin.test.Test18import kotlin.test.assertEquals19import kotlin.test.assertFalse20import kotlin.test.assertNull21import kotlin.test.assertTrue2223class SyncPathsTest {24    @Test25    fun nativeHubDocumentsRetainLicensedAndUpcomingStatuses() {26        for (status in listOf(eu.friedcutlet.yokan.database.DbStatus.Licensed, eu.friedcutlet.yokan.database.DbStatus.Upcoming)) {27            val manga = DbManga(id = 1, status = status)28            val doc = SyncPaths.doc(SyncPaths.KIND_MANGA, DbManga.serializer(), manga)29            assertEquals(JsonPrimitive(status.name), doc["status"])30            val restored = SyncPaths.json.decodeFromJsonElement(DbManga.serializer(), SyncPaths.fromDoc(SyncPaths.KIND_MANGA, doc))31            assertEquals(status, restored.status)32        }33    }3435    @Test36    fun releasePreferencesStayLocalThroughHubDocumentsAndIncomingSettings() {37        val local = DbSettings(chapterReleasePreferences = listOf(DbChapterReleasePreference(100, listOf("Preferred"), true)))38        val doc = SyncPaths.doc(SyncPaths.KIND_SETTINGS, DbSettings.serializer(), local)39        assertNull(doc["chapterReleasePreferences"])40        val before = SyncPaths.doc(SyncPaths.KIND_SETTINGS, DbSettings.serializer(), DbSettings())41        assertTrue(SyncPaths.diff(SyncPaths.KIND_SETTINGS, before, doc).isEmpty())42        val incoming = DbSettings(chapterReleasePreferences = listOf(DbChapterReleasePreference(100, listOf("Unrelated"))))43        assertEquals(local.chapterReleasePreferences, preserveDeviceLocalSettings(local, incoming).chapterReleasePreferences)44    }4546    private val json = SyncPaths.json4748    @Test49    fun mangaCategoriesTravelAsIdSetAndCustomCoverStaysLocal() {50        val m = DbManga(id = 5, name = "T", categories = listOf(9, 3), customCoverImage = byteArrayOf(1, 2, 3))51        val doc = SyncPaths.doc(SyncPaths.KIND_MANGA, DbManga.serializer(), m)52        assertEquals(setOf("3", "9"), doc["categories"]!!.jsonObject.keys)53        assertNull(doc["customCoverImage"])54        assertNull(doc["id"], "the entity id is the key, not a field")5556        val back = SyncPaths.fromDoc(SyncPaths.KIND_MANGA, doc)57        assertEquals("[3,9]", back["categories"].toString())58    }5960    @Test61    fun keyedSettingsListsBecomeMapsOfWholeElements() {62        val s = DbSettings(63            personalReaderModeList = listOf(DbPersonalReaderMode(111, DbReaderMode.Vertical), DbPersonalReaderMode(222, DbReaderMode.Webtoon)),64            sourcePreferences = listOf(DbSourcePreference(7, "lang", "en")),65            cookiesList = null,66        )67        val doc = SyncPaths.doc(SyncPaths.KIND_SETTINGS, DbSettings.serializer(), s)68        val modes = doc["personalReaderModeList"]!!.jsonObject69        assertEquals(setOf("111", "222"), modes.keys)70        assertEquals("111", modes["111"]!!.jsonObject["mangaId"]!!.jsonPrimitive.content)71        assertEquals(setOf("7:lang"), doc["sourcePreferences"]!!.jsonObject.keys)72        assertNull(doc["cookiesList"])73        assertNull(doc["jrePath"], "device-local settings never leave the device")7475        val restored = json.decodeFromJsonElement(DbSettings.serializer(), SyncPaths.fromDoc(SyncPaths.KIND_SETTINGS, doc))76        assertEquals(s.personalReaderModeList, restored.personalReaderModeList)77        assertEquals(s.sourcePreferences, restored.sourcePreferences)78    }7980    @Test81    fun diffIsPathLevelAndDescendsIntoKeyedFields() {82        val before = SyncPaths.doc(SyncPaths.KIND_MANGA, DbManga.serializer(), DbManga(id = 1, name = "A", favorite = false, categories = listOf(1, 2)))83        val after = SyncPaths.doc(SyncPaths.KIND_MANGA, DbManga.serializer(), DbManga(id = 1, name = "A", favorite = true, categories = listOf(2, 3)))84        val changes = SyncPaths.diff(SyncPaths.KIND_MANGA, before, after).associate { it.path to it.value }85        assertEquals(setOf("favorite", "categories.1", "categories.3"), changes.keys)86        assertEquals(JsonPrimitive(true), changes["favorite"])87        assertNull(changes["categories.1"], "removed set member is a path delete")88        assertEquals(JsonPrimitive(true), changes["categories.3"])89    }9091    @Test92    fun diffAgainstNothingListsEveryPresentField() {93        val after = SyncPaths.doc(SyncPaths.KIND_MANGA, DbManga.serializer(), DbManga(id = 1, name = "A"))94        val paths = SyncPaths.diff(SyncPaths.KIND_MANGA, null, after).map { it.path }95        assertTrue("name" in paths)96        assertTrue("favorite" in paths, "non-null defaults are explicit on the wire")97        assertFalse("link" in paths, "null fields are absent, not sent")98    }99100    @Test101    fun applyIncomingKeepsLocalOnlyFieldsAndDropsAbsentSyncedOnes() {102        val local = json.encodeToJsonElement(DbManga.serializer(), DbManga(id = 1, name = "Old", author = "X", customCoverImage = byteArrayOf(9))).jsonObject103        val incoming = buildJsonObject { put("name", "New"); put("favorite", true) }104        val merged = json.decodeFromJsonElement(DbManga.serializer(), SyncPaths.applyIncoming(SyncPaths.KIND_MANGA, local, incoming, 1))105        assertEquals("New", merged.name)106        assertTrue(merged.favorite)107        assertNull(merged.author, "absent on the hub means removed")108        assertEquals(1L, merged.id)109        assertTrue(merged.customCoverImage!!.contentEquals(byteArrayOf(9)), "excluded fields stay local")110    }111112    @Test113    fun chapterDownloadMarkStaysOnTheDeviceThatDownloaded() {114        val downloaded = DbChapter(id = 1, mangaId = 5, url = "/c/1", archivePath = "/data/Yokan/ch1.cbz", downloadSize = "3.1 MB")115        val doc = SyncPaths.doc(SyncPaths.KIND_CHAPTER, DbChapter.serializer(), downloaded)116        assertNull(doc["downloadSize"])117        assertEquals(JsonPrimitive(""), doc["archivePath"])118        val plain = SyncPaths.doc(SyncPaths.KIND_CHAPTER, DbChapter.serializer(), downloaded.copy(archivePath = "", downloadSize = null))119        assertTrue(SyncPaths.diff(SyncPaths.KIND_CHAPTER, plain, doc).isEmpty(), "finishing a download sends nothing")120121        val localLibrary = DbChapter(id = 2, mangaId = 6, url = "Comics/T/ch.cbz", archivePath = "Comics/T/ch.cbz")122        assertEquals(JsonPrimitive("Comics/T/ch.cbz"), SyncPaths.doc(SyncPaths.KIND_CHAPTER, DbChapter.serializer(), localLibrary)["archivePath"])123124        fun pull(local: DbChapter?, incoming: JsonObject) = json.decodeFromJsonElement(125            DbChapter.serializer(),126            SyncPaths.applyIncoming(SyncPaths.KIND_CHAPTER, local?.let { json.encodeToJsonElement(DbChapter.serializer(), it).jsonObject }, incoming, 1),127        )128        val read = pull(downloaded, SyncPaths.doc(SyncPaths.KIND_CHAPTER, DbChapter.serializer(), downloaded.copy(isRead = true, archivePath = "", downloadSize = null)))129        assertTrue(read.isRead)130        assertEquals("/data/Yokan/ch1.cbz" to "3.1 MB", read.archivePath to read.downloadSize, "a hub update keeps this device's download")131132        val pushedByOlderClient = buildJsonObject { put("url", "/c/1"); put("archivePath", "/other/ch1"); put("downloadSize", "3.1 MB") }133        val elsewhere = pull(DbChapter(id = 1, mangaId = 5, url = "/c/1"), pushedByOlderClient)134        assertEquals("" to null, elsewhere.archivePath to elsewhere.downloadSize, "another device's download path is ignored")135        assertEquals("Comics/T/ch.cbz", pull(null, SyncPaths.doc(SyncPaths.KIND_CHAPTER, DbChapter.serializer(), localLibrary)).archivePath)136    }137138    @Test139    fun overlayPutsPendingLocalValuesBack() {140        val doc = buildJsonObject { put("name", "Remote"); put("favorite", false) }141        val pending = listOf(142            DbOutboxOp(SyncPaths.KIND_MANGA, 1, "favorite", "true", 1, 1),143            DbOutboxOp(SyncPaths.KIND_MANGA, 1, "categories.7", "true", 2, 2),144        )145        val out = SyncPaths.overlay(doc, pending)146        assertEquals(JsonPrimitive(true), out["favorite"])147        assertEquals(JsonPrimitive(true), out["categories"]!!.jsonObject["7"])148        assertEquals(JsonPrimitive("Remote"), out["name"])149    }150}151

Search chapters, reference and source filenames.