Yokan / rebuild
Course 1 8624561

DownloadManagerRowsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/downloads/DownloadManagerRowsTest.kt

test / fixture95 lines
1package eu.friedcutlet.yokan.ui.screens.downloads23import eu.friedcutlet.yokan.database.DbDownload4import eu.friedcutlet.yokan.database.DbItemType5import eu.friedcutlet.yokan.services.download.DownloadProgress6import kotlin.test.*78class DownloadManagerRowsTest {9    private val record = DbDownload(id = 1, chapterId = 10, succeeded = 2, failed = 0, total = 10, isStartDownload = true)10    private val live = DownloadProgress("chapter-10", 4, 0, 10, DbItemType.Manga)11    private fun row(saved: DbDownload = record, progress: DownloadProgress? = null, location: String? = null) =12        downloadManagerRows(listOf(saved), listOfNotNull(progress)) { location }.single()1314    @Test fun cachedDetailsFollowRenamesAndOtherRowsKeepTheirIdentity() {15        val snapshot = eu.friedcutlet.yokan.database.seedDatabaseSnapshot().copy(16            mangas = listOf(eu.friedcutlet.yokan.database.DbManga(id = 1, name = "Old title")),17            chapters = listOf(eu.friedcutlet.yokan.database.DbChapter(id = 10, mangaId = 1, name = "Old chapter")),18            downloads = listOf(record, DbDownload(id = 2, chapterId = 20, isDownload = true)),19        )20        val builder = DownloadQueueEntryBuilder()21        val before = builder.build(snapshot, listOf(live)) { null }22        val progressed = builder.build(snapshot, listOf(live.copy(succeeded = 8))) { null }23        assertEquals(8, progressed.first().row.succeeded)24        assertSame(before.last(), progressed.last())25        val renamed = builder.build(snapshot.copy(26            mangas = listOf(snapshot.mangas.single().copy(name = "New title")),27            chapters = listOf(snapshot.chapters.single().copy(name = "New chapter")),28        ), listOf(live)) { null }29        assertEquals("New title", renamed.first().mangaName)30        assertEquals("New chapter", renamed.first().chapterName)31    }3233    @Test fun liveWinsAndTaskIdsAreDeduplicated() {34        val rows = downloadManagerRows(listOf(record, record.copy(id = 2)), listOf(live, live.copy(succeeded = 6))) { null }35        assertEquals(1, rows.size)36        assertEquals(6, rows.single().succeeded)37        assertEquals(.6f, rows.single().fraction)38        assertEquals(DownloadSection.InProgress, rows.single().section)39        assertFalse(rows.single().queued)40    }4142    @Test fun failuresAreAttentionOnlyWhenTerminal() {43        assertEquals(DownloadSection.InProgress, row(progress = live.copy(failed = 1)).section)44        val terminal = row(progress = live.copy(succeeded = 9, failed = 1, isCompleted = true), location = "/downloads/chapter")45        assertEquals(DownloadSection.NeedsAttention, terminal.section)46        assertTrue(terminal.canRetry)47        assertFalse(terminal.queued)48        assertEquals(DownloadSection.NeedsAttention, row(record.copy(isStartDownload = false, failed = 1)).section)49    }5051    @Test fun onlySuccessfulRecordsAreCompleted() {52        assertEquals(DownloadSection.InProgress, row(progress = live.copy(succeeded = 10, isCompleted = true)).section)53        assertEquals(DownloadSection.Completed, row(record.copy(isDownload = true, succeeded = 10)).section)54        assertEquals(DownloadSection.NeedsAttention, row(record.copy(isDownload = true, failed = 1)).section)55        assertEquals(DownloadSection.InProgress, row(record.copy(isDownload = true), live).section)56    }5758    @Test fun restartRowsRetryFromTheirChapterButCannotReorder() {59        for (saved in listOf(record, record.copy(total = 0), record.copy(isStartDownload = false), record.copy(failed = 2))) {60            val restarted = row(saved)61            assertEquals(DownloadSection.NeedsAttention, restarted.section)62            assertTrue(restarted.canRetry)63            assertFalse(restarted.queued)64        }65        assertFalse(row(DbDownload(id = 3, failed = 1)).canRetry)66        assertTrue(row(record.copy(succeeded = 0), location = "/session/request").queued)67        assertFalse(row(location = "/session/request").queued)68        assertFalse(row(progress = live.copy(isPaused = true), location = "/session/request").queued)69        assertFalse(row(record.copy(isDownload = true), location = "/session/request").queued)70    }7172    @Test fun unknownTotalNeverProducesAPercentage() {73        assertNull(row(record.copy(total = null)).fraction)74        assertNull(row(progress = live.copy(total = 0)).fraction)75        assertNull(row(progress = live.copy(total = -1)).fraction)76        assertEquals(1f, row(progress = live.copy(succeeded = 20)).fraction)77    }7879    @Test fun orphanAndLiveOnlyRowsArePreserved() {80        val rows = downloadManagerRows(listOf(DbDownload(id = 3), record.copy(chapterId = 999)), listOf(live)) { null }81        assertEquals(setOf("download-3", "chapter-999", "chapter-10"), rows.map { it.taskId }.toSet())82        assertNull(rows.last().record)83        assertEquals(DownloadSection.NeedsAttention, rows.first().section)84    }8586    @Test fun sessionFailuresAndCancelledRequestsCanRetryButNotReorder() {87        for (saved in listOf(record.copy(isStartDownload = false), record.copy(isStartDownload = false, failed = 3))) {88            val stopped = row(saved, location = "/session/request")89            assertEquals(DownloadSection.NeedsAttention, stopped.section)90            assertTrue(stopped.canRetry)91            assertFalse(stopped.queued)92        }93        assertFalse(row(record.copy(isDownload = true), location = "/session/request").canRetry)94    }95}96

Search chapters, reference and source filenames.