LibraryIndexTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/data/LibraryIndexTest.kt
1package eu.friedcutlet.yokan.data23import eu.friedcutlet.yokan.database.DbChapter4import eu.friedcutlet.yokan.database.DbDownload5import eu.friedcutlet.yokan.database.DbItemType6import eu.friedcutlet.yokan.database.DbManga7import eu.friedcutlet.yokan.database.DbTrack8import eu.friedcutlet.yokan.database.DbTrackStatus9import eu.friedcutlet.yokan.database.InMemoryYokanDatabase10import eu.friedcutlet.yokan.database.seedDatabaseSnapshot11import kotlin.test.Test12import kotlin.test.assertEquals13import kotlin.test.assertSame14import kotlin.test.assertTrue15import kotlinx.coroutines.flow.first16import kotlinx.coroutines.test.runTest1718@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)19class LibraryIndexTest {20 private fun database() = InMemoryYokanDatabase(21 seedDatabaseSnapshot().copy(22 mangas = listOf(23 DbManga(id = 1, name = "One", itemType = DbItemType.Manga, favorite = true),24 DbManga(id = 2, name = "Two", itemType = DbItemType.Manga, favorite = true),25 ),26 chapters = listOf(27 DbChapter(id = 10, mangaId = 1, url = "/10", name = "Ch 1"),28 DbChapter(id = 20, mangaId = 2, url = "/20", name = "Ch 1"),29 DbChapter(id = 11, mangaId = 1, url = "/11", name = "Ch 2"),30 ),31 downloads = listOf(32 DbDownload(id = 100, chapterId = 10, isDownload = true),33 DbDownload(id = 101, chapterId = 10, isDownload = true),34 // Queued, not completed: neither counted nor "downloaded".35 DbDownload(id = 102, chapterId = 20, isDownload = false),36 ),37 tracks = listOf(DbTrack(id = 1, mangaId = 2, status = DbTrackStatus.Reading)),38 ),39 )4041 @Test42 fun progressCheckpointsDoNotPublishAnotherLibraryIndex() = runTest {43 val database = database()44 val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope,45 kotlinx.coroutines.test.UnconfinedTestDispatcher(testScheduler))46 val before = store.index.first { it !== LibraryIndex.Empty }47 repeat(20) { value ->48 database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = false, succeeded = value))49 }50 assertSame(before, store.index.value)51 database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = true))52 assertEquals(1, store.index.value.completedDownloadCountsByChapterId[20])53 }5455 @Test56 @OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)57 fun trackerStatusChangesPublishWithoutRegroupingChaptersButRefreshOnlyWritesDoNot() = runTest {58 val database = database()59 val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope,60 kotlinx.coroutines.test.UnconfinedTestDispatcher(testScheduler))61 val before = store.index.first { it !== LibraryIndex.Empty }62 val track = database.tracks().single()63 database.upsertTrack(track.copy(lastChapterRead = 10, updatedAt = 100))64 assertSame(before, store.index.value)65 database.upsertTrack(track.copy(status = DbTrackStatus.Completed, score = 8))66 val after = store.index.value67 assertSame(before.chaptersByManga, after.chaptersByManga)68 assertEquals(DbTrackStatus.Completed, after.tracksByManga[2]?.single()?.status)69 assertEquals(8, after.tracksByManga[2]?.single()?.score)70 }7172 @Test73 fun registeredProviderScalesAreCapturedForNewTrackerIds() = runTest {74 val database = database()75 database.upsertTrack(checkNotNull(database.tracks().singleOrNull()).copy(syncId = 99, score = 10))76 val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope, trackerScoreRanges = mapOf(99 to (0..20)))77 val index = store.index.first { it !== LibraryIndex.Empty }78 val track = index.tracksByManga.getValue(2).single()79 assertEquals(20, track.scoreMaximum)80 assertEquals(5.0, eu.friedcutlet.yokan.services.tracking.normalizedTrackerScore(track.providerId, track.score, track.scoreMaximum))81 }8283 @Test84 fun indexGroupsChaptersPerTitleAndCountsCompletedDownloads() = runTest {85 val store = LibraryIndexStore(DefaultYokanRepository(database()), backgroundScope)8687 val index = store.index.first { it !== LibraryIndex.Empty }8889 assertEquals(listOf(10L, 11L), index.chapters(1).map { it.id })90 assertEquals(listOf(20L), index.chapters(2).map { it.id })91 assertTrue(index.chapters(3).isEmpty())92 assertEquals(mapOf(10L to 2), index.completedDownloadCountsByChapterId)93 assertEquals(setOf(10L), index.downloadedChapterIds)94 assertEquals(setOf(2L), index.trackedMangaIds)95 }9697 @Test98 fun completedDownloadRefreshesCountsWithoutRegroupingChapters() = runTest {99 val database = database()100 val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope)101 val before = store.index.first { it !== LibraryIndex.Empty }102103 database.upsertDownload(DbDownload(id = 102, chapterId = 20, isDownload = true))104 val after = store.index.first { it !== before }105106 assertEquals(mapOf(10L to 2, 20L to 1), after.completedDownloadCountsByChapterId)107 assertEquals(setOf(10L, 20L), after.downloadedChapterIds)108 assertSame(before.chaptersByManga, after.chaptersByManga)109 }110111 @Test112 fun chapterChangeRegroupsThatTitle() = runTest {113 val database = database()114 val store = LibraryIndexStore(DefaultYokanRepository(database), backgroundScope)115 val before = store.index.first { it !== LibraryIndex.Empty }116117 database.upsertChapter(DbChapter(id = 21, mangaId = 2, url = "/21", name = "Ch 2"))118 val after = store.index.first { it !== before }119120 assertEquals(listOf(20L, 21L), after.chapters(2).map { it.id })121 assertEquals(listOf(10L, 11L), after.chapters(1).map { it.id })122 }123}124