package eu.friedcutlet.yokan.services.download import eu.friedcutlet.yokan.database.* import kotlin.test.* class DownloadPolicyTest { @Test fun downloadNamesNeverCreateParentOrCurrentDirectorySegments() { assertEquals("_", sanitizeDownloadName("..")) assertEquals("_", sanitizeDownloadName(" . ")) assertEquals("_", sanitizeDownloadName(" ")) assertEquals("Title_Chapter", sanitizeDownloadName("Title/Chapter")) } @Test fun defaultsDoNotDownloadOrDeleteAnything() { val snapshot = policySnapshot() assertEquals(emptyList(), automaticDownloadCandidates(snapshot, setOf(10, 11))) assertEquals(emptyList(), downloadAheadCandidates(snapshot, 10)) assertEquals(emptyList(), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet())) } @Test fun automaticDownloadsApplyCategoriesUnreadFillerAndSourceRules() { val policy = DbDownloadPolicy(autoDownloadNew = true, includeCategoryIds = setOf(5)) val snapshot = policySnapshot(policy).let { it.copy(chapters = it.chapters.map { c -> when (c.id) { 11L -> c.copy(isRead = true) 12L -> c.copy(isFiller = true) else -> c } }) } assertEquals(listOf(10L, 13L, 14L), automaticDownloadCandidates(snapshot, snapshot.chapters.mapTo(mutableSetOf()) { it.id })) assertEquals(emptyList(), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(excludeCategoryIds = setOf(5))), setOf(10))) assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(sources = snapshot.sources.map { it.copy(isActive = false) }), setOf(10))) assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(favorite = false) }), setOf(10))) assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(isLocalArchive = true) }), setOf(10))) assertEquals(listOf(11L, 12L), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(unreadOnly = false, includeFiller = true)), setOf(11, 12))) } @Test fun stoppedPausedFailedAndCompletedRowsAreNeverAutomaticallyRequeued() { val snapshot = policySnapshot(DbDownloadPolicy(autoDownloadNew = true)).copy(downloads = listOf( DbDownload(id = 1, chapterId = 10, isPaused = true), DbDownload(id = 2, chapterId = 11, failed = 1), DbDownload(id = 3, chapterId = 12, isDownload = true), DbDownload(id = 4, chapterId = 13, isStartDownload = false), )) assertEquals(listOf(14L), automaticDownloadCandidates(snapshot, setOf(10, 11, 12, 13, 14))) } @Test fun aheadUsesReadingOrderAndExistingRowsCountTowardWindow() { val snapshot = policySnapshot(DbDownloadPolicy(aheadManga = 2)).let { it.copy( chapters = it.chapters.reversed(), downloads = listOf(DbDownload(chapterId = 11, isPaused = true)), ) } assertEquals(listOf(12L), downloadAheadCandidates(snapshot, 10)) assertEquals(emptyList(), downloadAheadCandidates(snapshot.copy(settings = snapshot.settings.copy(incognitoMode = true)), 10)) } @Test fun aheadAppliesPerMediaCountsAndSkipsReadAndFiller() { for (type in DbItemType.entries) { val snapshot = policySnapshot(DbDownloadPolicy(aheadManga = 2, aheadAnime = 2, aheadNovel = 2)).let { it.copy( mangas = it.mangas.map { m -> m.copy(itemType = type) }, chapters = it.chapters.map { c -> when (c.id) { 11L -> c.copy(isRead = true); 12L -> c.copy(isFiller = true); else -> c } }, ) } assertEquals(listOf(13L, 14L), downloadAheadCandidates(snapshot, 10), type.name) } } @Test fun manualFillerMarksOverrideTheSourceValueForAutomaticAndAheadDownloads() { val policy = DbDownloadPolicy(autoDownloadNew = true, includeCategoryIds = setOf(5), aheadManga = 2) val snapshot = policySnapshot(policy).let { it.copy(chapters = it.chapters.map { c -> when (c.id) { 11L -> c.copy(fillerOverride = true) 12L -> c.copy(isFiller = true, fillerOverride = false) else -> c } }) } assertEquals(listOf(10L, 12L, 13L, 14L), automaticDownloadCandidates(snapshot, snapshot.chapters.mapTo(mutableSetOf()) { it.id })) assertEquals(listOf(12L, 13L), downloadAheadCandidates(snapshot, 10)) // A manually marked filler that is currently open still drives download-ahead. assertEquals(listOf(12L, 13L), downloadAheadCandidates(snapshot, 11)) assertEquals(listOf(11L, 12L), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(includeFiller = true)), setOf(11, 12))) } @Test fun cleanupRequiresConsumptionProofAndRespectsRetentionAndBookmarks() { val policy = DbDownloadPolicy(cleanupAfterConsumption = true, consumedChapterIds = setOf(10, 11, 12, 13, 14), keepLastConsumed = 1) val snapshot = policySnapshot(policy).let { it.copy( chapters = it.chapters.map { c -> c.copy(isRead = true, isBookmarked = c.id == 11L) }, downloads = it.chapters.map { c -> DbDownload(id = c.id, chapterId = c.id, isDownload = true) }, ) } assertEquals(listOf(10L, 12L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, setOf(13))) assertEquals(emptyList(), downloadCleanupCandidates(snapshot.withPolicy(policy.copy(consumedChapterIds = emptySet())), 1, DownloadCleanupReason.Consumption, emptySet())) assertEquals(emptyList(), downloadCleanupCandidates(snapshot.withPolicy(policy.copy(cleanupExcludedCategoryIds = setOf(5))), 1, DownloadCleanupReason.Consumption, emptySet())) assertEquals(emptyList(), downloadCleanupCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(isLocalArchive = true) }), 1, DownloadCleanupReason.Consumption, emptySet())) } @Test fun manualCleanupIsIndependentAndProtectsIncompleteOrPausedWork() { val snapshot = policySnapshot(DbDownloadPolicy(cleanupAfterManualMark = true, manuallyMarkedChapterIds = setOf(10, 11, 12, 13))).let { it.copy( chapters = it.chapters.map { c -> c.copy(isRead = true) }, downloads = listOf( DbDownload(chapterId = 10, isDownload = true), DbDownload(chapterId = 11, isDownload = true, isStartDownload = true), DbDownload(chapterId = 12, isDownload = true, isPaused = true), DbDownload(chapterId = 13, isDownload = false), ), ) } assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.ManualMark, emptySet())) assertEquals(emptyList(), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet())) } @Test fun legacyDeletePreferenceUsesNewProtections() { val snapshot = policySnapshot(DbDownloadPolicy(consumedChapterIds = setOf(10))).let { it.copy( settings = it.settings.copy(deleteDownloadAfterReading = true), chapters = it.chapters.map { c -> c.copy(isRead = true) }, downloads = listOf(DbDownload(chapterId = 10, isDownload = true)), ) } assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet())) } @Test fun automaticDownloadsUseConfiguredReleasePriorityRatherThanEveryDuplicate() { val snapshot = policySnapshot(DbDownloadPolicy(autoDownloadNew = true)).let { it.copy( chapters = listOf( DbChapter(id = 10, mangaId = 1, name = "Chapter 1", scanlator = "Other"), DbChapter(id = 11, mangaId = 1, name = "Chapter 1", scanlator = "Preferred"), ), settings = it.settings.copy(chapterReleasePreferences = listOf(DbChapterReleasePreference(mangaId = 1, scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true))), ) } assertEquals(listOf(11L), automaticDownloadCandidates(snapshot, setOf(10, 11))) } @Test fun cleanupIncludesConsumedAlternatesAndRetentionKeepsEveryVariantOfRetainedChapter() { val snapshot = policySnapshot(DbDownloadPolicy(cleanupAfterConsumption = true, consumedChapterIds = setOf(10, 11, 12), keepLastConsumed = 1)).let { it.copy( chapters = listOf( DbChapter(id = 10, mangaId = 1, name = "Chapter 1", scanlator = "Other", isRead = true), DbChapter(id = 11, mangaId = 1, name = "Chapter 2", scanlator = "Preferred", isRead = true), DbChapter(id = 12, mangaId = 1, name = "Chapter 2", scanlator = "Other", isRead = true), ), downloads = (10L..12L).map { id -> DbDownload(id = id, chapterId = id, isDownload = true) }, settings = it.settings.copy(chapterReleasePreferences = listOf(DbChapterReleasePreference(mangaId = 1, scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true))), ) } assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet())) } } internal fun policySnapshot(policy: DbDownloadPolicy = DbDownloadPolicy()): YokanDatabaseSnapshot = seedDatabaseSnapshot().copy( settings = DbSettings(features = DbFeaturePreferences(downloads = policy)), mangas = listOf(DbManga(id = 1, name = "Title", sourceId = 7, favorite = true, categories = listOf(5), itemType = DbItemType.Manga)), sources = listOf(DbSource(id = 7, name = "Source", isAdded = true)), chapters = (10L..14L).map { DbChapter(id = it, mangaId = 1, name = "Chapter $it", url = "/$it") }, downloads = emptyList(), ) internal fun YokanDatabaseSnapshot.withPolicy(policy: DbDownloadPolicy): YokanDatabaseSnapshot = copy(settings = settings.copy(features = settings.features.copy(downloads = policy)))