|
1 | 1 | package codemode |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 | "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/docker/cagent/pkg/tools" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | func TestCodeModeTool_Tools(t *testing.T) { |
@@ -74,11 +77,117 @@ func TestCodeModeTool_Instructions(t *testing.T) { |
74 | 77 | } |
75 | 78 |
|
76 | 79 | func TestCodeModeTool_StartStop(t *testing.T) { |
77 | | - tool := &codeModeTool{} |
| 80 | + inner := &testToolSet{} |
| 81 | + |
| 82 | + tool := Wrap(inner) |
| 83 | + |
| 84 | + assert.Equal(t, 0, inner.start) |
| 85 | + assert.Equal(t, 0, inner.stop) |
78 | 86 |
|
79 | 87 | err := tool.Start(t.Context()) |
80 | 88 | require.NoError(t, err) |
| 89 | + assert.Equal(t, 1, inner.start) |
| 90 | + assert.Equal(t, 0, inner.stop) |
81 | 91 |
|
82 | 92 | err = tool.Stop(t.Context()) |
83 | 93 | require.NoError(t, err) |
| 94 | + assert.Equal(t, 1, inner.start) |
| 95 | + assert.Equal(t, 1, inner.stop) |
84 | 96 | } |
| 97 | + |
| 98 | +func TestCodeModeTool_CallHello(t *testing.T) { |
| 99 | + tool := Wrap(&testToolSet{ |
| 100 | + tools: []tools.Tool{{ |
| 101 | + Name: "hello_world", |
| 102 | + Handler: func(ctx context.Context, toolCall tools.ToolCall) (*tools.ToolCallResult, error) { |
| 103 | + return &tools.ToolCallResult{ |
| 104 | + Output: "Hello, World!", |
| 105 | + }, nil |
| 106 | + }, |
| 107 | + }}, |
| 108 | + }) |
| 109 | + |
| 110 | + allTools, err := tool.Tools(t.Context()) |
| 111 | + require.NoError(t, err) |
| 112 | + require.Len(t, allTools, 1) |
| 113 | + |
| 114 | + result, err := allTools[0].Handler(t.Context(), tools.ToolCall{ |
| 115 | + Function: tools.FunctionCall{ |
| 116 | + Arguments: `{"script":"return hello_world();"}`, |
| 117 | + }, |
| 118 | + }) |
| 119 | + require.NoError(t, err) |
| 120 | + |
| 121 | + var scriptResult ScriptResult |
| 122 | + err = json.Unmarshal([]byte(result.Output), &scriptResult) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + require.Equal(t, "Hello, World!", scriptResult.Value) |
| 126 | + require.Empty(t, scriptResult.StdErr) |
| 127 | + require.Empty(t, scriptResult.StdOut) |
| 128 | +} |
| 129 | + |
| 130 | +func TestCodeModeTool_CallEcho(t *testing.T) { |
| 131 | + type EchoArgs struct { |
| 132 | + Message string `json:"message" jsonschema:"Message to echo"` |
| 133 | + } |
| 134 | + |
| 135 | + tool := Wrap(&testToolSet{ |
| 136 | + tools: []tools.Tool{{ |
| 137 | + Name: "echo", |
| 138 | + Handler: func(ctx context.Context, toolCall tools.ToolCall) (*tools.ToolCallResult, error) { |
| 139 | + return &tools.ToolCallResult{ |
| 140 | + Output: "ECHO", |
| 141 | + }, nil |
| 142 | + }, |
| 143 | + Parameters: tools.MustSchemaFor[EchoArgs](), |
| 144 | + }}, |
| 145 | + }) |
| 146 | + |
| 147 | + allTools, err := tool.Tools(t.Context()) |
| 148 | + require.NoError(t, err) |
| 149 | + require.Len(t, allTools, 1) |
| 150 | + |
| 151 | + result, err := allTools[0].Handler(t.Context(), tools.ToolCall{ |
| 152 | + Function: tools.FunctionCall{ |
| 153 | + Arguments: `{"script":"return echo({'message':'ECHO'});"}`, |
| 154 | + }, |
| 155 | + }) |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + var scriptResult ScriptResult |
| 159 | + err = json.Unmarshal([]byte(result.Output), &scriptResult) |
| 160 | + require.NoError(t, err) |
| 161 | + |
| 162 | + require.Equal(t, "ECHO", scriptResult.Value) |
| 163 | + require.Empty(t, scriptResult.StdErr) |
| 164 | + require.Empty(t, scriptResult.StdOut) |
| 165 | +} |
| 166 | + |
| 167 | +type testToolSet struct { |
| 168 | + tools []tools.Tool |
| 169 | + start int |
| 170 | + stop int |
| 171 | +} |
| 172 | + |
| 173 | +func (t *testToolSet) Tools(ctx context.Context) ([]tools.Tool, error) { |
| 174 | + return t.tools, nil |
| 175 | +} |
| 176 | + |
| 177 | +func (t *testToolSet) Instructions() string { |
| 178 | + return "" |
| 179 | +} |
| 180 | + |
| 181 | +func (t *testToolSet) Start(context.Context) error { |
| 182 | + t.start++ |
| 183 | + return nil |
| 184 | +} |
| 185 | + |
| 186 | +func (t *testToolSet) Stop(context.Context) error { |
| 187 | + t.stop++ |
| 188 | + return nil |
| 189 | +} |
| 190 | + |
| 191 | +func (t *testToolSet) SetElicitationHandler(tools.ElicitationHandler) {} |
| 192 | + |
| 193 | +func (t *testToolSet) SetOAuthSuccessHandler(func()) {} |
0 commit comments