package eu.friedcutlet.yokan.ui.screens.activity import eu.friedcutlet.yokan.data.LibraryIndex import eu.friedcutlet.yokan.domain.testManga import eu.friedcutlet.yokan.database.DbDownload import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.domain.History import eu.friedcutlet.yokan.domain.ItemType import eu.friedcutlet.yokan.domain.UpdateEntry import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNull class ActivityRowsTest { private fun update(id: Long, mangaId: Long, chapterId: Long, updatedAt: Long, itemType: ItemType = ItemType.Manga) = UpdateEntry( id = id, mangaId = mangaId, chapterId = chapterId, itemType = itemType, title = "stored $mangaId", chapterName = "stored ch $chapterId", source = "src", date = "", isRead = false, updatedAt = updatedAt, ) private fun history(id: Long, mangaId: Long, chapterId: Long, updatedAt: Long, itemType: ItemType = ItemType.Manga) = History( id = id, mangaId = mangaId, chapterId = chapterId, itemType = itemType, title = "stored $mangaId", chapterName = "stored ch $chapterId", date = "", readingTimeSeconds = 0, updatedAt = updatedAt, ) @Test fun updateRowsResolveTitleChapterAndFirstDownloadPerTypeNewestFirst() { val one = testManga(1, "One") val index = LibraryIndex( chaptersByManga = mapOf(1L to listOf(Chapter(10, 1, "Ch 10", "/10", isRead = true), Chapter(11, 1, "", "/11"))), ) val downloads = listOf( DbDownload(id = 7, chapterId = 10, succeeded = 1), DbDownload(id = 8, chapterId = 10, succeeded = 2), ) val rows = buildUpdateRows( updates = listOf( update(100, 1, 10, updatedAt = 1), update(101, 1, 11, updatedAt = 3), update(102, 2, 20, updatedAt = 2, itemType = ItemType.Anime), ), titles = mapOf(1L to one), index = index, downloads = downloads, ) val manga = rows.getValue(ItemType.Manga) assertEquals(listOf(101L, 100L), manga.map { it.update.id }) val read = manga.last() assertEquals(one, read.manga) assertEquals("One", read.title) assertEquals("Ch 10", read.chapterName) assertEquals(true, read.isRead) assertEquals(7L, read.download?.id) // A blank chapter name falls back to the stored one. assertEquals("stored ch 11", manga.first().chapterName) val orphan = rows.getValue(ItemType.Anime).single() assertNull(orphan.manga) assertNull(orphan.chapter) assertEquals("stored 2", orphan.title) } @Test fun historyRowsKeepNewestEntryPerTitle() { val index = LibraryIndex(chaptersByManga = mapOf(1L to listOf(Chapter(10, 1, "Ch 10", "/10")))) val rows = buildHistoryRows( history = listOf( history(1, mangaId = 1, chapterId = 10, updatedAt = 5), history(2, mangaId = 1, chapterId = 11, updatedAt = 1), history(3, mangaId = 2, chapterId = 20, updatedAt = 9), history(4, mangaId = 3, chapterId = 30, updatedAt = 7, itemType = ItemType.Novel), ), titles = mapOf(1L to testManga(1, "One")), index = index, ) val manga = rows.getValue(ItemType.Manga) assertEquals(listOf(3L, 1L), manga.map { it.history.id }) assertEquals("Ch 10", manga.last().chapter?.name) assertEquals(listOf(4L), rows.getValue(ItemType.Novel).map { it.history.id }) } }