|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestInvokeTool(t *testing.T) { |
| 26 | + // Create a temporary tools file |
| 27 | + tmpDir := t.TempDir() |
| 28 | + |
| 29 | + toolsFileContent := ` |
| 30 | +sources: |
| 31 | + my-sqlite: |
| 32 | + kind: sqlite |
| 33 | + database: test.db |
| 34 | +tools: |
| 35 | + hello-sqlite: |
| 36 | + kind: sqlite-sql |
| 37 | + source: my-sqlite |
| 38 | + description: "hello tool" |
| 39 | + statement: "SELECT 'hello' as greeting" |
| 40 | + echo-tool: |
| 41 | + kind: sqlite-sql |
| 42 | + source: my-sqlite |
| 43 | + description: "echo tool" |
| 44 | + statement: "SELECT ? as msg" |
| 45 | + parameters: |
| 46 | + - name: message |
| 47 | + type: string |
| 48 | + description: message to echo |
| 49 | +` |
| 50 | + |
| 51 | + toolsFilePath := filepath.Join(tmpDir, "tools.yaml") |
| 52 | + if err := os.WriteFile(toolsFilePath, []byte(toolsFileContent), 0644); err != nil { |
| 53 | + t.Fatalf("failed to write tools file: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + tcs := []struct { |
| 57 | + desc string |
| 58 | + args []string |
| 59 | + want string |
| 60 | + wantErr bool |
| 61 | + errStr string |
| 62 | + }{ |
| 63 | + { |
| 64 | + desc: "success - basic tool call", |
| 65 | + args: []string{"invoke", "hello-sqlite", "--tools-file", toolsFilePath}, |
| 66 | + want: `"greeting": "hello"`, |
| 67 | + }, |
| 68 | + { |
| 69 | + desc: "success - tool call with parameters", |
| 70 | + args: []string{"invoke", "echo-tool", `{"message": "world"}`, "--tools-file", toolsFilePath}, |
| 71 | + want: `"msg": "world"`, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "error - tool not found", |
| 75 | + args: []string{"invoke", "non-existent", "--tools-file", toolsFilePath}, |
| 76 | + wantErr: true, |
| 77 | + errStr: `tool "non-existent" not found`, |
| 78 | + }, |
| 79 | + { |
| 80 | + desc: "error - invalid JSON params", |
| 81 | + args: []string{"invoke", "echo-tool", `invalid-json`, "--tools-file", toolsFilePath}, |
| 82 | + wantErr: true, |
| 83 | + errStr: `params must be a valid JSON string`, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tc := range tcs { |
| 88 | + t.Run(tc.desc, func(t *testing.T) { |
| 89 | + _, got, err := invokeCommandWithContext(context.Background(), tc.args) |
| 90 | + if (err != nil) != tc.wantErr { |
| 91 | + t.Fatalf("got error %v, wantErr %v", err, tc.wantErr) |
| 92 | + } |
| 93 | + if tc.wantErr && !strings.Contains(err.Error(), tc.errStr) { |
| 94 | + t.Fatalf("got error %v, want error containing %q", err, tc.errStr) |
| 95 | + } |
| 96 | + if !tc.wantErr && !strings.Contains(got, tc.want) { |
| 97 | + t.Fatalf("got %q, want it to contain %q", got, tc.want) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestInvokeTool_AuthUnsupported(t *testing.T) { |
| 104 | + tmpDir := t.TempDir() |
| 105 | + toolsFileContent := ` |
| 106 | +sources: |
| 107 | + my-bq: |
| 108 | + kind: bigquery |
| 109 | + project: my-project |
| 110 | + useClientOAuth: true |
| 111 | +tools: |
| 112 | + bq-tool: |
| 113 | + kind: bigquery-sql |
| 114 | + source: my-bq |
| 115 | + description: "bq tool" |
| 116 | + statement: "SELECT 1" |
| 117 | +` |
| 118 | + toolsFilePath := filepath.Join(tmpDir, "auth_tools.yaml") |
| 119 | + if err := os.WriteFile(toolsFilePath, []byte(toolsFileContent), 0644); err != nil { |
| 120 | + t.Fatalf("failed to write tools file: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + args := []string{"invoke", "bq-tool", "--tools-file", toolsFilePath} |
| 124 | + _, _, err := invokeCommandWithContext(context.Background(), args) |
| 125 | + if err == nil { |
| 126 | + t.Fatal("expected error for tool requiring client auth, but got nil") |
| 127 | + } |
| 128 | + if !strings.Contains(err.Error(), "client authorization is not supported") { |
| 129 | + t.Fatalf("unexpected error message: %v", err) |
| 130 | + } |
| 131 | +} |
0 commit comments