musicbrainz: fix compile error
- Commit
ba3301f194992911b52f2225f28439d68ca75291- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-21 03:52 UTC (3 months ago)
- Parents
5190a9e
Diff
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.ktindex 6348a5a50c..6965477ad3 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt@@ -88,12 +88,6 @@ data class SearchUiState( val tab: SearchTab = SearchTab.Releases, val loading: Boolean = false, val error: String? = null,- /** The catalogue is still importing, so this is a wait rather than a fault. */- val notReady: Boolean = false,- /** The server's reason, when a missing catalogue is not simply on its way. */- val notReadyReason: String? = null,- /** Whether asking again could ever produce a catalogue. The server decides this. */- val notReadyRetryable: Boolean = true, val hasSearched: Boolean = false, val artists: List<ArtistRow> = emptyList(), val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -103,12 +97,6 @@ data class SearchUiState( data class ArtistUiState( val loading: Boolean = true, val error: String? = null,- /** The catalogue is still importing, so this is a wait rather than a fault. */- val notReady: Boolean = false,- /** The server's reason, when a missing catalogue is not simply on its way. */- val notReadyReason: String? = null,- /** Whether asking again could ever produce a catalogue. The server decides this. */- val notReadyRetryable: Boolean = true, val name: String = "", val subtitle: String? = null, val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -117,12 +105,6 @@ data class ArtistUiState( data class ReleaseGroupUiState( val loading: Boolean = true, val error: String? = null,- /** The catalogue is still importing, so this is a wait rather than a fault. */- val notReady: Boolean = false,- /** The server's reason, when a missing catalogue is not simply on its way. */- val notReadyReason: String? = null,- /** Whether asking again could ever produce a catalogue. The server decides this. */- val notReadyRetryable: Boolean = true, val title: String = "", val artist: String = "", val coverUrl: String? = null,@@ -132,12 +114,6 @@ data class ReleaseGroupUiState( data class ReleaseUiState( val loading: Boolean = true, val error: String? = null,- /** The catalogue is still importing, so this is a wait rather than a fault. */- val notReady: Boolean = false,- /** The server's reason, when a missing catalogue is not simply on its way. */- val notReadyReason: String? = null,- /** Whether asking again could ever produce a catalogue. The server decides this. */- val notReadyRetryable: Boolean = true, val id: String = "", val title: String = "", val artist: String = "",
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.ktindex 60f445d203..e76cb552a7 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt@@ -10,7 +10,6 @@ import com.vayunmathur.musicbrainz.data.library.LibraryScanner import com.vayunmathur.musicbrainz.data.library.LibrarySnapshot import com.vayunmathur.musicbrainz.data.tidal.TidalAuth import com.vayunmathur.musicbrainz.data.tidal.TidalPollResult-import com.vayunmathur.musicbrainz.network.api.CatalogueNotReadyException import com.vayunmathur.musicbrainz.network.api.CoverArt import com.vayunmathur.musicbrainz.network.api.MbRecording import com.vayunmathur.musicbrainz.network.api.MbRelease@@ -143,9 +142,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat _search.value = _search.value.copy( loading = true, error = null,- notReady = false,- notReadyReason = null,- notReadyRetryable = true, hasSearched = true, ) try {@@ -186,9 +182,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat _search.value = _search.value.copy( loading = false, error = e.readableMessage(),- notReady = e is CatalogueNotReadyException,- notReadyReason = e.notReadyReason(),- notReadyRetryable = e.notReadyRetryable(), ) } }@@ -240,9 +233,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat _artist.value = _artist.value.copy( loading = false, error = e.readableMessage(),- notReady = e is CatalogueNotReadyException,- notReadyReason = e.notReadyReason(),- notReadyRetryable = e.notReadyRetryable(), ) } }@@ -299,9 +289,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat _releaseGroup.value = ReleaseGroupUiState( loading = false, error = e.readableMessage(),- notReady = e is CatalogueNotReadyException,- notReadyReason = e.notReadyReason(),- notReadyRetryable = e.notReadyRetryable(), ) } }@@ -323,9 +310,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat _release.value = ReleaseUiState( loading = false, error = e.readableMessage(),- notReady = e is CatalogueNotReadyException,- notReadyReason = e.notReadyReason(),- notReadyRetryable = e.notReadyRetryable(), ) } }@@ -595,17 +579,6 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat title = row.title, ) - /**- * The server's explanation for a catalogue that is not merely on its way, so the screen can- * say why instead of promising it will be along shortly.- */- private fun Exception.notReadyReason(): String? =- (this as? CatalogueNotReadyException)?.reason-- /** The server's word on whether retrying can help; assume it can when it did not say. */- private fun Exception.notReadyRetryable(): Boolean =- (this as? CatalogueNotReadyException)?.retryable ?: true- private fun Exception.readableMessage(): String = message?.takeIf { it.isNotBlank() }?.take(200) ?: "Something went wrong"
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.ktindex 8c700beac7..784303119b 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt@@ -16,6 +16,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.EmptyState+import com.vayunmathur.library.ui.ErrorState import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.LoadingState import com.vayunmathur.library.ui.MaterialTheme@@ -24,7 +25,6 @@ import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.musicbrainz.R import com.vayunmathur.musicbrainz.Route import com.vayunmathur.musicbrainz.ui.components.CoverArtImage-import com.vayunmathur.musicbrainz.ui.components.LoadFailureState import com.vayunmathur.musicbrainz.ui.components.SecondaryText import com.vayunmathur.musicbrainz.platform.ArtistUiState import com.vayunmathur.musicbrainz.platform.MusicBrainzActions@@ -50,12 +50,10 @@ fun ArtistScreen( ) { padding -> when { state.loading -> LoadingState(Modifier.fillMaxSize().padding(padding))- state.error != null -> LoadFailureState(- error = state.error,- notReady = state.notReady,- notReadyReason = state.notReadyReason,- notReadyRetryable = state.notReadyRetryable,+ state.error != null -> ErrorState(+ title = stringResource(R.string.load_failed), modifier = Modifier.fillMaxSize().padding(padding),+ message = state.error, ) state.releaseGroups.isEmpty() -> EmptyState( title = stringResource(R.string.no_releases),
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.ktindex a22d70f2c4..8976f3d397 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt@@ -17,6 +17,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.ErrorState import com.vayunmathur.library.ui.IconCheckCircle import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.LoadingState@@ -26,7 +27,6 @@ import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.musicbrainz.R import com.vayunmathur.musicbrainz.Route import com.vayunmathur.musicbrainz.ui.components.CoverArtImage-import com.vayunmathur.musicbrainz.ui.components.LoadFailureState import com.vayunmathur.musicbrainz.ui.components.SecondaryText import com.vayunmathur.musicbrainz.platform.MusicBrainzViewModel import com.vayunmathur.musicbrainz.platform.ReleaseGroupUiState@@ -61,12 +61,10 @@ fun ReleaseGroupScreen( ) { padding -> when { state.loading -> LoadingState(Modifier.fillMaxSize().padding(padding))- state.error != null -> LoadFailureState(- error = state.error,- notReady = state.notReady,- notReadyReason = state.notReadyReason,- notReadyRetryable = state.notReadyRetryable,+ state.error != null -> ErrorState(+ title = stringResource(R.string.load_failed), modifier = Modifier.fillMaxSize().padding(padding),+ message = state.error, ) else -> LazyColumn( Modifier.fillMaxSize(),
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.ktindex bfff9d63ab..105e6f7352 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt@@ -16,6 +16,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vayunmathur.library.ui.AppScaffold+import com.vayunmathur.library.ui.ErrorState import com.vayunmathur.library.ui.ExtendedFloatingActionButton import com.vayunmathur.library.ui.IconDownload import com.vayunmathur.library.ui.ListItem@@ -26,7 +27,6 @@ import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.musicbrainz.R import com.vayunmathur.musicbrainz.Route import com.vayunmathur.musicbrainz.ui.components.CoverArtImage-import com.vayunmathur.musicbrainz.ui.components.LoadFailureState import com.vayunmathur.musicbrainz.ui.components.durationLabel import com.vayunmathur.musicbrainz.ui.components.TrackTrailing import com.vayunmathur.musicbrainz.ui.components.SecondaryText@@ -71,12 +71,10 @@ fun ReleaseScreen( ) { padding -> when { state.loading -> LoadingState(Modifier.fillMaxSize().padding(padding))- state.error != null -> LoadFailureState(- error = state.error,- notReady = state.notReady,- notReadyReason = state.notReadyReason,- notReadyRetryable = state.notReadyRetryable,+ state.error != null -> ErrorState(+ title = stringResource(R.string.load_failed), modifier = Modifier.fillMaxSize().padding(padding),+ message = state.error, ) else -> LazyColumn( Modifier.fillMaxSize(),
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.ktindex 5d3e82b3d7..66db92cdc5 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt@@ -16,6 +16,7 @@ import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.EmptyState+import com.vayunmathur.library.ui.ErrorState import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.IconClose import com.vayunmathur.library.ui.IconDownload@@ -33,7 +34,6 @@ import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.musicbrainz.R import com.vayunmathur.musicbrainz.Route import com.vayunmathur.musicbrainz.ui.components.CoverArtImage-import com.vayunmathur.musicbrainz.ui.components.LoadFailureState import com.vayunmathur.musicbrainz.ui.components.durationLabel import com.vayunmathur.musicbrainz.ui.components.TrackTrailing import com.vayunmathur.musicbrainz.ui.components.SecondaryText@@ -98,13 +98,10 @@ fun SearchScreen( } when { state.loading -> LoadingState(Modifier.fillMaxSize())- state.error != null -> LoadFailureState(- error = state.error,- notReady = state.notReady,- notReadyReason = state.notReadyReason,- notReadyRetryable = state.notReadyRetryable,- modifier = Modifier.fillMaxSize(),+ state.error != null -> ErrorState( title = stringResource(R.string.search_failed),+ modifier = Modifier.fillMaxSize(),+ message = state.error, retryLabel = stringResource(R.string.retry), onRetry = actions::search, )
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.ktdeleted file mode 100644index bfb47ce74c..0000000000--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt+++ /dev/null@@ -1,96 +0,0 @@-package com.vayunmathur.musicbrainz.ui.components--import androidx.compose.runtime.Composable-import androidx.compose.ui.Modifier-import androidx.compose.ui.res.stringResource-import com.vayunmathur.library.ui.Button-import com.vayunmathur.library.ui.EmptyState-import com.vayunmathur.library.ui.ErrorState-import com.vayunmathur.library.ui.Text-import com.vayunmathur.musicbrainz.R--/**- * The copy a not-ready screen uses, chosen solely by whether waiting could change the answer.- *- * Pulled out of the composable so the rule that matters can be asserted rather than reviewed:- * a state that waiting cannot fix must never say "try again" and must never offer a button.- */-internal data class NotReadyCopy(- val title: Int,- val fallbackMessage: Int,- val showRetry: Boolean,-)--internal fun notReadyCopy(retryable: Boolean): NotReadyCopy = if (retryable) {- NotReadyCopy(- title = R.string.catalogue_not_ready,- fallbackMessage = R.string.catalogue_not_ready_message,- showRetry = true,- )-} else {- NotReadyCopy(- title = R.string.catalogue_unavailable,- fallbackMessage = R.string.catalogue_unavailable_message,- showRetry = false,- )-}--/**- * The terminal state for a page that has nothing to show.- *- * A catalogue that has not finished importing is drawn apart from a failure because it is a- * wait, not a fault: reporting it as an error sends the user hunting for a problem at their- * end, and there is no fallback source to fall back to. Either way this is a settled state- * rather than a spinner, so the screen never sits there implying progress it is not making.- *- * [notReadyRetryable] is the server answering one question - would waiting change the answer?- * It is the ONLY thing the copy branches on, deliberately, because the state cannot answer it:- * a catalogue can be absent and on its way, or absent and never coming.- *- * That honesty is structural rather than a consequence of the server always sending a- * [notReadyReason]: each branch carries its own copy, so a missing reason degrades to a vaguer- * TRUE message instead of a confident false one. The reason itself is displayed verbatim and- * never parsed.- */-@Composable-fun LoadFailureState(- error: String?,- notReady: Boolean,- modifier: Modifier = Modifier,- notReadyReason: String? = null,- notReadyRetryable: Boolean = true,- title: String = stringResource(R.string.load_failed),- retryLabel: String? = null,- onRetry: (() -> Unit)? = null,-) {- if (notReady) {- val copy = notReadyCopy(notReadyRetryable)- val reason = notReadyReason?.takeIf { it.isNotBlank() }- // When waiting helps, the server's words replace the copy. When it does not, they are- // shown ahead of it: the reason names the shortfall, the copy says where it has to be- // dealt with, and the user cannot infer the second from the first.- val message = when {- reason == null -> stringResource(copy.fallbackMessage)- copy.showRetry -> reason- else -> reason + "\n\n" + stringResource(copy.fallbackMessage)- }- EmptyState(- title = stringResource(copy.title),- modifier = modifier,- message = message,- action = if (copy.showRetry && onRetry != null && retryLabel != null) {- { Button(onClick = onRetry) { Text(retryLabel) } }- } else {- null- },- )- } else {- ErrorState(- title = title,- modifier = modifier,- message = error,- retryLabel = retryLabel,- onRetry = onRetry,- )- }-}
musicbrainz/src/main/res/values/strings.xml
diff --git a/musicbrainz/src/main/res/values/strings.xml b/musicbrainz/src/main/res/values/strings.xmlindex ab34d9d0a7..e83052a3ed 100644--- a/musicbrainz/src/main/res/values/strings.xml+++ b/musicbrainz/src/main/res/values/strings.xml@@ -24,14 +24,6 @@ <string name="search_failed">Search failed</string> <!-- Error title shown when a page could not be loaded. --> <string name="load_failed">Could not load</string>- <!-- Title shown when the server has no music catalogue loaded yet. Not an error: it is still being prepared. -->- <string name="catalogue_not_ready">Catalogue not ready</string>- <!-- Body shown when the server has no music catalogue loaded yet, telling the user it is temporary. -->- <string name="catalogue_not_ready_message">The music catalogue is still being prepared on the server. Browsing will work once it is ready \u2014 try again in a few minutes.</string>- <!-- Title shown when the server cannot build the music catalogue at all, so waiting will not help. -->- <string name="catalogue_unavailable">Catalogue unavailable</string>- <!-- Body shown when the server cannot build the music catalogue. Waiting will not help; the server itself needs attention. -->- <string name="catalogue_unavailable_message">The server cannot build the music catalogue, so browsing is unavailable. Waiting will not help \u2014 this needs attention on the server.</string> <!-- Button: retries a failed request. Verb. --> <string name="retry">Retry</string>
musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt
diff --git a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.ktindex 8e17548e02..1820e8f69c 100644--- a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt+++ b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt@@ -3,250 +3,25 @@ package com.vayunmathur.musicbrainz.network.api import java.io.IOException import kotlin.test.Test import kotlin.test.assertEquals-import kotlin.test.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue /**- * Pins how HTTP statuses from the self-hosted catalogue map onto failures.+ * Pins how HTTP statuses from WS/2 map onto failures. *- * This is the contract with the server written down as assertions: 503 means "no catalogue- * loaded yet", which is a wait, and 500 means a genuine fault. The two never overlap, which is- * what lets the app say "catalogue not ready" instead of "something went wrong". If the server- * ever starts answering 503 for real faults, or 500 while it is still importing, the screens- * quietly start lying to the user - so the split is asserted here rather than trusted.+ * Every non-2xx status is one kind of failure, 503 included: WS/2 answers it when a client+ * outruns the shared rate limit, and there is nothing extra for the user to do about that than+ * for any other fault. */ class MusicBrainzApiErrorTest { @Test- fun `503 is a wait rather than a fault`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","detail":"no catalogue pack on this server"}""",- )- assertTrue(- failure is CatalogueNotReadyException,- "503 must surface as not-ready, got ${failure?.javaClass?.simpleName}",- )- }-- /**- * An absent catalogue is not always on its way - the host may not be able to build one at- * all - so the server's explanation is carried through to replace copy that would otherwise- * promise it is coming shortly.- */- @Test- fun `carries the reason an absent catalogue is not coming`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","detail":"cannot build the catalogue here: 5.2 GB RAM free, a build needs 8.0 GB"}""",- ) as CatalogueNotReadyException- assertEquals(CatalogueNotReadyException.ABSENT, failure.state)- assertEquals(- "cannot build the catalogue here: 5.2 GB RAM free, a build needs 8.0 GB",- failure.reason,- )- }-- /**- * A build in progress genuinely does resolve on its own, so it carries NO reason - the- * screen keeps its own "still being prepared, try again shortly" copy, which is true here- * and would be a false promise for an absent catalogue. `detail` is build-stage jargon- * ("spilling tables") and is not shown to anyone.- */- @Test- fun `does not carry a reason while building`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"building","progress":0.42,"detail":"spilling tables"}""",- ) as CatalogueNotReadyException- assertEquals(CatalogueNotReadyException.BUILDING, failure.state)- assertNull(failure.reason, "a build in progress must not present itself as a dead end")- }-- /** A 503 whose body is not the documented JSON still has to read as not-ready. */- @Test- fun `survives a 503 body that is not the documented json`() {- for (body in listOf("", "Service Unavailable", "<html>502 upstream</html>", "{")) {- val failure = MusicBrainzApi.failureFor(503, body)- assertTrue(failure is CatalogueNotReadyException, "body ${'"'}$body${'"'} should be not-ready")- assertNull(failure.reason, "an unparseable body cannot supply a reason")- assertTrue(failure.retryable, "with nothing to go on, retrying is worth offering")- }- }-- /**- * `absent` is NOT the same as "never". A build that failed and is queued for the next check,- * and a first boot before the scheduler has run, are both absent yet worth retrying - so the- * server reports retryability and the app must not infer it from the state or from the- * presence of a reason.- */- @Test- fun `an absent catalogue that is still coming stays retryable`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","retryable":true,"detail":"a build failed and is queued for the next check"}""",- ) as CatalogueNotReadyException- assertTrue(failure.retryable, "a queued rebuild must keep the retry button")- assertEquals("a build failed and is queued for the next check", failure.reason)- }-- /** The case where a retry can never work, so the button must not be offered. */- @Test- fun `a host that cannot build is not retryable`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","retryable":false,"detail":"cannot build the catalogue here"}""",- ) as CatalogueNotReadyException- assertFalse(failure.retryable)- assertEquals("cannot build the catalogue here", failure.reason)- }-- /**- * If the server ever stops sending `retryable`, fall back to inferring it from the reason- * rather than offering a retry that cannot work: a body with a reason behaves as it did- * before the field existed, and one without stays retryable.- */- @Test- fun `infers retryability when the server does not report it`() {- val withReason = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","detail":"cannot build the catalogue here"}""",- ) as CatalogueNotReadyException- assertFalse(withReason.retryable, "a reason with no retryable field must not offer a retry")-- val building = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"building","progress":0.1}""",- ) as CatalogueNotReadyException- assertTrue(building.retryable)- }-- /**- * `progress` is ABSENT until it is known, not `0.0`. Decoding it as zero would report a build- * that has not started as one that has started and made no headway - and if it were ever- * shown as a percentage, a build about to finish could read 0% on the first poll.- */- @Test- fun `absent progress is unknown rather than zero`() {- val unknown = MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"error":"not_ready","state":"building","retryable":true}""",- )- assertNull(unknown.progress, "a missing progress must not decode as 0.0")-- val known = MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"error":"not_ready","state":"building","retryable":true,"progress":0.42}""",- )- assertEquals(0.42f, known.progress)-- // Zero sent explicitly is a real zero, and must stay distinguishable from absent.- val zero = MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"error":"not_ready","state":"building","retryable":true,"progress":0.0}""",- )- assertEquals(0f, zero.progress)- }-- /** `retryable` is always sent, so it decodes straight through in both directions. */- @Test- fun `retryable decodes from the status probe shape`() {- assertEquals(- true,- MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"state":"absent","retryable":true}""",- ).retryable,- )- assertEquals(- false,- MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"state":"absent","retryable":false}""",- ).retryable,- )- }-- /**- * The server's own pinned wire format, pasted verbatim.- *- * These three strings are asserted byte-for-byte on the server side (`9993b08`), so copying- * them here rather than paraphrasing puts both ends of the contract on the same literals: if- * the server's format drifts its test fails, and if this client's parsing drifts this one- * does. That matters more than usual because production still answers 404, so no integration- * test can catch a mismatch between us yet.- */- @Test- fun `parses the server's pinned bodies verbatim`() {- val builderDisabled = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"absent","retryable":false,"detail":"the offline catalogue builder is not enabled on this server yet"}""",- ) as CatalogueNotReadyException- assertEquals(CatalogueNotReadyException.ABSENT, builderDisabled.state)- assertFalse(builderDisabled.retryable, "a disabled builder cannot be waited out")- assertEquals(- "the offline catalogue builder is not enabled on this server yet",- builderDisabled.reason,- )-- val building = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"building","retryable":true,"progress":0.42,"detail":"spilling tables"}""",- ) as CatalogueNotReadyException- assertEquals(CatalogueNotReadyException.BUILDING, building.state)- assertTrue(building.retryable)- // Build-stage jargon is never shown, so it is deliberately not carried as a reason.- assertNull(building.reason)-- // The COMPLETE status body: no `error`, no `detail`. It has to decode all the same.- val status = MusicBrainzApi.json.decodeFromString<NotReadyBody>(- """{"state":"absent","retryable":true}""",- )- assertEquals("absent", status.state)- assertEquals(true, status.retryable)- assertNull(status.detail)- assertNull(status.progress)- assertEquals("", status.error)- }-- /** Mid-build answers 503 too, and reads the same to the user: come back shortly. */- @Test- fun `503 while building is also a wait`() {- val failure = MusicBrainzApi.failureFor(- 503,- """{"error":"not_ready","state":"building","progress":0.42,"detail":"spilling tables"}""",- )- assertTrue(failure is CatalogueNotReadyException)- }-- /**- * 500 is the server's signal for a real fault and must NOT be dressed up as a wait -- * telling the user to try again in a few minutes would hide a genuine server bug.- */- @Test- fun `500 is a genuine failure`() {- val failure = MusicBrainzApi.failureFor(500, """{"error":"internal"}""")- assertTrue(failure is IOException)- assertTrue(- failure !is CatalogueNotReadyException,- "500 must not be reported to the user as a catalogue that is still importing",- )- }-- /**- * A 404 means the route is not there at all, which is a deployment fault rather than a- * catalogue still importing. Reporting it as not-ready would reassure the user while- * hiding that the server was never rolled out.- */- @Test- fun `404 is a genuine failure, not a missing catalogue`() {- val failure = MusicBrainzApi.failureFor(404, "Not Found")- assertTrue(failure is IOException)- assertTrue(failure !is CatalogueNotReadyException)- }-- @Test- fun `other error statuses are genuine failures`() {- for (status in listOf(400, 401, 403, 429, 502, 504)) {- val failure = MusicBrainzApi.failureFor(status, "boom")- assertTrue(failure is IOException, "$status should fail")- assertTrue(failure !is CatalogueNotReadyException, "$status must not read as not-ready")+ fun `error statuses are failures`() {+ for (status in listOf(400, 401, 403, 404, 429, 500, 502, 503, 504)) {+ assertTrue(+ MusicBrainzApi.failureFor(status, "boom") is IOException,+ "$status should fail",+ ) } }
musicbrainz/src/test/java/com/vayunmathur/musicbrainz/ui/components/NotReadyCopyTest.kt
diff --git a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/ui/components/NotReadyCopyTest.kt b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/ui/components/NotReadyCopyTest.ktdeleted file mode 100644index 37a68bdafb..0000000000--- a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/ui/components/NotReadyCopyTest.kt+++ /dev/null@@ -1,70 +0,0 @@-package com.vayunmathur.musicbrainz.ui.components--import com.vayunmathur.musicbrainz.R-import kotlin.test.Test-import kotlin.test.assertEquals-import kotlin.test.assertFalse-import kotlin.test.assertNotEquals-import kotlin.test.assertTrue--/**- * Pins the one rule the not-ready screen must never break.- *- * The user's server has 5-7 GB free and a build currently needs 8 GB, so "the catalogue can never- * be built here" is the REAL steady state on that hardware, not a corner case. Telling that user- * to try again in a few minutes, forever, behind a button that cannot succeed, is the same- * dishonesty as an endless spinner. So the copy is chosen only by whether waiting could change- * the answer, and that choice is asserted here rather than left to review.- */-class NotReadyCopyTest {-- @Test- fun `waiting helps, so the wait copy and a retry are offered`() {- val copy = notReadyCopy(retryable = true)- assertEquals(R.string.catalogue_not_ready, copy.title)- assertEquals(R.string.catalogue_not_ready_message, copy.fallbackMessage)- assertTrue(copy.showRetry)- }-- /**- * The case that matters. No retry button, because pressing it can never work, and copy that- * does not promise the catalogue is coming.- */- @Test- fun `waiting cannot help, so no retry is offered`() {- val copy = notReadyCopy(retryable = false)- assertFalse(copy.showRetry, "a retry that cannot succeed must not be offered")- assertEquals(R.string.catalogue_unavailable, copy.title)- assertEquals(R.string.catalogue_unavailable_message, copy.fallbackMessage)- }-- /**- * The two branches must not share copy. If they ever collapse onto the same strings, the- * non-retryable case silently starts telling the user to try again shortly - which is the- * defect this whole split exists to prevent, and it would not fail any other test.- */- @Test- fun `the two branches never share copy`() {- val waiting = notReadyCopy(retryable = true)- val terminal = notReadyCopy(retryable = false)- assertNotEquals(waiting.title, terminal.title)- assertNotEquals(- waiting.fallbackMessage,- terminal.fallbackMessage,- "the terminal state must not reuse the 'try again in a few minutes' copy",- )- assertNotEquals(waiting.showRetry, terminal.showRetry)- }-- /**- * Retryability is the ONLY input. Nothing about the reason text, the state name or progress- * may influence it - the server has already decided, and second-guessing it here is how the- * two questions got conflated in the first place.- */- @Test- fun `retryability is the only input`() {- assertEquals(notReadyCopy(retryable = true), notReadyCopy(retryable = true))- assertEquals(notReadyCopy(retryable = false), notReadyCopy(retryable = false))- assertNotEquals(notReadyCopy(retryable = true), notReadyCopy(retryable = false))- }-}