picoclaw/pkg/tools/calculator_test.go
google-labs-jules[bot] e31cece76e feat(tools): add calculator tool for math expressions
- Adds a new tool in `pkg/tools/calculator.go` using `github.com/Knetic/govaluate` to securely evaluate mathematical expressions.
- Updates `pkg/config/tools.go` to include the tool configuration under `PICOCLAW_TOOLS_CALCULATOR_`.
- Registers the new tool conditionally in `pkg/agent/instance.go`.
- Adds unit tests in `pkg/tools/calculator_test.go` to verify correct expression evaluation, error handling, and float resolution.

Co-authored-by: hobbyistlabs-coder <267281733+hobbyistlabs-coder@users.noreply.github.com>
2026-03-16 02:22:12 +00:00

70 lines
1.3 KiB
Go

package tools
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCalculatorTool_Execute(t *testing.T) {
calc := NewCalculatorTool()
tests := []struct {
name string
expression string
want string
wantError bool
}{
{
name: "simple addition",
expression: "2 + 2",
want: "4",
},
{
name: "complex math",
expression: "10 * (5 - 3)",
want: "20",
},
{
name: "floating point result",
expression: "10 / 4",
want: "2.5",
},
{
name: "missing expression",
expression: "",
wantError: true,
},
{
name: "invalid expression",
expression: "2 + * 2",
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := map[string]any{}
if tt.expression != "" {
args["expression"] = tt.expression
}
result := calc.Execute(context.Background(), args)
if tt.wantError {
assert.True(t, result.IsError)
} else {
assert.False(t, result.IsError)
assert.Equal(t, tt.want, result.ForLLM)
}
})
}
}
func TestCalculatorTool_Info(t *testing.T) {
calc := NewCalculatorTool()
assert.Equal(t, "calculator", calc.Name())
assert.NotEmpty(t, calc.Description())
assert.NotNil(t, calc.Parameters())
}