globalesim: make the offline tests hermetic
- Commit
ee52c3144f887b7968babb5e4877014ff71da5f8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:56 UTC (28 days ago)
- Parents
4481de6
Five tests in run 8f356294 expected every eSIM API call to fail, but they only failed where the ambient network was unreachable. The CI box has internet, so the real api.maproj.org answered and the ViewModel settled with live data instead of the offline state. EsimApi gains a VisibleForTesting baseUrlOverride. The API test points it at a dead loopback port for the duration of the test and always resets it; EsimViewModelTest sets it in setUp and clears it in tearDown, so the offline path is exercised on any box. Production defaults are unchanged. Verified locally on devvm24805: :globalesim:testDevUnitTest :globalesim:compileDevKotlin green.
Diff
globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.ktindex 53239ae9ba..b962dcb649 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt@@ -14,9 +14,16 @@ private val HTTP_STATUS = Regex("^HTTP (\\d{3})\\b") object EsimApi { const val BASE_URL = "https://api.maproj.org" + // Test seam: point the API at an unroutable address so offline-path+ // tests are hermetic on boxes that have internet.+ @androidx.annotation.VisibleForTesting+ internal var baseUrlOverride: String? = null++ private val baseUrl: String get() = baseUrlOverride ?: BASE_URL+ private val json = Json { ignoreUnknownKeys = true; isLenient = true } - fun url(path: String): String = BASE_URL + path+ fun url(path: String): String = baseUrl + path /** HTTP failure with a parsed status code. NetworkClient surfaces * non-2xx as `IOException("HTTP <code>: ...")`. */
globalesim/src/test/java/com/vayunmathur/globalesim/data/api/EsimApiDtoTest.kt
diff --git a/globalesim/src/test/java/com/vayunmathur/globalesim/data/api/EsimApiDtoTest.kt b/globalesim/src/test/java/com/vayunmathur/globalesim/data/api/EsimApiDtoTest.ktindex 6c6e25ef6c..df797e3426 100644--- a/globalesim/src/test/java/com/vayunmathur/globalesim/data/api/EsimApiDtoTest.kt+++ b/globalesim/src/test/java/com/vayunmathur/globalesim/data/api/EsimApiDtoTest.kt@@ -74,15 +74,22 @@ class EsimApiDtoTest { } @Test fun endpointsFailOffline() = runTest {- // The devserver has no route to api.maproj.org: every call fails as an- // IOException (never a successful fake), which ViewModels map to the- // "No connection" message.- assertTrue(runCatching { EsimApi.rates() }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.status("acc") }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.stripeKey() }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.stripeIntent("acc", 500L) }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.nowPayInvoice("acc", 500L) }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.nowPayCancel("acc") }.exceptionOrNull() is java.io.IOException)- assertTrue(runCatching { EsimApi.claim("sim", "acc") }.exceptionOrNull() is java.io.IOException)+ // Hermetic offline: a dead loopback address fails on any box, so+ // every call surfaces as an IOException (never a successful fake),+ // which ViewModels map to the "No connection" message. These tests+ // previously relied on the ambient network being unreachable; on+ // the internet-connected CI box the real API answered instead.+ EsimApi.baseUrlOverride = "https://127.0.0.1:9"+ try {+ assertTrue(runCatching { EsimApi.rates() }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.status("acc") }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.stripeKey() }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.stripeIntent("acc", 500L) }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.nowPayInvoice("acc", 500L) }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.nowPayCancel("acc") }.exceptionOrNull() is java.io.IOException)+ assertTrue(runCatching { EsimApi.claim("sim", "acc") }.exceptionOrNull() is java.io.IOException)+ } finally {+ EsimApi.baseUrlOverride = null+ } } }
globalesim/src/test/java/com/vayunmathur/globalesim/platform/EsimViewModelTest.kt
diff --git a/globalesim/src/test/java/com/vayunmathur/globalesim/platform/EsimViewModelTest.kt b/globalesim/src/test/java/com/vayunmathur/globalesim/platform/EsimViewModelTest.ktindex e19c330f54..e18c9cfac1 100644--- a/globalesim/src/test/java/com/vayunmathur/globalesim/platform/EsimViewModelTest.kt+++ b/globalesim/src/test/java/com/vayunmathur/globalesim/platform/EsimViewModelTest.kt@@ -3,7 +3,9 @@ package com.vayunmathur.globalesim.platform import android.app.Application import android.os.Looper import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.globalesim.data.api.EsimApi import com.vayunmathur.globalesim.data.api.ProfileDto+import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals@@ -25,9 +27,16 @@ class EsimViewModelTest { private lateinit var vm: EsimViewModel @BeforeTest fun setUp() {+ // Hermetic offline: dead loopback, so the API fails on any box+ // (CI has internet and the real API answered these tests before).+ EsimApi.baseUrlOverride = "https://127.0.0.1:9" vm = EsimViewModel(app) } + @AfterTest fun tearDown() {+ EsimApi.baseUrlOverride = null+ }+ private fun await(timeoutMs: Long = 15_000, cond: () -> Boolean): Boolean { val deadline = System.currentTimeMillis() + timeoutMs while (System.currentTimeMillis() < deadline) {