Files
skillshare/tests/integration/install_project_test.go
T
Willie 898cde8add fix(install): reload metadata before project reconcile
Config-driven `install -p` wrote new entries to .metadata.json but then
reconciled with the store loaded before the install. Skills installed as
plain copies (subdir sources, no .git) were not in that stale store and
not detectable as tracked repos, so reconcile pruned their entries from
config.yaml, breaking the teammate `install -p && sync` flow.

Move the reload into reconcileProjectRemoteSkills so every caller
(config install, direct install, search) sees the on-disk state.

Refs #280
2026-09-10 18:27:43 +08:00

213 lines
7.0 KiB
Go

//go:build !online
package integration
import (
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/install"
"skillshare/internal/testutil"
)
func TestInstallProject_LocalPath(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
// Create a source skill to install from
sourceSkill := filepath.Join(sb.Root, "external-skill")
os.MkdirAll(sourceSkill, 0755)
os.WriteFile(filepath.Join(sourceSkill, "SKILL.md"), []byte("---\nname: external-skill\n---\n# External"), 0644)
result := sb.RunCLIInDir(projectRoot, "install", sourceSkill, "-p")
result.AssertSuccess(t)
// Skill name comes from directory name, not frontmatter
if !sb.FileExists(filepath.Join(projectRoot, ".skillshare", "skills", "external-skill", "SKILL.md")) {
t.Error("skill should be installed to .skillshare/skills/")
}
}
func TestInstallProject_CurrentDirectoryRejected(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
os.WriteFile(filepath.Join(projectRoot, "SKILL.md"), []byte("---\nname: project-root\n---\n# Project"), 0644)
result := sb.RunCLIInDir(projectRoot, "install", "./", "--name", "project-root", "-p", "--skip-audit")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "cannot install the project root into itself")
result.AssertAnyOutputContains(t, "skillshare install ./my-skill -p")
if sb.FileExists(filepath.Join(projectRoot, ".skillshare", "skills", "project-root")) {
t.Fatal("project root install should not create a skill")
}
}
func TestInstallProject_CustomName(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
sourceSkill := filepath.Join(sb.Root, "my-source")
os.MkdirAll(sourceSkill, 0755)
os.WriteFile(filepath.Join(sourceSkill, "SKILL.md"), []byte("---\nname: original\n---\n# S"), 0644)
result := sb.RunCLIInDir(projectRoot, "install", sourceSkill, "--name", "custom", "-p")
result.AssertSuccess(t)
if !sb.FileExists(filepath.Join(projectRoot, ".skillshare", "skills", "custom", "SKILL.md")) {
t.Error("skill should be installed with custom name")
}
}
func TestInstallProject_DryRun(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
sourceSkill := filepath.Join(sb.Root, "dry-skill")
os.MkdirAll(sourceSkill, 0755)
os.WriteFile(filepath.Join(sourceSkill, "SKILL.md"), []byte("---\nname: dry\n---\n# D"), 0644)
result := sb.RunCLIInDir(projectRoot, "install", sourceSkill, "--dry-run", "-p")
result.AssertSuccess(t)
if sb.FileExists(filepath.Join(projectRoot, ".skillshare", "skills", "dry")) {
t.Error("dry-run should not install skill")
}
}
func TestInstallProject_TrackRequiresGitSource(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
// --track with a non-git source should fail with git-related error
result := sb.RunCLIInDir(projectRoot, "install", "/some/path", "--track", "-p")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "git repository source")
}
func TestInstallProject_FromConfig_SkipsExisting(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
// Create an already-installed skill
sb.CreateProjectSkill(projectRoot, "already-here", map[string]string{
"SKILL.md": "# Already",
})
// Write metadata entry so config-based install sees it
skillsDir := filepath.Join(projectRoot, ".skillshare", "skills")
store, err := install.LoadMetadata(skillsDir)
if err != nil {
t.Fatalf("failed to load metadata: %v", err)
}
store.Set("already-here", &install.MetadataEntry{
Source: "someone/skills/already-here",
Type: "github",
})
if err := store.Save(skillsDir); err != nil {
t.Fatalf("failed to save metadata: %v", err)
}
sb.WriteProjectConfig(projectRoot, `targets:
- claude
skills:
- name: already-here
source: someone/skills/already-here
`)
// install (no args) → should skip existing
result := sb.RunCLIInDir(projectRoot, "install", "-p")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "skipped")
}
func TestInstallProject_FromConfig_EmptySkills(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
result := sb.RunCLIInDir(projectRoot, "install", "-p")
result.AssertSuccess(t)
result.AssertAnyOutputContains(t, "No remote skills")
}
func TestInstallProject_NoSource_WithName_Errors(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
result := sb.RunCLIInDir(projectRoot, "install", "-p", "--name", "custom")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "require a source argument")
}
func TestInstallProject_NoSource_WithExclude_Errors(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
result := sb.RunCLIInDir(projectRoot, "install", "-p", "--exclude", "skip-me")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "require a source argument")
}
func TestInstallProject_AuditThresholdShortFlag_BlocksHighFinding(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
highPath := filepath.Join(sb.Root, "project-flag-high")
os.MkdirAll(highPath, 0755)
os.WriteFile(filepath.Join(highPath, "SKILL.md"),
[]byte("---\nname: project-flag-high\n---\n# CI helper\nsudo apt-get install -y jq"), 0644)
result := sb.RunCLIInDir(projectRoot, "install", highPath, "-p", "-T", "high")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "at/above HIGH")
projectSkillPath := filepath.Join(projectRoot, ".skillshare", "skills", "project-flag-high", "SKILL.md")
if sb.FileExists(projectSkillPath) {
t.Error("project install should be blocked when -T high is set")
}
}
// Regression for #280: config-driven `install -p` must not prune the skills:
// entries it just installed from config.yaml. Uses a subdir source so the
// skill is a plain copy (no .git) — the case the stale store cannot see.
func TestInstallProject_FromConfig_KeepsConfigSkills(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
repoPath := filepath.Join(sb.Root, "cfg-src")
os.MkdirAll(filepath.Join(repoPath, "skills", "cfg-skill"), 0755)
os.WriteFile(filepath.Join(repoPath, "skills", "cfg-skill", "SKILL.md"), []byte("---\nname: cfg-skill\n---\n# Cfg"), 0644)
initGitRepo(t, repoPath)
sb.WriteProjectConfig(projectRoot, `targets:
- claude
skills:
- name: cfg-skill
source: file://`+repoPath+`//skills/cfg-skill
`)
sb.RunCLIInDir(projectRoot, "install", "-p").AssertSuccess(t)
if !sb.FileExists(filepath.Join(projectRoot, ".skillshare", "skills", "cfg-skill", "SKILL.md")) {
t.Fatal("skill should be installed from config")
}
cfg := sb.ReadFile(filepath.Join(projectRoot, ".skillshare", "config.yaml"))
if !strings.Contains(cfg, "name: cfg-skill") {
t.Errorf("config.yaml lost the skills entry after install -p:\n%s", cfg)
}
}