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:
M57andM57 authored and GitHub committed 2026-08-06 15:07:35 +03:00
1 parent dd8dabecb0
commit d9f437939a
14 files changed
+225 -73

No files matched your search

@@ -101,6 +101,11 @@ class AuthManagerTest {
@After
fun tearDown() {
// Cancel the init() appScope (DataStore collector) so it does not leak
// into later classes — leaked collectors re-touch deleted/recreated
// server_store.json and surface as UncaughtExceptionsBeforeTest
// phantoms in whichever test class runs next.
AuthManager.resetAuthStateForTest()
val tempDir = java.io.File(System.getProperty("java.io.tmpdir") ?: "/tmp")
val tempFile = java.io.File(tempDir, "server_store.json")
if (tempFile.exists()) {
@@ -465,4 +470,70 @@ class AuthManagerTest {
verify { mockEditor.remove("auth_token") }
verify { mockEditor.putBoolean("legacy_default_migrated", true) }
}
// ── Active Hermes profile (server-side scope) ─────────────────────────
/**
* THE live profile-switch regression (2026-08-06 logcat): the coordinator
* persisted the server profile via setSelectedProfileId, but
* ServerStore.selfHealed() clamps ids that have no local
* ConnectionProfile — "meow" never exists locally, so the scope silently
* fell back to default and the re-dialed socket created the fresh session
* in the WRONG profile. The active profile state is deliberately NOT
* routed through the server store, so no clamp can touch it.
*/
@Test
fun testActiveProfileId_notClampedByLocalConnectionProfiles() {
// The OLD path (server store) clamps ids with no local ConnectionProfile:
// this is the bug the live logcat exposed. Prove the clamp still bites
// there, and that the NEW active-profile state carries the same id.
AuthManager.setSelectedProfileId("meow")
assertNull(AuthManager.getSelectedProfileId())
AuthManager.setActiveProfileId("meow")
assertEquals("meow", AuthManager.activeProfileId.value)
// Connection-level selection is untouched by the scope change — the
// clamp already nulled it, and setActiveProfileId does not revive it.
assertNull(AuthManager.getSelectedProfileId())
}
@Test
fun testActiveProfileId_persistedToPrefs() {
AuthManager.setActiveProfileId("meow")
verify { mockEditor.putString("active_profile_id", "meow") }
}
@Test
fun testActiveProfileId_restoredFromPrefsAfterRestart() {
every { mockPrefs.getString("active_profile_id", null) } returns "meow"
AuthManager.resetAuthStateForTest()
AuthManager.init(testContext)
kotlinx.coroutines.runBlocking {
val field = AuthManager::class.java.getDeclaredField("prefsDeferred")
field.isAccessible = true
(field.get(AuthManager) as? kotlinx.coroutines.Deferred<*>)?.await()
}
assertEquals("meow", AuthManager.activeProfileId.value)
// Cancel the second init's appScope so its DataStore collector does
// not leak into later test classes (leaked scopes re-touch mocked
// prefs/DataStore state and break subsequent mockkObject(AuthManager)
// classes — the "Missing mocked calls" / UncaughtExceptionsBeforeTest
// suite-order failures).
AuthManager.resetAuthStateForTest()
}
@Test
fun testActiveProfileId_blankAndNullClear() {
AuthManager.setActiveProfileId("meow")
AuthManager.setActiveProfileId("")
assertNull(AuthManager.activeProfileId.value)
AuthManager.setActiveProfileId("meow")
AuthManager.setActiveProfileId(null)
assertNull(AuthManager.activeProfileId.value)
}
}
@@ -102,6 +102,11 @@ class Issue647ProfileUrlTest {
@After
fun tearDown() {
// Cancel the init() appScope (DataStore collector) so it does not leak
// into later classes — leaked collectors re-touch deleted/recreated
// server_store.json and surface as UncaughtExceptionsBeforeTest
// phantoms in whichever test class runs next.
AuthManager.resetAuthStateForTest()
val tempDir = java.io.File(System.getProperty("java.io.tmpdir") ?: "/tmp")
val tempFile = java.io.File(tempDir, "server_store.json")
if (tempFile.exists()) tempFile.delete()