fix: an unproven update-channel guess no longer moves stable users to Nightly (#1067)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-27 22:27:20 -04:00
commit 3716c8acdf
2 files changed
+90 -11

No files matched your search

@@ -200,18 +200,31 @@ object AppUpdates {
fun message(context: Context): String = context.getString(text, *args)
}
fun follow(context: Context, catalog: Catalog?): Follow {
fun follow(context: Context, catalog: Catalog?): Follow = follow(context, catalog, installed())
internal fun follow(context: Context, catalog: Catalog?, me: Installed): Follow {
stored(context)?.let { return it }
val me = installed()
val inferred = when {
me.pr != 0 -> Follow(Channel.TEST, me.pr)
catalog == null -> return Follow(Channel.STABLE)
catalog.stable != null && isRunning(catalog.stable, me) -> Follow(Channel.STABLE)
me.updatable -> Follow(Channel.NIGHTLY)
else -> Follow(Channel.STABLE)
}
setFollow(context, inferred)
return inferred
val guess = guessFollow(catalog, me)
if (guess.certain) setFollow(context, guess.follow)
return guess.follow
}
internal class Guess(val follow: Follow, val certain: Boolean)
/**
* Stable and Preview builds of one commit are the same APK. Preview history can include older Stable releases,
* so an overlapping older-build guess keeps the Stable default for this run without persisting it.
*/
internal fun guessFollow(catalog: Catalog?, me: Installed): Guess {
if (me.pr != 0) return Guess(Follow(Channel.TEST, me.pr), true)
val stable = catalog?.stable
if (stable != null && isRunning(stable, me)) return Guess(Follow(Channel.STABLE), true)
val publishedPreview = catalog != null && me.updatable && (
catalog.preview?.let { isRunning(it, me) } == true ||
catalog.recentPreviews.any { matchesInstalledCommit(it.commit, me.commit) }
)
if (stable?.version?.let { compareVersions(me.version, it) < 0 } == true) return Guess(Follow(Channel.STABLE), !publishedPreview)
return Guess(Follow(if (publishedPreview) Channel.NIGHTLY else Channel.STABLE), false)
}
fun setFollow(context: Context, follow: Follow) {
@@ -8,6 +8,7 @@ import com.droiddeck.launcher.update.AppUpdates.Offer
import com.droiddeck.launcher.update.AppUpdates.PreviewChange
import com.droiddeck.launcher.update.AppUpdates.PreviewHistoryKind
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.json.JSONArray
@@ -285,4 +286,69 @@ class AppUpdatesTest {
} catch (_: IOException) {
}
}
@Test fun anOlderStableIsGuessedStableAndSaved() {
val g = AppUpdates.guessFollow(catalog, me("eeeeeee999", version = "0.1.9"))
assertEquals(AppUpdates.Channel.STABLE, g.follow.channel)
assertTrue(g.certain)
}
@Test fun anOlderBuildInPreviewHistoryKeepsStableAsAnUncertainGuess() {
val withHistory = AppUpdates.Catalog(stable, release("1111111"), emptyList(), 0L, recentPreview)
val g = AppUpdates.guessFollow(withHistory, me("2".repeat(40), version = "0.1.0"))
assertEquals(Channel.STABLE, g.follow.channel)
assertFalse(g.certain)
}
@Test fun anOlderCataloguedBuildDoesNotPersistAGuessAndExplicitChoicesStillWin() {
val context = org.robolectric.RuntimeEnvironment.getApplication()
val preferences = context.getSharedPreferences("app_updates", android.content.Context.MODE_PRIVATE)
preferences.edit().clear().commit()
try {
val installed = me("2".repeat(40), version = "0.1.0")
val newerStable = release("0".repeat(40), version = "9999.0.0")
val currentPreview = release(installed.commit)
val newerCatalog = AppUpdates.Catalog(newerStable, currentPreview, emptyList(), 0L)
assertEquals(Channel.STABLE, AppUpdates.follow(context, newerCatalog, installed).channel)
assertNull(preferences.getString("channel", null))
assertEquals(Channel.STABLE, AppUpdates.follow(context, null, installed).channel)
assertNull(preferences.getString("channel", null))
for (choice in listOf(Follow(Channel.STABLE), Follow(Channel.NIGHTLY), Follow(Channel.TEST, 93))) {
AppUpdates.setFollow(context, choice)
val actual = AppUpdates.follow(context, newerCatalog, installed)
assertEquals(choice.channel, actual.channel)
assertEquals(choice.pr, actual.pr)
}
} finally {
preferences.edit().clear().commit()
}
}
@Test fun runningTheCatalogStableIsCertainStable() {
val g = AppUpdates.guessFollow(catalog, me(stable.commit))
assertEquals(AppUpdates.Channel.STABLE, g.follow.channel)
assertTrue(g.certain)
}
@Test fun aPublishedPreviewPastStableIsGuessedNightlyWithoutSaving() {
val withHistory = AppUpdates.Catalog(stable, release("1111111"), emptyList(), 0L, recentPreview)
val g = AppUpdates.guessFollow(withHistory, me("2".repeat(40), version = "0.3.0"))
assertEquals(AppUpdates.Channel.NIGHTLY, g.follow.channel)
assertFalse(g.certain)
}
@Test fun anUncataloguedNewerBuildStaysStableForNow() {
val g = AppUpdates.guessFollow(catalog, me("9".repeat(40), version = "0.3.0"))
assertEquals(AppUpdates.Channel.STABLE, g.follow.channel)
assertFalse(g.certain)
val offline = AppUpdates.guessFollow(null, me("9".repeat(40), version = "0.3.0"))
assertEquals(AppUpdates.Channel.STABLE, offline.follow.channel)
assertFalse(offline.certain)
}
@Test fun aPullRequestBuildIsCertainTest() {
val g = AppUpdates.guessFollow(catalog, me("ccccccc2222222222222222222222222222222", pr = 93))
assertEquals(AppUpdates.Channel.TEST, g.follow.channel)
assertTrue(g.certain)
}
}