Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ suspend fun <T, R> requestRouteAPI(
): R? {
val response =
try {
val preferences = MixinApplication.appContext.defaultSharedPreferences
if (preferences.getString(PREF_ROUTE_BOT_PK, null).isNullOrBlank()) {
val sessionResponse = requestSession(listOf(ROUTE_BOT_USER_ID))
if (!sessionResponse.isSuccess) {
defaultErrorHandle(MixinResponse<T>(requireNotNull(sessionResponse.error)))
endBlock?.invoke()
return null
Comment on lines +39 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route bootstrap errors through caller failure handling

When the key is absent after a fresh install or account switch and fetchSessionsSuspend returns an API error, this branch calls only defaultErrorHandle and returns without notifying failureBlock or exceptionBlock. For example, FetchWalletViewModel.importWallet enters IMPORTING at line 524 and only transitions to IMPORT_ERROR in those callbacks at lines 612–623; because it supplies no endBlock, the import UI remains stuck loading after this failure. Provide a dedicated session-sync failure callback or otherwise notify callers without treating a session 404 as a wallet 404.

Useful? React with 👍 / 👎.

}
val publicKey = sessionResponse.data?.firstOrNull { it.userId == ROUTE_BOT_USER_ID }?.publicKey
check(!publicKey.isNullOrBlank()) { "Route bot public key is missing" }
preferences.putString(PREF_ROUTE_BOT_PK, publicKey)
Comment on lines +43 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Persist the prefetched bot session in the participant database

This stores the newly fetched key only in preferences, unlike the existing authentication-retry path, which also updates ParticipantSession. After switching back to an account, preferences are cleared while that account's database is retained; if the database contains a pre-rotation route key, this preflight can fetch the current key but leave the stale database row intact. A later UserRepository.getBotPublicKey(..., false) reads that row and overwrites the current preference, and RefreshDappJob does exactly that before its direct route request; its 401 branch refreshes the key but does not retry the failed dapp fetch. Save the returned session alongside the preference so the database cannot restore a superseded key.

Useful? React with 👍 / 👎.

}
invokeNetwork()
} catch (t: Throwable) {
if (exceptionBlock?.invoke(t) != true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package one.mixin.android.ui.common

import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.runBlocking
import one.mixin.android.Constants.Account.PREF_ROUTE_BOT_PK
import one.mixin.android.MixinApplication
import one.mixin.android.api.MixinResponse
import one.mixin.android.api.ResponseError
import one.mixin.android.extension.defaultSharedPreferences
import one.mixin.android.ui.wallet.fiatmoney.requestRouteAPI
import one.mixin.android.util.ErrorHandler
import one.mixin.android.vo.WalletCategory
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class LoginRecoveryPolicyTest {
@Test
fun missingWalletIsDeletedAndItsErrorIsHandledDuringLogin() = runBlocking {
MixinApplication.appContext = ApplicationProvider.getApplicationContext()
MixinApplication.appContext.defaultSharedPreferences.edit().putString(PREF_ROUTE_BOT_PK, "cached-key").commit()
val wallets = mutableListOf("missing", "active")
var defaultErrorHandled = false
val result = requestRouteAPI<Unit, Boolean>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package one.mixin.android.ui.wallet.fiatmoney

import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.runBlocking
import one.mixin.android.Constants.Account.PREF_ROUTE_BOT_PK
import one.mixin.android.Constants.RouteConfig.ROUTE_BOT_USER_ID
import one.mixin.android.MixinApplication
import one.mixin.android.api.MixinResponse
import one.mixin.android.api.ResponseError
import one.mixin.android.api.response.UserSession
import one.mixin.android.extension.defaultSharedPreferences
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class RouteAPITest {
@Test
fun waitsForPublicKeyAndStopsWhenSynchronizationFails() = runBlocking {
MixinApplication.appContext = ApplicationProvider.getApplicationContext()
val preferences = MixinApplication.appContext.defaultSharedPreferences
val events = mutableListOf<String>()
for (mode in listOf("missing", "cached", "failed", "empty")) {
preferences.edit().remove(PREF_ROUTE_BOT_PK).commit()
if (mode == "cached") preferences.edit().putString(PREF_ROUTE_BOT_PK, "key").commit()
events.clear()
val result = requestRouteAPI<Unit, Boolean>(
invokeNetwork = {
assertEquals("key", preferences.getString(PREF_ROUTE_BOT_PK, null))
events.add("request")
MixinResponse<Unit>()
},
successBlock = { true },
failureBlock = { error("Session failure must not invoke the wallet failure handler") },
defaultErrorHandle = { events.add("error") },
defaultExceptionHandle = { events.add("exception") },
endBlock = { events.add("end") },
requestSession = { ids ->
assertEquals(listOf(ROUTE_BOT_USER_ID), ids)
events.add("sync")
when (mode) {
"cached" -> error("Cached public key must be reused")
"failed" -> MixinResponse(ResponseError(404, 404, "Not found"))
else -> MixinResponse<List<UserSession>>().apply {
data = listOf(UserSession(ROUTE_BOT_USER_ID, "session", null, if (mode == "empty") "" else "key"))
}
}
},
)
when (mode) {
"missing" -> assertEquals(listOf("sync", "request", "end"), events)
"cached" -> assertEquals(listOf("request", "end"), events)
"failed" -> assertEquals(listOf("sync", "error", "end"), events)
"empty" -> assertEquals(listOf("sync", "exception", "end"), events)
}
if (mode == "failed" || mode == "empty") assertNull(result) else assertEquals(true, result)
}
}
}
Loading