Files
Willie 18d6b11347 test: test each rule once, in the package that owns it
Many integration and handler tests re-checked rules that internal/
packages already test, duplicated each other, or ran no product code at
all (a few re-implemented the logic inline, two could never fail). They
slowed the suite without catching anything new.

Delete those, keeping tests for CLI wiring, JSON output, exit codes,
project/global mode and cmd-only logic. Move the agents/extras overlap
check into sync.AgentTargetPaths so the CLI and the server share one
implementation and it is covered by unit tests instead of seven
integration tests.
2026-10-08 16:41:09 +08:00

436 lines
12 KiB
Go

//go:build !online
package integration
import (
"encoding/json"
"fmt"
"strings"
"testing"
"skillshare/internal/testutil"
)
func TestAnalyze_TableOutput(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{
"SKILL.md": "---\nname: skill1\ndescription: First skill description\n---\n# Skill 1\nBody content",
})
sb.CreateSkill("skill2", map[string]string{
"SKILL.md": "---\nname: skill2\ndescription: Second skill\n---\n# Skill 2\nMore body",
})
target1 := sb.CreateTarget("claude")
target2 := sb.CreateTarget("cursor")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target1 + `
cursor:
path: ` + target2 + `
`)
result := sb.RunCLI("analyze")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Always loaded ")
result.AssertOutputContains(t, "On-demand max ")
result.AssertOutputContains(t, "claude")
result.AssertOutputContains(t, "cursor")
}
func TestAnalyze_Verbose(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
for i := 1; i <= 12; i++ {
name := fmt.Sprintf("skill%d", i)
desc := strings.Repeat("x", i*50)
content := fmt.Sprintf("---\nname: %s\ndescription: %s\n---\n# %s\nBody", name, desc, name)
sb.CreateSkill(name, map[string]string{"SKILL.md": content})
}
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "--verbose")
result.AssertSuccess(t)
result.AssertOutputContains(t, "claude")
result.AssertOutputContains(t, "12 skills")
result.AssertOutputContains(t, "Always loaded ")
result.AssertOutputContains(t, "Largest descriptions")
result.AssertOutputContains(t, "… 2 more skills")
}
func TestAnalyze_SingleTarget(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{
"SKILL.md": "---\nname: skill1\ndescription: test\n---\n# S1",
})
target1 := sb.CreateTarget("claude")
target2 := sb.CreateTarget("cursor")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target1 + `
cursor:
path: ` + target2 + `
`)
result := sb.RunCLI("analyze", "claude")
result.AssertSuccess(t)
result.AssertOutputContains(t, "claude")
result.AssertOutputNotContains(t, "cursor")
}
func TestAnalyze_JSON(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{
"SKILL.md": "---\nname: skill1\ndescription: A test skill\n---\n# Skill 1\nBody here",
})
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "--json")
result.AssertSuccess(t)
var output struct {
Targets []struct {
Name string `json:"name"`
SkillCount int `json:"skill_count"`
AlwaysLoaded struct {
Chars int `json:"chars"`
EstimatedTokens int `json:"estimated_tokens"`
} `json:"always_loaded"`
OnDemandMax struct {
Chars int `json:"chars"`
} `json:"on_demand_max"`
Skills []struct {
Name string `json:"name"`
DescriptionChars int `json:"description_chars"`
BodyChars int `json:"body_chars"`
} `json:"skills"`
} `json:"targets"`
}
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.Targets) != 1 {
t.Fatalf("expected 1 target, got %d", len(output.Targets))
}
tgt := output.Targets[0]
if tgt.Name != "claude" {
t.Errorf("expected target name 'claude', got %q", tgt.Name)
}
if tgt.SkillCount != 1 {
t.Errorf("expected 1 skill, got %d", tgt.SkillCount)
}
if tgt.AlwaysLoaded.Chars <= 0 {
t.Errorf("expected positive always_loaded.chars, got %d", tgt.AlwaysLoaded.Chars)
}
if tgt.OnDemandMax.Chars <= 0 {
t.Errorf("expected positive on_demand_max.chars, got %d", tgt.OnDemandMax.Chars)
}
if len(tgt.Skills) != 1 {
t.Fatalf("expected 1 skill in list, got %d", len(tgt.Skills))
}
if tgt.Skills[0].DescriptionChars <= 0 {
t.Errorf("expected positive description_chars, got %d", tgt.Skills[0].DescriptionChars)
}
}
func TestAnalyze_EmptySource(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze")
result.AssertSuccess(t)
result.AssertOutputContains(t, "No skills to analyze")
}
func TestAnalyze_NoTargets_SaysSo(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("s1", map[string]string{"SKILL.md": "---\nname: s1\ndescription: d\n---\n"})
sb.WriteConfig("source: " + sb.SourcePath + "\ntargets: {}\n")
result := sb.RunCLI("analyze", "--no-tui")
result.AssertSuccess(t)
result.AssertOutputContains(t, "No targets configured")
}
func TestAnalyze_NoSkillReachesTarget_DoesNotSuggestTargetAdd(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("cursor-only", map[string]string{"SKILL.md": "---\nname: cursor-only\ntargets: [cursor]\ndescription: d\n---\n"})
target := sb.CreateTarget("claude")
sb.WriteConfig("source: " + sb.SourcePath + "\ntargets:\n claude:\n path: " + target + "\n")
result := sb.RunCLI("analyze", "--no-tui")
result.AssertSuccess(t)
result.AssertOutputContains(t, "No skills reach any target")
result.AssertOutputNotContains(t, "target add")
}
func TestAnalyze_UnknownTarget(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{"SKILL.md": "---\nname: s\n---\n# S"})
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "nonexistent")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "not configured")
}
func TestAnalyze_ProjectMode(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectDir := sb.SetupProjectDir("claude")
sb.CreateProjectSkill(projectDir, "proj-skill", map[string]string{
"SKILL.md": "---\nname: proj-skill\ndescription: project skill\n---\n# Project",
})
result := sb.RunCLIInDir(projectDir, "analyze", "-p")
result.AssertSuccess(t)
}
func TestAnalyze_HelpShowsNoTUI(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
result := sb.RunCLI("analyze", "--help")
result.AssertSuccess(t)
result.AssertOutputContains(t, "--no-tui")
}
func TestAnalyze_JSONFilter(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
// Create skills in different groups — use CreateNestedSkill
sb.CreateNestedSkill("frontend/react", map[string]string{
"SKILL.md": "---\nname: react\ndescription: React best practices for building UIs\n---\n# React\nBody content here",
})
sb.CreateNestedSkill("frontend/vue", map[string]string{
"SKILL.md": "---\nname: vue\ndescription: Vue framework patterns\n---\n# Vue\nVue body",
})
sb.CreateNestedSkill("backend/go", map[string]string{
"SKILL.md": "---\nname: go\ndescription: Go patterns and idioms\n---\n# Go\nGo body content",
})
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "claude", "--json", "--filter", "frontend")
result.AssertSuccess(t)
var output struct {
Filter string `json:"filter"`
MatchedCount int `json:"matched_count"`
TotalCount int `json:"total_count"`
FilteredSummary struct {
AlwaysLoaded struct {
Chars int `json:"chars"`
Tokens int `json:"tokens"`
} `json:"always_loaded"`
OnDemand struct {
Chars int `json:"chars"`
Tokens int `json:"tokens"`
} `json:"on_demand"`
Total struct {
Chars int `json:"chars"`
Tokens int `json:"tokens"`
} `json:"total"`
} `json:"filtered_summary"`
Skills []struct {
Name string `json:"name"`
} `json:"skills"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON: %v\noutput: %s", err, result.Stdout)
}
if output.Filter != "frontend" {
t.Errorf("expected filter 'frontend', got %q", output.Filter)
}
if output.MatchedCount != 2 {
t.Errorf("expected 2 matched skills, got %d", output.MatchedCount)
}
if output.TotalCount != 3 {
t.Errorf("expected 3 total skills, got %d", output.TotalCount)
}
if len(output.Skills) != 2 {
t.Fatalf("expected 2 skills in array, got %d", len(output.Skills))
}
if output.FilteredSummary.AlwaysLoaded.Chars <= 0 {
t.Errorf("expected positive always_loaded.chars, got %d", output.FilteredSummary.AlwaysLoaded.Chars)
}
if output.FilteredSummary.Total.Chars <= 0 {
t.Errorf("expected positive total.chars, got %d", output.FilteredSummary.Total.Chars)
}
}
func TestAnalyze_JSONFilterNoMatch(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.CreateSkill("skill1", map[string]string{
"SKILL.md": "---\nname: skill1\ndescription: A test skill\n---\n# Skill 1\nBody",
})
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "claude", "--json", "--filter", "nonexistent")
result.AssertSuccess(t)
var output struct {
MatchedCount int `json:"matched_count"`
TotalCount int `json:"total_count"`
Skills []struct {
Name string `json:"name"`
} `json:"skills"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON: %v\noutput: %s", err, result.Stdout)
}
if output.MatchedCount != 0 {
t.Errorf("expected 0 matched skills, got %d", output.MatchedCount)
}
if output.TotalCount != 1 {
t.Errorf("expected 1 total skill, got %d", output.TotalCount)
}
if len(output.Skills) != 0 {
t.Fatalf("expected 0 skills in array, got %d", len(output.Skills))
}
}
func TestAnalyze_JSONLintIssues(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
// Skill with lint issues: no description, empty body
sb.CreateSkill("bad-skill", map[string]string{
"SKILL.md": "---\nname: bad-skill\n---\n",
})
// Clean skill
sb.CreateSkill("good-skill", map[string]string{
"SKILL.md": "---\nname: good-skill\ndescription: Use this skill when you need to do good things and help users accomplish tasks efficiently\n---\n# Good Skill\nThis is the body content.",
})
target := sb.CreateTarget("claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
path: ` + target + `
`)
result := sb.RunCLI("analyze", "--json")
result.AssertSuccess(t)
var output struct {
Targets []struct {
Name string `json:"name"`
Skills []struct {
Name string `json:"name"`
LintIssues []struct {
Rule string `json:"rule"`
Severity string `json:"severity"`
Category string `json:"category"`
Message string `json:"message"`
} `json:"lint_issues"`
} `json:"skills"`
} `json:"targets"`
}
if err := json.Unmarshal([]byte(result.Stdout), &output); err != nil {
t.Fatalf("failed to parse JSON: %v", err)
}
if len(output.Targets) == 0 {
t.Fatal("expected at least one target")
}
// Find bad-skill and verify it has lint issues
var badSkillIssues int
var goodSkillIssues int
for _, target := range output.Targets {
for _, skill := range target.Skills {
switch skill.Name {
case "bad-skill":
badSkillIssues = len(skill.LintIssues)
hasDesc := false
hasBody := false
for _, issue := range skill.LintIssues {
if issue.Rule == "missing-description" {
hasDesc = true
if issue.Severity != "error" {
t.Errorf("missing-description should be error, got %s", issue.Severity)
}
if issue.Category != "structure" {
t.Errorf("missing-description category should be structure, got %s", issue.Category)
}
}
if issue.Rule == "empty-body" {
hasBody = true
}
}
if !hasDesc {
t.Error("bad-skill should have missing-description issue")
}
if !hasBody {
t.Error("bad-skill should have empty-body issue")
}
case "good-skill":
goodSkillIssues = len(skill.LintIssues)
}
}
}
if badSkillIssues == 0 {
t.Error("bad-skill should have lint issues")
}
if goodSkillIssues != 0 {
t.Errorf("good-skill should have no lint issues, got %d", goodSkillIssues)
}
}