Haystack/backend/agents/client/tools_test.go

128 lines
2.3 KiB
Go

package client
import (
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/suite"
)
type ToolTestSuite struct {
suite.Suite
handler ToolsHandlers
}
func (suite *ToolTestSuite) SetupTest() {
suite.handler = ToolsHandlers{
handlers: &map[string]ToolHandler{},
}
suite.handler.AddTool("a", func(info ToolHandlerInfo, args string, call ToolCall) (any, error) {
return true, nil
})
}
func (suite *ToolTestSuite) TestSingleToolCall() {
assert := suite.Assert()
require := suite.Require()
response, err := suite.handler.Handle(
ToolHandlerInfo{
UserId: uuid.Nil,
ImageId: uuid.Nil,
},
AgentAssistantToolCall{
Role: "assistant",
Content: "",
ToolCalls: []ToolCall{{
Index: 0,
Id: "1",
Function: FunctionCall{
Name: "a",
Arguments: "",
},
}},
})
require.NoError(err, "Tool call shouldnt return an error")
assert.EqualValues(response, []AgentTextMessage{{
Role: "tool",
Content: "true",
ToolCallId: "1",
Name: "a",
}})
}
func (suite *ToolTestSuite) TestEmptyCall() {
require := suite.Require()
_, err := suite.handler.Handle(
ToolHandlerInfo{
UserId: uuid.Nil,
ImageId: uuid.Nil,
},
AgentAssistantToolCall{
Role: "assistant",
Content: "",
ToolCalls: []ToolCall{},
})
require.ErrorIs(err, NoToolCallError)
}
func (suite *ToolTestSuite) TestMultipleToolCalls() {
assert := suite.Assert()
require := suite.Require()
response, err := suite.handler.Handle(
ToolHandlerInfo{
UserId: uuid.Nil,
ImageId: uuid.Nil,
},
AgentAssistantToolCall{
Role: "assistant",
Content: "",
ToolCalls: []ToolCall{
{
Index: 0,
Id: "1",
Function: FunctionCall{
Name: "a",
Arguments: "",
},
},
{
Index: 1,
Id: "2",
Function: FunctionCall{
Name: "a",
Arguments: "",
},
},
},
})
require.NoError(err, "Tool call shouldnt return an error")
assert.EqualValues(response, []AgentTextMessage{
{
Role: "tool",
Content: "true",
ToolCallId: "1",
Name: "a",
},
{
Role: "tool",
Content: "true",
ToolCallId: "2",
Name: "a",
},
})
}
func TestToolSuite(t *testing.T) {
suite.Run(t, &ToolTestSuite{})
}