games-hub: make hub test fixtures deterministic
- Commit
f55f343dfe13bf0a761cd2842ca3fac2263c8211- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-04 22:44 UTC (1 day ago)
- Parents
3cb27c7
The games hub module failed lintVitalRelease with seven fatal FixtureDeterminism errors, blocking pushes of main. Three test files seeded fixtures from the wall clock (LocalDate.now() and System.currentTimeMillis()), so the fixtures changed on every run and could drift across days and time zones. Replace every wall-clock fixture read with named fixed constants, preserving each test's intent and assertions: - HubCalculatorsTest: anchor sessions and the explicit now passed to StreakCalculator to a fixed today (2026-01-17). The fixed now is local noon of that date in the system-default zone, matching how StreakCalculator buckets by local date, so today/yesterday/stale streak semantics hold in any time zone. The no-now cases still exercise non-qualifying sessions, which return a zero streak regardless of the date. - HubPagesCoverageTest: use a fixed 2026-01-17T12:00:00Z instant for the dashboard activity event and the seeded game's lastPlayedAt. No assertion depends on the rendered relative-age label or on recency filtering. - HubScreensPass3Test: use the same fixed instant for the seeded game's lastPlayedAt, the play session (fixed instant minus 60s, preserving its 60s duration), and the activity event. Assertions check presence, filtering and clearing, not relative-time text. No FIXTURE RELATIVE-TIME EXCEPTION marker was needed: no test genuinely has to seed relative to the system-under-test's real clock. Deadline-polling loops, which the detector already exempts, are untouched. No production code, lint configuration or baselines were changed. Verified fresh in this worktree after deleting games/hub/build: ./gradlew :games:hub:lint :games:hub:lintVitalRelease :games:hub:testDevUnitTest --rerun-tasks --max-workers=128 (JAVA_HOME JDK 21, Android SDK, shared Gradle cache) - BUILD SUCCESSFUL, 112 unit tests passed with no failures, including HubCalculatorsTest (8), HubPagesCoverageTest (15) and HubScreensPass3Test (9). A second fresh lint-only rerun after again deleting games/hub/build also succeeded.
Diff
games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubPagesCoverageTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubPagesCoverageTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubPagesCoverageTest.ktindex 9d8d9ee5b8..58d8825757 100644--- a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubPagesCoverageTest.kt+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubPagesCoverageTest.kt@@ -63,7 +63,12 @@ import org.robolectric.annotation.Config @Config(sdk = [34]) class HubPagesCoverageTest { @get:Rule val rule = createComposeRule()- companion object { private var sharedDb: GamesHubDatabase? = null }+ companion object {+ private var sharedDb: GamesHubDatabase? = null++ /** Fixed fixture instant: 2026-01-17T12:00:00Z. */+ private const val FIXED_NOW_EPOCH_MS = 1_768_651_200_000L+ } private lateinit var repo: GamesHubRepository private lateinit var vm: GameHubViewModel @BeforeTest fun setUp() {@@ -97,7 +102,7 @@ class HubPagesCoverageTest { override fun openProfile(){profile=true}; override fun openActivity(){activity=true}; override fun openGamesList(){gamesList=true}; override fun openController(){controller=true} override fun openGame(gameId:String){opened=gameId}; override fun playGame(game:HubGameEntity){played=game.gameId} }- val event = ActivityEventEntity(id=42,type=ActivityEventEntity.TYPE_SESSION_COMPLETED,gameId="chess",title="Played chess",description="Session 60s",timestamp=System.currentTimeMillis())+ val event = ActivityEventEntity(id=42,type=ActivityEventEntity.TYPE_SESSION_COMPLETED,gameId="chess",title="Played chess",description="Session 60s",timestamp=FIXED_NOW_EPOCH_MS) rule.setContent { DynamicTheme { DashboardScreen( DashboardUiState(playerName="Ada",avatarSymbol="star",level=3,title="Novice",totalXp=450, stats=CrossGameStats(totalPlaytimeMs=3600000,totalSessions=5,totalGames=2,totalAchievementsUnlocked=3,totalAchievements=10,currentStreak=2,longestStreak=7),@@ -204,7 +209,7 @@ class HubPagesCoverageTest { rule.onNodeWithText("Leave").performClick(); assertTrue(left) } private fun seedPages() = kotlinx.coroutines.runBlocking {- repo.games.upsertGame(game("chess","Chess",3600000,System.currentTimeMillis()))+ repo.games.upsertGame(game("chess","Chess",3600000,FIXED_NOW_EPOCH_MS)) repo.profile.upsertProfile(com.vayunmathur.games.hub.data.entities.PlayerProfileEntity(displayName="Page Player")) repo.yuus.createYuu("Page Yuu", YuuFaceConfig(hairStyle=1)) }
games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.ktindex 9bbd33099a..a3256ed95d 100644--- a/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/ui/HubScreensPass3Test.kt@@ -58,6 +58,9 @@ class HubScreensPass3Test { companion object { private var sharedDb: GamesHubDatabase? = null++ /** Fixed fixture instant: 2026-01-17T12:00:00Z. */+ private const val FIXED_NOW_EPOCH_MS = 1_768_651_200_000L } private lateinit var repo: GamesHubRepository@@ -75,14 +78,14 @@ class HubScreensPass3Test { private fun seedLibrary() = runBlocking { repo.games.upsertGame(- HubGameEntity("chess", "com.example.chess", "Chess", description = "Board game", totalPlaytimeMs = 3_600_000, lastPlayedAt = System.currentTimeMillis()),+ HubGameEntity("chess", "com.example.chess", "Chess", description = "Board game", totalPlaytimeMs = 3_600_000, lastPlayedAt = FIXED_NOW_EPOCH_MS), ) repo.achievements.upsertDef(AchievementDefEntity("chess", "win1", "First Win", "Win a game", xpReward = 50)) repo.achievements.upsertDef(AchievementDefEntity("chess", "master", "Master", "Win 10", xpReward = 100, targetProgress = 10)) repo.achievements.upsertProgress(AchievementProgressEntity("chess", "win1", progress = 1, isUnlocked = true, unlockedAt = 100)) repo.achievements.upsertProgress(AchievementProgressEntity("chess", "master", progress = 4))- repo.sessions.upsertSession(PlaySessionEntity(gameId = "chess", sessionId = "s1", startTime = System.currentTimeMillis() - 60_000, endTime = System.currentTimeMillis(), durationMs = 60_000))- repo.activity.upsertActivity(ActivityEventEntity(timestamp = System.currentTimeMillis(), type = ActivityEventEntity.TYPE_ACHIEVEMENT_UNLOCKED, gameId = "chess", title = "First Win"))+ repo.sessions.upsertSession(PlaySessionEntity(gameId = "chess", sessionId = "s1", startTime = FIXED_NOW_EPOCH_MS - 60_000, endTime = FIXED_NOW_EPOCH_MS, durationMs = 60_000))+ repo.activity.upsertActivity(ActivityEventEntity(timestamp = FIXED_NOW_EPOCH_MS, type = ActivityEventEntity.TYPE_ACHIEVEMENT_UNLOCKED, gameId = "chess", title = "First Win")) } @Test
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.ktindex 94607ebf99..64ee08bbc7 100644--- 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.kt@@ -6,6 +6,17 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue +/** Fixed fixture "today" (2026-01-17) for the streak calculators. */+private val FIXED_TODAY: java.time.LocalDate = java.time.LocalDate.of(2026, 1, 17)++/**+ * Fixed fixture "now": noon of [FIXED_TODAY] in the system-default zone.+ * [StreakCalculator] buckets by local date in that same zone, so anchoring+ * the instant to local noon keeps `now` on [FIXED_TODAY] in every time zone.+ */+private val FIXED_NOW_EPOCH_MS: Long = FIXED_TODAY.atTime(12, 0)+ .atZone(java.time.ZoneId.systemDefault()).toInstant().toEpochMilli()+ /** XP levels, streaks and formatting helpers. */ class HubCalculatorsTest { @@ -40,9 +51,9 @@ class HubCalculatorsTest { } private fun session(dayOffset: Long, durationMs: Long? = 120_000, endTime: Long? = 1L): PlaySessionEntity {- // Anchor to the local start of today: StreakCalculator works in local- // days, so UTC-day multiples land in the wrong local day outside UTC.- val base = java.time.LocalDate.now().minusDays(dayOffset)+ // Anchor to local noon of the fixed today: StreakCalculator works in+ // local days, so UTC-day multiples land in the wrong local day outside UTC.+ val base = FIXED_TODAY.minusDays(dayOffset) .atTime(12, 0).atZone(java.time.ZoneId.systemDefault()).toInstant().toEpochMilli() return PlaySessionEntity( gameId = "g", sessionId = "s$dayOffset-${durationMs}-${endTime}",@@ -66,7 +77,7 @@ class HubCalculatorsTest { @Test fun streakConsecutiveDaysAndGap() {- val now = System.currentTimeMillis()+ val now = FIXED_NOW_EPOCH_MS val result = StreakCalculator.calculate( listOf(session(0), session(1), session(2), session(4), session(5)), now = now,