summaryrefslogtreecommitdiffhomepage
path: root/internal/llm/tools/sourcegraph_test.go
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-04-04 13:31:21 +0200
committerKujtim Hoxha <[email protected]>2025-04-04 13:31:21 +0200
commiteb9877ee20c44b7cd34f78e9110d315db71977f6 (patch)
treec2d1a0f3e77bc18467b096b792b2e52d4b56dc48 /internal/llm/tools/sourcegraph_test.go
parentf8e05a0d9a42139b442437b26c752cf983c51e2a (diff)
downloadopencode-eb9877ee20c44b7cd34f78e9110d315db71977f6.tar.gz
opencode-eb9877ee20c44b7cd34f78e9110d315db71977f6.zip
add sourcegraph tool
Diffstat (limited to 'internal/llm/tools/sourcegraph_test.go')
-rw-r--r--internal/llm/tools/sourcegraph_test.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/internal/llm/tools/sourcegraph_test.go b/internal/llm/tools/sourcegraph_test.go
new file mode 100644
index 000000000..5657ccd7e
--- /dev/null
+++ b/internal/llm/tools/sourcegraph_test.go
@@ -0,0 +1,115 @@
+package tools
+
+import (
+ "context"
+ "encoding/json"
+ "testing"
+
+ "github.com/kujtimiihoxha/termai/internal/permission"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestSourcegraphTool_Info(t *testing.T) {
+ tool := NewSourcegraphTool()
+ info := tool.Info()
+
+ assert.Equal(t, SourcegraphToolName, info.Name)
+ assert.NotEmpty(t, info.Description)
+ assert.Contains(t, info.Parameters, "query")
+ assert.Contains(t, info.Parameters, "count")
+ assert.Contains(t, info.Parameters, "timeout")
+ assert.Contains(t, info.Required, "query")
+}
+
+func TestSourcegraphTool_Run(t *testing.T) {
+ // Setup a mock permission handler that always allows
+ origPermission := permission.Default
+ defer func() {
+ permission.Default = origPermission
+ }()
+ permission.Default = newMockPermissionService(true)
+
+ t.Run("handles missing query parameter", func(t *testing.T) {
+ tool := NewSourcegraphTool()
+ params := SourcegraphParams{
+ Query: "",
+ }
+
+ paramsJSON, err := json.Marshal(params)
+ require.NoError(t, err)
+
+ call := ToolCall{
+ Name: SourcegraphToolName,
+ Input: string(paramsJSON),
+ }
+
+ response, err := tool.Run(context.Background(), call)
+ require.NoError(t, err)
+ assert.Contains(t, response.Content, "Query parameter is required")
+ })
+
+ t.Run("handles invalid parameters", func(t *testing.T) {
+ tool := NewSourcegraphTool()
+ call := ToolCall{
+ Name: SourcegraphToolName,
+ Input: "invalid json",
+ }
+
+ response, err := tool.Run(context.Background(), call)
+ require.NoError(t, err)
+ assert.Contains(t, response.Content, "Failed to parse sourcegraph parameters")
+ })
+
+ t.Run("handles permission denied", func(t *testing.T) {
+ permission.Default = newMockPermissionService(false)
+
+ tool := NewSourcegraphTool()
+ params := SourcegraphParams{
+ Query: "test query",
+ }
+
+ paramsJSON, err := json.Marshal(params)
+ require.NoError(t, err)
+
+ call := ToolCall{
+ Name: SourcegraphToolName,
+ Input: string(paramsJSON),
+ }
+
+ response, err := tool.Run(context.Background(), call)
+ require.NoError(t, err)
+ assert.Contains(t, response.Content, "Permission denied")
+ })
+
+ t.Run("normalizes count parameter", func(t *testing.T) {
+ // Test cases for count normalization
+ testCases := []struct {
+ name string
+ inputCount int
+ expectedCount int
+ }{
+ {"negative count", -5, 10}, // Should use default (10)
+ {"zero count", 0, 10}, // Should use default (10)
+ {"valid count", 50, 50}, // Should keep as is
+ {"excessive count", 150, 100}, // Should cap at 100
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Verify count normalization logic directly
+ assert.NotPanics(t, func() {
+ // Apply the same normalization logic as in the tool
+ normalizedCount := tc.inputCount
+ if normalizedCount <= 0 {
+ normalizedCount = 10
+ } else if normalizedCount > 100 {
+ normalizedCount = 100
+ }
+
+ assert.Equal(t, tc.expectedCount, normalizedCount)
+ })
+ })
+ }
+ })
+}