games/hub: collection progress inserts never regress an unlock
- Commit
53fdbcc6d5a3f3b6a1cccc7c3f4a1f9ee885a93e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 22:37 UTC (5 days ago)
- Parents
a0688c0
The collection (bulk) progress path wrote the raw row with @Upsert, bypassing the mergeProgress helper every sibling path uses (max progress, OR unlock, preserve first unlockedAt). A bulk insert with progress=0 and no unlock keys after a per-item unlock therefore regressed the stored progress to 0, dropped the achievement's XP and level (flowTotalXp joins on isUnlocked=1), and let a later unlock re-report insert a second ACHIEVEMENT_UNLOCKED activity. insertProgress now fetches, merges, and gates the unlock activity on merged-vs-previous state, identical to the per-item path. New regression test failed pre-fix (progress 1 became 0) and asserts progress/unlock/unlockedAt survive and the unlock activity stays singular. Streaks, XP, catalogue/launch routing and session persistence were audited and cleared. Verified: :games:hub:testDevUnitTest 61 tests and lint pass.
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 08fde22903..9440d11553 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@@ -41,15 +41,19 @@ internal suspend fun GamesHubProvider.insertProgress( repo: GamesHubRepository, ): Uri? { val prog = valuesToProgress(v, uri)- val wasUnlocked = repo.achievements.getProgress(prog.gameId, prog.achievementId)?.isUnlocked- ?: false+ val existing = repo.achievements.getProgress(prog.gameId, prog.achievementId)+ val wasUnlocked = existing?.isUnlocked ?: false val def = repo.achievements.getAchievementDef(prog.gameId, prog.achievementId)- repo.achievements.upsertProgress(prog)- if (prog.isUnlocked && !wasUnlocked && def != null) {+ // Same non-regression merge as the per-item insert/update paths: a stale+ // collection-level re-report must not un-unlock an achievement, drop its+ // XP, or clear its unlock time (which would also re-log the unlock).+ val merged = mergeProgress(existing, prog)+ repo.achievements.upsertProgress(merged)+ if (merged.isUnlocked && !wasUnlocked && def != null) { repo.activity.upsertActivity( ActivityEventEntity( type = ActivityEventEntity.TYPE_ACHIEVEMENT_UNLOCKED,- gameId = prog.gameId,+ gameId = merged.gameId, title = def.name, description = def.description )
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 dd2e22476d..94f97954ab 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@@ -2,6 +2,7 @@ package com.vayunmathur.games.hub.provider import android.app.Application import android.content.ContentValues+import android.net.Uri import android.provider.BaseColumns import androidx.room3.Room import androidx.test.core.app.ApplicationProvider@@ -141,6 +142,63 @@ class HubProviderTest { } } + @Test+ fun bulkProgressInsertNeverRegressesUnlockOrDuplicatesUnlockActivity() = runBlocking {+ val gameId = "bulkchess"+ provider.insert(+ GameHubContract.buildAchievementDefsUri(gameId),+ values {+ put(GameHubContract.AchievementDefs.ACHIEVEMENT_ID, "bulk1")+ put(GameHubContract.AchievementDefs.NAME, "Bulk Win")+ put(GameHubContract.AchievementDefs.DESCRIPTION, "Win via bulk")+ put(GameHubContract.AchievementDefs.XP_REWARD, 50)+ },+ )+ // Total XP is hub-global over the shared test DB, so assert deltas.+ val xpBefore = repo.achievements.totalXpFlow().first()+ provider.insert(+ GameHubContract.buildAchievementProgressItemUri(gameId, "bulk1"),+ values {+ put(GameHubContract.AchievementProgressCols.PROGRESS, 1)+ put(GameHubContract.AchievementProgressCols.IS_UNLOCKED, 1)+ put(GameHubContract.AchievementProgressCols.UNLOCKED_AT, 1234L)+ },+ )+ assertEquals(xpBefore + 50, repo.achievements.totalXpFlow().first())++ // A stale collection-level re-report (progress 0, not unlocked) must not+ // un-unlock the achievement, drop its XP, or clear its unlock time.+ val bulkUri = Uri.parse("content://${GameHubContract.AUTHORITY}/achievements/$gameId/progress")+ provider.insert(+ bulkUri,+ values {+ put(GameHubContract.AchievementProgressCols.ACHIEVEMENT_ID, "bulk1")+ put(GameHubContract.AchievementProgressCols.PROGRESS, 0)+ },+ )+ val kept = repo.achievements.getProgress(gameId, "bulk1")!!+ assertEquals(1, kept.progress)+ assertTrue(kept.isUnlocked)+ assertEquals(1234L, kept.unlockedAt)+ assertEquals(xpBefore + 50, repo.achievements.totalXpFlow().first())++ // Re-reporting the unlock through the collection path must not log a+ // second ACHIEVEMENT_UNLOCKED activity for the same achievement.+ provider.insert(+ bulkUri,+ values {+ put(GameHubContract.AchievementProgressCols.ACHIEVEMENT_ID, "bulk1")+ put(GameHubContract.AchievementProgressCols.PROGRESS, 1)+ put(GameHubContract.AchievementProgressCols.IS_UNLOCKED, 1)+ },+ )+ assertEquals(+ 1,+ repo.activity.allActivityFlow().first()+ .count { it.type == ActivityEventEntity.TYPE_ACHIEVEMENT_UNLOCKED && it.gameId == gameId },+ )+ }+ @Test fun achievementUpdatePathMergesAndUnlocks() = runBlocking { // Update path follows the same merge + unlock-activity rules.