From d9045b666bb38272399855f7462f8d81d038f30d Mon Sep 17 00:00:00 2001 From: slam Date: Tue, 4 Jun 2024 19:36:55 +0800 Subject: [PATCH] test(ut): add ut for DefaultHttpRequester and Exceptions - refactor codes - remove unused class --- .../kotlin/com/tddworks/anthropic/di/Koin.kt | 2 - .../com/tddworks/anthropic/di/Koin.jvm.kt | 7 -- .../com/tddworks/anthropic/di/Koin.macos.kt | 7 -- build.gradle.kts | 1 - .../openai/api/chat/api/ChatMessage.kt | 71 +------------------ .../openai/api/chat/api/ChatMessageTest.kt | 34 +++++---- .../com/tddworks/openai/di/OpenAIKoinTest.kt | 21 ++++++ .../api/messages/api/ExtensionsTest.kt | 25 ------- 8 files changed, 38 insertions(+), 130 deletions(-) delete mode 100644 anthropic-client/anthropic-client-core/src/jvmMain/kotlin/com/tddworks/anthropic/di/Koin.jvm.kt delete mode 100644 anthropic-client/anthropic-client-core/src/macosMain/kotlin/com/tddworks/anthropic/di/Koin.macos.kt create mode 100644 openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/di/OpenAIKoinTest.kt diff --git a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt index ec8fb52..1ff26a1 100644 --- a/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt +++ b/anthropic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.kt @@ -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 = {} diff --git a/anthropic-client/anthropic-client-core/src/jvmMain/kotlin/com/tddworks/anthropic/di/Koin.jvm.kt b/anthropic-client/anthropic-client-core/src/jvmMain/kotlin/com/tddworks/anthropic/di/Koin.jvm.kt deleted file mode 100644 index 2a1ae15..0000000 --- a/anthropic-client/anthropic-client-core/src/jvmMain/kotlin/com/tddworks/anthropic/di/Koin.jvm.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.tddworks.anthropic.di - -import org.koin.core.module.Module - -actual fun platformModule(): Module { - TODO("Not yet implemented") -} \ No newline at end of file diff --git a/anthropic-client/anthropic-client-core/src/macosMain/kotlin/com/tddworks/anthropic/di/Koin.macos.kt b/anthropic-client/anthropic-client-core/src/macosMain/kotlin/com/tddworks/anthropic/di/Koin.macos.kt deleted file mode 100644 index 2a1ae15..0000000 --- a/anthropic-client/anthropic-client-core/src/macosMain/kotlin/com/tddworks/anthropic/di/Koin.macos.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.tddworks.anthropic.di - -import org.koin.core.module.Module - -actual fun platformModule(): Module { - TODO("Not yet implemented") -} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 8017d91..26e6506 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 diff --git a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/ChatMessage.kt b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/ChatMessage.kt index 75f3b29..0c3c58e 100644 --- a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/ChatMessage.kt +++ b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/ChatMessage.kt @@ -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? = 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) = VisionMessage(content) } @@ -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? = 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 /** @@ -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::class) { override fun selectDeserializer(element: JsonElement): KSerializer { @@ -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") } } diff --git a/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/api/chat/api/ChatMessageTest.kt b/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/api/chat/api/ChatMessageTest.kt index c221790..00b5085 100644 --- a/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/api/chat/api/ChatMessageTest.kt +++ b/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/api/chat/api/ChatMessageTest.kt @@ -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 @@ -69,7 +58,10 @@ class ChatMessageTest { } """.trimIndent() - assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.AssistantMessage.serializer(), message)) + assertEquals( + expectedJson, + prettyJson.encodeToString(ChatMessage.AssistantMessage.serializer(), message) + ) } @Test @@ -83,7 +75,10 @@ class ChatMessageTest { } """.trimIndent() - assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.UserMessage.serializer(), message)) + assertEquals( + expectedJson, + prettyJson.encodeToString(ChatMessage.UserMessage.serializer(), message) + ) } @Test @@ -97,6 +92,9 @@ class ChatMessageTest { } """.trimIndent() - assertEquals(expectedJson, prettyJson.encodeToString(ChatMessage.SystemMessage.serializer(), message)) + assertEquals( + expectedJson, + prettyJson.encodeToString(ChatMessage.SystemMessage.serializer(), message) + ) } } \ No newline at end of file diff --git a/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/di/OpenAIKoinTest.kt b/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/di/OpenAIKoinTest.kt new file mode 100644 index 0000000..9cb1309 --- /dev/null +++ b/openai-client/openai-client-core/src/jvmTest/kotlin/com/tddworks/openai/di/OpenAIKoinTest.kt @@ -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() + } +} \ No newline at end of file diff --git a/openai-gateway/openai-gateway-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/messages/api/ExtensionsTest.kt b/openai-gateway/openai-gateway-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/messages/api/ExtensionsTest.kt index 710fe9c..cf9ff2e 100644 --- a/openai-gateway/openai-gateway-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/messages/api/ExtensionsTest.kt +++ b/openai-gateway/openai-gateway-core/src/jvmTest/kotlin/com/tddworks/anthropic/api/messages/api/ExtensionsTest.kt @@ -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