package eu.friedcutlet.yokan.ui.screens.library import eu.friedcutlet.yokan.data.LibraryIndex import eu.friedcutlet.yokan.database.DbSortLibraryManga import eu.friedcutlet.yokan.database.DbChapterReleasePreference import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.domain.Category import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.domain.Status import kotlin.test.Test import kotlin.test.assertEquals class LibraryContentTest { @Test fun trackerMetadataRefreshKeepsReleaseSelectionAndExposesNewStatus() { val selector = LibraryReleaseSelector() val selected = selector.select(library, index, query()) val changed = LibraryIndex(index.chaptersByManga, index.completedDownloadCountsByChapterId, setOf(1), mapOf(1L to listOf(eu.friedcutlet.yokan.data.LibraryTrack(1, eu.friedcutlet.yokan.database.DbTrackStatus.Completed, 7)))) val after = selector.select(library, changed, query()) kotlin.test.assertSame(selected.chaptersByManga, after.chaptersByManga) assertEquals(changed.tracksByManga, after.tracksByManga) } @Test fun searchAndSortReuseReleaseSelectionButOfflinePolicyInvalidatesIt() { val selector = LibraryReleaseSelector() val selected = selector.select(library, index, query()) kotlin.test.assertSame(selected, selector.select(library, index, query(search = "alpha"))) kotlin.test.assertSame(selected, selector.select(library, index, query(sort = DbSortLibraryManga(index = 3)))) val offline = selector.select(library, index, query(downloadedOnly = true)) kotlin.test.assertNotSame(selected, offline) assertEquals(listOf(beta), libraryContent(library, categories, index, query(downloadedOnly = true), offline).items(null)) } @Test fun advancedSearchCountsUseSelectedReleasesAndIgnoreFiltersReusesTheSameMatches() { val rows = listOf(chapter(11, 1, true).copy(name = "Chapter 1", scanlator = "Old"), chapter(12, 1, false).copy(name = "Chapter 1", scanlator = "Preferred")) val raw = LibraryIndex(chaptersByManga = mapOf(1L to rows, 3L to index.chapters(3))) val advanced = query(search = "unread=0 AND title:alpha", downloadedOnly = true).copy( releasePreferences = listOf(DbChapterReleasePreference(scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true)), ) val content = libraryContent(listOf(alpha, gamma), categories, raw, advanced) assertEquals(emptyList(), content.items(null)) assertEquals(listOf(alpha), content.searchMatches) assertEquals(listOf(alpha), libraryContent(listOf(alpha, gamma), categories, raw, advanced.copy(downloadedOnly = false)).items(null)) } @Test fun unreadFiltersSortAndBadgesUseSelectedReleasesAndAggregatedProgress() { val rows = listOf(chapter(11, 1, true).copy(name = "Chapter 1", scanlator = "Old"), chapter(12, 1, false).copy(name = "Chapter 1", scanlator = "Preferred")) val releaseIndex = LibraryIndex(chaptersByManga = mapOf(1L to rows, 3L to index.chapters(3))) val db = DbSettings(chapterReleasePreferences = listOf(DbChapterReleasePreference( scanlatorPriority = listOf("Preferred"), skipDuplicateReleases = true))) val selectedQuery = query(filters = noFilters.copy(unread = 1)).copy(releasePreferences = db.chapterReleasePreferences) assertEquals(listOf(gamma), libraryContent(listOf(alpha, gamma), categories, releaseIndex, selectedQuery).items(null)) val badges = buildLibraryBadgeSummaries(releaseIndex.chaptersByManga, emptyMap(), mapOf(1L to alpha.name, 3L to gamma.name), db) assertEquals(LibraryBadgeSummary(unreadCount = 0, totalCount = 1, readCount = 1), badges[1]) assertEquals(3, badges[3]?.unreadCount) assertEquals(listOf(alpha, gamma), libraryContent(listOf(gamma, alpha), categories, releaseIndex, selectedQuery.copy(filters = noFilters, sort = DbSortLibraryManga(index = 3))).items(null)) } private val alpha = manga(1, "Alpha", categories = listOf(10)) private val beta = manga(2, "Beta") private val gamma = manga(3, "Gamma", categories = listOf(10, 20)) private val delta = manga(4, "Delta", categories = listOf(20)).copy(alternativeTitles = listOf("Delta", "Zeta")) private val library = listOf(alpha, beta, gamma, delta) private val categories = listOf( Category(id = 10, name = "Reading", forItemType = ItemType.Manga, position = 0), Category(id = 20, name = "Hidden", forItemType = ItemType.Manga, position = 1, hidden = true), ) private val index = LibraryIndex( chaptersByManga = mapOf( 1L to listOf(chapter(11, 1, isRead = false), chapter(12, 1, isRead = true)), 2L to listOf(chapter(21, 2, isRead = true)), 3L to listOf(chapter(31, 3, isRead = false), chapter(32, 3, isRead = false), chapter(33, 3, isRead = false)), ), completedDownloadCountsByChapterId = mapOf(21L to 1), ) @Test fun filterAndSortRunBeforeTheCategorySplitAndTabsFollowTheUnfilteredLibrary() { val content = libraryContent( library = library, categories = categories, index = index, query = query(filters = noFilters.copy(unread = 1), sort = DbSortLibraryManga(reverse = true, index = 3)), ) assertEquals(listOf(gamma, alpha), content.items(null)) assertEquals(listOf(gamma, alpha), content.items(10)) assertEquals(listOf(gamma), content.items(20)) assertEquals(emptyList(), content.items(UncategorizedCategoryId)) assertEquals( listOf(LibraryCategoryTab(UncategorizedCategoryId, "", 0), LibraryCategoryTab(10, "Reading", 2)), content.tabs, ) } @Test fun downloadedOnlyHidesTitlesWithoutDownloadsButIgnoringFiltersSearchStillFindsThem() { val content = libraryContent( library = library, categories = categories, index = index, query = query(search = "zeta", downloadedOnly = true), ) assertEquals(emptyList(), content.items(null)) assertEquals(listOf(delta), content.searchMatches) assertEquals(listOf(beta), libraryContent(library, categories, index, query(downloadedOnly = true)).items(null)) } private val noFilters = LibraryFilterTypes(downloaded = 0, unread = 0, started = 0, bookmarked = 0, completed = 0, tracking = 0) private fun query( search: String = "", filters: LibraryFilterTypes = noFilters, sort: DbSortLibraryManga = DbSortLibraryManga(), downloadedOnly: Boolean = false, ) = LibraryQuery(search = search, filters = filters, sort = sort, downloadedOnly = downloadedOnly) private fun manga(id: Long, name: String, categories: List = emptyList()) = Manga( id = id, name = name, link = "/manga/$id", description = "", status = Status.Ongoing, itemType = ItemType.Manga, favorite = true, source = "Source", lang = "en", dateAddedMillis = id, lastUpdateMillis = id, categories = categories, ) private fun chapter(id: Long, mangaId: Long, isRead: Boolean) = Chapter( id = id, mangaId = mangaId, name = "Chapter $id", url = "/chapter/$id", isRead = isRead, ) }