Skip to content

Commit a15d586

Browse files
committed
test: Create ChatCompletions tests
1 parent 67a810b commit a15d586

File tree

7 files changed

+148
-8
lines changed

7 files changed

+148
-8
lines changed

ychat/src/commonTest/kotlin/co/yml/ychat/di/LibraryModuleTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import co.yml.ychat.data.api.ChatGptApi
44
import co.yml.ychat.data.infrastructure.ApiExecutor
55
import co.yml.ychat.data.storage.ChatLogStorage
66
import co.yml.ychat.di.module.LibraryModule
7+
import co.yml.ychat.domain.usecases.ChatCompletionsUseCase
78
import co.yml.ychat.domain.usecases.CompletionUseCase
9+
import co.yml.ychat.entrypoint.features.ChatCompletions
810
import co.yml.ychat.entrypoint.features.Completion
911
import io.ktor.client.HttpClient
1012
import kotlin.test.AfterTest
@@ -35,5 +37,7 @@ class LibraryModuleTest : KoinTest {
3537
get<ApiExecutor>()
3638
get<ChatGptApi>()
3739
get<CompletionUseCase>()
40+
get<ChatCompletionsUseCase>()
41+
get<ChatCompletions>()
3842
}
3943
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package co.yml.ychat.domain.model
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class ChatCompletionsParamsTest {
7+
8+
@Test
9+
fun `on ChatCompletionsParams verify default values`() {
10+
// arrange
11+
val params = ChatCompletionsParams()
12+
13+
// assert
14+
assertEquals(true, params.messages.isEmpty())
15+
assertEquals("gpt-3.5-turbo", params.model)
16+
assertEquals(4096, params.maxTokens)
17+
assertEquals(1, params.maxResults)
18+
assertEquals(1.0, params.temperature)
19+
assertEquals(1.0, params.topP)
20+
}
21+
}

ychat/src/commonTest/kotlin/co/yml/ychat/domain/CompletionParamsTest.kt renamed to ychat/src/commonTest/kotlin/co/yml/ychat/domain/model/CompletionParamsTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package co.yml.ychat.domain
1+
package co.yml.ychat.domain.model
22

3-
import co.yml.ychat.domain.model.CompletionParams
43
import kotlin.test.Test
54
import kotlin.test.assertEquals
65

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package co.yml.ychat.domain.usecases
2+
3+
import co.yml.ychat.data.api.ChatGptApi
4+
import co.yml.ychat.data.dto.ChatCompletionsChoiceDto
5+
import co.yml.ychat.data.dto.ChatCompletionsDto
6+
import co.yml.ychat.data.dto.ChatMessageDto
7+
import co.yml.ychat.data.dto.UsageDto
8+
import co.yml.ychat.data.exception.ChatGptException
9+
import co.yml.ychat.data.infrastructure.ApiResult
10+
import co.yml.ychat.domain.model.ChatCompletionsParams
11+
import co.yml.ychat.domain.model.ChatMessage
12+
import io.mockk.coEvery
13+
import io.mockk.mockk
14+
import kotlin.test.BeforeTest
15+
import kotlin.test.Test
16+
import kotlin.test.assertEquals
17+
import kotlinx.coroutines.runBlocking
18+
19+
class ChatCompletionsUseCaseTest {
20+
21+
private lateinit var chatCompletionsUseCase: ChatCompletionsUseCase
22+
23+
private val chatGptApiMock = mockk<ChatGptApi>()
24+
25+
@BeforeTest
26+
fun setup() {
27+
chatCompletionsUseCase = ChatCompletionsUseCase(chatGptApiMock)
28+
}
29+
30+
@Test
31+
fun `on requestChatCompletions when request succeed then should return formatted result`() {
32+
// arrange
33+
val messages = arrayListOf(ChatMessage("user", "Say this is a test."))
34+
val chatCompletionDto = buildChatCompletionsDto("This indeed a test")
35+
val params = ChatCompletionsParams(messages)
36+
val apiResult = ApiResult(body = chatCompletionDto)
37+
coEvery { chatGptApiMock.chatCompletions(any()) } returns apiResult
38+
39+
// act
40+
val result = runBlocking { chatCompletionsUseCase.requestChatCompletions(params) }
41+
42+
// assert
43+
assertEquals("This indeed a test", result.last().content)
44+
}
45+
46+
@Test
47+
fun `on requestChatCompletions when not request succeed then should throw an exception`() {
48+
// arrange
49+
val messages = arrayListOf(ChatMessage("user", "Say this is a test."))
50+
val params = ChatCompletionsParams(messages)
51+
val apiResult = ApiResult<ChatCompletionsDto>(exception = ChatGptException())
52+
coEvery { chatGptApiMock.chatCompletions(any()) } returns apiResult
53+
54+
// act
55+
val result =
56+
runCatching { runBlocking { chatCompletionsUseCase.requestChatCompletions(params) } }
57+
58+
// assert
59+
assertEquals(true, result.exceptionOrNull() is ChatGptException)
60+
}
61+
62+
private fun buildChatCompletionsDto(answer: String): ChatCompletionsDto {
63+
return ChatCompletionsDto(
64+
id = "1",
65+
model = "gpt",
66+
choices = listOf(
67+
ChatCompletionsChoiceDto(
68+
index = 0,
69+
message = ChatMessageDto("assistance", answer),
70+
finishReason = ""
71+
)
72+
),
73+
usage = UsageDto(
74+
promptToken = 1,
75+
completionTokens = 1,
76+
totalTokens = 1
77+
)
78+
)
79+
}
80+
}

ychat/src/commonTest/kotlin/co/yml/ychat/domain/CompletionUseCaseTest.kt renamed to ychat/src/commonTest/kotlin/co/yml/ychat/domain/usecases/CompletionUseCaseTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package co.yml.ychat.domain
1+
package co.yml.ychat.domain.usecases
22

33
import co.yml.ychat.data.api.ChatGptApi
44
import co.yml.ychat.data.dto.ChoiceDto
@@ -7,7 +7,6 @@ import co.yml.ychat.data.dto.UsageDto
77
import co.yml.ychat.data.exception.ChatGptException
88
import co.yml.ychat.data.infrastructure.ApiResult
99
import co.yml.ychat.data.storage.ChatLogStorage
10-
import co.yml.ychat.domain.usecases.CompletionUseCase
1110
import co.yml.ychat.domain.model.CompletionParams
1211
import io.mockk.coEvery
1312
import io.mockk.every
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,29 @@ import kotlin.test.assertEquals
1515
import kotlinx.coroutines.runBlocking
1616
import org.koin.dsl.module
1717

18-
class CompletionIntegrationTest {
18+
class YChatTest {
1919

2020
private lateinit var yChat: YChatImpl
2121

2222
@BeforeTest
2323
fun setup() {
24-
yChat = YChat.create("api.key") as YChatImpl
24+
yChat = YChatImpl("api.key")
25+
}
26+
27+
@Test
28+
fun `on create method should return singleton instance`() {
29+
// arrange
30+
val yChatOne = YChat.create("api.key")
31+
val yChatTwo = YChat.create("api.key")
32+
33+
// assert
34+
assertEquals(yChatOne, yChatTwo)
2535
}
2636

2737
@Test
2838
fun `on completion execute method should return result successfully`() {
2939
// arrange
30-
val textResult = "This in indeed a text"
40+
val textResult = "This in indeed a test"
3141
val completionSuccessResult = MockStorage.completionSuccessResult(textResult)
3242
mockHttpEngine(completionSuccessResult)
3343

@@ -39,7 +49,28 @@ class CompletionIntegrationTest {
3949
}
4050

4151
// assert
42-
assertEquals("This in indeed a text", result)
52+
assertEquals("This in indeed a test", result)
53+
}
54+
55+
@Test
56+
fun `on chatCompletions execute method should return result successfully`() {
57+
// arrange
58+
val textResult = "This in indeed a test"
59+
val chatCompletionSuccessResult = MockStorage.chatCompletionsSuccessResult(textResult)
60+
mockHttpEngine(chatCompletionSuccessResult)
61+
62+
// act
63+
val result = runBlocking {
64+
yChat.chatCompletions()
65+
.setMaxResults(1)
66+
.setTemperature(1.0)
67+
.setTopP(1.0)
68+
.execute("Say this is a test")
69+
.first().content
70+
}
71+
72+
// assert
73+
assertEquals("This in indeed a test", result)
4374
}
4475

4576
private fun mockHttpEngine(result: String) {

ychat/src/commonTest/kotlin/infrastructure/MockStorage.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ object MockStorage {
77
"\"choices\":[{\"text\":\"\n\n$text\",\"index\":0,\"logprobs\":null," +
88
"\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":9," +
99
"\"total_tokens\":17}}"
10+
11+
fun chatCompletionsSuccessResult(text: String) = "{\"id\":\"1\",\"object\":\"chat.completion\"," +
12+
"\"created\":1678144798,\"model\":\"gpt-3.5-turbo-0301\"," +
13+
"\"usage\":{\"prompt_tokens\":13,\"completion_tokens\":12,\"total_tokens\":25}," +
14+
"\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"$text\"}," +
15+
"\"finish_reason\":\"stop\",\"index\":0}]}"
1016
}

0 commit comments

Comments
 (0)