|
15 | 15 | package tools |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
| 19 | + "strings" |
18 | 20 | "testing" |
| 21 | + |
| 22 | + "github.com/GoogleCloudPlatform/kubectl-ai/pkg/sandbox" |
19 | 23 | ) |
20 | 24 |
|
21 | 25 | func TestCustomTool_AddCommandPrefix(t *testing.T) { |
@@ -111,3 +115,64 @@ func TestCustomTool_AddCommandPrefix(t *testing.T) { |
111 | 115 | }) |
112 | 116 | } |
113 | 117 | } |
| 118 | + |
| 119 | +// MockExecutor implements sandbox.Executor for testing |
| 120 | +type MockExecutor struct { |
| 121 | + CapturedCommand string |
| 122 | + CapturedEnv []string |
| 123 | + CapturedWorkDir string |
| 124 | +} |
| 125 | + |
| 126 | +func (m *MockExecutor) Execute(ctx context.Context, command string, env []string, workDir string) (*sandbox.ExecResult, error) { |
| 127 | + m.CapturedCommand = command |
| 128 | + m.CapturedEnv = env |
| 129 | + m.CapturedWorkDir = workDir |
| 130 | + return &sandbox.ExecResult{Stdout: "executed"}, nil |
| 131 | +} |
| 132 | + |
| 133 | +func (m *MockExecutor) Close(ctx context.Context) error { |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func TestCustomTool_CloneWithExecutor(t *testing.T) { |
| 138 | + config := CustomToolConfig{ |
| 139 | + Name: "test-tool", |
| 140 | + Command: "echo", |
| 141 | + } |
| 142 | + |
| 143 | + tool, err := NewCustomTool(config) |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("failed to create tool: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + mockExec := &MockExecutor{} |
| 149 | + clonedTool := tool.CloneWithExecutor(mockExec) |
| 150 | + |
| 151 | + ctx := context.WithValue(context.Background(), WorkDirKey, "/tmp") |
| 152 | + args := map[string]any{ |
| 153 | + "command": "hello", |
| 154 | + } |
| 155 | + |
| 156 | + result, err := clonedTool.Run(ctx, args) |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("tool run failed: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + execResult, ok := result.(*sandbox.ExecResult) |
| 162 | + if !ok { |
| 163 | + t.Fatalf("expected *sandbox.ExecResult, got %T", result) |
| 164 | + } |
| 165 | + if execResult.Stdout != "executed" { |
| 166 | + t.Errorf("expected Stdout 'executed', got %q", execResult.Stdout) |
| 167 | + } |
| 168 | + |
| 169 | + if mockExec.CapturedCommand != "echo hello" { |
| 170 | + t.Errorf("expected command 'echo hello', got %q", mockExec.CapturedCommand) |
| 171 | + } |
| 172 | + if !strings.Contains(strings.Join(mockExec.CapturedEnv, "\n"), "PATH") { |
| 173 | + t.Errorf("expected captured environment to contain PATH") |
| 174 | + } |
| 175 | + if mockExec.CapturedWorkDir != "/tmp" { |
| 176 | + t.Errorf("expected workdir '/tmp', got %q", mockExec.CapturedWorkDir) |
| 177 | + } |
| 178 | +} |
0 commit comments