1package soy.iko.opencode.data.network
3import io.ktor.client.HttpClient
4import io.ktor.client.engine.mock.MockEngine
5import io.ktor.client.engine.mock.respond
6import io.ktor.client.engine.mock.respondError
7import io.ktor.client.plugins.ClientRequestException
8import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
9import io.ktor.http.HttpStatusCode
10import io.ktor.http.headersOf
11import io.ktor.serialization.kotlinx.json.json
12import kotlinx.coroutines.test.runTest
13import org.junit.Assert.assertEquals
14import org.junit.Assert.assertTrue
16import soy.iko.opencode.data.model.PermissionResponse
17import java.io.IOException
19class OpencodeApiClientTest {
21 private fun makeClient(engine: MockEngine): HttpClient =
24 install(ContentNegotiation) { json(OpencodeJson) }
28 fun respondPermissionRetriesOnTransientFailure() = runTest {
30 val engine = MockEngine {
32 if (calls < 3) throw IOException("transient")
33 respond("", HttpStatusCode.OK, headersOf("Content-Length", "0"))
35 val api = OpencodeApiClient(makeClient(engine))
36 api.respondPermission("s1", "p1", PermissionResponse.ALWAYS)
37 assertEquals("transient failures should be retried", 3, calls)
41 fun respondPermissionDoesNotRetryClientError() = runTest {
43 val engine = MockEngine {
45 respondError(HttpStatusCode.BadRequest)
47 val api = OpencodeApiClient(makeClient(engine))
48 val error = runCatching {
49 api.respondPermission("s1", "p1", PermissionResponse.REJECT)
51 assertEquals("4xx must not be retried", 1, calls)
52 assertTrue("expected ClientRequestException, got $error", error is ClientRequestException)
56 fun respondPermissionSucceedsOnFirstTry() = runTest {
58 val engine = MockEngine {
60 respond("", HttpStatusCode.OK, headersOf("Content-Length", "0"))
62 val api = OpencodeApiClient(makeClient(engine))
63 api.respondPermission("s1", "p1", PermissionResponse.ONCE)
64 assertEquals(1, calls)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list