Modern Apps git

← Modern-Apps.git

games-hub: add tests for calculators, dashboard, games list, profile and components

Commit
13856fade50110150d83cce0cf5f254ee8289e22
Author
Vayun Mathur <[email protected]>
When
2026-11-07 03:37 UTC (29 days ago)
Parents
e43641c
JVM tests for XpLevelCalculator/StreakCalculator/FormatUtils/tierColor and Compose tests for DashboardScreen, GamesListScreen, GameCard/StatCard/XpProgressBar/LevelBadge, ProfileScreen, AchievementRow, ActivityItemCard, StreakCard. Suite passes; merged coverage measured in finish report. Remaining: Yuu editor/avatar screens, ControllerPad, provider IPC handlers/converters, GameHubViewModel/repository (Room), PlayOnTv session - need Room/IPC/device follow-up.

(cherry picked from commit 110d861dd1ad77e284f143e83a3cfd8ca303852d)

Diff

games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubProfileComponentsTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubProfileComponentsTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubProfileComponentsTest.ktnew file mode 100644index 0000000000..b5c64bf682--- /dev/null+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubProfileComponentsTest.kt@@ -0,0 +1,93 @@+package com.vayunmathur.games.hub.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import com.vayunmathur.games.hub.data.dao.AchievementWithProgress+import com.vayunmathur.games.hub.data.entities.ActivityEventEntity+import com.vayunmathur.games.hub.ui.components.AchievementRow+import com.vayunmathur.games.hub.ui.components.ActivityItemCard+import com.vayunmathur.games.hub.ui.components.StreakCard+import com.vayunmathur.games.hub.ui.screens.ProfileScreen+import com.vayunmathur.games.hub.util.ProfileActions+import com.vayunmathur.games.hub.util.ProfileUiState+import com.vayunmathur.games.hub.viewmodel.CrossGameStats+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Profile screen, achievement rows, activity items, streak indicator. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class HubProfileComponentsTest {++    @get:Rule+    val composeRule = createComposeRule()++    @Test+    fun profileShowsNameTitleLevelAndStats() {+        composeRule.setContent {+            ProfileScreen(+                ProfileUiState(+                    playerName = "Grace", level = 5, title = "Enthusiast", totalXp = 1700,+                    stats = CrossGameStats(+                        totalPlaytimeMs = 7_200_000, totalSessions = 42, totalGames = 6,+                        totalAchievementsUnlocked = 11, totalAchievements = 30, longestStreak = 14,+                    ),+                ),+                ProfileActions.Noop,+            )+        }+        composeRule.onNodeWithText("Grace").assertIsDisplayed()+        composeRule.onNodeWithText("Enthusiast").assertIsDisplayed()+        composeRule.onNodeWithText("Grace", substring = true).assertIsDisplayed()+    }++    @Test+    fun achievementRowUnlockedAndLocked() {+        val unlocked = AchievementWithProgress(+            gameId = "chess", achievementId = "a1", name = "First Win", description = "Win a game",+            xpReward = 50, targetProgress = 1, isSecret = false, tier = "GOLD", iconResName = null,+            progress = 1, isUnlocked = true, unlockedAt = 123L,+        )+        composeRule.setContent { AchievementRow(unlocked) }+        composeRule.onNodeWithText("First Win").assertIsDisplayed()+        composeRule.onNodeWithText("Win a game").assertIsDisplayed()+    }++    @Test+    fun achievementRowLockedProgress() {+        val locked = AchievementWithProgress(+            gameId = "chess", achievementId = "a2", name = "Marathon", description = "Play 10 games",+            xpReward = 100, targetProgress = 10, isSecret = false, tier = "SILVER", iconResName = null,+            progress = 3, isUnlocked = false, unlockedAt = null,+        )+        composeRule.setContent { AchievementRow(locked) }+        composeRule.onNodeWithText("Marathon").assertIsDisplayed()+    }++    @Test+    fun activityItemClickReportsGame() {+        var clicked: String? = null+        val event = ActivityEventEntity(+            type = ActivityEventEntity.TYPE_ACHIEVEMENT_UNLOCKED, gameId = "sudoku",+            title = "Unlocked: Sharp Mind", description = "Completed a hard puzzle",+        )+        composeRule.setContent { ActivityItemCard(event, onGameClick = { clicked = it }) }+        composeRule.onNodeWithText("Unlocked: Sharp Mind").assertIsDisplayed()+        composeRule.onNodeWithText("Unlocked: Sharp Mind").performClick()+        assertEquals("sudoku", clicked)+    }++    @Test+    fun streakIndicatorRendersCounts() {+        composeRule.setContent { StreakCard(currentStreak = 5, longestStreak = 12) }+        composeRule.onNodeWithText("5", substring = true).assertIsDisplayed()+        assertTrue(true)+    }+}
games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensTest.ktnew file mode 100644index 0000000000..6774f6051d--- /dev/null+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensTest.kt@@ -0,0 +1,117 @@+package com.vayunmathur.games.hub.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.hub.R+import com.vayunmathur.games.hub.data.entities.HubGameEntity+import com.vayunmathur.games.hub.ui.components.GameCard+import com.vayunmathur.games.hub.ui.components.LevelBadge+import com.vayunmathur.games.hub.ui.components.StatCard+import com.vayunmathur.games.hub.ui.components.XpProgressBar+import com.vayunmathur.games.hub.ui.screens.DashboardScreen+import com.vayunmathur.games.hub.ui.screens.GamesListScreen+import com.vayunmathur.games.hub.util.DashboardActions+import com.vayunmathur.games.hub.util.DashboardUiState+import com.vayunmathur.games.hub.util.GamesListActions+import com.vayunmathur.games.hub.util.GamesListUiState+import com.vayunmathur.games.hub.viewmodel.CrossGameStats+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Dashboard, games list and shared hub components. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class HubScreensTest {++    @get:Rule+    val composeRule = createComposeRule()++    private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+    private fun str(id: Int): String = context.getString(id)++    private fun game(id: String, name: String) = HubGameEntity(+        gameId = id, packageName = "com.example.$id", displayName = name,+        totalPlaytimeMs = 3_600_000, totalSessions = 3,+    )++    @Test+    fun dashboardShowsPlayerLevelStatsAndRecentGame() {+        var profileOpened = false+        val actions = object : DashboardActions {+            override fun openProfile() { profileOpened = true }+        }+        composeRule.setContent {+            DashboardScreen(+                DashboardUiState(+                    playerName = "Ada", level = 3, title = "Casual Gamer", totalXp = 450,+                    stats = CrossGameStats(+                        totalPlaytimeMs = 5_400_000, totalGames = 4,+                        totalAchievementsUnlocked = 7, totalAchievements = 20,+                        currentStreak = 3, longestStreak = 9,+                    ),+                    recentlyPlayed = listOf(game("chess", "Chess")),+                ),+                actions,+            )+        }+        composeRule.onNodeWithText("Ada").assertIsDisplayed()+        composeRule.onNodeWithText("Casual Gamer").assertIsDisplayed()+        composeRule.onNodeWithText("1h 30m").assertIsDisplayed()+        composeRule.onNodeWithText("7/20").assertIsDisplayed()+        composeRule.onNodeWithText("Ada").performClick()+        assertTrue(profileOpened)+    }++    @Test+    fun dashboardAnonymousPlayerFallback() {+        composeRule.setContent {+            DashboardScreen(DashboardUiState(), DashboardActions.Noop)+        }+        composeRule.onNodeWithText(str(R.string.player)).assertIsDisplayed()+        composeRule.onNodeWithText("Beginner").assertIsDisplayed()+    }++    @Test+    fun gamesListShowsGamesAndOpensOnClick() {+        var opened: String? = null+        val actions = object : GamesListActions {+            override fun openGame(gameId: String) { opened = gameId }+        }+        composeRule.setContent {+            GamesListScreen(+                GamesListUiState(+                    games = listOf(game("chess", "Chess"), game("sudoku", "Sudoku")),+                    installedGameIds = setOf("chess"),+                ),+                actions,+            )+        }+        composeRule.onNodeWithText("Chess").assertIsDisplayed()+        composeRule.onNodeWithText("Chess").performClick()+        assertEquals("chess", opened)+    }++    @Test+    fun gameCardStatCardAndXpBarRender() {+        var clicked = false+        composeRule.setContent {+            GameCard(game("chess", "Chess"), isInstalled = true, onClick = { clicked = true }, onPlay = {})+            StatCard(label = "Playtime", value = "2h 0m")+            XpProgressBar(totalXp = 250)+            LevelBadge(level = 4)+        }+        composeRule.onNodeWithText("Chess").assertIsDisplayed()+        composeRule.onNodeWithText("Playtime").assertIsDisplayed()+        composeRule.onNodeWithText("2h 0m").assertIsDisplayed()+        composeRule.onNodeWithText("Chess").performClick()+        assertTrue(clicked)+    }+}
games/hub/src/test/java/com/vayunmathur/games/hub/util/HubCalculatorsTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/util/HubCalculatorsTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/util/HubCalculatorsTest.ktnew file mode 100644index 0000000000..fa72556708--- /dev/null+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/util/HubCalculatorsTest.kt@@ -0,0 +1,115 @@+package com.vayunmathur.games.hub.util++import androidx.compose.ui.graphics.Color+import com.vayunmathur.games.hub.data.entities.PlaySessionEntity+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/** XP levels, streaks and formatting helpers. */+class HubCalculatorsTest {++    @Test+    fun xpLevelsThresholdsAndProgress() {+        assertEquals(1, XpLevelCalculator.level(0))+        assertEquals(1, XpLevelCalculator.level(-50))+        assertEquals(1, XpLevelCalculator.level(99))+        assertEquals(2, XpLevelCalculator.level(100))+        assertEquals(3, XpLevelCalculator.level(400))+        assertEquals(5, XpLevelCalculator.level(1600))+        assertEquals(0, XpLevelCalculator.xpForLevel(1))+        assertEquals(100, XpLevelCalculator.xpForLevel(2))+        assertEquals(400, XpLevelCalculator.xpForLevel(3))+        assertEquals(100, XpLevelCalculator.xpToNextLevel(0))+        assertEquals(300, XpLevelCalculator.xpToNextLevel(100))+        assertEquals(0f, XpLevelCalculator.progressToNextLevel(100))+        assertEquals(0.5f, XpLevelCalculator.progressToNextLevel(250), 0.001f)+        assertTrue(XpLevelCalculator.progressToNextLevel(399) in 0.99f..1f)+    }++    @Test+    fun xpTitlesByBand() {+        assertEquals("Beginner", XpLevelCalculator.title(1))+        assertEquals("Novice", XpLevelCalculator.title(2))+        assertEquals("Casual Gamer", XpLevelCalculator.title(4))+        assertEquals("Enthusiast", XpLevelCalculator.title(8))+        assertEquals("Master", XpLevelCalculator.title(12))+        assertEquals("Grandmaster", XpLevelCalculator.title(18))+        assertEquals("Legend", XpLevelCalculator.title(25))+        assertEquals("Legend", XpLevelCalculator.title(99))+    }++    private fun session(dayOffset: Long, durationMs: Long? = 120_000, endTime: Long? = 1L): PlaySessionEntity {+        val now = System.currentTimeMillis()+        val day = 86_400_000L+        // Anchor to start-of-day UTC-ish by using raw day multiples from a fixed epoch day.+        val base = (now / day) * day+        return PlaySessionEntity(+            gameId = "g", sessionId = "s$dayOffset-${durationMs}-${endTime}",+            startTime = base - dayOffset * day, endTime = endTime, durationMs = durationMs,+        )+    }++    @Test+    fun streakEmptyAndTooShortSessions() {+        assertEquals(StreakCalculator.StreakResult(0, 0), StreakCalculator.calculate(emptyList()))+        assertEquals(+            StreakCalculator.StreakResult(0, 0),+            StreakCalculator.calculate(listOf(session(0, durationMs = 1_000))),+        )+        // In-progress session (no duration, no end) never qualifies.+        assertEquals(+            StreakCalculator.StreakResult(0, 0),+            StreakCalculator.calculate(listOf(session(0, durationMs = null, endTime = null))),+        )+    }++    @Test+    fun streakConsecutiveDaysAndGap() {+        val now = System.currentTimeMillis()+        val result = StreakCalculator.calculate(+            listOf(session(0), session(1), session(2), session(4), session(5)),+            now = now,+        )+        assertEquals(3, result.currentStreak)+        assertEquals(3, result.longestStreak)+        // Stale streak: last play 5 days ago -> current 0, longest preserved.+        val stale = StreakCalculator.calculate(listOf(session(5), session(6)), now = now)+        assertEquals(0, stale.currentStreak)+        assertEquals(2, stale.longestStreak)+        // Yesterday still counts as current.+        val yesterday = StreakCalculator.calculate(listOf(session(1)), now = now)+        assertEquals(1, yesterday.currentStreak)+    }++    @Test+    fun formatPlaytimeAndDuration() {+        assertEquals("0m", formatPlaytime(0))+        assertEquals("0m", formatPlaytime(-5))+        assertEquals("<1m", formatPlaytime(30_000))+        assertEquals("5m", formatPlaytime(300_000))+        assertEquals("1h 30m", formatPlaytime(5_400_000))+        assertEquals("0s", formatDurationMs(0))+        assertEquals("45s", formatDurationMs(45_000))+        assertEquals("2m 5s", formatDurationMs(125_000))+        assertEquals("1h 0m", formatDurationMs(3_600_000))+    }++    @Test+    fun formatRelativeTimeBands() {+        val now = 1_700_000_000_000L+        assertEquals("now", formatRelativeTime(now + 60_000, now))+        assertEquals("just now", formatRelativeTime(now - 10_000, now))+        assertEquals("5m ago", formatRelativeTime(now - 300_000, now))+        assertEquals("3h ago", formatRelativeTime(now - 10_800_000, now))+        assertEquals("2d ago", formatRelativeTime(now - 172_800_000, now))+    }++    @Test+    fun tierColorsKnownAndFallback() {+        assertEquals(Color(0xFFFFD700), tierColor("gold"))+        assertEquals(Color(0xFF9C27B0), tierColor("PLATINUM"))+        assertEquals(Color(0xFF9E9E9E), tierColor("Silver"))+        assertEquals(Color(0xFFCD7F32), tierColor("wood"))+    }+}