OutboxTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/database/sync/OutboxTest.kt
1package eu.friedcutlet.yokan.database.sync23import eu.friedcutlet.yokan.database.DbCategory4import eu.friedcutlet.yokan.database.DbItemType5import eu.friedcutlet.yokan.database.DbManga6import eu.friedcutlet.yokan.database.DbOutboxOp7import eu.friedcutlet.yokan.database.DbPersonalReaderMode8import eu.friedcutlet.yokan.database.DbReaderMode9import eu.friedcutlet.yokan.database.DbSyncPreference10import eu.friedcutlet.yokan.database.InMemoryYokanDatabase11import eu.friedcutlet.yokan.database.emptySnapshot12import kotlin.test.Test13import kotlin.test.assertEquals14import kotlin.test.assertNotSame15import kotlin.test.assertSame16import kotlin.test.assertTrue17import kotlinx.coroutines.Dispatchers18import kotlinx.coroutines.coroutineScope19import kotlinx.coroutines.launch20import kotlinx.coroutines.test.runTest2122class OutboxTest {23 private fun loggedIn() = InMemoryYokanDatabase(24 emptySnapshot().copy(25 syncPreferences = listOf(DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "tok", syncOn = true, hubEpoch = 1)),26 ),27 )2829 @Test30 fun nothingIsRecordedWithoutALoggedInHubAndUnsendableOpsAreDropped() {31 val db = InMemoryYokanDatabase(32 emptySnapshot().copy(33 outbox = listOf(34 DbOutboxOp(SyncPaths.KIND_MANGA, 1, "name", "\"never sent\"", hlc = 1, seq = 1),35 DbOutboxOp(SyncPaths.KIND_MANGA, 2, "name", "\"pending since logout\"", hlc = 2, seq = 2, epoch = 3),36 ),37 ),38 )39 assertEquals(listOf(2L), db.outbox().map { it.id }, "epoch-0 ops are dropped, ops of a real hub epoch kept")4041 val manga = db.upsertManga(DbManga(name = "T", link = "/t", sourceId = 1))42 db.updateSettings(db.settings().copy(themeIsDark = !db.settings().themeIsDark))43 db.removeManga(manga.id)44 assertEquals(listOf(2L), db.outbox().map { it.id }, "no hub: the first sync seeds full state instead")4546 db.upsertSyncPreference(DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "tok", syncOn = true, hubEpoch = 3))47 val next = db.upsertManga(DbManga(name = "T2", link = "/t2", sourceId = 1))48 assertTrue(db.outbox().any { it.id == next.id && it.path == "name" && it.epoch == 3 })49 }5051 @Test52 fun newOutboxOpsUseTheActiveHubEpoch() {53 val db = InMemoryYokanDatabase(54 emptySnapshot().copy(55 syncPreferences = listOf(56 DbSyncPreference(syncId = 2, server = "https://other.test", authToken = "other", hubEpoch = 99),57 DbSyncPreference(syncId = 3, server = "cloud://drive", authToken = "cloud", syncOn = true, hubEpoch = 88),58 DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "hub", syncOn = true, hubEpoch = 7),59 ),60 ),61 )6263 db.upsertManga(DbManga(name = "T", link = "/t", sourceId = 1))6465 assertTrue(db.outbox().isNotEmpty())66 assertTrue(db.outbox().all { it.epoch == 7 })67 }6869 @Test70 fun insertsRecordEveryFieldAndUpdatesOnlyTheChangedPath() {71 val db = loggedIn()72 val saved = db.upsertManga(DbManga(name = "T", link = "/t", sourceId = 1))73 assertEquals(SyncIds.forManga(saved), saved.id, "new rows get a stable id")74 val first = db.outbox()75 assertTrue(first.any { it.kind == SyncPaths.KIND_MANGA && it.id == saved.id && it.path == "name" && it.value == "\"T\"" })7677 db.upsertManga(saved.copy(favorite = true))78 val delta = db.outbox().filter { it.seq > first.maxOf { o -> o.seq } }79 assertEquals(listOf("favorite"), delta.map { it.path })80 assertEquals("true", delta.single().value)8182 // Coalesced: the second write to the same path replaces the first row83 db.upsertManga(saved.copy(favorite = false))84 assertEquals(1, db.outbox().count { it.path == "favorite" })85 assertEquals("false", db.outbox().single { it.path == "favorite" }.value)86 assertTrue(db.outbox().zipWithNext().all { (a, b) -> a.seq < b.seq }, "outbox is ordered by seq")87 }8889 @Test90 fun deleteBecomesTombstoneOpAndRemoteApplyIsSilent() {91 val db = loggedIn()92 val cat = db.upsertCategory(DbCategory(name = "Reading", forItemType = DbItemType.Manga))93 db.removeCategory(cat.id)94 val tomb = db.outbox().last()95 assertEquals(SyncPaths.DELETED, tomb.path)96 assertEquals("true", tomb.value)9798 val before = db.outbox().size99 db.applyRemote { db.upsertManga(DbManga(name = "from hub", link = "/h", sourceId = 2)) }100 assertEquals(before, db.outbox().size, "hub writes are not echoed")101 }102103 @Test104 fun settingsDiffIsKeyedPerTitle() {105 val db = loggedIn()106 val s0 = db.settings()107 db.updateSettings(s0.copy(personalReaderModeList = listOf(DbPersonalReaderMode(111, DbReaderMode.Vertical))))108 db.updateSettings(db.settings().copy(personalReaderModeList = listOf(DbPersonalReaderMode(111, DbReaderMode.Vertical), DbPersonalReaderMode(222, DbReaderMode.Webtoon))))109 val paths = db.outbox().filter { it.kind == SyncPaths.KIND_SETTINGS }.map { it.path }110 assertEquals(listOf("personalReaderModeList.111", "personalReaderModeList.222"), paths)111112 // device-local settings never produce ops113 db.updateSettings(db.settings().copy(jrePath = "/opt/jdk"))114 assertEquals(paths, db.outbox().filter { it.kind == SyncPaths.KIND_SETTINGS }.map { it.path })115 }116117 @Test118 fun removeOutboxOnlyDropsOpsThatWereSent() {119 val db = loggedIn()120 val m = db.upsertManga(DbManga(name = "T", link = "/t", sourceId = 1))121 val sent = db.outbox()122 db.upsertManga(m.copy(name = "T2")) // rewrites the "name" path with a newer seq123 db.removeOutbox(sent)124 assertEquals(listOf("name"), db.outbox().map { it.path })125 assertEquals("\"T2\"", db.outbox().single().value)126 }127128 @Test129 fun batchEmitsOneSnapshot() {130 val db = InMemoryYokanDatabase(emptySnapshot())131 var emissions = 0132 val start = db.snapshots().value133 db.batch {134 db.upsertManga(DbManga(name = "A", link = "/a", sourceId = 1))135 db.upsertManga(DbManga(name = "B", link = "/b", sourceId = 1))136 if (db.snapshots().value !== start) emissions++137 }138 assertEquals(0, emissions, "nothing emitted inside the batch")139 assertEquals(2, db.snapshots().value.mangas.size)140 }141142 @Test143 fun oneTableMutationReusesUnchangedSnapshotLists() {144 val db = InMemoryYokanDatabase(emptySnapshot())145 val before = db.snapshots().value146147 db.upsertManga(DbManga(name = "A", link = "/a", sourceId = 1))148149 val after = db.snapshots().value150 assertNotSame(before.mangas, after.mangas)151 assertSame(before.chapters, after.chapters)152 assertSame(before.downloads, after.downloads)153 }154155 @Test156 fun aMangaEditIsPublishedEvenWhenNothingElseChanges() {157 val db = InMemoryYokanDatabase(emptySnapshot())158 val manga = db.upsertManga(DbManga(name = "A", link = "/a", sourceId = 1))159160 db.upsertManga(manga.copy(favorite = true))161162 assertTrue(db.snapshots().value.mangas.single().favorite, "snapshot flows drop values equal to the current one")163 }164165 @Test166 fun concurrentMutationsKeepRowsAndOutboxSequencesConsistent() = runTest {167 val db = loggedIn()168 coroutineScope {169 repeat(4) { worker ->170 launch(Dispatchers.Default) {171 repeat(25) { index ->172 val id = worker * 25L + index + 1L173 db.upsertManga(DbManga(id = id, name = "M$id", link = "/$id", sourceId = 1))174 }175 }176 }177 }178179 assertEquals(100, db.mangas().size)180 val sequences = db.outbox().map { it.seq }181 assertEquals(sequences.size, sequences.distinct().size)182 assertEquals(sequences.sorted(), sequences)183 }184}185