package eu.friedcutlet.yokan.data import eu.friedcutlet.yokan.database.DbChapter import eu.friedcutlet.yokan.database.DbDownload import eu.friedcutlet.yokan.database.DbItemType import eu.friedcutlet.yokan.database.DbManga import eu.friedcutlet.yokan.database.DbTrack import eu.friedcutlet.yokan.database.DbTrackStatus import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.database.seedDatabaseSnapshot import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertSame import kotlin.test.assertTrue import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) class LibraryIndexTest { private fun database() = InMemoryYokanDatabase( seedDatabaseSnapshot().copy( mangas = listOf( DbManga(id = 1, name = "One", itemType = DbItemType.Manga, favorite = true), DbManga(id = 2, name = "Two", itemType = DbItemType.Manga, favorite = true), ), chapters = listOf( DbChapter(id = 10, mangaId = 1, url = "/10", name = "Ch 1"), DbChapter(id = 20, mangaId = 2, url = "/20", name = "Ch 1"), DbChapter(id = 11, mangaId = 1, url = "/11", name = "Ch 2"), ), downloads = listOf( DbDownload(id = 100, chapterId = 10, isDownload = true), DbDownload(id = 101, chapterId = 10, isDownload = true), // Queued, not completed: neither counted nor "downloaded". DbDownload(id = 102, chapterId = 20, isDownload = false), ), tracks = listOf(DbTrack(id = 1, mangaId = 2, status = DbTrackStatus.Reading)), ), ) @Test fun progressCheckpointsDoNotPublishAnotherLibraryIndex() = runTest { val database = database() val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope, kotlinx.coroutines.test.UnconfinedTestDispatcher(testScheduler)) val before = store.index.first { it !== LibraryIndex.Empty } repeat(20) { value -> database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = false, succeeded = value)) } assertSame(before, store.index.value) database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = true)) assertEquals(1, store.index.value.completedDownloadCountsByChapterId[20]) } @Test @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) fun trackerStatusChangesPublishWithoutRegroupingChaptersButRefreshOnlyWritesDoNot() = runTest { val database = database() val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope, kotlinx.coroutines.test.UnconfinedTestDispatcher(testScheduler)) val before = store.index.first { it !== LibraryIndex.Empty } val track = database.tracks().single() database.upsertTrack(track.copy(lastChapterRead = 10, updatedAt = 100)) assertSame(before, store.index.value) database.upsertTrack(track.copy(status = DbTrackStatus.Completed, score = 8)) val after = store.index.value assertSame(before.chaptersByManga, after.chaptersByManga) assertEquals(DbTrackStatus.Completed, after.tracksByManga[2]?.single()?.status) assertEquals(8, after.tracksByManga[2]?.single()?.score) } @Test fun registeredProviderScalesAreCapturedForNewTrackerIds() = runTest { val database = database() database.upsertTrack(checkNotNull(database.tracks().singleOrNull()).copy(syncId = 99, score = 10)) val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope, trackerScoreRanges = mapOf(99 to (0..20))) val index = store.index.first { it !== LibraryIndex.Empty } val track = index.tracksByManga.getValue(2).single() assertEquals(20, track.scoreMaximum) assertEquals(5.0, eu.friedcutlet.yokan.services.tracking.normalizedTrackerScore(track.providerId, track.score, track.scoreMaximum)) } @Test fun indexGroupsChaptersPerTitleAndCountsCompletedDownloads() = runTest { val store = LibraryIndexStore(DefaultYokanRepository(database()), backgroundScope) val index = store.index.first { it !== LibraryIndex.Empty } assertEquals(listOf(10L, 11L), index.chapters(1).map { it.id }) assertEquals(listOf(20L), index.chapters(2).map { it.id }) assertTrue(index.chapters(3).isEmpty()) assertEquals(mapOf(10L to 2), index.completedDownloadCountsByChapterId) assertEquals(setOf(10L), index.downloadedChapterIds) assertEquals(setOf(2L), index.trackedMangaIds) } @Test fun completedDownloadRefreshesCountsWithoutRegroupingChapters() = runTest { val database = database() val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope) val before = store.index.first { it !== LibraryIndex.Empty } database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = true)) val after = store.index.first { it !== before } assertEquals(mapOf(10L to 2, 20L to 1), after.completedDownloadCountsByChapterId) assertEquals(setOf(10L, 20L), after.downloadedChapterIds) assertSame(before.chaptersByManga, after.chaptersByManga) } @Test fun chapterChangeRegroupsThatTitle() = runTest { val database = database() val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope) val before = store.index.first { it !== LibraryIndex.Empty } database.upsertChapter(DbChapter(id = 21, mangaId = 2, url = "/21", name = "Ch 2")) val after = store.index.first { it !== before } assertEquals(listOf(20L, 21L), after.chapters(2).map { it.id }) assertEquals(listOf(10L, 11L), after.chapters(1).map { it.id }) } }