Modern Apps git

← Modern-Apps.git

calculator: make the currency-offline test hermetic

Commit
c64e9c616dc8c08788ef8c746afbc7ce80ba66b6
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:58 UTC (28 days ago)
Parents
ee52c31
viewModelConverterStartLoadsAndFailsCurrencyGracefully expected
the live currency fetch to fail, which was only true on boxes with
no route to api.maproj.org. On the internet-connected CI box the
fetch succeeded, the Currency tab populated, and the expected
error state never appeared (run 8f356294).

UnitConverterTab now takes a ratesFetcher seam defaulting to
CurrencyApi.rates; the test injects a throwing fetcher through
CalculatorViewModel's existing converter parameter, so the
offline path is exercised deterministically on any box.
Production behaviour is unchanged.

Verified locally on devvm24805:
:calculator:testDevUnitTest for ViewModelAndConverterTest green.

Diff

calculator/src/main/java/com/vayunmathur/calculator/util/UnitConverterTab.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/util/UnitConverterTab.kt b/calculator/src/main/java/com/vayunmathur/calculator/util/UnitConverterTab.ktindex cf0a6db8bf..c31775d2a7 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/util/UnitConverterTab.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/util/UnitConverterTab.kt@@ -25,7 +25,14 @@ internal fun unitsToKey(categoryName: String) = "calculator_units_to_$categoryNa  * the ViewModel delegates [UnitConverterActions] to this and exposes [uiState]. Call  * [start] once the ViewModel's scope is available.  */-class UnitConverterTab(private val application: Application) {+class UnitConverterTab(+    private val application: Application,+    // Test seam: the currency-rate fetch, so unit tests can force the+    // offline path instead of depending on the ambient network being+    // unreachable (on a box with internet the old test's "fails offline"+    // fetch succeeded and no error state ever appeared).+    private val ratesFetcher: suspend () -> CurrencyRatesDto = { CurrencyApi.rates() },+) {     private val dataStore = DataStoreUtils.getInstance(application)     private var scope: CoroutineScope? = null @@ -168,7 +175,7 @@ class UnitConverterTab(private val application: Application) {         currencyLoading = true         currencyError = null         scope?.launch {-            runCatching { CurrencyApi.rates() }+            runCatching { ratesFetcher() }                 .onSuccess { dto ->                     val category = UnitRegistry.currencyCategory(dto.rates)                     if (category == null) {
calculator/src/test/java/com/vayunmathur/calculator/util/ViewModelAndConverterTest.kt
diff --git a/calculator/src/test/java/com/vayunmathur/calculator/util/ViewModelAndConverterTest.kt b/calculator/src/test/java/com/vayunmathur/calculator/util/ViewModelAndConverterTest.ktindex 1f1e7cdbea..61c5673e06 100644--- a/calculator/src/test/java/com/vayunmathur/calculator/util/ViewModelAndConverterTest.kt+++ b/calculator/src/test/java/com/vayunmathur/calculator/util/ViewModelAndConverterTest.kt@@ -95,10 +95,15 @@ class ViewModelAndConverterTest {      @Test fun viewModelConverterStartLoadsAndFailsCurrencyGracefully() {         // The VM starts the converter tab: it restores the persisted-        // selection and attempts a currency fetch, which fails offline-        // (no NetworkClient in tests) and must surface as an error state-        // on the Currency tab rather than crashing.-        val vm = CalculatorViewModel(app)+        // selection and attempts a currency fetch, which is made to fail+        // below and must surface as an error state on the Currency tab+        // rather than crashing.+        // Hermetic offline: inject a failing fetcher (the real one reaches+        // api.maproj.org on internet-connected boxes and succeeds).+        val vm = CalculatorViewModel(+            app,+            converter = UnitConverterTab(app) { throw java.io.IOException("offline (test)") },+        )         val deadline = System.currentTimeMillis() + 10_000         var state = vm.unitConverterUiState         while (System.currentTimeMillis() < deadline) {