-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(api): Add unit test for Stream class
- Added missing unit tests for the Stream class in the api module to ensure proper functionality and coverage.
- Loading branch information
Showing
5 changed files
with
108 additions
and
41 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...hropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/Delta.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.tddworks.anthropic.api.messages.api | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* { | ||
* "stop_reason": "end_turn", | ||
* "stop_sequence": null, | ||
* "usage": { | ||
* "output_tokens": 15 | ||
* } | ||
* } | ||
* or | ||
* { | ||
* "type": "text_delta", | ||
* "text": "!" | ||
* } | ||
*/ | ||
@Serializable | ||
data class Delta( | ||
val type: String? = null, | ||
val text: String? = null, | ||
@SerialName("stop_reason") | ||
val stopReason: String? = null, | ||
@SerialName("stop_sequence") | ||
val stopSequence: String? = null, | ||
val usage: Usage? = null, | ||
) { | ||
companion object { | ||
fun dummy() = Delta( | ||
type = "text_delta", | ||
text = "Hello", | ||
stopReason = "end_turn", | ||
stopSequence = null, | ||
usage = Usage(outputTokens = 15) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
common/src/jvmTest/kotlin/com/tddworks/common/network/api/ktor/api/StreamTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.tddworks.common.network.api.ktor.api | ||
|
||
import app.cash.turbine.test | ||
import com.tddworks.common.network.api.ktor.StreamResponse | ||
import com.tddworks.common.network.api.ktor.internal.JsonLenient | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.mock.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.utils.io.* | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.json.Json | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import org.koin.dsl.module | ||
import org.koin.test.junit5.AutoCloseKoinTest | ||
import org.koin.test.junit5.KoinTestExtension | ||
import kotlin.test.assertEquals | ||
|
||
class StreamTest : AutoCloseKoinTest() { | ||
|
||
@JvmField | ||
@RegisterExtension | ||
val koinTestExtension = KoinTestExtension.create { | ||
modules( | ||
module { | ||
single<Json> { JsonLenient } | ||
}) | ||
} | ||
|
||
@Test | ||
fun `test streamEventsFrom with stream response`(): Unit = runBlocking { | ||
val channel = ByteChannel(autoFlush = true) | ||
val mockEngine = MockEngine { request -> | ||
when (request.url.toString()) { | ||
"http://example.com/stream" -> | ||
respond( | ||
content = channel, | ||
status = HttpStatusCode.OK | ||
) | ||
|
||
else -> respond("", HttpStatusCode.NotFound) | ||
} | ||
} | ||
|
||
val client = HttpClient(mockEngine) | ||
|
||
val content = flow<StreamResponse> { | ||
client.preparePost("http://example.com/stream").execute { | ||
streamEventsFrom(it) | ||
} | ||
} | ||
|
||
channel.writeStringUtf8("Hello world!\n") | ||
channel.writeStringUtf8("data: {\"content\": \"some-content-1\"}\n") | ||
channel.writeStringUtf8("data: {\"content\": \"some-content-2\"}\n") | ||
channel.writeStringUtf8("data: [DONE]\n") | ||
content.test { | ||
assertEquals(StreamResponse("some-content-1"), awaitItem()) | ||
assertEquals(StreamResponse("some-content-2"), awaitItem()) | ||
awaitComplete() | ||
channel.close() | ||
} | ||
} | ||
} |