astronomy: injectable asset loader for CatalogRepository
- Commit
59cb25a3d31204e74d14def3aecafbda85ce9646- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 14:24 UTC (29 days ago)
- Parents
66e0cee
Behaviour-preserving testability seam: CatalogRepository gains an assetLoader constructor parameter whose default reads the asset from the context exactly as before. Tests can now feed missing, corrupt or hand-written catalog JSON and exercise the built-in fallbacks without touching the bundled assets. (cherry picked from commit 9b5ca240b3943ee1cbd4cb256ee2208b37155638)
Diff
astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.ktindex 10df44d9e0..b52df08047 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/data/CatalogRepository.kt@@ -15,7 +15,17 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlinx.serialization.json.Json -class CatalogRepository(private val context: Context) {+class CatalogRepository(+ private val context: Context,+ /** Reads one bundled asset as text, or null when it cannot be read. Injectable for tests. */+ private val assetLoader: (String) -> String? = { path ->+ try {+ context.assets.open(path).bufferedReader().use { it.readText() }+ } catch (_: Exception) {+ null+ }+ },+) { private val json = Json { ignoreUnknownKeys = true; isLenient = true } var stars: List<Star> = emptyList(); private set var constellations: List<Constellation> = emptyList(); private set@@ -37,12 +47,7 @@ class CatalogRepository(private val context: Context) { loaded = true } - private fun loadJsonAsset(path: String): String? =- try {- context.assets.open(path).bufferedReader().use { it.readText() }- } catch (_: Exception) {- null- }+ private fun loadJsonAsset(path: String): String? = assetLoader(path) private fun loadStars(): List<Star> { val text = loadJsonAsset("catalog/stars.json") ?: return BuiltInCatalogs.stars