package eu.friedcutlet.yokan.services.backup import eu.friedcutlet.yokan.database.DbActionType import eu.friedcutlet.yokan.services.notifications.NoNotifications import eu.friedcutlet.yokan.database.DbCategory import eu.friedcutlet.yokan.database.DbChangedPart import eu.friedcutlet.yokan.database.DbChapter import eu.friedcutlet.yokan.database.DbHistory import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.DbSyncPreference import eu.friedcutlet.yokan.database.DbTrack import eu.friedcutlet.yokan.database.DbTrackStatus import eu.friedcutlet.yokan.database.DbUpdate import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.seedDatabaseSnapshot import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.database.sync.SyncPaths import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertTrue import kotlinx.coroutines.test.runTest import kotlinx.serialization.json.Json private val LenientBackupJson = Json { ignoreUnknownKeys = true } class BackupSelectiveRestoreTest { @Test fun restoreReplacesInsteadOfMerging() = runTest { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Stale title"), DbManga(id = 2, name = "Another stale"), ), chapters = listOf(DbChapter(id = 10, mangaId = 1, name = "Stale ch", url = "u")), ), ) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val backup = BackupSnapshot( createdAtMillis = 1, mangas = listOf(DbManga(id = 7, name = "Restored title")), chapters = listOf(DbChapter(id = 70, mangaId = 7, name = "Restored ch", url = "u7")), ) val bytes = native(backup) service.restore(bytes) assertEquals(listOf("Restored title"), database.mangas().map { it.name }) assertEquals(listOf("Restored ch"), database.chapters().map { it.name }) } @Test fun restoredDifferencesAreQueuedForTheHub() = runTest { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( mangas = listOf(DbManga(id = 1, name = "Hub name"), DbManga(id = 2, name = "Gone"), DbManga(id = 3, name = "Same")), syncPreferences = listOf(DbSyncPreference(syncId = 1, server = "https://hub.test", authToken = "tok", syncOn = true, hubEpoch = 4, hubHead = 50)), ), ) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) service.restore(native(BackupSnapshot(createdAtMillis = 1, mangas = listOf(DbManga(id = 1, name = "Backup name"), DbManga(id = 3, name = "Same"))))) val ops = database.outbox().filter { it.kind == SyncPaths.KIND_MANGA } assertEquals("\"Backup name\"", ops.single { it.id == 1L && it.path == "name" }.value) assertEquals(SyncPaths.DELETED, ops.single { it.id == 2L }.path) assertTrue(ops.none { it.id == 3L }, "unchanged titles are not re-sent") assertTrue(ops.all { it.epoch == 4 }, "ops belong to the logged-in hub generation") assertEquals(0L, database.syncPreferences().single().hubHead) } @Test fun restoreKeepsOnlyDownloadMarksWhoseFilesExistOnThisDevice() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot()) val files = MemoryFileService().apply { files["/here/ch1.cbz"] = byteArrayOf(1) } val service = DefaultBackupService(database, files, PassthroughCompression(), NoNotifications) service.restore(native(BackupSnapshot( createdAtMillis = 1, mangas = listOf(DbManga(id = 1, name = "Source title"), DbManga(id = 2, name = "Local title", isLocalArchive = true)), chapters = listOf( DbChapter(id = 10, mangaId = 1, name = "Here", url = "/c/1", archivePath = "/here/ch1.cbz", downloadSize = "1.0 MB"), DbChapter(id = 11, mangaId = 1, name = "Other device", url = "/c/2", archivePath = "/other/ch2", downloadSize = "2.0 MB"), DbChapter(id = 20, mangaId = 2, name = "Local", url = "local/Title/ch.cbz", archivePath = "local/Title/ch.cbz"), ), ))) val chapters = database.chapters().associateBy { it.id } assertEquals("/here/ch1.cbz" to "1.0 MB", chapters.getValue(10).let { it.archivePath to it.downloadSize }) assertEquals("" to null, chapters.getValue(11).let { it.archivePath to it.downloadSize }, "another device's download is not restored") assertEquals("local/Title/ch.cbz", chapters.getValue(20).archivePath, "local-library paths are not download marks") } @Test fun openAndPreviewDoNotMutateDatabase() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf(DbManga(id = 1, name = "Local")), chapters = listOf(DbChapter(id = 2, mangaId = 1, name = "Local ch", url = "local")), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val before = database.snapshot() val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga", "chapters"), mangas = listOf(DbManga(id = 7, name = "Incoming")), chapters = listOf(DbChapter(id = 8, mangaId = 7, name = "Incoming ch", url = "incoming")), )) val opened = service.openBackup(bytes) assertTrue(opened is BackupOpenResult.Ready) service.previewBackup(opened.plan) assertEquals(before, database.snapshot()) } @Test fun applyReplacesOnlySelectedCollectionsAndEmptySelectedClears() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Manga local", itemType = DbItemType.Manga), DbManga(id = 2, name = "Anime local", itemType = DbItemType.Anime), ), chapters = listOf( DbChapter(id = 3, mangaId = 1, name = "Clear", url = "clear"), DbChapter(id = 5, mangaId = 2, name = "Keep excluded", url = "keep-excluded"), ), categories = listOf(DbCategory(id = 4, name = "Keep category", forItemType = DbItemType.Manga)), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga", "categories"), mangas = emptyList(), categories = emptyList(), chapters = listOf(DbChapter(id = 99, mangaId = 99, name = "Ignored", url = "ignored")), )) val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) assertEquals(listOf("Anime local"), database.mangas().map { it.name }) assertTrue(database.categories().isEmpty(), "present-empty selected collection was not cleared") assertEquals(listOf("Keep excluded"), database.chapters().map { it.name }) } @Test fun selectingOneTitleTypePreservesTheOtherTypes() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Manga local", itemType = DbItemType.Manga), DbManga(id = 2, name = "Anime local", itemType = DbItemType.Anime), DbManga(id = 3, name = "Novel local", itemType = DbItemType.Novel), ), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("anime"), mangas = listOf( DbManga(id = 20, name = "Anime incoming", itemType = DbItemType.Anime), DbManga(id = 21, name = "Should stay excluded", itemType = DbItemType.Manga), ), )) val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) assertEquals(setOf("Manga local", "Anime incoming", "Novel local"), database.mangas().map { it.name }.toSet()) } @Test fun incomingDependentRowsCannotCrossManifestTitleBoundary() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Manga local", itemType = DbItemType.Manga), DbManga(id = 2, name = "Anime local", itemType = DbItemType.Anime), ), chapters = listOf(DbChapter(id = 20, mangaId = 2, name = "Anime episode")), histories = listOf(DbHistory(id = 21, mangaId = 2, chapterId = 20, itemType = DbItemType.Anime)), updates = listOf(DbUpdate(id = 22, mangaId = 2, chapterId = 20)), tracks = listOf(DbTrack(id = 23, mangaId = 2, status = DbTrackStatus.Watching)), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga", "chapters", "history", "updates", "tracks"), mangas = listOf(DbManga(id = 10, name = "Imported manga", itemType = DbItemType.Manga)), chapters = listOf( DbChapter(id = 30, mangaId = 10, name = "Imported chapter"), DbChapter(id = 31, mangaId = 2, name = "Excluded incoming episode"), ), histories = listOf( DbHistory(id = 40, mangaId = 10, chapterId = 30, itemType = DbItemType.Manga), DbHistory(id = 41, mangaId = 2, chapterId = 20, itemType = DbItemType.Anime), ), updates = listOf( DbUpdate(id = 50, mangaId = 10, chapterId = 30), DbUpdate(id = 51, mangaId = 2, chapterId = 20), ), tracks = listOf( DbTrack(id = 60, mangaId = 10, status = DbTrackStatus.Reading), DbTrack(id = 61, mangaId = 2, status = DbTrackStatus.Watching), ), )) val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) assertEquals(setOf(10L, 2L), database.mangas().map { it.id }.toSet()) assertEquals(setOf(30L, 20L), database.chapters().map { it.id }.toSet()) assertEquals(setOf(40L, 21L), database.histories().map { it.id }.toSet()) assertEquals(setOf(50L, 22L), database.updates().map { it.id }.toSet()) assertEquals(setOf(60L, 23L), database.tracks().map { it.id }.toSet()) } @Test fun identityCollisionRejectsBeforeMutation() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Manga local", itemType = DbItemType.Manga), DbManga(id = 2, name = "Anime local", itemType = DbItemType.Anime), ), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val before = database.snapshot() val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga"), mangas = listOf(DbManga(id = 2, name = "Colliding manga", itemType = DbItemType.Manga)), )) assertFailsWith { val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) } assertEquals(before, database.snapshot()) } @Test fun dependentIdentityCollisionRejectsBeforeMutation() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "Manga local", itemType = DbItemType.Manga), DbManga(id = 2, name = "Anime local", itemType = DbItemType.Anime), ), chapters = listOf(DbChapter(id = 7, mangaId = 2, name = "Anime episode")), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val before = database.snapshot() val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga", "chapters"), mangas = listOf(DbManga(id = 3, name = "Incoming manga", itemType = DbItemType.Manga)), chapters = listOf(DbChapter(id = 7, mangaId = 3, name = "Colliding chapter")), )) assertFailsWith { val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) } assertEquals(before, database.snapshot()) } @Test fun changedPartsAreMergedOnlyWhenSelected() = runTest { val localChange = DbChangedPart( id = 1, actionType = DbActionType.RemoveItem, isarId = 10, data = "local", clientDate = 1, ) val incomingChange = localChange.copy(id = 99, data = "incoming") val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy(changedParts = listOf(localChange))) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val selected = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("changedParts"), changedParts = listOf(incomingChange), )) val selectedPlan = service.openBackup(selected) as BackupOpenResult.Ready service.applyBackup(selectedPlan.plan) assertEquals(listOf("local", "incoming"), database.changedParts().map { it.data }) assertEquals(listOf(1L, 99L), database.changedParts().map { it.id }) val absent = native(BackupSnapshot(createdAtMillis = 1, includedSections = emptyList(), changedParts = emptyList())) val absentPlan = service.openBackup(absent) as BackupOpenResult.Ready service.applyBackup(absentPlan.plan) assertEquals(listOf("local", "incoming"), database.changedParts().map { it.data }) val selectedEmpty = native(BackupSnapshot(createdAtMillis = 1, includedSections = listOf("changedParts"))) val selectedEmptyPlan = service.openBackup(selectedEmpty) as BackupOpenResult.Ready service.applyBackup(selectedEmptyPlan.plan) assertEquals(listOf("local", "incoming"), database.changedParts().map { it.data }) } @Test fun applyFiltersInvalidDependentRecordsAgainstTheFinalGraph() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot()) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val bytes = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("manga", "categories", "chapters", "history", "updates", "tracks"), mangas = listOf(DbManga(id = 7, name = "Incoming", categories = listOf(1, 999))), categories = listOf(DbCategory(id = 1, name = "Valid", forItemType = DbItemType.Manga)), chapters = listOf( DbChapter(id = 70, mangaId = 7, name = "Valid chapter"), DbChapter(id = 71, mangaId = 999, name = "Orphan chapter"), ), histories = listOf( DbHistory(id = 80, mangaId = 7, chapterId = 70, itemType = DbItemType.Manga), DbHistory(id = 81, mangaId = 999, chapterId = 70, itemType = DbItemType.Manga), ), updates = listOf( DbUpdate(id = 90, mangaId = 7, chapterId = 70), DbUpdate(id = 91, mangaId = 7, chapterId = 999), ), tracks = listOf( DbTrack(id = 100, mangaId = 7, status = DbTrackStatus.Reading), DbTrack(id = 101, mangaId = 999, status = DbTrackStatus.Reading), ), )) val opened = service.openBackup(bytes) as BackupOpenResult.Ready service.applyBackup(opened.plan) assertEquals(listOf(7L), database.mangas().map { it.id }) assertEquals(listOf(1L), database.mangas().single().categories) assertEquals(listOf(70L), database.chapters().map { it.id }) assertEquals(listOf(80L), database.histories().map { it.id }) assertEquals(listOf(90L), database.updates().map { it.id }) assertEquals(listOf(100L), database.tracks().map { it.id }) } @Test fun selectedDependentSectionWithoutTitleTypesReplacesWholeSection() = runTest { val database = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf(DbManga(id = 1, name = "Existing")), chapters = listOf(DbChapter(id = 10, mangaId = 1, name = "Old chapter")), )) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val replacement = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("chapters"), chapters = listOf( DbChapter(id = 20, mangaId = 1, name = "Valid replacement"), DbChapter(id = 21, mangaId = 999, name = "Orphan replacement"), ), )) val replacementPlan = service.openBackup(replacement) as BackupOpenResult.Ready service.applyBackup(replacementPlan.plan) assertEquals(listOf("Valid replacement"), database.chapters().map { it.name }) val selectedEmpty = native(BackupSnapshot( createdAtMillis = 1, includedSections = listOf("chapters"), chapters = emptyList(), )) val selectedEmptyPlan = service.openBackup(selectedEmpty) as BackupOpenResult.Ready service.applyBackup(selectedEmptyPlan.plan) assertTrue(database.chapters().isEmpty(), "selected-empty dependent section did not clear") } @Test fun selectiveExportOpenApplyKeepsExcludedTypesAndTheirDependencies() = runTest { val manga = DbManga(id = 1, name = "Manga", itemType = DbItemType.Manga, categories = listOf(10), sourceId = 20) val anime = DbManga(id = 2, name = "Anime", itemType = DbItemType.Anime, categories = listOf(11), sourceId = 21) val sourceManga = DbSource(id = 20, name = "Manga source", itemType = DbItemType.Manga) val sourceAnime = DbSource(id = 21, name = "Anime source", itemType = DbItemType.Anime) val mangaChapter = DbChapter(id = 30, mangaId = 1, name = "Manga chapter") val animeChapter = DbChapter(id = 31, mangaId = 2, name = "Anime episode") val sourceDatabase = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf(manga, anime), categories = listOf( DbCategory(id = 10, name = "Manga category", forItemType = DbItemType.Manga), DbCategory(id = 11, name = "Anime category", forItemType = DbItemType.Anime), ), sources = listOf(sourceManga, sourceAnime), chapters = listOf(mangaChapter, animeChapter), histories = listOf( DbHistory(id = 40, mangaId = 1, chapterId = 30, itemType = DbItemType.Manga), DbHistory(id = 41, mangaId = 2, chapterId = 31, itemType = DbItemType.Anime), ), updates = listOf( DbUpdate(id = 50, mangaId = 1, chapterId = 30), DbUpdate(id = 51, mangaId = 2, chapterId = 31), ), tracks = listOf( DbTrack(id = 60, mangaId = 1, status = DbTrackStatus.Reading), DbTrack(id = 61, mangaId = 2, status = DbTrackStatus.Watching), ), )) val exporter = DefaultBackupService(sourceDatabase, MemoryFileService(), PassthroughCompression(), NoNotifications) val export = exporter.exportBackup(BackupOptions( includeAnime = false, includeNovel = false, includeSettings = false, compressionLevel = 0, )) val exported = LenientBackupJson .decodeFromString(BackupSnapshot.serializer(), export.bytes.decodeToString()) assertEquals(listOf(1L), exported.mangas.map { it.id }) assertEquals(listOf(30L), exported.chapters.map { it.id }) assertEquals(listOf(40L), exported.histories.map { it.id }) assertEquals(listOf(50L), exported.updates.map { it.id }) assertEquals(listOf(60L), exported.tracks.map { it.id }) assertEquals(listOf(10L), exported.categories.map { it.id }) assertEquals(listOf(20L), exported.sources.map { it.id }) val targetDatabase = InMemoryYokanDatabase(seedDatabaseSnapshot().copy( mangas = listOf(manga.copy(name = "Old manga"), anime), categories = listOf( DbCategory(id = 10, name = "Old manga category", forItemType = DbItemType.Manga), DbCategory(id = 11, name = "Anime category", forItemType = DbItemType.Anime), ), sources = listOf(sourceManga, sourceAnime), chapters = listOf(mangaChapter.copy(name = "Old chapter"), animeChapter), histories = listOf( DbHistory(id = 40, mangaId = 1, chapterId = 30, itemType = DbItemType.Manga), DbHistory(id = 41, mangaId = 2, chapterId = 31, itemType = DbItemType.Anime), ), updates = listOf( DbUpdate(id = 50, mangaId = 1, chapterId = 30), DbUpdate(id = 51, mangaId = 2, chapterId = 31), ), tracks = listOf( DbTrack(id = 60, mangaId = 1, status = DbTrackStatus.Reading), DbTrack(id = 61, mangaId = 2, status = DbTrackStatus.Watching), ), )) val applier = DefaultBackupService(targetDatabase, MemoryFileService(), PassthroughCompression(), NoNotifications) val opened = applier.openBackup(export.bytes) as BackupOpenResult.Ready applier.applyBackup(opened.plan) assertEquals(setOf("Manga", "Anime"), targetDatabase.mangas().map { it.name }.toSet()) assertEquals(setOf(30L, 31L), targetDatabase.chapters().map { it.id }.toSet()) assertEquals(setOf(40L, 41L), targetDatabase.histories().map { it.id }.toSet()) assertEquals(setOf(50L, 51L), targetDatabase.updates().map { it.id }.toSet()) assertEquals(setOf(60L, 61L), targetDatabase.tracks().map { it.id }.toSet()) assertEquals(setOf(10L, 11L), targetDatabase.categories().map { it.id }.toSet()) assertEquals(setOf(20L, 21L), targetDatabase.sources().map { it.id }.toSet()) } @Test fun restoreWithMergeModePreservesExistingTitlesAndMergesChaptersAndCategories() = runTest { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( categories = listOf( DbCategory(id = 1, name = "Action", forItemType = DbItemType.Manga), ), mangas = listOf( DbManga(id = 10, name = "One Piece", link = "https://example.com/op", favorite = true, categories = listOf(1L)), DbManga(id = 20, name = "Naruto", link = "https://example.com/naruto", favorite = true), ), chapters = listOf( DbChapter(id = 100, mangaId = 10, name = "Ch 1", url = "op/1", isRead = false, lastPageRead = "5"), ), ), ) val fileService = MemoryFileService() val service = DefaultBackupService(database, fileService, PassthroughCompression(), NoNotifications) val incomingBackup = BackupSnapshot( createdAtMillis = 100, categories = listOf( DbCategory(id = 99, name = "Action", forItemType = DbItemType.Manga), DbCategory(id = 98, name = "Adventure", forItemType = DbItemType.Manga), ), mangas = listOf( DbManga(id = 50, name = "One Piece", link = "https://example.com/op", categories = listOf(99L, 98L)), DbManga(id = 60, name = "Bleach", link = "https://example.com/bleach", categories = listOf(98L)), ), chapters = listOf( DbChapter(id = 501, mangaId = 50, name = "Ch 1", url = "op/1", isRead = true, lastPageRead = "15"), DbChapter(id = 502, mangaId = 50, name = "Ch 2", url = "op/2", isRead = false, lastPageRead = "1"), DbChapter(id = 601, mangaId = 60, name = "Ch 1", url = "bleach/1", isRead = true), ), ) val bytes = native(incomingBackup) val opened = service.openBackup(bytes) as BackupOpenResult.Ready // Verify preview stats assertEquals(1, opened.plan.preview.newSeriesCount) // Bleach assertEquals(1, opened.plan.preview.updatedSeriesCount) // One Piece assertEquals(2, opened.plan.preview.newChapterCount) // One Piece Ch 2 + Bleach Ch 1 assertEquals(listOf("Action"), opened.plan.preview.conflictingCategories) // Apply in Merge mode service.applyBackup(opened.plan, RestoreMode.Merge) val finalMangas = database.mangas().associateBy { it.name } assertTrue("One Piece" in finalMangas) assertTrue("Naruto" in finalMangas, "Existing title not in backup must be preserved in merge mode") assertTrue("Bleach" in finalMangas, "New title from backup must be added in merge mode") val op = finalMangas.getValue("One Piece") assertTrue(op.favorite) val opCategoryNames = database.categories().filter { it.id in op.categories.orEmpty() }.map { it.name }.toSet() assertEquals(setOf("Action", "Adventure"), opCategoryNames) val opChapters = database.chapters(op.id).associateBy { it.url } val ch1 = opChapters.getValue("op/1") assertTrue(ch1.isRead, "Chapter progress should be merged (isRead = true)") assertEquals("15", ch1.lastPageRead, "Chapter lastPageRead should be maxOf(5, 15)") val ch2 = opChapters.getValue("op/2") assertFalse(ch2.isRead) val bleach = finalMangas.getValue("Bleach") val bleachChapters = database.chapters(bleach.id) assertEquals(1, bleachChapters.size) assertEquals("bleach/1", bleachChapters.first().url) } @Test fun restoreWithReplaceModeWipesNonBackupTitles() = runTest { val database = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 10, name = "One Piece", link = "https://example.com/op", favorite = true), DbManga(id = 20, name = "Naruto", link = "https://example.com/naruto", favorite = true), ), ), ) val service = DefaultBackupService(database, MemoryFileService(), PassthroughCompression(), NoNotifications) val incomingBackup = BackupSnapshot( createdAtMillis = 100, mangas = listOf( DbManga(id = 60, name = "Bleach", link = "https://example.com/bleach"), ), ) val bytes = native(incomingBackup) val opened = service.openBackup(bytes) as BackupOpenResult.Ready // Apply in Replace mode service.applyBackup(opened.plan, RestoreMode.Replace) val mangaNames = database.mangas().map { it.name } assertEquals(listOf("Bleach"), mangaNames, "Replace mode must replace titles of included sections") } }