Yokan / rebuild
Course 1 8624561

SelectiveDataClearServiceTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/library/SelectiveDataClearServiceTest.kt

test / fixture137 lines
1package eu.friedcutlet.yokan.services.library23import eu.friedcutlet.yokan.database.*4import eu.friedcutlet.yokan.database.sync.SyncPaths5import eu.friedcutlet.yokan.domain.ItemType6import eu.friedcutlet.yokan.services.download.DownloadProgress7import eu.friedcutlet.yokan.services.download.DownloadRequest8import eu.friedcutlet.yokan.services.download.DownloadService9import kotlinx.coroutines.test.runTest10import kotlin.test.Test11import kotlin.test.assertEquals12import kotlin.test.assertNotNull13import kotlin.test.assertTrue1415class SelectiveDataClearServiceTest {16    @Test17    fun clearingMangaRemovesItsDatabaseDependentsAndKeepsOtherMediaUntouched() {18        val snapshot = mixedSnapshot()19        val plan = planLibraryClear(snapshot, setOf(DbItemType.Manga))2021        assertNotNull(plan.after)22        assertTrue(plan.error == null)23        assertEquals(listOf(2L, 3L), plan.after.mangas.map { it.id })24        assertEquals(listOf(20L, 30L), plan.after.chapters.map { it.id })25        assertEquals(listOf(2L, 3L), plan.after.histories.map { it.id })26        assertEquals(listOf(2L, 3L), plan.after.updates.map { it.id })27        assertEquals(listOf(2L, 3L), plan.after.tracks.map { it.id })28        assertEquals(listOf(2L, 3L), plan.after.trackSearches.map { it.libraryId })29        assertEquals(listOf(2L, 3L), plan.after.downloads.map { it.id })30        assertEquals(listOf(20L, 30L), plan.after.pageCache.mapNotNull { it.chapterId })31        assertEquals(snapshot.categories, plan.after.categories)32        assertEquals(snapshot.sources, plan.after.sources)33        assertEquals(snapshot.mangas.drop(1), plan.after.mangas)34        assertEquals(snapshot.chapters.drop(1), plan.after.chapters)35        assertEquals(listOf(2L, 3L), plan.after.settings.titleMetadata?.map { it.mangaId })36        assertEquals(listOf(20L, 30L), plan.after.settings.chapterPageIndexList?.mapNotNull { it.chapterId })37    }3839    @Test40    fun replacementOutboxContainsTitleAndKeyedSettingsTombstones() {41        val before = mixedSnapshot().copy(42            syncPreferences = listOf(DbSyncPreference(syncOn = true, authToken = "token", server = "https://hub", hubEpoch = 1, hubBootstrapComplete = true)),43        )44        val after = planLibraryClear(before, setOf(DbItemType.Manga)).after45        val outbox = SyncPaths.outboxForReplacement(before, after)4647        assertTrue(outbox.any { it.kind == SyncPaths.KIND_MANGA && it.id == 1L && it.path == SyncPaths.DELETED })48        assertTrue(outbox.any { it.kind == SyncPaths.KIND_SETTINGS && it.path == "titleMetadata.1" && it.value == null })49    }5051    @Test52    fun mismatchedExplicitHistoryTypeRejectsUnsafeCascade() {53        val snapshot = mixedSnapshot().copy(histories = listOf(DbHistory(id = 1, mangaId = 1, chapterId = 10, itemType = DbItemType.Anime)))54        val plan = planLibraryClear(snapshot, setOf(DbItemType.Manga))5556        assertNotNull(plan.error)57        assertEquals(snapshot, plan.after)58    }5960    @Test61    fun clearingSourcesRemovesOnlyTheirPreferencesAndCache() {62        val snapshot = mixedSnapshot().copy(63            settings = DbSettings(sourcePreferences = listOf(64                DbSourcePreference(1, "a", "1"),65                DbSourcePreference(2, "b", "2"),66            )),67            pageCache = listOf(DbChapterPageUrls(chapterId = 10, sourceId = 1), DbChapterPageUrls(chapterId = 20, sourceId = 2)),68        )6970        val after = planSourceClear(snapshot, setOf(DbItemType.Manga)).after7172        assertEquals(listOf(2L), after.sources.map { it.id })73        assertEquals(listOf(DbSourcePreference(2, "b", "2")), after.settings.sourcePreferences)74        assertEquals(listOf(20L), after.pageCache.mapNotNull { it.chapterId })75        assertEquals(snapshot.mangas, after.mangas)76    }7778    @Test79    fun activePersistedDownloadRefusesClearWithoutChangingSnapshot() = runTest {80        val snapshot = mixedSnapshot().copy(downloads = listOf(DbDownload(id = 1, chapterId = 10, isDownload = false, isStartDownload = true)))81        val database = InMemoryYokanDatabase(snapshot)82        val result = SelectiveDataClearService(database, NoDownloads).clearLibrary(setOf(ItemType.Manga))8384        assertTrue(!result.cleared)85        assertNotNull(result.error)86        assertEquals(snapshot, database.snapshot())87    }8889    @Test90    fun successfulClearRemovesRowsWithoutCallingPhysicalFileDeletion() = runTest {91        val database = InMemoryYokanDatabase(mixedSnapshot())92        val result = SelectiveDataClearService(database, NoDownloads).clearLibrary(setOf(ItemType.Manga))9394        assertTrue(result.cleared)95        assertEquals(1, result.titlesRemoved)96        assertEquals(listOf(2L, 3L), database.snapshot().mangas.map { it.id })97    }9899    private fun mixedSnapshot(): YokanDatabaseSnapshot {100        val manga = DbManga(id = 1, itemType = DbItemType.Manga, favorite = false, isLocalArchive = true)101        val anime = DbManga(id = 2, itemType = DbItemType.Anime, favorite = true)102        val novel = DbManga(id = 3, itemType = DbItemType.Novel)103        return YokanDatabaseSnapshot(104            settings = DbSettings(105                titleMetadata = listOf(DbTitleMetadata(1), DbTitleMetadata(2), DbTitleMetadata(3)),106                chapterPageIndexList = listOf(DbChapterPageIndex(10, 1), DbChapterPageIndex(20, 2), DbChapterPageIndex(30, 3)),107                downloadQueueOrder = listOf(1, 2, 3),108            ),109            mangas = listOf(manga, anime, novel),110            chapters = listOf(DbChapter(id = 10, mangaId = 1), DbChapter(id = 20, mangaId = 2), DbChapter(id = 30, mangaId = 3)),111            categories = listOf(DbCategory(id = 1, name = "Manga", forItemType = DbItemType.Manga)),112            sources = listOf(DbSource(id = 1, itemType = DbItemType.Manga), DbSource(id = 2, itemType = DbItemType.Anime)),113            histories = listOf(1, 2, 3).map { DbHistory(id = it.toLong(), mangaId = it.toLong(), chapterId = it * 10L, itemType = DbItemType.entries[it - 1]) },114            updates = listOf(1, 2, 3).map { DbUpdate(id = it.toLong(), mangaId = it.toLong(), chapterId = it * 10L) },115            downloads = listOf(1, 2, 3).map { DbDownload(id = it.toLong(), chapterId = it * 10L, isStartDownload = false) },116            tracks = listOf(1, 2, 3).map { DbTrack(id = it.toLong(), mangaId = it.toLong(), itemType = DbItemType.entries[it - 1], status = DbTrackStatus.Reading) },117            trackPreferences = emptyList(),118            trackSearches = listOf(1, 2, 3).map { DbTrackSearch(id = it.toLong(), libraryId = it.toLong()) },119            syncPreferences = emptyList(),120            changedParts = emptyList(),121            customButtons = emptyList(),122            pageCache = listOf(10L, 20L, 30L).map { DbChapterPageUrls(chapterId = it) },123        )124    }125126    private object NoDownloads : DownloadService {127        override suspend fun initialize(poolSize: Int) = Unit128        override fun enqueue(request: DownloadRequest, onProgress: (DownloadProgress) -> Unit) = Unit129        override fun cancel(taskId: String) = Unit130        override fun pause(taskId: String) = Unit131        override fun resume(taskId: String) = Unit132        override fun retry(taskId: String) = false133        override fun storageLocation(taskId: String): String? = null134        override fun deleteArtifacts(taskId: String) = error("Selective clear must not delete media files")135        override fun activeDownloads(): List<DownloadProgress> = emptyList()136    }137}138

Search chapters, reference and source filenames.