test(connect): phase 5 — restart lands on the selected profile, no login (#827)
* test(connect): pin restart-with-scoped-profile token resolution (phase 5) * fix(profiles): scope switch via un-clamped active-profile state The switch persisted the server profile through serverStore.selectedProfileId, whose selfHealed() clamps ids absent from the local connection list — server profiles like "meow" never exist there, so the scope silently reset to default and the re-dialed socket created the fresh session in the wrong profile (live logcat 2026-08-06: session.create carried profile=default right after switching to meow). - AuthManager: prefs-backed activeProfileId state, immune to the clamp, restored on startup; resetAuthStateForTest clears it - ProfileScopeInterceptor + WsProfileParams + ChatViewModel fetchContextUsage: read the active profile instead of the connection selection - ProfileSwitchCoordinator: write activeProfileId; dial the socket on Dispatchers.IO (the ticket mint did blocking I/O on the main thread — NetworkOnMainThreadException — and fell back to a 1s reconnect) - ProfilesViewModel: inject the IO dispatcher; ProfilesViewModelTest drops its static Dispatchers mock (JVM-wide bleed class) - Tests: WsProfileParams/Interceptor tests use the real prefs-backed state instead of mockkObject(AuthManager) (re-mock breaks in suite order after real AuthManager init); AuthManagerTest + Issue647ProfileUrlTest cancel the init() DataStore scope in tearDown (UncaughtExceptionsBeforeTest phantoms); regression test pins the clamp on the old path vs the new state * fix(profiles): resolve getter/property JVM collision; de-poison slash test - AuthManager.activeProfileId (StateFlow) and a getActiveProfileId() function generated the same JVM getter name — calls silently resolved to the property getter at runtime (ReadonlyStateFlow -> String ClassCastException, CI unit red 2026-08-06). Function removed; callers read activeProfileId.value. - ChatViewModel: inject ioDispatcher (38 sites) so SlashCommandDispatchRpcTest can drop its mockkStatic(Dispatchers) bleed without racing real IO. - SlashCommandDispatchRpcTest: drop the static Dispatchers mock (JVM-wide bleed; CI cross-wired COMMAND_DISPATCH captures in suite order), construct the VM with the test dispatcher. --------- Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
1 parent
dd8dabecb0
commit
d9f437939a
14 files changed
+225
-73
No files matched your search
@@ -64,10 +64,11 @@ class SlashCommandDispatchRpcTest {
|
||||
every { android.util.Log.e(any(), any(), any()) } returns 0
|
||||
|
||||
// NOTE: no mockkStatic(Dispatchers) here — a static Dispatchers mock
|
||||
// bleeds JVM-wide and breaks later classes (CI 2026-08-06: this
|
||||
// class's own static IO mock cross-wired COMMAND_DISPATCH captures in
|
||||
// suite order). setMain alone is safe — the slash-dispatch send path
|
||||
// is synchronous (no IO hop).
|
||||
// bleeds JVM-wide and breaks later classes (the same poison removed
|
||||
// from ProfileSwitchCoordinatorTest / ProfilesViewModelTest; CI
|
||||
// 2026-08-06: this class's own static IO mock cross-wired
|
||||
// COMMAND_DISPATCH captures in suite order). setMain alone is safe —
|
||||
// the slash-dispatch send path is synchronous (no IO hop).
|
||||
mockkObject(AuthManager)
|
||||
mockkObject(HermesWsClient)
|
||||
mockkObject(ApiClient)
|
||||
|
||||
@@ -414,6 +414,32 @@ class ConnectViewModelTest {
|
||||
assertEquals("https://127.0.0.1:9119/", state.baseUrl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLoadSavedValues_restartWithScopedProfile_resolvesTokenViaFallback() {
|
||||
val meow =
|
||||
ConnectionProfile(
|
||||
id = "meow",
|
||||
name = "meow",
|
||||
baseUrl = "https://127.0.0.1:9119/",
|
||||
)
|
||||
// Restart seam (phase 1): the app was killed while meow was selected;
|
||||
// only the default profile holds the connection token. The per-server
|
||||
// token fallback resolves it, so the restart lands on meow WITHOUT a
|
||||
// login screen — desktop-equivalent "kill + reopen → still meow".
|
||||
every { AuthManager.getToken() } returns "conn-token"
|
||||
every { AuthManager.getBaseUrl() } returns "https://127.0.0.1:9119/"
|
||||
every { AuthManager.getConnectionProfiles() } returns listOf(meow)
|
||||
every { AuthManager.getSelectedProfileId() } returns "meow"
|
||||
|
||||
val viewModel = ConnectViewModel(mockApp)
|
||||
val state = viewModel.uiState.value
|
||||
|
||||
assertEquals("conn-token", state.token)
|
||||
assertEquals("meow", state.profileName)
|
||||
assertEquals(meow, state.selectedProfile)
|
||||
assertEquals("https://127.0.0.1:9119/", state.baseUrl)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSelectProfile_withoutToken_usesEmptyString() {
|
||||
every { AuthManager.getProfileToken(any()) } returns null
|
||||
|
||||
@@ -22,7 +22,6 @@ import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
@@ -65,12 +64,11 @@ class ProfilesViewModelTest {
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
// NOTE: no mockkStatic(Dispatchers) here — a static Dispatchers mock
|
||||
// bleeds into later test classes in the same JVM (see the same comment
|
||||
// in ProfileSwitchCoordinatorTest). setMain alone is safe: it only
|
||||
// affects Dispatchers.Main and is undone by resetMain in tearDown.
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
val testMainDispatcher = Dispatchers.Main
|
||||
|
||||
mockkStatic(Dispatchers::class)
|
||||
every { Dispatchers.IO } returns testDispatcher
|
||||
every { Dispatchers.Main } returns testMainDispatcher
|
||||
|
||||
mockkObject(AuthManager)
|
||||
storedPinnedModels = mutableListOf()
|
||||
@@ -113,7 +111,7 @@ class ProfilesViewModelTest {
|
||||
}
|
||||
|
||||
private fun createViewModel(): ProfilesViewModel {
|
||||
val vm = ProfilesViewModel()
|
||||
val vm = ProfilesViewModel(ioDispatcher = testDispatcher)
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
return vm
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user