DownloadPolicyTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/download/DownloadPolicyTest.kt
1package eu.friedcutlet.yokan.services.download23import eu.friedcutlet.yokan.database.*4import kotlin.test.*56class DownloadPolicyTest {7 @Test fun downloadNamesNeverCreateParentOrCurrentDirectorySegments() {8 assertEquals("_", sanitizeDownloadName(".."))9 assertEquals("_", sanitizeDownloadName(" . "))10 assertEquals("_", sanitizeDownloadName(" "))11 assertEquals("Title_Chapter", sanitizeDownloadName("Title/Chapter"))12 }13 @Test fun defaultsDoNotDownloadOrDeleteAnything() {14 val snapshot = policySnapshot()15 assertEquals(emptyList(), automaticDownloadCandidates(snapshot, setOf(10, 11)))16 assertEquals(emptyList(), downloadAheadCandidates(snapshot, 10))17 assertEquals(emptyList(), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet()))18 }1920 @Test fun automaticDownloadsApplyCategoriesUnreadFillerAndSourceRules() {21 val policy = DbDownloadPolicy(autoDownloadNew = true, includeCategoryIds = setOf(5))22 val snapshot = policySnapshot(policy).let { it.copy(chapters = it.chapters.map { c -> when (c.id) {23 11L -> c.copy(isRead = true)24 12L -> c.copy(isFiller = true)25 else -> c26 } }) }27 assertEquals(listOf(10L, 13L, 14L), automaticDownloadCandidates(snapshot, snapshot.chapters.mapTo(mutableSetOf()) { it.id }))28 assertEquals(emptyList(), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(excludeCategoryIds = setOf(5))), setOf(10)))29 assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(sources = snapshot.sources.map { it.copy(isActive = false) }), setOf(10)))30 assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(favorite = false) }), setOf(10)))31 assertEquals(emptyList(), automaticDownloadCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(isLocalArchive = true) }), setOf(10)))32 assertEquals(listOf(11L, 12L), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(unreadOnly = false, includeFiller = true)), setOf(11, 12)))33 }3435 @Test fun stoppedPausedFailedAndCompletedRowsAreNeverAutomaticallyRequeued() {36 val snapshot = policySnapshot(DbDownloadPolicy(autoDownloadNew = true)).copy(downloads = listOf(37 DbDownload(id = 1, chapterId = 10, isPaused = true),38 DbDownload(id = 2, chapterId = 11, failed = 1),39 DbDownload(id = 3, chapterId = 12, isDownload = true),40 DbDownload(id = 4, chapterId = 13, isStartDownload = false),41 ))42 assertEquals(listOf(14L), automaticDownloadCandidates(snapshot, setOf(10, 11, 12, 13, 14)))43 }4445 @Test fun aheadUsesReadingOrderAndExistingRowsCountTowardWindow() {46 val snapshot = policySnapshot(DbDownloadPolicy(aheadManga = 2)).let { it.copy(47 chapters = it.chapters.reversed(),48 downloads = listOf(DbDownload(chapterId = 11, isPaused = true)),49 ) }50 assertEquals(listOf(12L), downloadAheadCandidates(snapshot, 10))51 assertEquals(emptyList(), downloadAheadCandidates(snapshot.copy(settings = snapshot.settings.copy(incognitoMode = true)), 10))52 }5354 @Test fun aheadAppliesPerMediaCountsAndSkipsReadAndFiller() {55 for (type in DbItemType.entries) {56 val snapshot = policySnapshot(DbDownloadPolicy(aheadManga = 2, aheadAnime = 2, aheadNovel = 2)).let { it.copy(57 mangas = it.mangas.map { m -> m.copy(itemType = type) },58 chapters = it.chapters.map { c -> when (c.id) { 11L -> c.copy(isRead = true); 12L -> c.copy(isFiller = true); else -> c } },59 ) }60 assertEquals(listOf(13L, 14L), downloadAheadCandidates(snapshot, 10), type.name)61 }62 }6364 @Test fun manualFillerMarksOverrideTheSourceValueForAutomaticAndAheadDownloads() {65 val policy = DbDownloadPolicy(autoDownloadNew = true, includeCategoryIds = setOf(5), aheadManga = 2)66 val snapshot = policySnapshot(policy).let { it.copy(chapters = it.chapters.map { c -> when (c.id) {67 11L -> c.copy(fillerOverride = true)68 12L -> c.copy(isFiller = true, fillerOverride = false)69 else -> c70 } }) }71 assertEquals(listOf(10L, 12L, 13L, 14L), automaticDownloadCandidates(snapshot, snapshot.chapters.mapTo(mutableSetOf()) { it.id }))72 assertEquals(listOf(12L, 13L), downloadAheadCandidates(snapshot, 10))73 // A manually marked filler that is currently open still drives download-ahead.74 assertEquals(listOf(12L, 13L), downloadAheadCandidates(snapshot, 11))75 assertEquals(listOf(11L, 12L), automaticDownloadCandidates(snapshot.withPolicy(policy.copy(includeFiller = true)), setOf(11, 12)))76 }7778 @Test fun cleanupRequiresConsumptionProofAndRespectsRetentionAndBookmarks() {79 val policy = DbDownloadPolicy(cleanupAfterConsumption = true, consumedChapterIds = setOf(10, 11, 12, 13, 14), keepLastConsumed = 1)80 val snapshot = policySnapshot(policy).let { it.copy(81 chapters = it.chapters.map { c -> c.copy(isRead = true, isBookmarked = c.id == 11L) },82 downloads = it.chapters.map { c -> DbDownload(id = c.id, chapterId = c.id, isDownload = true) },83 ) }84 assertEquals(listOf(10L, 12L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, setOf(13)))85 assertEquals(emptyList(), downloadCleanupCandidates(snapshot.withPolicy(policy.copy(consumedChapterIds = emptySet())), 1, DownloadCleanupReason.Consumption, emptySet()))86 assertEquals(emptyList(), downloadCleanupCandidates(snapshot.withPolicy(policy.copy(cleanupExcludedCategoryIds = setOf(5))), 1, DownloadCleanupReason.Consumption, emptySet()))87 assertEquals(emptyList(), downloadCleanupCandidates(snapshot.copy(mangas = snapshot.mangas.map { it.copy(isLocalArchive = true) }), 1, DownloadCleanupReason.Consumption, emptySet()))88 }8990 @Test fun manualCleanupIsIndependentAndProtectsIncompleteOrPausedWork() {91 val snapshot = policySnapshot(DbDownloadPolicy(cleanupAfterManualMark = true, manuallyMarkedChapterIds = setOf(10, 11, 12, 13))).let { it.copy(92 chapters = it.chapters.map { c -> c.copy(isRead = true) },93 downloads = listOf(94 DbDownload(chapterId = 10, isDownload = true),95 DbDownload(chapterId = 11, isDownload = true, isStartDownload = true),96 DbDownload(chapterId = 12, isDownload = true, isPaused = true),97 DbDownload(chapterId = 13, isDownload = false),98 ),99 ) }100 assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.ManualMark, emptySet()))101 assertEquals(emptyList(), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet()))102 }103104 @Test fun legacyDeletePreferenceUsesNewProtections() {105 val snapshot = policySnapshot(DbDownloadPolicy(consumedChapterIds = setOf(10))).let { it.copy(106 settings = it.settings.copy(deleteDownloadAfterReading = true),107 chapters = it.chapters.map { c -> c.copy(isRead = true) },108 downloads = listOf(DbDownload(chapterId = 10, isDownload = true)),109 ) }110 assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet()))111 }112113 @Test fun automaticDownloadsUseConfiguredReleasePriorityRatherThanEveryDuplicate() {114 val snapshot = policySnapshot(DbDownloadPolicy(autoDownloadNew = true)).let { it.copy(115 chapters = listOf(116 DbChapter(id = 10, mangaId = 1, name = "Chapter 1", scanlator = "Other"),117 DbChapter(id = 11, mangaId = 1, name = "Chapter 1", scanlator = "Preferred"),118 ),119 settings = it.settings.copy(chapterReleasePreferences = listOf(DbChapterReleasePreference(mangaId = 1, scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true))),120 ) }121 assertEquals(listOf(11L), automaticDownloadCandidates(snapshot, setOf(10, 11)))122 }123124 @Test fun cleanupIncludesConsumedAlternatesAndRetentionKeepsEveryVariantOfRetainedChapter() {125 val snapshot = policySnapshot(DbDownloadPolicy(cleanupAfterConsumption = true, consumedChapterIds = setOf(10, 11, 12), keepLastConsumed = 1)).let { it.copy(126 chapters = listOf(127 DbChapter(id = 10, mangaId = 1, name = "Chapter 1", scanlator = "Other", isRead = true),128 DbChapter(id = 11, mangaId = 1, name = "Chapter 2", scanlator = "Preferred", isRead = true),129 DbChapter(id = 12, mangaId = 1, name = "Chapter 2", scanlator = "Other", isRead = true),130 ),131 downloads = (10L..12L).map { id -> DbDownload(id = id, chapterId = id, isDownload = true) },132 settings = it.settings.copy(chapterReleasePreferences = listOf(DbChapterReleasePreference(mangaId = 1, scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true))),133 ) }134 assertEquals(listOf(10L), downloadCleanupCandidates(snapshot, 1, DownloadCleanupReason.Consumption, emptySet()))135 }136}137138internal fun policySnapshot(policy: DbDownloadPolicy = DbDownloadPolicy()): YokanDatabaseSnapshot = seedDatabaseSnapshot().copy(139 settings = DbSettings(features = DbFeaturePreferences(downloads = policy)),140 mangas = listOf(DbManga(id = 1, name = "Title", sourceId = 7, favorite = true, categories = listOf(5), itemType = DbItemType.Manga)),141 sources = listOf(DbSource(id = 7, name = "Source", isAdded = true)),142 chapters = (10L..14L).map { DbChapter(id = it, mangaId = 1, name = "Chapter $it", url = "/$it") },143 downloads = emptyList(),144)145146internal fun YokanDatabaseSnapshot.withPolicy(policy: DbDownloadPolicy): YokanDatabaseSnapshot =147 copy(settings = settings.copy(features = settings.features.copy(downloads = policy)))148