Modern Apps git

← Modern-Apps.git

shop: add profile-store and image-fetch test seams

Commit
9608e880c96b4e9b2930d6e6d6d882bc75d748ec
Author
Vayun Mathur <[email protected]>
When
2026-11-07 11:31 UTC (29 days ago)
Parents
a1f747a
Behavior-preserving seams so the shop pipeline can be driven
off-device (each defaults to the current behavior):

- Extract a ProfileStore interface from ProfileRepository and let
  ShopViewModel take a profileStoreFactory (null means the real
  encrypted Room repository). The SQLCipher/AndroidKeyStore database
  cannot open under Robolectric, which otherwise makes the
  ViewModel, profile screen and navigation untestable on the JVM.
- Add RemoteImageFetchers.fetchOverride (null means the real
  NetworkClient fetch) so image-pipeline branches can be tested
  without touching the network.

No behavior change; the work-testing test dependency added for
worker tests is called out in the test commit.

(cherry picked from commit e5ed0742aa5e463eb277944ae80e9e50a8b35537)

Diff

shop/src/main/java/com/vayunmathur/shop/data/ProfileRepository.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/data/ProfileRepository.kt b/shop/src/main/java/com/vayunmathur/shop/data/ProfileRepository.ktindex ebb3568560..2345460916 100644--- a/shop/src/main/java/com/vayunmathur/shop/data/ProfileRepository.kt+++ b/shop/src/main/java/com/vayunmathur/shop/data/ProfileRepository.kt@@ -4,12 +4,18 @@ import android.content.Context import com.vayunmathur.library.room.RoomRepository import kotlinx.coroutines.flow.Flow +/** The profile surface [com.vayunmathur.shop.platform.ShopViewModel] needs; faked in tests. */+interface ProfileStore {+    val profile: Flow<ShopProfile?>+    suspend fun save(profile: ShopProfile)+}+ class ProfileRepository private constructor(context: Context) :-    RoomRepository<ProfileDatabase>(context, ProfileDatabase::class, PROFILE_DB_NAME) {+    RoomRepository<ProfileDatabase>(context, ProfileDatabase::class, PROFILE_DB_NAME), ProfileStore { -    val profile: Flow<ShopProfile?> get() = db.profileDao().observe()+    override val profile: Flow<ShopProfile?> get() = db.profileDao().observe() -    suspend fun save(profile: ShopProfile) = db.profileDao().upsert(profile.copy(id = 0))+    override suspend fun save(profile: ShopProfile) = db.profileDao().upsert(profile.copy(id = 0))      companion object {         @Volatile private var instance: ProfileRepository? = null
shop/src/main/java/com/vayunmathur/shop/data/RemoteImageFetchers.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/data/RemoteImageFetchers.kt b/shop/src/main/java/com/vayunmathur/shop/data/RemoteImageFetchers.ktindex 37848cbd8c..cc4986b51e 100644--- a/shop/src/main/java/com/vayunmathur/shop/data/RemoteImageFetchers.kt+++ b/shop/src/main/java/com/vayunmathur/shop/data/RemoteImageFetchers.kt@@ -18,6 +18,10 @@ import kotlinx.coroutines.withContext  * local fetchers (#884).  */ object RemoteImageFetchers {++    /** Test seam: replaces the HTTP fetch. Null means the real [NetworkClient] call. */+    internal var fetchOverride: (suspend (String) -> FetchResult?)? = null+     suspend fun network(data: Any?, @Suppress("UNUSED_PARAMETER") context: Context): FetchResult? {         val url: String = when (data) {             is String -> data@@ -28,6 +32,7 @@ object RemoteImageFetchers {             else -> return null         }         if (!url.startsWith("http://") && !url.startsWith("https://")) return null+        fetchOverride?.let { return it(url) }         return withContext(Dispatchers.IO) {             val resp = NetworkClient.execute(url)             if (!resp.isSuccess) {
shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.ktindex 0b9266943e..45999e2203 100644--- a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt+++ b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt@@ -19,6 +19,7 @@ import com.vayunmathur.shop.data.EmbeddedCheckoutRequest import com.vayunmathur.shop.data.Money import com.vayunmathur.shop.data.PlacedOrder import com.vayunmathur.shop.data.ProfileRepository+import com.vayunmathur.shop.data.ProfileStore import com.vayunmathur.shop.data.ShopCartStore import com.vayunmathur.shop.data.ShopProfile import com.vayunmathur.shop.data.UcpProduct@@ -80,12 +81,16 @@ class ShopViewModel(     var profileLoaded by mutableStateOf(false)         private set     val profileComplete: Boolean get() = profile?.validationError() == null && profile != null-    private var profileRepository: ProfileRepository? = null+    private var profileRepository: ProfileStore? = null     private val nativeCheckout = NativeCheckoutCoordinator() +    /** Test seam: replaces the encrypted profile database. Null means [ProfileRepository]. */+    internal var profileStoreFactory: ((Context) -> ProfileStore)? = null+     fun observeProfile(context: Context) {         if (profileRepository != null) return-        val repository = ProfileRepository.get(context).also { profileRepository = it }+        val repository = (profileStoreFactory?.invoke(context) ?: ProfileRepository.get(context))+            .also { profileRepository = it }         viewModelScope.launch { repository.profile.collect { profile = it; profileLoaded = true } }     }