Yokan / rebuild
Course 1 8624561

ChapterListDefaultsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/detail/ChapterListDefaultsTest.kt

test / fixture101 lines
1package eu.friedcutlet.yokan.ui.screens.detail23import eu.friedcutlet.yokan.database.DatabaseJson4import eu.friedcutlet.yokan.database.DbChapterFilter5import eu.friedcutlet.yokan.database.DbChapterListDefaults6import eu.friedcutlet.yokan.database.DbItemType7import eu.friedcutlet.yokan.database.DbSettings8import eu.friedcutlet.yokan.database.DbSortChapter9import eu.friedcutlet.yokan.domain.Chapter10import eu.friedcutlet.yokan.domain.ItemType11import eu.friedcutlet.yokan.domain.Manga12import eu.friedcutlet.yokan.domain.Status13import kotlin.test.Test14import kotlin.test.assertEquals15import kotlin.test.assertTrue16import eu.friedcutlet.yokan.services.library.ChapterFilterKind17import eu.friedcutlet.yokan.services.library.ChapterFilterTypes18import eu.friedcutlet.yokan.services.library.chapterFilters19import eu.friedcutlet.yokan.services.library.chapterSort20import eu.friedcutlet.yokan.services.library.visibleChapters21import eu.friedcutlet.yokan.services.library.withChapterFilterCycled22import eu.friedcutlet.yokan.services.library.withChapterListDefaultFrom23import eu.friedcutlet.yokan.services.library.withChapterSortToggled2425class ChapterListDefaultsTest {26    private fun title(id: Long, type: ItemType, favorite: Boolean = true) = Manga(id, "Title $id", link = "/$id", description = "",27        status = Status.Ongoing, itemType = type, favorite = favorite, source = "Source", lang = "en", dateAddedMillis = 0, lastUpdateMillis = 0)2829    private val chapters = (1L..3L).map { Chapter(it, 0, "Chapter $it", "/$it", isRead = it == 2L) }3031    @Test fun withoutADefaultUnconfiguredTitlesKeepTheBuiltInSortAndNoFilters() {32        val manga = title(1, ItemType.Anime)33        assertEquals(DbSortChapter(mangaId = 1, index = 1, reverse = false), DbSettings().chapterSort(manga))34        assertEquals(ChapterFilterTypes(0, 0, 0), DbSettings().chapterFilters(manga))35        assertEquals(listOf(3L, 2L, 1L), visibleChapters(manga, chapters, DbSettings(), emptySet()).map { it.id })36    }3738    @Test fun setAsDefaultAppliesToUnconfiguredTitlesOfTheSameTypeOnly() {39        val source = title(1, ItemType.Anime)40        val configured = DbSettings()41            .withChapterSortToggled(source, 1) // re-selecting the current sort flips its direction42            .withChapterFilterCycled(source, ChapterFilterKind.Unread)43        val db = configured.withChapterListDefaultFrom(source, emptyList())44        assertEquals(DbChapterListDefaults(sortIndex = 1, sortReverse = true, unread = 1), db.features.library.chapterListDefault(DbItemType.Anime))45        val newAnime = title(2, ItemType.Anime, favorite = false)46        assertEquals(listOf(1L, 3L), visibleChapters(newAnime, chapters, db, emptySet()).map { it.id })47        assertEquals(listOf(3L, 2L, 1L), visibleChapters(title(3, ItemType.Manga), chapters, db, emptySet()).map { it.id })48        // A title's own choice still wins over the default, per field.49        val ownSort = db.copy(sortChapterList = db.sortChapterList.orEmpty() + DbSortChapter(mangaId = 2, index = 1, reverse = false))50        assertEquals(listOf(3L, 1L), visibleChapters(newAnime, chapters, ownSort, emptySet()).map { it.id })51        val ownFilter = db.copy(chapterFilterUnreadList = db.chapterFilterUnreadList.orEmpty() + DbChapterFilter(mangaId = 2, type = 0))52        assertEquals(listOf(1L, 2L, 3L), visibleChapters(newAnime, chapters, ownFilter, emptySet()).map { it.id })53    }5455    @Test fun applyingToTheLibraryResetsOnlyLibraryTitlesOfThatMediaType() {56        val source = title(1, ItemType.Anime)57        val libraryAnime = title(2, ItemType.Anime)58        val libraryManga = title(3, ItemType.Manga)59        val browsedAnime = title(4, ItemType.Anime, favorite = false)60        val own = listOf(2L, 3L, 4L)61        val db = DbSettings(62            sortChapterList = own.map { DbSortChapter(mangaId = it, index = 3) },63            chapterFilterBookmarkedList = own.map { DbChapterFilter(mangaId = it, type = 2) },64        ).withChapterFilterCycled(source, ChapterFilterKind.Downloaded)65        val applied = db.withChapterListDefaultFrom(source, listOf(source, libraryAnime, libraryManga, browsedAnime))66        assertEquals(listOf(3L, 4L), applied.sortChapterList?.map { it.mangaId })67        assertEquals(listOf(3L, 4L), applied.chapterFilterBookmarkedList?.map { it.mangaId })68        assertEquals(ChapterFilterTypes(1, 0, 0), applied.chapterFilters(libraryAnime))69        assertEquals(DbSortChapter(mangaId = 3, index = 3), applied.chapterSort(libraryManga))70        // Outside the library: keeps its own bookmark filter, unset fields follow the new default.71        assertEquals(ChapterFilterTypes(1, 0, 2), applied.chapterFilters(browsedAnime))72    }7374    @Test fun settingsJsonWithoutDefaultsStillDecodesAndDefaultsRoundTrip() {75        val legacy = DatabaseJson.decodeFromString(DbSettings.serializer(), """{"features":{"library":{"defaultCategoryIds":{"Anime":3}}},"sortChapterList":[{"mangaId":1,"index":2}]}""")76        assertEquals(3L, legacy.features.library.defaultCategory(DbItemType.Anime))77        assertTrue(legacy.features.library.chapterListDefaults.isEmpty())78        assertEquals(DbSortChapter(mangaId = 1, index = 2), legacy.chapterSort(title(1, ItemType.Manga)))79        val withDefault = legacy.copy(features = legacy.features.copy(library = legacy.features.library80            .withChapterListDefault(DbItemType.Novel, DbChapterListDefaults(sortIndex = 3, bookmarked = 1))))81        val decoded = DatabaseJson.decodeFromString(DbSettings.serializer(), DatabaseJson.encodeToString(DbSettings.serializer(), withDefault))82        assertEquals(DbChapterListDefaults(sortIndex = 3, bookmarked = 1), decoded.features.library.chapterListDefault(DbItemType.Novel))83        // Choosing the built-in values again stores nothing.84        assertTrue(decoded.features.library.withChapterListDefault(DbItemType.Novel, DbChapterListDefaults()).chapterListDefaults.isEmpty())85    }8687    @Test fun fillerFilterUsesTheEffectiveMarkAndBecomesPartOfTheDefault() {88        val show = title(1, ItemType.Anime)89        // Episode 1: source filler the user unmarked; episode 3: user-marked filler.90        val episodes = listOf(chapters[0].copy(isFiller = false, fillerOverride = false), chapters[1], chapters[2].copy(isFiller = true, fillerOverride = true))91        val hide = DbSettings().withChapterFilterCycled(show, ChapterFilterKind.Filler).withChapterFilterCycled(show, ChapterFilterKind.Filler)92        assertEquals(listOf(2L, 1L), visibleChapters(show, episodes, hide, emptySet()).map { it.id })93        val only = DbSettings().withChapterFilterCycled(show, ChapterFilterKind.Filler)94        assertEquals(listOf(3L), visibleChapters(show, episodes, only, emptySet()).map { it.id })95        val other = title(2, ItemType.Anime)96        val applied = hide.withChapterListDefaultFrom(show, listOf(show, other))97        assertEquals(2, applied.features.library.chapterListDefault(DbItemType.Anime).filler)98        assertTrue(applied.features.library.chapterFillerFilters.isEmpty())99        assertEquals(listOf(2L, 1L), visibleChapters(other, episodes, applied, emptySet()).map { it.id })100    }101}102

Search chapters, reference and source filenames.