games hub: a replayed session insert is no longer double-counted
- Commit
c6d3d414b44d9b2019187f3cefb5ff9ab5262d6b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:43 UTC (5 days ago)
- Parents
7a41b2c
The by-game collection insert (sessions/<gameId> -> insertSession) called GameDao.markPlayed unconditionally, while the per-item path deduped on sessionId before counting (and updateSession is idempotent). A game-side retry or replay of the same sessionId therefore inflated hub_games.totalSessions — feeding the cross-game profile totals — and rewound lastPlayedAt to the replayed start time. The regression test inserts identical values twice and saw totalSessions 2 pre-fix. insertSession now counts only when no session with that sessionId exists, mirroring insertSessionItem; the session row upsert is unchanged. Achievement merges, XP aggregation, streak logic and profile restore were audited and cleared. Verified: :games:hub:testDevUnitTest 62 tests pass; :games:hub:lint BUILD SUCCESS (warnings are pre-existing in library dependencies).
Diff
games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubInsertHandlers.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubInsertHandlers.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubInsertHandlers.ktindex 9440d11553..55be453567 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubInsertHandlers.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/provider/GamesHubInsertHandlers.kt@@ -131,7 +131,11 @@ internal suspend fun GamesHubProvider.insertSession( ): Uri? { val session = valuesToSession(v, uri) ensureGameExists(session.gameId, repo)- database.gameDao().markPlayed(session.gameId, session.startTime)+ // Same dedup as the per-item insert: a retried session start must not+ // count one play twice in totalSessions (or rewind lastPlayedAt).+ if (repo.sessions.getSessionById(session.sessionId) == null) {+ database.gameDao().markPlayed(session.gameId, session.startTime)+ } repo.sessions.upsertSession(session) return uri }
games/hub/src/test/java/com/vayunmathur/games/hub/provider/HubProviderTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/provider/HubProviderTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/provider/HubProviderTest.ktindex 94f97954ab..b94d4cb68f 100644--- a/games/hub/src/test/java/com/vayunmathur/games/hub/provider/HubProviderTest.kt+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/provider/HubProviderTest.kt@@ -217,6 +217,27 @@ class HubProviderTest { assertEquals(0, provider.delete(GameHubContract.buildGameUri("chess"), null, null)) } + @Test+ fun bulkSessionInsertCountsDuplicateSessionOnlyOnce() = runBlocking {+ // The per-item session insert dedups on sessionId (a game-side retry+ // must not count one play twice); the by-game collection insert must+ // follow the same rule.+ val gameId = "bulksessiongame"+ val bulkUri = Uri.parse("content://${GameHubContract.AUTHORITY}/sessions/$gameId")+ repeat(2) {+ provider.insert(+ bulkUri,+ values {+ put(GameHubContract.Sessions.SESSION_ID, "bulk-sess-1")+ put(GameHubContract.Sessions.START_TIME, 2_000_000L)+ },+ )+ }+ assertNotNull(repo.sessions.getSessionById("bulk-sess-1"))+ assertEquals(1, repo.games.getGame(gameId)?.totalSessions)+ assertEquals(2_000_000L, repo.games.getGame(gameId)?.lastPlayedAt)+ }+ @Test fun sessionsAndStreaksIngestWithPlaytimeAndBestProtection() = runBlocking { val sessionUri = GameHubContract.buildSessionItemUri("chess", "sess-1")