Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions internal/assistant/anthropic_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assistant

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -42,6 +43,10 @@ func TestAnthropicOAuthPayloadAddsClaudeCodeIdentity(t *testing.T) {
assert.Len(t, systemBlocks, 2)
assert.Contains(t, systemBlocks[0]["text"], "Claude Code")
assert.Equal(t, anthropicTestSystemPrompt, systemBlocks[1][jsonTextKey])
encodedTools := encodeTestJSON(t, payload["tools"])
assert.Contains(t, encodedTools, `"name":"Read"`)
assert.Contains(t, encodedTools, `"name":"Write"`)
assert.Contains(t, encodedTools, `"eager_input_streaming":true`)
}

func TestAnthropicPayloadAddsBudgetThinking(t *testing.T) {
Expand Down Expand Up @@ -73,6 +78,56 @@ func TestAnthropicPayloadDisablesThinkingWhenOff(t *testing.T) {
assert.NotContains(t, payload, "output_config")
}

func TestAnthropicToolNameMapping(t *testing.T) {
t.Parallel()

tests := []struct {
run func(t *testing.T)
name string
}{
{
name: "api key payload keeps local tool names",
run: assertAnthropicPayloadKeepsLocalToolNames,
},
{
name: "native claude code tool calls map to local names",
run: assertAnthropicToolCallMapsClaudeCodeName,
},
}

for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

testCase.run(t)
})
}
}

func assertAnthropicPayloadKeepsLocalToolNames(t *testing.T) {
t.Helper()

payload := anthropicPayload(testCompletionRequestAuth("sk-ant-api03-secret"), nil)

encodedTools := encodeTestJSON(t, payload["tools"])
assert.Contains(t, encodedTools, `"name":"`+jsonReadToolName+`"`)
assert.Contains(t, encodedTools, `"name":"`+jsonWriteToolName+`"`)
assert.NotContains(t, encodedTools, `"name":"`+anthropicReadToolName+`"`)
}

func assertAnthropicToolCallMapsClaudeCodeName(t *testing.T) {
t.Helper()

call := anthropicToolCall(testAnthropicToolUseID, "Write", map[string]any{
jsonPathKey: "hello.txt",
jsonContentKey: "hello",
})

assert.Equal(t, jsonWriteToolName, call.Name)
assert.Equal(t, "hello.txt", call.Arguments[jsonPathKey])
assert.Equal(t, "hello", call.Arguments[jsonContentKey])
}

func TestAnthropicPayloadAddsAdaptiveThinking(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -115,6 +170,15 @@ func TestAnthropicHeadersUseBearerForOAuth(t *testing.T) {
assert.NotContains(t, headers["anthropic-beta"], "interleaved-thinking-2025-05-14")
}

func encodeTestJSON(t *testing.T, value any) string {
t.Helper()

encoded, err := json.Marshal(value)
assert.NoError(t, err)

return string(encoded)
}

func testCompletionRequestAuth(args ...string) *CompletionRequest {
provider := "anthropic"
apiKey := ""
Expand Down
4 changes: 2 additions & 2 deletions internal/assistant/tool_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ func writeToolSchema() map[string]any {
jsonPathKey: stringSchema(
"Path to create or overwrite, relative to the current workspace or absolute.",
),
"content": stringSchema("Complete file content to write."),
jsonContentKey: stringSchema("Complete file content to write."),
},
jsonRequiredKey: []string{jsonPathKey, "content"},
jsonRequiredKey: []string{jsonPathKey, jsonContentKey},
}
}

Expand Down
27 changes: 27 additions & 0 deletions internal/tool/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ func TestRegistry_ExecuteJSONRunsBashInWorkingDirectory(t *testing.T) {
assert.Contains(t, result.Text(), "ok")
}

func TestRegistry_ExecuteJSONRejectsEmptyWriteContent(t *testing.T) {
t.Parallel()

tests := []struct {
name string
content string
}{
{name: "empty", content: ""},
{name: "whitespace only", content: " \n\t"},
}

registry := tool.NewRegistry(t.TempDir())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

payload, err := json.Marshal(map[string]any{"path": "empty.txt", "content": tt.content})
require.NoError(t, err)

_, err = registry.ExecuteJSON(context.Background(), "write", payload)

require.Error(t, err)
assert.Contains(t, err.Error(), "write content is required")
})
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func executeTool(
ctx context.Context,
t *testing.T,
Expand Down
3 changes: 3 additions & 0 deletions internal/tool/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (writeTool *WriteTool) Write(ctx context.Context, input WriteInput) (Result
if strings.TrimSpace(input.Path) == "" {
return emptyToolResult(), oops.In("tool").Code("write_path_required").Errorf("write path is required")
}
if strings.TrimSpace(input.Content) == "" {
return emptyToolResult(), oops.In("tool").Code("write_content_required").Errorf("write content is required")
}
absolutePath, err := ResolveToCWD(input.Path, writeTool.cwd)
if err != nil {
return emptyToolResult(), oops.In("tool").Code("write_resolve_path").Wrapf(err, "resolve write path")
Expand Down
Loading