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

@@ -1,9 +1,6 @@
package com.m57.hermescontrol.data.remote
import com.m57.hermescontrol.data.local.AuthManager
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.unmockkAll
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
@@ -20,7 +17,12 @@ class ProfileScopeInterceptorTest {
@Before
fun setUp() {
mockkObject(AuthManager)
// NOTE: deliberately NO mockkObject(AuthManager) — mocking the object
// from a class that runs after real AuthManager init (AuthManagerTest
// re-inits + leaks scopes) fails with "Missing mocked calls inside
// every{}" in suite order. The real prefs-backed setActiveProfileId is
// runCatching-safe pre-init, so tests use the REAL state instead.
AuthManager.resetAuthStateForTest()
server = MockWebServer()
server.start()
}
@@ -28,12 +30,12 @@ class ProfileScopeInterceptorTest {
@After
fun tearDown() {
server.shutdown()
unmockkAll()
AuthManager.resetAuthStateForTest()
}
/** Builds a real client with the interceptor + a stubbed AuthManager profile. */
/** Builds a real client with the interceptor + a real active profile state. */
private fun clientFor(profile: String?): OkHttpClient {
every { AuthManager.getSelectedProfileId() } returns profile
AuthManager.setActiveProfileId(profile)
return OkHttpClient
.Builder()
.addInterceptor(ProfileScopeInterceptor)
@@ -64,7 +66,7 @@ class ProfileScopeInterceptorTest {
@Test
fun explicitProfileParam_wins() {
every { AuthManager.getSelectedProfileId() } returns "work"
AuthManager.setActiveProfileId("work")
val client = OkHttpClient.Builder().addInterceptor(ProfileScopeInterceptor).build()
server.enqueue(MockResponse().setResponseCode(200).setBody("{}"))
val req =
@@ -80,7 +82,7 @@ class ProfileScopeInterceptorTest {
@Test
fun nonScopedEndpoint_untouched() {
// Pairing is machine-global (not profile-scoped on the backend).
every { AuthManager.getSelectedProfileId() } returns "work"
AuthManager.setActiveProfileId("work")
val client = OkHttpClient.Builder().addInterceptor(ProfileScopeInterceptor).build()
server.enqueue(MockResponse().setResponseCode(200).setBody("{}"))
val req = Request.Builder().url(server.url("api/pairing")).build()
@@ -120,7 +122,7 @@ class ProfileScopeInterceptorTest {
fun lookalikePath_notScoped() {
// Sourcery review (PR #540): `startsWith` must not match non-segment
// suffixes like /api/statusXYZ or /api/gatewayExtra.
every { AuthManager.getSelectedProfileId() } returns "work"
AuthManager.setActiveProfileId("work")
val client = OkHttpClient.Builder().addInterceptor(ProfileScopeInterceptor).build()
server.enqueue(MockResponse().setResponseCode(200).setBody("{}"))
val req = Request.Builder().url(server.url("api/statusXYZ")).build()
@@ -134,7 +136,7 @@ class ProfileScopeInterceptorTest {
fun scopedSubPath_isScoped() {
// A scoped prefix with a trailing segment (/api/status/health) MUST
// still receive the profile param.
every { AuthManager.getSelectedProfileId() } returns "work"
AuthManager.setActiveProfileId("work")
val client = OkHttpClient.Builder().addInterceptor(ProfileScopeInterceptor).build()
server.enqueue(MockResponse().setResponseCode(200).setBody("{}"))
val req = Request.Builder().url(server.url("api/status/health")).build()