Skip to content

Commit

Permalink
test(ut): add ut for DefaultHttpRequester and Exceptions
Browse files Browse the repository at this point in the history
 - refactor codes
 - remove unused class
  • Loading branch information
hanrw committed Jun 4, 2024
1 parent 6682450 commit d9045b6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import org.koin.core.qualifier.named
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module

expect fun platformModule(): Module

fun iniAnthropic(
config: AnthropicConfig,
appDeclaration: KoinAppDeclaration = {}
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ koverReport {
"*.BuildKonfig", // BuildKonfig generated
"*.ComposableSingletons*", // Jetpack Compose generated
"*.*\$*Preview\$*", // Jetpack Compose Preview functions
"*.di.*", // Koin
"*.ui.preview.*", // Jetpack Compose Preview providers
"*.*Test", // Test files
"*.*Test*", // Test cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ sealed interface ChatMessage {
companion object {
fun system(content: String) = SystemMessage(content)
fun user(content: String) = UserMessage(content)
fun assistant(content: String, toolCalls: List<ToolCall>? = null) =
AssistantMessage(content, toolCalls = toolCalls)

fun tool(content: String, toolCallId: String) =
ToolMessage(content = content, toolCallId = toolCallId)
fun assistant(content: String) = AssistantMessage(content)

fun vision(content: List<VisionMessageContent>) = VisionMessage(content)
}
Expand Down Expand Up @@ -57,27 +53,6 @@ sealed interface ChatMessage {
@SerialName("role")
@EncodeDefault(EncodeDefault.Mode.ALWAYS)
override val role: Role = Role.Assistant,
@SerialName("tool_calls")
val toolCalls: List<ToolCall>? = null,
) : ChatMessage

@Serializable
data class ToolMessage(
/**
* The contents of the tool message.
*/
@SerialName("content")
override val content: String,
/**
* The role of the messages author, in this case tool.
*/
@SerialName("role")
override val role: Role = Role.Tool,
/**
* Tool call that this message is responding to.
*/
@SerialName("tool_call_id")
val toolCallId: String,
) : ChatMessage

/**
Expand All @@ -99,49 +74,6 @@ sealed interface ChatMessage {
) : ChatMessage
}

@Serializable
data class ToolCall(
/**
* id string
* Required
* The ID of the tool call.
*/
@SerialName("id")
val id: String,

/**
* string Required
* The type of the tool. Currently, only function is supported.
*/
@SerialName("type")
val type: String,

/**
* object Required
* The function that the model called.
*/
@SerialName("function")
val function: Function,
)

@Serializable
data class Function(
/**
* string Required
* The name of the function to call.
*/
@SerialName("name")
val name: String,

/**
* object Required
* The parameters to pass to the function.
* The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
*/
@SerialName("arguments")
val arguments: String,
)

object ChatMessageSerializer :
JsonContentPolymorphicSerializer<ChatMessage>(ChatMessage::class) {
override fun selectDeserializer(element: JsonElement): KSerializer<out ChatMessage> {
Expand All @@ -151,7 +83,6 @@ object ChatMessageSerializer :
Role.System.name -> ChatMessage.SystemMessage.serializer()
Role.User.name -> ChatMessage.UserMessage.serializer()
Role.Assistant.name -> ChatMessage.AssistantMessage.serializer()
Role.Tool.name -> ChatMessage.ToolMessage.serializer()
else -> throw IllegalArgumentException("Unknown type")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,10 @@ class ChatMessageTest {
}
""".trimIndent()

assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.VisionMessage.serializer(), message))
}

@Test
fun `should return correct json for tool message`() {
val message = ChatMessage.tool("Hello, how may I assist you today?", "tool-id")

val expectedJson = """
{
"content": "Hello, how may I assist you today?",
"tool_call_id": "tool-id"
}
""".trimIndent()

assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.ToolMessage.serializer(), message))
assertEquals(
expectedJson,
prettyJson.encodeToString(ChatMessage.VisionMessage.serializer(), message)
)
}

@Test
Expand All @@ -69,7 +58,10 @@ class ChatMessageTest {
}
""".trimIndent()

assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.AssistantMessage.serializer(), message))
assertEquals(
expectedJson,
prettyJson.encodeToString(ChatMessage.AssistantMessage.serializer(), message)
)
}

@Test
Expand All @@ -83,7 +75,10 @@ class ChatMessageTest {
}
""".trimIndent()

assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.UserMessage.serializer(), message))
assertEquals(
expectedJson,
prettyJson.encodeToString(ChatMessage.UserMessage.serializer(), message)
)
}

@Test
Expand All @@ -97,6 +92,9 @@ class ChatMessageTest {
}
""".trimIndent()

assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.SystemMessage.serializer(), message))
assertEquals(
expectedJson,
prettyJson.encodeToString(ChatMessage.SystemMessage.serializer(), message)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tddworks.openai.di

import com.tddworks.openai.api.OpenAI
import com.tddworks.openai.api.OpenAIConfig
import org.junit.jupiter.api.Test
import org.koin.dsl.koinApplication
import org.koin.test.check.checkModules
import org.koin.test.junit5.AutoCloseKoinTest

class OpenAIKoinTest : AutoCloseKoinTest() {

@Test
fun `should initialize openai koin modules`() {
koinApplication {
initOpenAI(OpenAIConfig(
baseUrl = { OpenAI.BASE_URL },
apiKey = { System.getenv("OPENAI_API_KEY") ?: "CONFIGURE_ME" }
))
}.checkModules()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,6 @@ class ExtensionsTest {
)
}

@Test
fun `should throw IllegalArgumentException when role is not user or assistant`() {
// Given
val chatCompletionRequest =
ChatCompletionRequest(
listOf(
ChatMessage.ToolMessage(
content = "Hello! How can I assist you today?",
toolCallId = "tool_call_id"
)
),
)

// When
val exception = assertThrows(IllegalArgumentException::class.java) {
chatCompletionRequest.toAnthropicRequest()
}

// Then
assertEquals(
"Unknown message type: ToolMessage(content=Hello! How can I assist you today?, role=Role(name=tool), toolCallId=tool_call_id)",
exception.message
)
}

@Test
fun `should convert user to anthropic request`() {
// Given
Expand Down

0 comments on commit d9045b6

Please sign in to comment.