|
| 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 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGenerateSkill(t *testing.T) { |
| 27 | + // Create a temporary directory for tests |
| 28 | + tmpDir := t.TempDir() |
| 29 | + outputDir := filepath.Join(tmpDir, "skills") |
| 30 | + |
| 31 | + // Create a tools.yaml file with a sqlite tool |
| 32 | + toolsFileContent := ` |
| 33 | +sources: |
| 34 | + my-sqlite: |
| 35 | + kind: sqlite |
| 36 | + database: test.db |
| 37 | +tools: |
| 38 | + hello-sqlite: |
| 39 | + kind: sqlite-sql |
| 40 | + source: my-sqlite |
| 41 | + description: "hello tool" |
| 42 | + statement: "SELECT 'hello' as greeting" |
| 43 | +` |
| 44 | + |
| 45 | + toolsFilePath := filepath.Join(tmpDir, "tools.yaml") |
| 46 | + if err := os.WriteFile(toolsFilePath, []byte(toolsFileContent), 0644); err != nil { |
| 47 | + t.Fatalf("failed to write tools file: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + args := []string{ |
| 51 | + "skills-generate", |
| 52 | + "--tools-file", toolsFilePath, |
| 53 | + "--output-dir", outputDir, |
| 54 | + "--name", "hello-sqlite", |
| 55 | + "--description", "hello tool", |
| 56 | + } |
| 57 | + |
| 58 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 59 | + defer cancel() |
| 60 | + |
| 61 | + _, got, err := invokeCommandWithContext(ctx, args) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("command failed: %v\nOutput: %s", err, got) |
| 64 | + } |
| 65 | + |
| 66 | + // Verify generated directory structure |
| 67 | + skillPath := filepath.Join(outputDir, "hello-sqlite") |
| 68 | + if _, err := os.Stat(skillPath); os.IsNotExist(err) { |
| 69 | + t.Fatalf("skill directory not created: %s", skillPath) |
| 70 | + } |
| 71 | + |
| 72 | + // Check SKILL.md |
| 73 | + skillMarkdown := filepath.Join(skillPath, "SKILL.md") |
| 74 | + content, err := os.ReadFile(skillMarkdown) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("failed to read SKILL.md: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + expectedFrontmatter := `--- |
| 80 | +name: hello-sqlite |
| 81 | +description: hello tool |
| 82 | +---` |
| 83 | + if !strings.HasPrefix(string(content), expectedFrontmatter) { |
| 84 | + t.Errorf("SKILL.md does not have expected frontmatter format.\nExpected prefix:\n%s\nGot:\n%s", expectedFrontmatter, string(content)) |
| 85 | + } |
| 86 | + |
| 87 | + if !strings.Contains(string(content), "## Usage") { |
| 88 | + t.Errorf("SKILL.md does not contain '## Usage' section") |
| 89 | + } |
| 90 | + |
| 91 | + if !strings.Contains(string(content), "## Scripts") { |
| 92 | + t.Errorf("SKILL.md does not contain '## Scripts' section") |
| 93 | + } |
| 94 | + |
| 95 | + if !strings.Contains(string(content), "### hello-sqlite") { |
| 96 | + t.Errorf("SKILL.md does not contain '### hello-sqlite' tool header") |
| 97 | + } |
| 98 | + |
| 99 | + // Check script file |
| 100 | + scriptFilename := "hello-sqlite.js" |
| 101 | + scriptPath := filepath.Join(skillPath, "scripts", scriptFilename) |
| 102 | + if _, err := os.Stat(scriptPath); os.IsNotExist(err) { |
| 103 | + t.Fatalf("script file not created: %s", scriptPath) |
| 104 | + } |
| 105 | + |
| 106 | + scriptContent, err := os.ReadFile(scriptPath) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("failed to read script file: %v", err) |
| 109 | + } |
| 110 | + if !strings.Contains(string(scriptContent), "hello-sqlite") { |
| 111 | + t.Errorf("script file does not contain expected tool name") |
| 112 | + } |
| 113 | + |
| 114 | + // Check assets |
| 115 | + assetPath := filepath.Join(skillPath, "assets", "hello-sqlite.yaml") |
| 116 | + if _, err := os.Stat(assetPath); os.IsNotExist(err) { |
| 117 | + t.Fatalf("asset file not created: %s", assetPath) |
| 118 | + } |
| 119 | + assetContent, err := os.ReadFile(assetPath) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("failed to read asset file: %v", err) |
| 122 | + } |
| 123 | + if !strings.Contains(string(assetContent), "hello-sqlite") { |
| 124 | + t.Errorf("asset file does not contain expected tool name") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestGenerateSkill_NoConfig(t *testing.T) { |
| 129 | + tmpDir := t.TempDir() |
| 130 | + outputDir := filepath.Join(tmpDir, "skills") |
| 131 | + |
| 132 | + args := []string{ |
| 133 | + "skills-generate", |
| 134 | + "--output-dir", outputDir, |
| 135 | + "--name", "test", |
| 136 | + "--description", "test", |
| 137 | + } |
| 138 | + |
| 139 | + _, _, err := invokeCommandWithContext(context.Background(), args) |
| 140 | + if err == nil { |
| 141 | + t.Fatal("expected command to fail when no configuration is provided and tools.yaml is missing") |
| 142 | + } |
| 143 | + |
| 144 | + // Should not have created the directory if no config was processed |
| 145 | + if _, err := os.Stat(outputDir); !os.IsNotExist(err) { |
| 146 | + t.Errorf("output directory should not have been created") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestGenerateSkill_MissingArguments(t *testing.T) { |
| 151 | + tmpDir := t.TempDir() |
| 152 | + toolsFilePath := filepath.Join(tmpDir, "tools.yaml") |
| 153 | + if err := os.WriteFile(toolsFilePath, []byte("tools: {}"), 0644); err != nil { |
| 154 | + t.Fatalf("failed to write tools file: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + tests := []struct { |
| 158 | + name string |
| 159 | + args []string |
| 160 | + }{ |
| 161 | + { |
| 162 | + name: "missing name", |
| 163 | + args: []string{"skills-generate", "--tools-file", toolsFilePath, "--description", "test"}, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "missing description", |
| 167 | + args: []string{"skills-generate", "--tools-file", toolsFilePath, "--name", "test"}, |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + for _, tt := range tests { |
| 172 | + t.Run(tt.name, func(t *testing.T) { |
| 173 | + _, got, err := invokeCommandWithContext(context.Background(), tt.args) |
| 174 | + if err == nil { |
| 175 | + t.Fatalf("expected command to fail due to missing arguments, but it succeeded\nOutput: %s", got) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments