test: de-poison unit suite — drop static Dispatchers mocks, kill leaked WS timers (#833)

Co-authored-by: M57 <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andM57 authored and GitHub committed 2026-08-07 04:18:14 +03:00
1 parent fe79ff42b4
commit 4645a2b6e7
8 files changed
+51 -33

No files matched your search

@@ -84,7 +84,6 @@ class ChatViewModelTest {
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
val testMainDispatcher = Dispatchers.Main
reqCount = 0
mockkStatic(Log::class)
@@ -93,13 +92,21 @@ class ChatViewModelTest {
every { Log.w(any(), any<String>()) } returns 0
every { Log.e(any(), any(), any()) } returns 0
mockkStatic(Dispatchers::class)
every { Dispatchers.IO } returns testDispatcher
every { Dispatchers.Main } returns testMainDispatcher
mockkObject(AuthManager)
every { AuthManager.getPinnedModels() } returns emptyList()
mockkObject(HermesWsClient)
// Catch-all for unstubbed request() calls: the real implementation
// registers a pending call and launches a 120s timeout job on the
// singleton's real-IO wsScope. The timer outlives the test class,
// fires after unmockkAll and crashes an unrelated later test with
// MockK's "can't find stub" (UncaughtExceptionsBeforeTest). Mirror
// the real request()'s send() delegation (tests verify send calls)
// but return a never-completing deferred — same unanswered behavior,
// no leaked timer.
every { HermesWsClient.request(any(), any(), any()) } answers {
HermesWsClient.send(arg(0), arg(1)) {}
CompletableDeferred<Any?>()
}
mockkObject(ApiClient)
mockkObject(HermesDatabase)
@@ -173,7 +180,9 @@ class ChatViewModelTest {
/** Create a ViewModel with the fake repo injected directly. */
private fun createViewModel(startCleanup: Boolean = false): ChatViewModel =
ChatViewModel(app, startCleanup, fakeRepo, testDispatcher)
// Both dispatchers injected — a real ioDispatcher would race the
// test scheduler (repo writes hop it) and shuffle RPC ordering.
ChatViewModel(app, startCleanup, fakeRepo, testDispatcher, testDispatcher)
/**
* Create ViewModel, simulate GatewayReady, feed SESSION_CREATE result,
@@ -102,6 +102,16 @@ class SlashCommandDispatchRpcTest {
arg<((String) -> Unit)?>(2)?.invoke(id)
id
}
// Catch-all for tests that don't stub request() explicitly: the real
// implementation leaks a 120s timeout job on the singleton's real-IO
// wsScope that fires after unmockkAll and crashes a later test with
// MockK's "can't find stub". Mirror the real send() delegation (so
// send-based verifications still see the call) but skip the timer.
// Specific per-test request() stubs registered later take precedence.
every { HermesWsClient.request(any(), any(), any()) } answers {
HermesWsClient.send(arg(0), arg(1)) {}
CompletableDeferred<Any?>()
}
}
@After
@@ -35,11 +35,6 @@ class ConnectViewModelTest {
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
val testMainDispatcher = Dispatchers.Main
mockkStatic(Dispatchers::class)
every { Dispatchers.IO } returns testDispatcher
every { Dispatchers.Main } returns testMainDispatcher
mockkObject(AuthManager)
mockkObject(ApiClient)
mockkStatic(android.util.Base64::class)
@@ -7,7 +7,6 @@ import io.mockk.coEvery
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
@@ -34,8 +33,6 @@ class GatewayViewModelTest {
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
mockkStatic(Dispatchers::class)
every { Dispatchers.IO } returns testDispatcher
mockApi = mockk()
mockkObject(ApiClient)