Yokan / rebuild
Course 1 8624561

StatisticsTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/statistics/StatisticsTest.kt

test / fixture102 lines
1package eu.friedcutlet.yokan.ui.screens.settings.statistics23import androidx.compose.ui.geometry.Rect4import kotlinx.datetime.isoDayNumber5import kotlinx.datetime.toLocalDateTime6import eu.friedcutlet.yokan.domain.*7import kotlinx.datetime.TimeZone8import kotlin.math.abs9import kotlin.test.Test10import kotlin.test.assertEquals11import kotlin.test.assertTrue1213class StatisticsTest {14    @Test fun progressDistinguishesCaughtUpFromPublicationStatusAndPartialReading() {15        val titles = listOf(title(1), title(2), title(3), title(4).copy(status = Status.Completed), title(5).copy(favorite = false))16        val chapters = mapOf(17            1L to listOf(chapter(1, 1, true)),18            2L to listOf(chapter(2, 2, false).copy(lastPageRead = "4")),19            3L to emptyList(),20            4L to listOf(chapter(4, 4, false)),21            5L to listOf(chapter(5, 5, true)),22        )23        val result = summarizeStatistics(titles, chapters, emptyList(), setOf(1), setOf(1, 5))24        assertEquals(4, result.titles)25        assertEquals(3, result.chapters)26        assertEquals(1, result.read)27        assertEquals(1, result.caughtUp)28        assertEquals(1, result.inProgress)29        assertEquals(2, result.notStarted)30        assertEquals(1, result.downloadedTitles)31        assertEquals(1, result.trackedTitles)32    }3334    @Test fun historyTotalsDoNotOverflow() {35        val history = listOf(history(1, Int.MAX_VALUE, 1), history(2, Int.MAX_VALUE, 2), history(3, -1, 3))36        val result = summarizeStatistics(emptyList(), emptyMap(), history, emptySet(), emptySet())37        assertEquals(Int.MAX_VALUE.toLong() * 2, result.seconds)38        assertEquals(3, result.activeTitles)39    }4041    @Test fun activityBucketsByLocalDayAndHourInsideAMondayAlignedWindow() {42        val now = 1_790_000_000_000L // 2026-09-21T14:13Z43        val day = 86_400_000L44        val history = listOf(history(1, 60, now), history(2, 60, now - 1), history(3, 60, now - 3 * day),45            history(4, 60, now - 400 * day), history(5, 60, 0))46        val result = summarizeStatistics(emptyList(), emptyMap(), history, emptySet(), emptySet(), now, TimeZone.UTC)47        val days = result.dailyActivity48        assertTrue(days.size in (ACTIVITY_WEEKS - 1) * 7 + 1..ACTIVITY_WEEKS * 7)49        val today = kotlin.time.Instant.fromEpochMilliseconds(now).toLocalDateTime(TimeZone.UTC).dayOfWeek.isoDayNumber50        assertEquals(today, (days.size - 1) % 7 + 1) // window starts on a Monday and ends today51        assertEquals(2, days.last())52        assertEquals(1, days[days.size - 4])53        assertEquals(3, days.sum()) // out-of-window and unknown timestamps are dropped54        assertEquals(4, result.hourly[14]) // hour-of-day spans all history, not just the heatmap window55        assertEquals(4, result.hourly.sum())56    }5758    @Test fun growthIsCumulativeAndGenresMergeCaseAndRankBySize() {59        val month = 31L * 86_400_000L60        val now = 1_790_000_000_000L61        val titles = listOf(62            title(1).copy(dateAddedMillis = now - 3 * month, genres = listOf("Action", "Drama")),63            title(2).copy(dateAddedMillis = now - month, genres = listOf("action ", "Comedy")),64            title(3).copy(dateAddedMillis = 0, genres = listOf("Action", "ACTION")),65            title(4).copy(dateAddedMillis = now, favorite = false, genres = listOf("Comedy")),66        )67        val result = summarizeStatistics(titles, emptyMap(), emptyList(), emptySet(), emptySet(), now, TimeZone.UTC)68        assertEquals(result.growth.sorted(), result.growth)69        assertEquals(2, result.growth.first()) // undated titles count from the start70        assertEquals(3, result.growth.last())71        assertEquals("Action" to 3, result.genres.first())72        assertEquals(listOf("Action", "Comedy", "Drama"), result.genres.map { it.first })73    }7475    @Test fun emptyLibraryHasNoInventedProgressOrActivity() {76        val result = summarizeStatistics(emptyList(), emptyMap(), emptyList(), emptySet(), emptySet())77        assertEquals(0, result.caughtUp)78        assertEquals(0, result.notStarted)79        assertEquals(0L, result.seconds)80        assertEquals(0, result.dailyActivity.sum())81        assertEquals(listOf(0), result.growth)82        assertEquals(emptyList(), result.genres)83    }8485    @Test fun treemapTilesBoundsWithAreasProportionalToValues() {86        val values = listOf(40, 25, 15, 10, 6, 4)87        val bounds = Rect(0f, 0f, 300f, 200f)88        val rects = layoutTreemap(values, bounds)89        assertEquals(values.size, rects.size)90        val total = values.sum().toFloat()91        rects.forEachIndexed { i, r ->92            assertTrue(r.left >= 0f && r.top >= 0f && r.right <= 300.01f && r.bottom <= 200.01f)93            assertTrue(abs(r.width * r.height - bounds.width * bounds.height * values[i] / total) < 1f, "cell $i area")94        }95        assertTrue(abs(rects.sumOf { (it.width * it.height).toDouble() } - 60_000.0) < 1.0)96    }9798    private fun title(id: Long) = Manga(id, "Title $id", link = "", description = "", status = Status.Ongoing,99        itemType = ItemType.Manga, favorite = true, source = "", lang = "en", dateAddedMillis = 0, lastUpdateMillis = 0)100    private fun chapter(id: Long, title: Long, read: Boolean) = Chapter(id, title, "Chapter", "", isRead = read)101    private fun history(id: Long, seconds: Int, updated: Long) = History(id, id, id, ItemType.Manga, "Title $id", "Chapter", "Now", seconds, updated)102}103

Search chapters, reference and source filenames.