Skip to content

Commit

Permalink
test(api): add test cases for CreateMessageResponse, Usage, ContentMe…
Browse files Browse the repository at this point in the history
…ssage, and Delta

Added test cases for CreateMessageResponse, Usage, ContentMessage, and Delta classes. The tests cover creation, dummy data generation, JSON serialization, and JSON deserialization for each respective class.
  • Loading branch information
hanrw committed Jun 4, 2024
1 parent a9d22ff commit 54a17b2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tddworks.anthropic.api.messages.api

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class ContentMessageTest {

@Test
fun `should create a ContentMessage`() {
val dummyMessage = ContentMessage("Hi! My name is Claude.", "text")

assertEquals("Hi! My name is Claude.", dummyMessage.text)
assertEquals("text", dummyMessage.type)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.tddworks.anthropic.api.messages.api

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test


internal class CreateMessageResponseTest {

@Test
fun `should create a dummy CreateMessageResponse`() {

val dummyResponse = CreateMessageResponse.dummy()

assertEquals("msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", dummyResponse.id)
assertEquals("claude-3-opus-20240229", dummyResponse.model)
assertEquals("assistant", dummyResponse.role)
assertNull(dummyResponse.stopReason)
assertNull(dummyResponse.stopSequence)
assertEquals("message", dummyResponse.type)
assertEquals(Usage(25, 1), dummyResponse.usage)
assertEquals(listOf(ContentMessage("Hi! My name is Claude.", "text")), dummyResponse.content)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tddworks.anthropic.api.messages.api

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test


class DeltaTest {

@Test
fun `should create dummy Delta`() {
val delta = Delta.dummy()
assertEquals("text_delta", delta.type)
assertEquals("Hello", delta.text)
assertEquals("end_turn", delta.stopReason)
assertEquals(null, delta.stopSequence)
assertEquals(Usage(outputTokens = 15), delta.usage)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.tddworks.anthropic.api.messages.api

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class UsageTest {
@Test
fun `should create a empty Usage`() {
val usage = Usage()
val json = Json.encodeToString(usage)

val expectedJson = "{}"
assertEquals(expectedJson, json)
}

@Test
fun `should create a dummy Usage`() {
val usage = Usage(inputTokens = 10, outputTokens = 20)
val json = Json.encodeToString(usage)

val expectedJson = "{\"input_tokens\":10,\"output_tokens\":20}"
assertEquals(expectedJson, json)
}

@Test
fun `should parse a Usage from json`() {
val json = "{\"input_tokens\":5,\"output_tokens\":15}"
val usage = Json.decodeFromString<Usage>(json)

assertEquals(5, usage.inputTokens)
assertEquals(15, usage.outputTokens)
}
}

0 comments on commit 54a17b2

Please sign in to comment.