Files
Willie 55161e44ba fix(check): keep --json output valid when a name or group does not resolve (#868)
check <name> --json printed its warnings and the group expansion note on stdout before the JSON, so the output could not be parsed. When no name or group resolved it then printed an empty result and exited 0, while text mode exits 1, so a CI gate with a mistyped name passed.

With --json, check now exits 1 with an error object when nothing resolves, lists skipped names in a warnings field when some do, and reports every other failure (bad flag combination, missing config) as the same error object. Text mode is unchanged.
2026-11-12 00:35:40 +08:00

998 lines
30 KiB
Go

//go:build !online
package integration
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"skillshare/internal/install"
"skillshare/internal/testutil"
)
func TestCheck_NoItems(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("check")
result.AssertSuccess(t)
result.AssertOutputContains(t, "No tracked repositories or updatable skills found")
}
func TestCheck_Help(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("check", "--help")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Check for available updates")
}
func TestCheck_TrackedRepo_UpToDate(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Create a "remote" bare repo
remoteRepo := filepath.Join(sb.Root, "remote-repo.git")
gitInit(t, remoteRepo, true) // bare repo
// Clone it as a tracked repo
trackedPath := filepath.Join(sb.SourcePath, "_test-repo")
gitClone(t, remoteRepo, trackedPath)
// Create a skill inside so it's discovered
os.MkdirAll(filepath.Join(trackedPath, "my-skill"), 0755)
os.WriteFile(filepath.Join(trackedPath, "my-skill", "SKILL.md"), []byte("# Test"), 0644)
gitAddCommit(t, trackedPath, "add skill")
// Push to remote so origin is in sync
gitPush(t, trackedPath)
result := sb.RunCLI("check")
result.AssertSuccess(t)
result.AssertOutputContains(t, "up to date")
}
func TestCheck_TrackedRepo_Behind(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
// Now tracked repo is behind
result := sb.RunCLI("check")
result.AssertSuccess(t)
result.AssertOutputContains(t, "behind")
}
// behindTrackedRepo sets up a tracked repo that is one commit behind its remote.
func behindTrackedRepo(t *testing.T, sb *testutil.Sandbox) {
t.Helper()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
remoteRepo := filepath.Join(sb.Root, "remote-repo.git")
gitInit(t, remoteRepo, true)
trackedPath := filepath.Join(sb.SourcePath, "_test-repo")
gitClone(t, remoteRepo, trackedPath)
os.MkdirAll(filepath.Join(trackedPath, "my-skill"), 0755)
os.WriteFile(filepath.Join(trackedPath, "my-skill", "SKILL.md"), []byte("# Test"), 0644)
gitAddCommit(t, trackedPath, "initial")
gitPush(t, trackedPath)
otherClone := filepath.Join(sb.Root, "other-clone")
gitClone(t, remoteRepo, otherClone)
os.WriteFile(filepath.Join(otherClone, "my-skill", "SKILL.md"), []byte("# Updated"), 0644)
gitAddCommit(t, otherClone, "update from other")
gitPush(t, otherClone)
}
func TestCheck_ExitCode_FailsWhenBehind(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
result := sb.RunCLI("check", "--exit-code")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "1 item not up to date")
}
func TestCheck_ExitCode_JSONKeepsResults(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
result := sb.RunCLI("check", "--json", "--exit-code")
result.AssertExitCode(t, 1)
var output struct {
TrackedRepos []struct {
Status string `json:"status"`
} `json:"tracked_repos"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON output: %v\noutput: %s", err, result.Stdout)
}
if len(output.TrackedRepos) != 1 || output.TrackedRepos[0].Status != "behind" {
t.Errorf("expected one behind repo, got %+v", output.TrackedRepos)
}
}
func TestCheck_ExitCode_SucceedsWhenUpToDate(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
sb.CreateSkill("local-skill", map[string]string{"SKILL.md": "# Local"})
writeMetaEntry(t, filepath.Join(sb.SourcePath, "local-skill"), &install.MetadataEntry{
Source: "/local/path",
Type: "local",
})
sb.RunCLI("check", "--exit-code").AssertSuccess(t)
}
func TestCheck_ExitCode_SingleNameFailsWhenBehind(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
result := sb.RunCLI("check", "_test-repo", "--exit-code")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "1 item not up to date")
}
func TestCheckProject_ExitCode_FailsOnceSourceChanges(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
skillFile := filepath.Join(projectRoot, "vendor", "team-skill", "SKILL.md")
sb.WriteFile(skillFile, "---\nname: team-skill\n---\n# v1")
sb.RunCLIInDir(projectRoot, "install", "./vendor/team-skill", "-p").AssertSuccess(t)
sb.RunCLIInDir(projectRoot, "check", "-p", "--exit-code").AssertSuccess(t)
sb.WriteFile(skillFile, "---\nname: team-skill\n---\n# v2")
sb.RunCLIInDir(projectRoot, "check", "-p", "--exit-code").AssertExitCode(t, 1)
}
// driftedAgent leaves the sandbox with one installed agent, tutor, whose file
// no longer matches the hash recorded at install.
func driftedAgent(t *testing.T, sb *testutil.Sandbox) {
t.Helper()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
agentsDir := filepath.Join(filepath.Dir(sb.SourcePath), "agents")
os.MkdirAll(agentsDir, 0755)
os.WriteFile(filepath.Join(agentsDir, "tutor.md"), []byte("# Tutor, edited"), 0644)
store, err := install.LoadMetadata(agentsDir)
if err != nil {
t.Fatalf("load agent metadata: %v", err)
}
store.Set("tutor", &install.MetadataEntry{
Source: "owner/agents",
Kind: "agent",
FileHashes: map[string]string{"tutor.md": "sha256:0000"},
})
if err := store.Save(agentsDir); err != nil {
t.Fatalf("save agent metadata: %v", err)
}
}
func TestCheck_Agents_ExitCode_FailsWhenDrifted(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
driftedAgent(t, sb)
sb.RunCLI("check", "agents").AssertSuccess(t)
result := sb.RunCLI("check", "agents", "--exit-code")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "1 item not up to date")
}
func TestCheck_All_ExitCode_FailsWhenAgentDrifted(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
driftedAgent(t, sb)
result := sb.RunCLI("check", "--all", "--exit-code")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "1 item not up to date")
}
func TestCheck_All_ExitCode_CountsSkillsAndAgents(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
driftedAgent(t, sb)
result := sb.RunCLI("check", "--all", "--exit-code")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "2 items not up to date")
}
func TestCheck_All_JSON_ListsAgentsBesideSkills(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
driftedAgent(t, sb)
result := sb.RunCLI("check", "--all", "--json")
result.AssertSuccess(t)
var out struct {
TrackedRepos []any `json:"tracked_repos"`
Skills []any `json:"skills"`
Agents []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"agents"`
}
if err := json.Unmarshal([]byte(result.Stdout), &out); err != nil {
t.Fatalf("check --all --json is not one JSON object: %v\n%s", err, result.Stdout)
}
if len(out.Agents) != 1 || out.Agents[0].Name != "tutor" || out.Agents[0].Status != "drifted" {
t.Errorf("agents = %+v, want tutor drifted", out.Agents)
}
}
func TestCheck_All_JSON_NoAgents_ListsNone(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("check", "--all", "--json")
result.AssertSuccess(t)
if !strings.Contains(result.Stdout, `"agents": []`) {
t.Errorf("want an empty agents array, got:\n%s", result.Stdout)
}
}
func TestCheck_JSON_WithoutAll_HasNoAgentsKey(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
driftedAgent(t, sb)
result := sb.RunCLI("check", "--json")
result.AssertSuccess(t)
if strings.Contains(result.Stdout, `"agents"`) {
t.Errorf("check --json should keep its shape, got:\n%s", result.Stdout)
}
}
func TestCheck_All_RejectsNames(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
driftedAgent(t, sb)
result := sb.RunCLI("check", "--all", "tutor")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "--all cannot be combined with names or --group")
}
func TestCheckProject_All_ReportsAgents(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
sb.CreateProjectSkill(projectRoot, "local-skill", map[string]string{
"SKILL.md": "---\nname: local-skill\n---\n# Local",
})
agentsDir := filepath.Join(projectRoot, ".skillshare", "agents")
os.MkdirAll(agentsDir, 0755)
os.WriteFile(filepath.Join(agentsDir, "reviewer.md"), []byte("# Reviewer"), 0644)
result := sb.RunCLIInDir(projectRoot, "check", "-p", "--all")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "reviewer")
}
func TestCheckProject_All_ChecksAgentsWithoutSkillsDir(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
os.RemoveAll(filepath.Join(projectRoot, ".skillshare", "skills"))
agentsDir := filepath.Join(projectRoot, ".skillshare", "agents")
os.MkdirAll(agentsDir, 0755)
os.WriteFile(filepath.Join(agentsDir, "reviewer.md"), []byte("# Reviewer"), 0644)
result := sb.RunCLIInDir(projectRoot, "check", "-p", "--all")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "reviewer")
}
func TestCheck_UnknownName_JSON_Fails(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
assertJSONError(t, sb.RunCLI("check", "no-such-skill", "--json"), "no-such-skill")
}
// assertJSONError fails unless the command exited 1 with stdout holding one
// JSON object whose error names want.
func assertJSONError(t *testing.T, result *testutil.Result, want string) {
t.Helper()
result.AssertExitCode(t, 1)
var out struct {
Error string `json:"error"`
}
if err := json.Unmarshal([]byte(result.Stdout), &out); err != nil {
t.Fatalf("stdout is not one JSON object: %v\n%s", err, result.Stdout)
}
if !strings.Contains(out.Error, want) {
t.Errorf("error = %q, want it to contain %q", out.Error, want)
}
}
func TestCheck_JSON_TwoUnknownNames_NamesBoth(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
result := sb.RunCLI("check", "first-typo", "second-typo", "--json")
assertJSONError(t, result, "first-typo")
assertJSONError(t, result, "second-typo")
}
func TestCheck_JSON_UnknownGroup_Fails(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
assertJSONError(t, sb.RunCLI("check", "--group", "no-such-group", "--json"), "no-such-group")
}
func TestCheck_JSON_GroupOfLocalSkillsOnly_Fails(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
localSkill := filepath.Join(sb.SourcePath, "mine", "notes")
os.MkdirAll(localSkill, 0755)
os.WriteFile(filepath.Join(localSkill, "SKILL.md"), []byte("---\nname: notes\n---\n# Notes"), 0644)
for _, args := range [][]string{{"check", "mine", "--json"}, {"check", "--group", "mine", "--json"}} {
assertJSONError(t, sb.RunCLI(args...), "no updatable skills in group")
}
}
func TestCheck_JSON_UnknownName_ExitCodeFlag_Fails(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
assertJSONError(t, sb.RunCLI("check", "no-such-skill", "--json", "--exit-code"), "no-such-skill")
}
func TestCheck_JSON_InvalidFlagCombination_IsJSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
assertJSONError(t, sb.RunCLI("check", "--all", "_test-repo", "--json"), "--all cannot be combined")
}
func TestCheckProject_JSON_UnknownName_Fails(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
assertJSONError(t, sb.RunCLIInDir(projectRoot, "check", "-p", "no-such-skill", "--json"), "no-such-skill")
}
func TestCheckProject_JSON_NoProjectConfig_IsJSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
emptyDir := filepath.Join(sb.Root, "not-a-project")
os.MkdirAll(emptyDir, 0755)
assertJSONError(t, sb.RunCLIInDir(emptyDir, "check", "-p", "--json"), "no project config")
}
func TestCheck_GroupName_JSON_KeepsStdoutJSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
groupSkill := filepath.Join(sb.SourcePath, "frontend", "ui-kit")
os.MkdirAll(groupSkill, 0755)
os.WriteFile(filepath.Join(groupSkill, "SKILL.md"), []byte("---\nname: ui-kit\n---\n# UI"), 0644)
store, err := install.LoadMetadata(sb.SourcePath)
if err != nil {
t.Fatalf("load metadata: %v", err)
}
store.Set("frontend/ui-kit", &install.MetadataEntry{Source: "owner/repo", Group: "frontend"})
if err := store.Save(sb.SourcePath); err != nil {
t.Fatalf("save metadata: %v", err)
}
result := sb.RunCLI("check", "frontend", "--json")
if !json.Valid([]byte(result.Stdout)) {
t.Errorf("stdout is not JSON:\n%s", result.Stdout)
}
if !strings.Contains(result.Stdout, "ui-kit") {
t.Errorf("the group should expand to ui-kit, got:\n%s", result.Stdout)
}
}
func TestCheck_UnknownNameBesideKnown_JSON_KeepsStdoutJSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
behindTrackedRepo(t, sb)
result := sb.RunCLI("check", "_test-repo", "no-such-skill", "--json")
result.AssertSuccess(t)
var out struct {
TrackedRepos []struct {
Name string `json:"name"`
} `json:"tracked_repos"`
Warnings []string `json:"warnings"`
}
if err := json.Unmarshal([]byte(result.Stdout), &out); err != nil {
t.Fatalf("stdout is not one JSON object: %v\n%s", err, result.Stdout)
}
if len(out.TrackedRepos) != 1 {
t.Errorf("tracked_repos = %+v, want _test-repo", out.TrackedRepos)
}
if len(out.Warnings) != 1 || !strings.Contains(out.Warnings[0], "no-such-skill") {
t.Errorf("warnings = %q, want one naming no-such-skill", out.Warnings)
}
}
func TestCheck_ExitCode_RejectsDiff(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("check", "x", "--diff", "--exit-code")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "--exit-code cannot be combined with --diff")
}
func TestCheck_TrackedRepo_TokenEnvDoesNotBreakFileFetch(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
remoteRepo := filepath.Join(sb.Root, "remote-repo.git")
gitInit(t, remoteRepo, true)
trackedPath := filepath.Join(sb.SourcePath, "_auth-check-repo")
gitClone(t, remoteRepo, trackedPath)
os.MkdirAll(filepath.Join(trackedPath, "my-skill"), 0755)
os.WriteFile(filepath.Join(trackedPath, "my-skill", "SKILL.md"), []byte("# Test"), 0644)
gitAddCommit(t, trackedPath, "initial")
gitPush(t, trackedPath)
t.Setenv("GITHUB_TOKEN", "ghp_fake_token_12345")
t.Setenv("GITLAB_TOKEN", "glpat-fake-token")
t.Setenv("BITBUCKET_TOKEN", "bb-fake-token")
t.Setenv("SKILLSHARE_GIT_TOKEN", "generic-fake-token")
result := sb.RunCLI("check")
result.AssertSuccess(t)
result.AssertOutputContains(t, "up to date")
}
func TestCheck_RegularSkill_ShowsMeta(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Create a skill with metadata (but local source, so check will show "local source")
sb.CreateSkill("my-skill", map[string]string{
"SKILL.md": "# My Skill",
})
writeMetaEntry(t, filepath.Join(sb.SourcePath, "my-skill"), &install.MetadataEntry{
Source: "/local/path",
Type: "local",
})
result := sb.RunCLI("check")
result.AssertSuccess(t)
// Unfiltered check summarizes local skills instead of listing each one
result.AssertOutputContains(t, "1 local skill skipped")
}
func TestCheck_JsonOutput(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Create a skill with metadata
sb.CreateSkill("json-skill", map[string]string{
"SKILL.md": "# JSON Skill",
})
writeMetaEntry(t, filepath.Join(sb.SourcePath, "json-skill"), &install.MetadataEntry{
Source: "/local/path",
Type: "local",
})
result := sb.RunCLI("check", "--json")
result.AssertSuccess(t)
// Verify JSON can be parsed
var output struct {
TrackedRepos []json.RawMessage `json:"tracked_repos"`
Skills []json.RawMessage `json:"skills"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON output: %v\noutput: %s", err, result.Stdout)
}
if len(output.Skills) != 1 {
t.Errorf("expected 1 skill in JSON, got %d", len(output.Skills))
}
}
func TestCheck_JsonOutput_Empty(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("check", "--json")
result.AssertSuccess(t)
var output struct {
TrackedRepos []json.RawMessage `json:"tracked_repos"`
Skills []json.RawMessage `json:"skills"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON output: %v\noutput: %s", err, result.Stdout)
}
if len(output.TrackedRepos) != 0 {
t.Errorf("expected 0 tracked_repos, got %d", len(output.TrackedRepos))
}
if len(output.Skills) != 0 {
t.Errorf("expected 0 skills, got %d", len(output.Skills))
}
}
func TestCheck_LocalSource_DetectsChangeUntilUpdated(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
external := filepath.Join(sb.Root, "app-bundle", "app-skill")
sb.WriteFile(filepath.Join(external, "SKILL.md"), "---\nname: app-skill\n---\n# v1")
sb.RunCLI("install", external).AssertSuccess(t)
checkStatus := func() string {
t.Helper()
result := sb.RunCLI("check", "--json")
result.AssertSuccess(t)
var output checkOutput
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON output: %v\noutput: %s", err, result.Stdout)
}
for _, s := range output.Skills {
if s.Name == "app-skill" {
return s.Status
}
}
t.Fatalf("app-skill missing from check output: %s", result.Stdout)
return ""
}
if got := checkStatus(); got != "up_to_date" {
t.Fatalf("after install: status = %q, want up_to_date", got)
}
sb.WriteFile(filepath.Join(external, "SKILL.md"), "---\nname: app-skill\n---\n# v2")
if got := checkStatus(); got != "update_available" {
t.Fatalf("after source change: status = %q, want update_available", got)
}
sb.RunCLI("update", "app-skill").AssertSuccess(t)
if got := checkStatus(); got != "up_to_date" {
t.Fatalf("after update: status = %q, want up_to_date", got)
}
}
func TestCheck_LocalSource_Missing(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
external := filepath.Join(sb.Root, "app-bundle", "gone-skill")
sb.WriteFile(filepath.Join(external, "SKILL.md"), "---\nname: gone-skill\n---\n# v1")
sb.RunCLI("install", external).AssertSuccess(t)
os.RemoveAll(external)
result := sb.RunCLI("check", "gone-skill")
result.AssertExitCode(t, 1)
result.AssertAnyOutputContains(t, "local source not found")
for _, args := range [][]string{{"check", "--json"}, {"check", "gone-skill", "--json"}} {
result := sb.RunCLI(args...)
result.AssertExitCode(t, 1)
if result.Stderr != "" {
t.Errorf("JSON failure wrote to stderr: %s", result.Stderr)
}
var output checkOutput
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("invalid JSON: %v: %s", err, result.Stdout)
}
if len(output.Skills) != 1 || output.Skills[0].Status != "error" {
t.Fatalf("expected failed skill in JSON: %s", result.Stdout)
}
}
}
// ── Filtered check tests (multi-name + --group) ──────────
func TestCheck_SingleName(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateSkill("alpha", map[string]string{"SKILL.md": "# Alpha"})
writeMeta(t, d1)
d2 := sb.CreateSkill("beta", map[string]string{"SKILL.md": "# Beta"})
writeMeta(t, d2)
result := sb.RunCLI("check", "alpha")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "alpha")
result.AssertOutputNotContains(t, "beta")
}
func TestCheck_MultipleNames(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateSkill("alpha", map[string]string{"SKILL.md": "# Alpha"})
writeMeta(t, d1)
d2 := sb.CreateSkill("beta", map[string]string{"SKILL.md": "# Beta"})
writeMeta(t, d2)
d3 := sb.CreateSkill("gamma", map[string]string{"SKILL.md": "# Gamma"})
writeMeta(t, d3)
result := sb.RunCLI("check", "alpha", "gamma")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "alpha")
result.AssertAnyOutputContains(t, "gamma")
result.AssertOutputNotContains(t, "beta")
}
func TestCheck_Group(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateNestedSkill("frontend/react", map[string]string{"SKILL.md": "# React"})
writeMeta(t, d1)
d2 := sb.CreateNestedSkill("frontend/vue", map[string]string{"SKILL.md": "# Vue"})
writeMeta(t, d2)
result := sb.RunCLI("check", "--group", "frontend")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "react")
result.AssertAnyOutputContains(t, "vue")
}
func TestCheck_Group_SkipsLocal(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateNestedSkill("backend/api", map[string]string{"SKILL.md": "# API"})
writeMeta(t, d1)
// local-only has no metadata, so it's skipped by resolveGroupUpdatable
sb.CreateNestedSkill("backend/local-only", map[string]string{"SKILL.md": "# Local"})
result := sb.RunCLI("check", "--group", "backend")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "api")
result.AssertOutputNotContains(t, "local-only")
}
func TestCheck_GroupNotFound(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
result := sb.RunCLI("check", "--group", "nonexistent")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "nonexistent")
}
func TestCheck_PositionalGroupAutoDetect(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateNestedSkill("mygroup/s1", map[string]string{"SKILL.md": "# S1"})
writeMeta(t, d1)
result := sb.RunCLI("check", "mygroup")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "is a group")
result.AssertAnyOutputContains(t, "s1")
}
func TestCheck_Mixed_NamesAndGroup(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateSkill("standalone", map[string]string{"SKILL.md": "# Standalone"})
writeMeta(t, d1)
d2 := sb.CreateNestedSkill("frontend/react", map[string]string{"SKILL.md": "# React"})
writeMeta(t, d2)
result := sb.RunCLI("check", "standalone", "--group", "frontend")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "standalone")
result.AssertAnyOutputContains(t, "react")
}
func TestCheck_SingleName_JSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
d1 := sb.CreateSkill("alpha", map[string]string{"SKILL.md": "# Alpha"})
writeMeta(t, d1)
d2 := sb.CreateSkill("beta", map[string]string{"SKILL.md": "# Beta"})
writeMeta(t, d2)
result := sb.RunCLI("check", "alpha", "--json")
result.AssertSuccess(t)
var output checkOutput
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON: %v\noutput: %s", err, result.Stdout)
}
if len(output.Skills) != 1 {
t.Errorf("expected 1 skill in JSON, got %d", len(output.Skills))
}
if len(output.Skills) > 0 && output.Skills[0].Name != "alpha" {
t.Errorf("expected skill name 'alpha', got %q", output.Skills[0].Name)
}
}
func TestCheckProject_MultiNames(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
setupGlobalConfig(sb)
projectRoot := sb.SetupProjectDir("claude")
d1 := sb.CreateProjectSkill(projectRoot, "alpha", map[string]string{"SKILL.md": "# Alpha"})
writeMeta(t, d1)
d2 := sb.CreateProjectSkill(projectRoot, "beta", map[string]string{"SKILL.md": "# Beta"})
writeMeta(t, d2)
d3 := sb.CreateProjectSkill(projectRoot, "gamma", map[string]string{"SKILL.md": "# Gamma"})
writeMeta(t, d3)
result := sb.RunCLIInDir(projectRoot, "check", "alpha", "gamma")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "alpha")
result.AssertAnyOutputContains(t, "gamma")
result.AssertOutputNotContains(t, "beta")
}
// ── Tree hash tests ──────────────────────────────────────
// writeMetaEntry writes a single metadata entry to .metadata.json in the source root.
func writeMetaEntry(t *testing.T, skillDir string, entry *install.MetadataEntry) {
t.Helper()
sourceDir := findSourceRoot(skillDir)
rel, _ := filepath.Rel(sourceDir, skillDir)
store, err := install.LoadMetadata(sourceDir)
if err != nil {
t.Fatalf("writeMetaEntry: load: %v", err)
}
store.Set(rel, entry)
if err := store.Save(sourceDir); err != nil {
t.Fatalf("writeMetaEntry: save: %v", err)
}
}
// ── Tree hash test helpers ────────────────────────────────
func gitRevParse(t *testing.T, dir, ref string) string {
t.Helper()
cmd := exec.Command("git", "rev-parse", ref)
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
t.Fatalf("git rev-parse %s failed: %v", ref, err)
}
return strings.TrimSpace(string(out))
}
func writeMetaWithTreeHash(t *testing.T, skillDir, repoURL, version, treeHash, subdir string) {
t.Helper()
sourceDir := findSourceRoot(skillDir)
rel, _ := filepath.Rel(sourceDir, skillDir)
store, err := install.LoadMetadata(sourceDir)
if err != nil {
t.Fatalf("writeMetaWithTreeHash: load: %v", err)
}
store.Set(rel, &install.MetadataEntry{
Source: repoURL + "//" + subdir,
Type: "github",
RepoURL: repoURL,
Version: version,
TreeHash: treeHash,
Subdir: subdir,
})
if err := store.Save(sourceDir); err != nil {
t.Fatalf("writeMetaWithTreeHash: save: %v", err)
}
}
// checkOutput mirrors the JSON structure for test parsing
type checkOutput struct {
TrackedRepos []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"tracked_repos"`
Skills []struct {
Name string `json:"name"`
Status string `json:"status"`
} `json:"skills"`
}
// ── Git helpers ───────────────────────────────────────────
func gitInit(t *testing.T, dir string, bare bool) {
t.Helper()
if bare {
os.MkdirAll(dir, 0755)
run(t, dir, "git", "init", "--bare")
} else {
os.MkdirAll(dir, 0755)
run(t, dir, "git", "init")
run(t, dir, "git", "config", "user.email", "test@test.com")
run(t, dir, "git", "config", "user.name", "test")
}
}
func gitClone(t *testing.T, remote, dest string) {
t.Helper()
run(t, "", "git", "clone", remote, dest)
run(t, dest, "git", "config", "user.email", "test@test.com")
run(t, dest, "git", "config", "user.name", "test")
}
func gitAddCommit(t *testing.T, dir, msg string) {
t.Helper()
run(t, dir, "git", "add", "-A")
run(t, dir, "git", "commit", "-m", msg)
}
func gitPush(t *testing.T, dir string) {
t.Helper()
// Get current branch name
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
t.Fatalf("git rev-parse failed: %v", err)
}
branch := strings.TrimSpace(string(out))
run(t, dir, "git", "push", "-u", "origin", branch)
}
func TestCheck_CustomTarget_NoUnknownWarning(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
customPath := filepath.Join(sb.Home, ".custom-tool", "skills")
os.MkdirAll(customPath, 0755)
sb.CreateSkill("my-skill", map[string]string{
"SKILL.md": "---\nname: my-skill\ntargets: [claude, custom-tool]\n---\n# My Skill",
})
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + sb.CreateTarget("claude") + `
custom-tool:
path: ` + customPath + `
`)
result := sb.RunCLI("check")
result.AssertSuccess(t)
result.AssertOutputNotContains(t, "unknown target")
}
func run(t *testing.T, dir string, name string, args ...string) {
t.Helper()
cmd := exec.Command(name, args...)
if dir != "" {
cmd.Dir = dir
}
cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=test",
"GIT_AUTHOR_EMAIL=test@test.com",
"GIT_COMMITTER_NAME=test",
"GIT_COMMITTER_EMAIL=test@test.com",
)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s %v failed: %s\n%s", name, args, err, out)
}
}
func TestCheckProject_RelativeLocalSource_DetectsChangeUntilUpdated(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
skillFile := filepath.Join(projectRoot, "vendor", "team-skill", "SKILL.md")
sb.WriteFile(skillFile, "---\nname: team-skill\n---\n# v1")
sb.RunCLIInDir(projectRoot, "install", "./vendor/team-skill", "-p").AssertSuccess(t)
checkStatus := func() string {
t.Helper()
result := sb.RunCLIInDir(projectRoot, "check", "-p", "--json")
result.AssertSuccess(t)
var output checkOutput
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON output: %v\noutput: %s", err, result.Stdout)
}
for _, s := range output.Skills {
if s.Name == "team-skill" {
return s.Status
}
}
t.Fatalf("team-skill missing from check output: %s", result.Stdout)
return ""
}
if got := checkStatus(); got != "up_to_date" {
t.Fatalf("after install: status = %q, want up_to_date", got)
}
sb.WriteFile(skillFile, "---\nname: team-skill\n---\n# v2")
if got := checkStatus(); got != "update_available" {
t.Fatalf("after source change: status = %q, want update_available", got)
}
sb.RunCLIInDir(projectRoot, "update", "team-skill", "-p").AssertSuccess(t)
if got := checkStatus(); got != "up_to_date" {
t.Fatalf("after update: status = %q, want up_to_date", got)
}
}