Modern Apps git

← Modern-Apps.git

appstore: allow ViewModel tests to inject offline repositories

Commit
800c8efc71eba80ec4f60534e36c218dff7af163
Author
Vayun Mathur <[email protected]>
When
2026-11-14 23:09 UTC (21 days ago)
Parents
20eeca2
The phone/TV ViewModel tests asserted offline behaviour (Play
contributes no rows, the Accrescent allowlist consulted is the
seeded one) while constructing the real repositories, so with
live network Play dispensed real accounts and the Accrescent
refresh replaced the seeded allowlist with the real one.

AppStoreViewModel and TvStoreViewModel now accept optional
play/accrescent repository overrides (defaults unchanged; the
factories pass nothing). The five affected tests inject a Play
repository whose dispenser always fails and/or an Accrescent
repository backed by a dead in-process channel and a failing
repodata fetcher — the same failure modes an offline device
produces. Assertions unchanged.

Verified: targeted :appstore:testDevUnitTest run (see final
full-suite run).

Diff

appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.ktindex d3dece61fa..0e0d9daa94 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt@@ -55,11 +55,15 @@ import kotlinx.coroutines.withContext class TvStoreViewModel(     internal val context: Application,     db: AppDatabase,+    // Test seams: offline tests inject repositories whose network calls+    // fail deterministically; production uses the defaults.+    playOverride: PlayRepository? = null,+    accrescentOverride: AccrescentRepository? = null, ) : ViewModel() {      internal val catalog = CatalogRepository(context, db, viewModelScope)-    internal val play = PlayRepository(context)-    internal val accrescent = AccrescentRepository(context, db)+    internal val play = playOverride ?: PlayRepository(context)+    internal val accrescent = accrescentOverride ?: AccrescentRepository(context, db)     internal val installedRepo = InstalledAppsRepository(context)     internal val settings = SettingsRepository(context, viewModelScope)     internal val installer = InstallCoordinator(context, db, play, accrescent, viewModelScope) {
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.ktindex 39765df71d..1efb7fafb3 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -61,11 +61,15 @@ import kotlinx.coroutines.withContext class AppStoreViewModel(     internal val context: Application,     db: AppDatabase,+    // Test seams: offline tests inject repositories whose network calls+    // fail deterministically; production uses the defaults.+    playOverride: PlayRepository? = null,+    accrescentOverride: AccrescentRepository? = null, ) : ViewModel(), HomeActions, SearchActions, AppDetailActions, UpdatesActions, LibraryActions {      internal val catalog = CatalogRepository(context, db, viewModelScope)-    internal val play = PlayRepository(context)-    internal val accrescent = AccrescentRepository(context, db)+    internal val play = playOverride ?: PlayRepository(context)+    internal val accrescent = accrescentOverride ?: AccrescentRepository(context, db)     internal val installedRepo = InstalledAppsRepository(context)     internal val settings = SettingsRepository(context, viewModelScope)     internal val installer =
appstore/src/test/java/com/vayunmathur/appstore/tv/TvCheckBranchesTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvCheckBranchesTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvCheckBranchesTest.ktindex a95b06d1e2..29d2b2bab8 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvCheckBranchesTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvCheckBranchesTest.kt@@ -44,7 +44,36 @@ class TvCheckBranchesTest {                 listOf(AccrescentTrustEntity(ownPackage, "ab".repeat(32), 1L)),             )         }-        vm = TvStoreViewModel(app, db)+        // Hermetic offline: Play dispensing fails (so previous Play rows+        // are kept) and the Accrescent refresh fails (so the seeded+        // allowlist survives), whatever network the test host has.+        val offlinePlay = com.vayunmathur.appstore.data.play.PlayRepository(+            app,+            object : com.vayunmathur.appstore.data.play.AnonymousAuthRepository() {+                override suspend fun ensureAuthData(+                    deviceProps: java.util.Properties,+                    dispenserUrls: List<String>,+                ): Result<com.aurora.gplayapi.data.models.AuthData> =+                    Result.failure(java.io.IOException("offline test double"))+            },+        )+        val deadChannel = io.grpc.inprocess.InProcessChannelBuilder+            .forName("tv-check-offline").directExecutor().build()+        val failingFetcher = object :+            com.vayunmathur.appstore.data.accrescent.AccrescentRepoDataFetcher(app) {+            override suspend fun fetch():+                Result<com.vayunmathur.appstore.data.accrescent.RepoData> =+                Result.failure(java.io.IOException("offline test double"))+        }+        vm = TvStoreViewModel(+            app, db,+            playOverride = offlinePlay,+            accrescentOverride = com.vayunmathur.appstore.data.accrescent.AccrescentRepository(+                app, db,+                com.vayunmathur.appstore.data.accrescent.AccrescentApi { deadChannel },+                failingFetcher,+            ),+        )         vm.enabledSourcesFlow.value = setOf(             AppSource.PLAYSTORE, AppSource.ACCRESCENT, AppSource.MODERN_APPS,         )
appstore/src/test/java/com/vayunmathur/appstore/tv/TvLoadHomePlayAccrescentTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvLoadHomePlayAccrescentTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvLoadHomePlayAccrescentTest.ktindex 7b9f38bba1..bedecf189d 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvLoadHomePlayAccrescentTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvLoadHomePlayAccrescentTest.kt@@ -32,7 +32,35 @@ class TvLoadHomePlayAccrescentTest {             .setDriver(BundledSQLiteDriver())             .allowMainThreadQueries()             .build()-        vm = TvStoreViewModel(app, db)+        // Hermetic offline: Play dispensing fails and the Accrescent API+        // is a dead channel, whatever network the test host has.+        val offlinePlay = com.vayunmathur.appstore.data.play.PlayRepository(+            app,+            object : com.vayunmathur.appstore.data.play.AnonymousAuthRepository() {+                override suspend fun ensureAuthData(+                    deviceProps: java.util.Properties,+                    dispenserUrls: List<String>,+                ): Result<com.aurora.gplayapi.data.models.AuthData> =+                    Result.failure(java.io.IOException("offline test double"))+            },+        )+        val deadChannel = io.grpc.inprocess.InProcessChannelBuilder+            .forName("tv-loadhome-offline").directExecutor().build()+        val failingFetcher = object :+            com.vayunmathur.appstore.data.accrescent.AccrescentRepoDataFetcher(app) {+            override suspend fun fetch():+                Result<com.vayunmathur.appstore.data.accrescent.RepoData> =+                Result.failure(java.io.IOException("offline test double"))+        }+        vm = TvStoreViewModel(+            app, db,+            playOverride = offlinePlay,+            accrescentOverride = com.vayunmathur.appstore.data.accrescent.AccrescentRepository(+                app, db,+                com.vayunmathur.appstore.data.accrescent.AccrescentApi { deadChannel },+                failingFetcher,+            ),+        )         vm.enabledSourcesFlow.value = setOf(             AppSource.PLAYSTORE, AppSource.ACCRESCENT, AppSource.MODERN_APPS,         )
appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentLoadTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentLoadTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentLoadTest.ktindex b2001bdb42..7953a1ab42 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentLoadTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/util/AccrescentLoadTest.kt@@ -35,7 +35,24 @@ class AccrescentLoadTest {             .build()         // Not closed: the ViewModel scope keeps collecting DB flows; the         // in-memory DB dies with the Robolectric sandbox.-        vm = AppStoreViewModel(app, db)+        // Hermetic offline: dead gRPC channel + failing repodata fetcher,+        // whatever network the test host happens to have.+        val deadChannel = io.grpc.inprocess.InProcessChannelBuilder+            .forName("accrescent-load-offline").directExecutor().build()+        val failingFetcher = object :+            com.vayunmathur.appstore.data.accrescent.AccrescentRepoDataFetcher(app) {+            override suspend fun fetch():+                Result<com.vayunmathur.appstore.data.accrescent.RepoData> =+                Result.failure(java.io.IOException("offline test double"))+        }+        vm = AppStoreViewModel(+            app, db,+            accrescentOverride = com.vayunmathur.appstore.data.accrescent.AccrescentRepository(+                app, db,+                com.vayunmathur.appstore.data.accrescent.AccrescentApi { deadChannel },+                failingFetcher,+            ),+        )     }      @Test
appstore/src/test/java/com/vayunmathur/appstore/util/PhoneCheckAccrescentTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneCheckAccrescentTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneCheckAccrescentTest.ktindex 0bddd0a9fc..479888024f 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneCheckAccrescentTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneCheckAccrescentTest.kt@@ -47,7 +47,36 @@ class PhoneCheckAccrescentTest {             settings.setSourceEnabled(AppSource.ACCRESCENT, true)             settings.setSourceEnabled(AppSource.PLAYSTORE, true)         }-        vm = AppStoreViewModel(app, db)+        // Hermetic offline: Play dispensing fails, the Accrescent API is+        // a dead channel and its repodata refresh fails, so the seeded+        // allowlist above survives — whatever network the test host has.+        val offlinePlay = com.vayunmathur.appstore.data.play.PlayRepository(+            app,+            object : com.vayunmathur.appstore.data.play.AnonymousAuthRepository() {+                override suspend fun ensureAuthData(+                    deviceProps: java.util.Properties,+                    dispenserUrls: List<String>,+                ): Result<com.aurora.gplayapi.data.models.AuthData> =+                    Result.failure(java.io.IOException("offline test double"))+            },+        )+        val deadChannel = io.grpc.inprocess.InProcessChannelBuilder+            .forName("phone-check-offline").directExecutor().build()+        val failingFetcher = object :+            com.vayunmathur.appstore.data.accrescent.AccrescentRepoDataFetcher(app) {+            override suspend fun fetch():+                Result<com.vayunmathur.appstore.data.accrescent.RepoData> =+                Result.failure(java.io.IOException("offline test double"))+        }+        vm = AppStoreViewModel(+            app, db,+            playOverride = offlinePlay,+            accrescentOverride = com.vayunmathur.appstore.data.accrescent.AccrescentRepository(+                app, db,+                com.vayunmathur.appstore.data.accrescent.AccrescentApi { deadChannel },+                failingFetcher,+            ),+        )     }      private fun await(description: String, condition: () -> Boolean) {
appstore/src/test/java/com/vayunmathur/appstore/util/PhoneLoadHomePlayTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneLoadHomePlayTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneLoadHomePlayTest.ktindex 4119033643..547a12eb4c 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneLoadHomePlayTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/util/PhoneLoadHomePlayTest.kt@@ -46,7 +46,19 @@ class PhoneLoadHomePlayTest {                 ).toEntity(),             )         }-        vm = AppStoreViewModel(app, db)+        // Hermetic offline: Play dispensing always fails, whatever+        // network the host running the tests happens to have.+        val offlinePlay = com.vayunmathur.appstore.data.play.PlayRepository(+            app,+            object : com.vayunmathur.appstore.data.play.AnonymousAuthRepository() {+                override suspend fun ensureAuthData(+                    deviceProps: java.util.Properties,+                    dispenserUrls: List<String>,+                ): Result<com.aurora.gplayapi.data.models.AuthData> =+                    Result.failure(java.io.IOException("offline test double"))+            },+        )+        vm = AppStoreViewModel(app, db, playOverride = offlinePlay)     }      @Test