|
1 | 1 | package builtin |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "os" |
5 | 6 | "os/exec" |
6 | 7 | "runtime" |
@@ -55,6 +56,105 @@ func TestShellTool_HandlerWithCwd(t *testing.T) { |
55 | 56 | assert.Contains(t, result.Output, tmpDir) |
56 | 57 | } |
57 | 58 |
|
| 59 | +func TestRunShellArgs_UnmarshalJSON_AcceptsCmdAndCommand(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + |
| 62 | + tests := []struct { |
| 63 | + name string |
| 64 | + input string |
| 65 | + wantCmd string |
| 66 | + wantCwd string |
| 67 | + wantTO int |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "canonical cmd", |
| 71 | + input: `{"cmd":"ls -la","cwd":"/tmp","timeout":10}`, |
| 72 | + wantCmd: "ls -la", |
| 73 | + wantCwd: "/tmp", |
| 74 | + wantTO: 10, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "alias command", |
| 78 | + input: `{"command":"ls -la","cwd":"/tmp","timeout":10}`, |
| 79 | + wantCmd: "ls -la", |
| 80 | + wantCwd: "/tmp", |
| 81 | + wantTO: 10, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "both present cmd wins", |
| 85 | + input: `{"cmd":"from-cmd","command":"from-command"}`, |
| 86 | + wantCmd: "from-cmd", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "blank cmd falls back to command alias", |
| 90 | + input: `{"cmd":" ","command":"from-command"}`, |
| 91 | + wantCmd: "from-command", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "empty cmd falls back to command alias", |
| 95 | + input: `{"cmd":"","command":"from-command"}`, |
| 96 | + wantCmd: "from-command", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "empty object leaves cmd empty", |
| 100 | + input: `{}`, |
| 101 | + wantCmd: "", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + var got RunShellArgs |
| 109 | + require.NoError(t, json.Unmarshal([]byte(tt.input), &got)) |
| 110 | + assert.Equal(t, tt.wantCmd, got.Cmd) |
| 111 | + assert.Equal(t, tt.wantCwd, got.Cwd) |
| 112 | + assert.Equal(t, tt.wantTO, got.Timeout) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestRunShellBackgroundArgs_UnmarshalJSON_AcceptsCmdAndCommand(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + |
| 120 | + var viaCmd RunShellBackgroundArgs |
| 121 | + require.NoError(t, json.Unmarshal([]byte(`{"cmd":"sleep 1","cwd":"/tmp"}`), &viaCmd)) |
| 122 | + assert.Equal(t, "sleep 1", viaCmd.Cmd) |
| 123 | + assert.Equal(t, "/tmp", viaCmd.Cwd) |
| 124 | + |
| 125 | + var viaCommand RunShellBackgroundArgs |
| 126 | + require.NoError(t, json.Unmarshal([]byte(`{"command":"sleep 1"}`), &viaCommand)) |
| 127 | + assert.Equal(t, "sleep 1", viaCommand.Cmd) |
| 128 | + |
| 129 | + // A blank "cmd" must not shadow a valid "command" alias. |
| 130 | + var blankCmd RunShellBackgroundArgs |
| 131 | + require.NoError(t, json.Unmarshal([]byte(`{"cmd":" ","command":"sleep 1"}`), &blankCmd)) |
| 132 | + assert.Equal(t, "sleep 1", blankCmd.Cmd) |
| 133 | +} |
| 134 | + |
| 135 | +// Exercises the end-to-end dispatch path: a tool-call whose raw arguments |
| 136 | +// use "command" instead of "cmd" must execute normally rather than return |
| 137 | +// the missing-parameter error. |
| 138 | +func TestShellTool_HandlerAcceptsCommandAlias(t *testing.T) { |
| 139 | + tool := NewShellTool(nil, &config.RuntimeConfig{Config: config.Config{WorkingDir: t.TempDir()}}) |
| 140 | + |
| 141 | + var params RunShellArgs |
| 142 | + require.NoError(t, json.Unmarshal([]byte(`{"command":"echo hello-from-alias"}`), ¶ms)) |
| 143 | + |
| 144 | + result, err := tool.handler.RunShell(t.Context(), params) |
| 145 | + require.NoError(t, err) |
| 146 | + assert.Contains(t, result.Output, "hello-from-alias") |
| 147 | +} |
| 148 | + |
| 149 | +func TestShellTool_HandlerMissingCmdReturnsActionableError(t *testing.T) { |
| 150 | + tool := NewShellTool(nil, &config.RuntimeConfig{Config: config.Config{WorkingDir: t.TempDir()}}) |
| 151 | + |
| 152 | + result, err := tool.handler.RunShell(t.Context(), RunShellArgs{}) |
| 153 | + require.NoError(t, err) |
| 154 | + assert.Contains(t, result.Output, `"cmd"`, |
| 155 | + "error must name the expected parameter so the model can self-correct") |
| 156 | +} |
| 157 | + |
58 | 158 | func TestShellTool_HandlerError(t *testing.T) { |
59 | 159 | tool := NewShellTool(nil, &config.RuntimeConfig{Config: config.Config{WorkingDir: t.TempDir()}}) |
60 | 160 |
|
|
0 commit comments