Skip to content

Commit

Permalink
ToolInputTest.kt updates
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Dec 13, 2024
1 parent 4bd98f7 commit 26f6e4d
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/commonTest/kotlin/tool/ToolInputTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package com.xemantic.anthropic.tool

import com.xemantic.ai.tool.schema.meta.Description
import com.xemantic.anthropic.cache.CacheControl
import com.xemantic.anthropic.test.assert
import com.xemantic.kotlin.test.have
import com.xemantic.kotlin.test.should
import io.kotest.assertions.json.shouldEqualJson
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldMatch
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlin.test.Test
import kotlin.test.assertFailsWith

class ToolInputTest {

Expand All @@ -34,10 +33,11 @@ class ToolInputTest {
// when
val tool = Tool<TestToolInput>()

tool.assert {
name shouldBe "TestTool"
description shouldBe "A test tool receiving a message and outputting it back"
inputSchema.toString() shouldEqualJson """
tool should {
have(name == "TestTool")
have(description == "A test tool receiving a message and outputting it back")
have(cacheControl == null)
inputSchema.toString() shouldEqualJson /* language=json */ """
{
"type": "object",
"properties": {
Expand All @@ -51,7 +51,6 @@ class ToolInputTest {
]
}
"""
cacheControl shouldBe null
}
}

Expand All @@ -63,10 +62,11 @@ class ToolInputTest {
cacheControl = CacheControl(type = CacheControl.Type.EPHEMERAL)
)

tool.assert {
name shouldBe "TestTool"
description shouldBe "A test tool receiving a message and outputting it back"
inputSchema.toString() shouldEqualJson """
tool should {
have(name == "TestTool")
have(description == "A test tool receiving a message and outputting it back")
have(cacheControl == CacheControl(type = CacheControl.Type.EPHEMERAL))
inputSchema.toString() shouldEqualJson /* language=json */ """
{
"type": "object",
"properties": {
Expand All @@ -80,28 +80,35 @@ class ToolInputTest {
]
}
"""
cacheControl shouldBe CacheControl(type = CacheControl.Type.EPHEMERAL)
}
}

class NoAnnotationTool : ToolInput()

@Test
fun `Should fail to create a Tool without AnthropicTool annotation`() {
shouldThrow<SerializationException> {
val exception = assertFailsWith<SerializationException> {
Tool<NoAnnotationTool>()
}.message shouldMatch "Cannot find serializer for class .*NoAnnotationTool, " +
"make sure that it is annotated with @AnthropicTool and kotlin.serialization plugin is enabled for the project"
} should {
have(message!!.matches(Regex(
"Cannot find serializer for class .*NoAnnotationTool, " +
"make sure that it is annotated with @AnthropicTool and kotlin.serialization plugin is enabled for the project"
)))
}
}

@Serializable
class OnlySerializableAnnotationTool : ToolInput()

@Test
fun `Should fail to create a Tool with only Serializable annotation`() {
shouldThrow<SerializationException> {
assertFailsWith<SerializationException> {
Tool<OnlySerializableAnnotationTool>()
}.message shouldMatch "The class .*OnlySerializableAnnotationTool must be annotated with @AnthropicTool"
} should {
have(message!!.matches(Regex(
"The class .*OnlySerializableAnnotationTool must be annotated with @AnthropicTool"
)))
}
}

}

0 comments on commit 26f6e4d

Please sign in to comment.