Files
skillshare/tests/integration/install_project_test.go
T
Willie f28e5e23f4 fix(install): keep local installs updatable from any directory and in shape (#336)
* fix(install): keep local installs updatable from any directory and in shape

Follow-ups to local-source change detection (#334):

- Global installs recorded a relative local path as typed, so `update`
  and `check` resolved it against whatever directory they ran in and
  failed with "source path does not exist". Global CLI and dashboard
  installs now record the absolute path. Project mode keeps the path as
  written, because .skillshare/config.yaml is shared across machines.
- The dashboard installs the root of a directory that also holds child
  skills as its SKILL.md alone, but `update` re-copied the whole
  directory, pulling in child skills and unrelated files. Updates now
  keep that shape. The detection is shared with `check` so both agree on
  which files belong to the install.

* fix(install): never narrow a whole-directory update to SKILL.md alone

The SKILL.md-only shape was inferred from a child skill in the source that
the recorded hashes lack. A whole-directory install whose source gained a
child skill later matched that too, so `update` replaced it with SKILL.md
alone and deleted its other files. Decide from the recorded files instead:
only an install that recorded SKILL.md alone keeps that shape.
2026-10-02 20:30:27 +08:00

229 lines
7.7 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_RelativeLocalPath_StaysRelative(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
projectRoot := sb.SetupProjectDir("claude")
sb.WriteFile(filepath.Join(projectRoot, "vendor", "team-skill", "SKILL.md"), "---\nname: team-skill\n---\n# Team")
sb.RunCLIInDir(projectRoot, "install", "./vendor/team-skill", "-p").AssertSuccess(t)
// The project config is committed and replayed on other machines, so a
// relative source must not be rewritten to this machine's absolute path.
cfg := sb.ReadFile(filepath.Join(projectRoot, ".skillshare", "config.yaml"))
if !strings.Contains(cfg, "source: ./vendor/team-skill") {
t.Errorf("project config should keep the relative source, got:\n%s", cfg)
}
}
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)
}
}