LibraryContentTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/library/LibraryContentTest.kt
1package eu.friedcutlet.yokan.ui.screens.library23import eu.friedcutlet.yokan.data.LibraryIndex4import eu.friedcutlet.yokan.database.DbSortLibraryManga5import eu.friedcutlet.yokan.database.DbChapterReleasePreference6import eu.friedcutlet.yokan.database.DbSettings7import eu.friedcutlet.yokan.domain.Category8import eu.friedcutlet.yokan.domain.Chapter9import eu.friedcutlet.yokan.domain.ItemType10import eu.friedcutlet.yokan.domain.Manga11import eu.friedcutlet.yokan.domain.Status12import kotlin.test.Test13import kotlin.test.assertEquals1415class LibraryContentTest {16 @Test17 fun trackerMetadataRefreshKeepsReleaseSelectionAndExposesNewStatus() {18 val selector = LibraryReleaseSelector()19 val selected = selector.select(library, index, query())20 val changed = LibraryIndex(index.chaptersByManga, index.completedDownloadCountsByChapterId, setOf(1),21 mapOf(1L to listOf(eu.friedcutlet.yokan.data.LibraryTrack(1, eu.friedcutlet.yokan.database.DbTrackStatus.Completed, 7))))22 val after = selector.select(library, changed, query())23 kotlin.test.assertSame(selected.chaptersByManga, after.chaptersByManga)24 assertEquals(changed.tracksByManga, after.tracksByManga)25 }2627 @Test28 fun searchAndSortReuseReleaseSelectionButOfflinePolicyInvalidatesIt() {29 val selector = LibraryReleaseSelector()30 val selected = selector.select(library, index, query())31 kotlin.test.assertSame(selected, selector.select(library, index, query(search = "alpha")))32 kotlin.test.assertSame(selected, selector.select(library, index, query(sort = DbSortLibraryManga(index = 3))))33 val offline = selector.select(library, index, query(downloadedOnly = true))34 kotlin.test.assertNotSame(selected, offline)35 assertEquals(listOf(beta), libraryContent(library, categories, index, query(downloadedOnly = true), offline).items(null))36 }3738 @Test39 fun advancedSearchCountsUseSelectedReleasesAndIgnoreFiltersReusesTheSameMatches() {40 val rows = listOf(chapter(11, 1, true).copy(name = "Chapter 1", scanlator = "Old"),41 chapter(12, 1, false).copy(name = "Chapter 1", scanlator = "Preferred"))42 val raw = LibraryIndex(chaptersByManga = mapOf(1L to rows, 3L to index.chapters(3)))43 val advanced = query(search = "unread=0 AND title:alpha", downloadedOnly = true).copy(44 releasePreferences = listOf(DbChapterReleasePreference(scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true)),45 )46 val content = libraryContent(listOf(alpha, gamma), categories, raw, advanced)47 assertEquals(emptyList(), content.items(null))48 assertEquals(listOf(alpha), content.searchMatches)49 assertEquals(listOf(alpha), libraryContent(listOf(alpha, gamma), categories, raw, advanced.copy(downloadedOnly = false)).items(null))50 }5152 @Test53 fun unreadFiltersSortAndBadgesUseSelectedReleasesAndAggregatedProgress() {54 val rows = listOf(chapter(11, 1, true).copy(name = "Chapter 1", scanlator = "Old"),55 chapter(12, 1, false).copy(name = "Chapter 1", scanlator = "Preferred"))56 val releaseIndex = LibraryIndex(chaptersByManga = mapOf(1L to rows, 3L to index.chapters(3)))57 val db = DbSettings(chapterReleasePreferences = listOf(DbChapterReleasePreference(58 scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true)))59 val selectedQuery = query(filters = noFilters.copy(unread = 1)).copy(releasePreferences = db.chapterReleasePreferences)60 assertEquals(listOf(gamma), libraryContent(listOf(alpha, gamma), categories, releaseIndex, selectedQuery).items(null))61 val badges = buildLibraryBadgeSummaries(releaseIndex.chaptersByManga, emptyMap(), mapOf(1L to alpha.name, 3L to gamma.name), db)62 assertEquals(LibraryBadgeSummary(unreadCount = 0, totalCount = 1, readCount = 1), badges[1])63 assertEquals(3, badges[3]?.unreadCount)64 assertEquals(listOf(alpha, gamma), libraryContent(listOf(gamma, alpha), categories, releaseIndex,65 selectedQuery.copy(filters = noFilters, sort = DbSortLibraryManga(index = 3))).items(null))66 }6768 private val alpha = manga(1, "Alpha", categories = listOf(10))69 private val beta = manga(2, "Beta")70 private val gamma = manga(3, "Gamma", categories = listOf(10, 20))71 private val delta = manga(4, "Delta", categories = listOf(20)).copy(alternativeTitles = listOf("Delta", "Zeta"))72 private val library = listOf(alpha, beta, gamma, delta)73 private val categories = listOf(74 Category(id = 10, name = "Reading", forItemType = ItemType.Manga, position = 0),75 Category(id = 20, name = "Hidden", forItemType = ItemType.Manga, position = 1, hidden = true),76 )77 private val index = LibraryIndex(78 chaptersByManga = mapOf(79 1L to listOf(chapter(11, 1, isRead = false), chapter(12, 1, isRead = true)),80 2L to listOf(chapter(21, 2, isRead = true)),81 3L to listOf(chapter(31, 3, isRead = false), chapter(32, 3, isRead = false), chapter(33, 3, isRead = false)),82 ),83 completedDownloadCountsByChapterId = mapOf(21L to 1),84 )8586 @Test87 fun filterAndSortRunBeforeTheCategorySplitAndTabsFollowTheUnfilteredLibrary() {88 val content = libraryContent(89 library = library,90 categories = categories,91 index = index,92 query = query(filters = noFilters.copy(unread = 1), sort = DbSortLibraryManga(reverse = true, index = 3)),93 )9495 assertEquals(listOf(gamma, alpha), content.items(null))96 assertEquals(listOf(gamma, alpha), content.items(10))97 assertEquals(listOf(gamma), content.items(20))98 assertEquals(emptyList(), content.items(UncategorizedCategoryId))99 assertEquals(100 listOf(LibraryCategoryTab(UncategorizedCategoryId, "", 0), LibraryCategoryTab(10, "Reading", 2)),101 content.tabs,102 )103 }104105 @Test106 fun downloadedOnlyHidesTitlesWithoutDownloadsButIgnoringFiltersSearchStillFindsThem() {107 val content = libraryContent(108 library = library,109 categories = categories,110 index = index,111 query = query(search = "zeta", downloadedOnly = true),112 )113114 assertEquals(emptyList(), content.items(null))115 assertEquals(listOf(delta), content.searchMatches)116 assertEquals(listOf(beta), libraryContent(library, categories, index, query(downloadedOnly = true)).items(null))117 }118119 private val noFilters = LibraryFilterTypes(downloaded = 0, unread = 0, started = 0, bookmarked = 0, completed = 0, tracking = 0)120121 private fun query(122 search: String = "",123 filters: LibraryFilterTypes = noFilters,124 sort: DbSortLibraryManga = DbSortLibraryManga(),125 downloadedOnly: Boolean = false,126 ) = LibraryQuery(search = search, filters = filters, sort = sort, downloadedOnly = downloadedOnly)127128 private fun manga(id: Long, name: String, categories: List<Long> = emptyList()) = Manga(129 id = id,130 name = name,131 link = "/manga/$id",132 description = "",133 status = Status.Ongoing,134 itemType = ItemType.Manga,135 favorite = true,136 source = "Source",137 lang = "en",138 dateAddedMillis = id,139 lastUpdateMillis = id,140 categories = categories,141 )142143 private fun chapter(id: Long, mangaId: Long, isRead: Boolean) = Chapter(144 id = id,145 mangaId = mangaId,146 name = "Chapter $id",147 url = "/chapter/$id",148 isRead = isRead,149 )150}151