|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2e_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + |
| 16 | + "github.com/stacklok/toolhive/test/e2e" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("Skills CLI", Label("api", "cli", "skills", "e2e"), func() { |
| 20 | + var ( |
| 21 | + config *e2e.ServerConfig |
| 22 | + apiServer *e2e.Server |
| 23 | + thvConfig *e2e.TestConfig |
| 24 | + ) |
| 25 | + |
| 26 | + BeforeEach(func() { |
| 27 | + config = e2e.NewServerConfig() |
| 28 | + apiServer = e2e.StartServer(config) |
| 29 | + thvConfig = e2e.NewTestConfig() |
| 30 | + }) |
| 31 | + |
| 32 | + // thvSkillCmd creates a THVCommand for `thv skill <args>` with |
| 33 | + // TOOLHIVE_API_URL pointing to the test server. |
| 34 | + thvSkillCmd := func(args ...string) *e2e.THVCommand { |
| 35 | + fullArgs := append([]string{"skill"}, args...) |
| 36 | + return e2e.NewTHVCommand(thvConfig, fullArgs...). |
| 37 | + WithEnv("TOOLHIVE_API_URL=" + apiServer.BaseURL()) |
| 38 | + } |
| 39 | + |
| 40 | + Describe("thv skill validate", func() { |
| 41 | + It("should succeed for a valid skill directory", func() { |
| 42 | + skillDir := createTestSkillDir("cli-valid-skill", "A valid skill for CLI testing") |
| 43 | + |
| 44 | + stdout, _ := thvSkillCmd("validate", skillDir).ExpectSuccess() |
| 45 | + // Text output should not contain "Error:" lines for a valid skill |
| 46 | + Expect(stdout).ToNot(ContainSubstring("Error:")) |
| 47 | + }) |
| 48 | + |
| 49 | + It("should succeed with JSON output", func() { |
| 50 | + skillDir := createTestSkillDir("cli-valid-json", "A valid skill for JSON output") |
| 51 | + |
| 52 | + stdout, _ := thvSkillCmd("validate", "--format", "json", skillDir).ExpectSuccess() |
| 53 | + |
| 54 | + var result validationResultResponse |
| 55 | + Expect(json.Unmarshal([]byte(stdout), &result)).To(Succeed()) |
| 56 | + Expect(result.Valid).To(BeTrue()) |
| 57 | + }) |
| 58 | + |
| 59 | + It("should fail for an invalid skill directory", func() { |
| 60 | + emptyDir := GinkgoT().TempDir() |
| 61 | + |
| 62 | + _, _, err := thvSkillCmd("validate", emptyDir).Run() |
| 63 | + Expect(err).To(HaveOccurred(), "validate should fail for directory without SKILL.md") |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + Describe("thv skill build", func() { |
| 68 | + It("should build a valid skill and print the reference", func() { |
| 69 | + skillDir := createTestSkillDir("cli-build-skill", "A skill for CLI build testing") |
| 70 | + |
| 71 | + stdout, _ := thvSkillCmd("build", skillDir).ExpectSuccess() |
| 72 | + // The build command should output something (the reference) |
| 73 | + Expect(strings.TrimSpace(stdout)).ToNot(BeEmpty()) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + Describe("thv skill install and list", func() { |
| 78 | + It("should install a skill and list it", func() { |
| 79 | + skillName := fmt.Sprintf("cli-install-%d", GinkgoRandomSeed()) |
| 80 | + |
| 81 | + By("Installing the skill") |
| 82 | + thvSkillCmd("install", skillName).ExpectSuccess() |
| 83 | + |
| 84 | + By("Listing skills in text format — should show the installed skill") |
| 85 | + stdout, _ := thvSkillCmd("list").ExpectSuccess() |
| 86 | + Expect(stdout).To(ContainSubstring(skillName)) |
| 87 | + |
| 88 | + By("Listing skills in JSON format") |
| 89 | + jsonOut, _ := thvSkillCmd("list", "--format", "json").ExpectSuccess() |
| 90 | + var skills []json.RawMessage |
| 91 | + Expect(json.Unmarshal([]byte(jsonOut), &skills)).To(Succeed()) |
| 92 | + Expect(skills).ToNot(BeEmpty()) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + Describe("thv skill info", func() { |
| 97 | + It("should show info for an installed skill", func() { |
| 98 | + skillName := fmt.Sprintf("cli-info-%d", GinkgoRandomSeed()) |
| 99 | + |
| 100 | + By("Installing the skill") |
| 101 | + thvSkillCmd("install", skillName).ExpectSuccess() |
| 102 | + |
| 103 | + By("Getting info in text format") |
| 104 | + stdout, _ := thvSkillCmd("info", skillName).ExpectSuccess() |
| 105 | + Expect(stdout).To(ContainSubstring(skillName)) |
| 106 | + |
| 107 | + By("Getting info in JSON format") |
| 108 | + jsonOut, _ := thvSkillCmd("info", "--format", "json", skillName).ExpectSuccess() |
| 109 | + Expect(jsonOut).To(ContainSubstring(skillName)) |
| 110 | + }) |
| 111 | + |
| 112 | + It("should fail for a non-existent skill", func() { |
| 113 | + _, _, err := thvSkillCmd("info", "no-such-skill-xyz").Run() |
| 114 | + Expect(err).To(HaveOccurred()) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + Describe("thv skill uninstall", func() { |
| 119 | + It("should uninstall an installed skill", func() { |
| 120 | + skillName := fmt.Sprintf("cli-uninstall-%d", GinkgoRandomSeed()) |
| 121 | + |
| 122 | + By("Installing the skill") |
| 123 | + thvSkillCmd("install", skillName).ExpectSuccess() |
| 124 | + |
| 125 | + By("Uninstalling the skill") |
| 126 | + thvSkillCmd("uninstall", skillName).ExpectSuccess() |
| 127 | + |
| 128 | + By("Verifying the skill is no longer listed") |
| 129 | + stdout, _ := thvSkillCmd("list").ExpectSuccess() |
| 130 | + Expect(stdout).ToNot(ContainSubstring(skillName)) |
| 131 | + }) |
| 132 | + |
| 133 | + It("should fail for a non-existent skill", func() { |
| 134 | + _, _, err := thvSkillCmd("uninstall", "no-such-skill-xyz").Run() |
| 135 | + Expect(err).To(HaveOccurred()) |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + Describe("CLI full lifecycle", func() { |
| 140 | + It("should support validate → build → install → list → info → uninstall → list", func() { |
| 141 | + skillName := fmt.Sprintf("cli-lifecycle-%d", GinkgoRandomSeed()) |
| 142 | + |
| 143 | + By("Creating a valid skill directory") |
| 144 | + parentDir := GinkgoT().TempDir() |
| 145 | + skillDir := filepath.Join(parentDir, skillName) |
| 146 | + Expect(os.MkdirAll(skillDir, 0o755)).To(Succeed()) |
| 147 | + |
| 148 | + skillMD := fmt.Sprintf(`--- |
| 149 | +name: %s |
| 150 | +description: Full lifecycle CLI test |
| 151 | +version: 1.0.0 |
| 152 | +--- |
| 153 | +
|
| 154 | +# %s |
| 155 | +
|
| 156 | +A test skill for the full CLI lifecycle. |
| 157 | +`, skillName, skillName) |
| 158 | + Expect(os.WriteFile( |
| 159 | + filepath.Join(skillDir, "SKILL.md"), |
| 160 | + []byte(skillMD), |
| 161 | + 0o644, |
| 162 | + )).To(Succeed()) |
| 163 | + |
| 164 | + By("Validating the skill") |
| 165 | + thvSkillCmd("validate", skillDir).ExpectSuccess() |
| 166 | + |
| 167 | + By("Building the skill") |
| 168 | + thvSkillCmd("build", skillDir).ExpectSuccess() |
| 169 | + |
| 170 | + By("Installing the skill by name (pending)") |
| 171 | + thvSkillCmd("install", skillName).ExpectSuccess() |
| 172 | + |
| 173 | + By("Listing skills — should contain the skill") |
| 174 | + listOut, _ := thvSkillCmd("list").ExpectSuccess() |
| 175 | + Expect(listOut).To(ContainSubstring(skillName)) |
| 176 | + |
| 177 | + By("Getting skill info") |
| 178 | + infoOut, _ := thvSkillCmd("info", skillName).ExpectSuccess() |
| 179 | + Expect(infoOut).To(ContainSubstring(skillName)) |
| 180 | + |
| 181 | + By("Uninstalling the skill") |
| 182 | + thvSkillCmd("uninstall", skillName).ExpectSuccess() |
| 183 | + |
| 184 | + By("Listing skills — should no longer contain the skill") |
| 185 | + listOut2, _ := thvSkillCmd("list").ExpectSuccess() |
| 186 | + Expect(listOut2).ToNot(ContainSubstring(skillName)) |
| 187 | + }) |
| 188 | + }) |
| 189 | +}) |
0 commit comments