package eu.friedcutlet.yokan.services.library import eu.friedcutlet.yokan.database.* import eu.friedcutlet.yokan.database.sync.SyncPaths import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.services.download.DownloadProgress import eu.friedcutlet.yokan.services.download.DownloadRequest import eu.friedcutlet.yokan.services.download.DownloadService import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue class SelectiveDataClearServiceTest { @Test fun clearingMangaRemovesItsDatabaseDependentsAndKeepsOtherMediaUntouched() { val snapshot = mixedSnapshot() val plan = planLibraryClear(snapshot, setOf(DbItemType.Manga)) assertNotNull(plan.after) assertTrue(plan.error == null) assertEquals(listOf(2L, 3L), plan.after.mangas.map { it.id }) assertEquals(listOf(20L, 30L), plan.after.chapters.map { it.id }) assertEquals(listOf(2L, 3L), plan.after.histories.map { it.id }) assertEquals(listOf(2L, 3L), plan.after.updates.map { it.id }) assertEquals(listOf(2L, 3L), plan.after.tracks.map { it.id }) assertEquals(listOf(2L, 3L), plan.after.trackSearches.map { it.libraryId }) assertEquals(listOf(2L, 3L), plan.after.downloads.map { it.id }) assertEquals(listOf(20L, 30L), plan.after.pageCache.mapNotNull { it.chapterId }) assertEquals(snapshot.categories, plan.after.categories) assertEquals(snapshot.sources, plan.after.sources) assertEquals(snapshot.mangas.drop(1), plan.after.mangas) assertEquals(snapshot.chapters.drop(1), plan.after.chapters) assertEquals(listOf(2L, 3L), plan.after.settings.titleMetadata?.map { it.mangaId }) assertEquals(listOf(20L, 30L), plan.after.settings.chapterPageIndexList?.mapNotNull { it.chapterId }) } @Test fun replacementOutboxContainsTitleAndKeyedSettingsTombstones() { val before = mixedSnapshot().copy( syncPreferences = listOf(DbSyncPreference(syncOn = true, authToken = "token", server = "https://hub", hubEpoch = 1, hubBootstrapComplete = true)), ) val after = planLibraryClear(before, setOf(DbItemType.Manga)).after val outbox = SyncPaths.outboxForReplacement(before, after) assertTrue(outbox.any { it.kind == SyncPaths.KIND_MANGA && it.id == 1L && it.path == SyncPaths.DELETED }) assertTrue(outbox.any { it.kind == SyncPaths.KIND_SETTINGS && it.path == "titleMetadata.1" && it.value == null }) } @Test fun mismatchedExplicitHistoryTypeRejectsUnsafeCascade() { val snapshot = mixedSnapshot().copy(histories = listOf(DbHistory(id = 1, mangaId = 1, chapterId = 10, itemType = DbItemType.Anime))) val plan = planLibraryClear(snapshot, setOf(DbItemType.Manga)) assertNotNull(plan.error) assertEquals(snapshot, plan.after) } @Test fun clearingSourcesRemovesOnlyTheirPreferencesAndCache() { val snapshot = mixedSnapshot().copy( settings = DbSettings(sourcePreferences = listOf( DbSourcePreference(1, "a", "1"), DbSourcePreference(2, "b", "2"), )), pageCache = listOf(DbChapterPageUrls(chapterId = 10, sourceId = 1), DbChapterPageUrls(chapterId = 20, sourceId = 2)), ) val after = planSourceClear(snapshot, setOf(DbItemType.Manga)).after assertEquals(listOf(2L), after.sources.map { it.id }) assertEquals(listOf(DbSourcePreference(2, "b", "2")), after.settings.sourcePreferences) assertEquals(listOf(20L), after.pageCache.mapNotNull { it.chapterId }) assertEquals(snapshot.mangas, after.mangas) } @Test fun activePersistedDownloadRefusesClearWithoutChangingSnapshot() = runTest { val snapshot = mixedSnapshot().copy(downloads = listOf(DbDownload(id = 1, chapterId = 10, isDownload = false, isStartDownload = true))) val database = InMemoryYokanDatabase(snapshot) val result = SelectiveDataClearService(database, NoDownloads).clearLibrary(setOf(ItemType.Manga)) assertTrue(!result.cleared) assertNotNull(result.error) assertEquals(snapshot, database.snapshot()) } @Test fun successfulClearRemovesRowsWithoutCallingPhysicalFileDeletion() = runTest { val database = InMemoryYokanDatabase(mixedSnapshot()) val result = SelectiveDataClearService(database, NoDownloads).clearLibrary(setOf(ItemType.Manga)) assertTrue(result.cleared) assertEquals(1, result.titlesRemoved) assertEquals(listOf(2L, 3L), database.snapshot().mangas.map { it.id }) } private fun mixedSnapshot(): YokanDatabaseSnapshot { val manga = DbManga(id = 1, itemType = DbItemType.Manga, favorite = false, isLocalArchive = true) val anime = DbManga(id = 2, itemType = DbItemType.Anime, favorite = true) val novel = DbManga(id = 3, itemType = DbItemType.Novel) return YokanDatabaseSnapshot( settings = DbSettings( titleMetadata = listOf(DbTitleMetadata(1), DbTitleMetadata(2), DbTitleMetadata(3)), chapterPageIndexList = listOf(DbChapterPageIndex(10, 1), DbChapterPageIndex(20, 2), DbChapterPageIndex(30, 3)), downloadQueueOrder = listOf(1, 2, 3), ), mangas = listOf(manga, anime, novel), chapters = listOf(DbChapter(id = 10, mangaId = 1), DbChapter(id = 20, mangaId = 2), DbChapter(id = 30, mangaId = 3)), categories = listOf(DbCategory(id = 1, name = "Manga", forItemType = DbItemType.Manga)), sources = listOf(DbSource(id = 1, itemType = DbItemType.Manga), DbSource(id = 2, itemType = DbItemType.Anime)), histories = listOf(1, 2, 3).map { DbHistory(id = it.toLong(), mangaId = it.toLong(), chapterId = it * 10L, itemType = DbItemType.entries[it - 1]) }, updates = listOf(1, 2, 3).map { DbUpdate(id = it.toLong(), mangaId = it.toLong(), chapterId = it * 10L) }, downloads = listOf(1, 2, 3).map { DbDownload(id = it.toLong(), chapterId = it * 10L, isStartDownload = false) }, tracks = listOf(1, 2, 3).map { DbTrack(id = it.toLong(), mangaId = it.toLong(), itemType = DbItemType.entries[it - 1], status = DbTrackStatus.Reading) }, trackPreferences = emptyList(), trackSearches = listOf(1, 2, 3).map { DbTrackSearch(id = it.toLong(), libraryId = it.toLong()) }, syncPreferences = emptyList(), changedParts = emptyList(), customButtons = emptyList(), pageCache = listOf(10L, 20L, 30L).map { DbChapterPageUrls(chapterId = it) }, ) } private object NoDownloads : DownloadService { override suspend fun initialize(poolSize: Int) = Unit override fun enqueue(request: DownloadRequest, onProgress: (DownloadProgress) -> Unit) = Unit override fun cancel(taskId: String) = Unit override fun pause(taskId: String) = Unit override fun resume(taskId: String) = Unit override fun retry(taskId: String) = false override fun storageLocation(taskId: String): String? = null override fun deleteArtifacts(taskId: String) = error("Selective clear must not delete media files") override fun activeDownloads(): List = emptyList() } }