Skip to content

Commit ee7bf17

Browse files
robingenzclaude
andcommitted
test: the Android host's bundle answer on Robolectric
The runtime's answer to React Native's question for its bundle is tested over a core whose start answers the bundle its store names, Mockito constructing it as the Capacitor plugin's tests do: the served bundle's file when the store names one, the embedded bundle when it names none, and the running bundle across a reload the SDK did not perform, with a release downloaded for the next start waiting and the core told nothing. The runtime's constructor is internal so each test starts its own runtime; the app still reaches the one instance through get(). Closes #17 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c14d4a7 commit ee7bf17

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎android/src/main/java/com/hotcodepush/reactnative/HotCodePushRuntime.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface HotCodePushEventSink {
5050
* verdict — the pending switch, the rollback of a release that never became ready — is in before any JavaScript runs,
5151
* and the Turbo Module of every JavaScript instance since talks to that one core.
5252
*/
53-
class HotCodePushRuntime private constructor(private val context: Context) : CoreListener {
53+
class HotCodePushRuntime internal constructor(private val context: Context) : CoreListener {
5454
private val scheduler = HandlerScheduler()
5555
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
5656
private val storeDirectory = File(context.filesDir, "hotcodepush")
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.hotcodepush.reactnative
2+
3+
import android.content.Context
4+
import android.content.ContextWrapper
5+
import android.content.res.AssetManager
6+
import com.facebook.react.bridge.JSBundleLoader
7+
import com.facebook.react.bridge.JSBundleLoaderDelegate
8+
import com.hotcodepush.core.Core
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Test
11+
import org.junit.runner.RunWith
12+
import org.mockito.MockedConstruction
13+
import org.mockito.Mockito.`when`
14+
import org.mockito.Mockito.mock
15+
import org.mockito.Mockito.mockConstruction
16+
import org.mockito.Mockito.verify
17+
import org.mockito.Mockito.verifyNoMoreInteractions
18+
import org.robolectric.RobolectricTestRunner
19+
import org.robolectric.RuntimeEnvironment
20+
import org.robolectric.annotation.Config
21+
import java.io.File
22+
23+
/**
24+
* The bundle React Native loads, as its host asks the runtime at the start and at every reload, over a core whose start answers
25+
* the bundle its store names. A downloaded bundle is laid out and persisted through the SDK's own loader, where the core puts it.
26+
*/
27+
@RunWith(RobolectricTestRunner::class)
28+
@Config(sdk = [35])
29+
class HotCodePushRuntimeTest {
30+
private val context = contextWithResourceFile()
31+
private val runtime = HotCodePushRuntime(context)
32+
private val bundleLoader = ReactNativeBundleLoader(context, File(context.filesDir, "hotcodepush")) {}
33+
private val embeddedBundleLoader = JSBundleLoader.createAssetLoader(context, EMBEDDED_BUNDLE_URL, true)
34+
35+
@Test
36+
fun shouldServeTheBundleFileWhenTheCoresStoreNamesABundle() {
37+
val bundlePath = layOutBundle("b1")
38+
mockCoreWhoseStartAnswers("b1").use {
39+
assertEquals(bundlePath, askForBundle())
40+
}
41+
}
42+
43+
@Test
44+
fun shouldServeTheEmbeddedBundleWhenTheCoresStoreNamesNone() {
45+
mockCoreWhoseStartAnswers(null).use {
46+
assertEquals(EMBEDDED_BUNDLE_URL, askForBundle())
47+
}
48+
}
49+
50+
@Test
51+
fun shouldServeTheRunningBundleAndTellTheCoreNothingWhenTheHostReloadsWithoutTheSdkAskingForIt() {
52+
val bundlePath = layOutBundle("b1")
53+
mockCoreWhoseStartAnswers("b1").use { cores ->
54+
askForBundle()
55+
downloadForNextStart("b2")
56+
57+
assertEquals(bundlePath, askForBundle())
58+
val core = cores.constructed().single()
59+
verify(core).handleAppStartBlocking(false)
60+
verifyNoMoreInteractions(core)
61+
}
62+
}
63+
64+
/** The core the runtime creates, whose start answers the bundle its store names; the start is in the foreground, as Robolectric's process is. */
65+
private fun mockCoreWhoseStartAnswers(bundleId: String?): MockedConstruction<Core> =
66+
mockConstruction(Core::class.java) { core, _ -> `when`(core.handleAppStartBlocking(false)).thenReturn(bundleId) }
67+
68+
/** React Native asks its host for the bundle and loads what the answer points at: the path it loads. */
69+
private fun askForBundle(): String = runtime.resolveBundleLoader(embeddedBundleLoader).loadScript(mock(JSBundleLoaderDelegate::class.java))
70+
71+
/** A downloaded bundle's JavaScript where the core's download lays it out: answers the file's path. */
72+
private fun layOutBundle(bundleId: String): String {
73+
val bundleDirectory = bundleLoader.projectionDirectory(bundleId).also { it.mkdirs() }
74+
return File(bundleDirectory, ReactNativeBundleLoader.BUNDLE_FILE_NAME).also { it.writeText("") }.path
75+
}
76+
77+
/** A release downloaded for the next start, laid out and persisted as the core does through the loader. */
78+
private fun downloadForNextStart(bundleId: String) {
79+
layOutBundle(bundleId)
80+
bundleLoader.persistServedBundle(bundleId)
81+
}
82+
83+
private fun contextWithResourceFile(): Context {
84+
val resourceAssets = mock(AssetManager::class.java)
85+
`when`(resourceAssets.open("hotcodepush.json")).thenAnswer { RESOURCE_FILE.byteInputStream() }
86+
return object : ContextWrapper(RuntimeEnvironment.getApplication()) {
87+
override fun getAssets(): AssetManager = resourceAssets
88+
}
89+
}
90+
91+
companion object {
92+
private const val EMBEDDED_BUNDLE_URL = "assets://index.android.bundle"
93+
private const val RESOURCE_FILE = """{"appId":"0b6d5c1e-2f3a-4b5c-8d9e-0f1a2b3c4d5e","channelId":null,"builtAt":"2026-10-08T00:00:00.000Z","embeddedBundleManifest":null}"""
94+
}
95+
}

0 commit comments

Comments
 (0)