git.iko.soy/opencode-android
on 2811bd0 help sign in
37 lines 1.4 kB history raw
1package soy.iko.opencode.data.network
3import io.ktor.client.HttpClient
4import io.ktor.client.engine.mock.MockEngine
5import io.ktor.client.engine.mock.respondError
6import io.ktor.client.plugins.sse.SSE
7import io.ktor.http.HttpStatusCode
8import kotlinx.coroutines.ExperimentalCoroutinesApi
9import kotlinx.coroutines.cancel
10import org.junit.Assert.assertEquals
11import org.junit.Test
13@OptIn(ExperimentalCoroutinesApi::class)
14class EventStreamClientTest {
16 /**
17 * The SSE plugin requires a streaming response body which MockEngine can't provide,
18 * so full SSE integration isn't testable in pure JVM tests. The reconnect/backoff
19 * logic is covered by [WithRetryTest] (same exponential-backoff formula) and the
20 * event decoding is covered by [OpencodeJsonTest]. Here we verify only the initial
21 * state — the connection-state machine starts in Disconnected.
22 */
23 @Test
24 fun stateIsDisconnectedBeforeSubscribing() {
25 val engine = MockEngine { respondError(HttpStatusCode.InternalServerError) }
26 val client = HttpClient(engine) { install(SSE) }
27 val scope = kotlinx.coroutines.CoroutineScope(
28 kotlinx.coroutines.SupervisorJob() + kotlinx.coroutines.Dispatchers.Unconfined,
29 )
30 val sse = EventStreamClient(client, scope)
32 assertEquals(EventStreamClient.ConnectionState.Disconnected, sse.state.value)
34 scope.cancel()
35 client.close()
36 }