Files
skillshare/internal/server/handler_uninstall_test.go
T
Willie 115edf3f24 fix(server): correct project-mode gitignore path + prevent registry prune basename collision
P2-1: handleUninstallRepo and handleBatchUninstall used s.cfg.Source for
gitignore removal, but project-mode installs write to .skillshare/.gitignore
with a "skills/" prefix. Add gitignoreDir() helper that returns the correct
base directory per mode, and prefix entries in project mode.

P2-2: Registry prune used filepath.Base(repoName) to derive the legacy group,
causing sibling nested repos (e.g., org/_team-skills vs dept/_team-skills) to
collide on the same basename. Now uses exact Group match for nested repos and
restricts legacy basename matching to top-level repos only.

Tests added (15 new):
- resolveTrackedRepo: auto _ prefix, nested, already-prefixed, not-found,
  basename fallback
- Registry prune: full-path Group, nested members by prefix, sibling isolation
- Project-mode gitignore: single + batch uninstall
- updateSingle: basename fallback, not-found, regular skill priority
- cleanEmptyParents: multi-level, stops at non-empty, stops at boundary
2026-04-04 17:05:22 +08:00

97 lines
2.7 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
)
func TestHandleBatchUninstall_ProjectMode_GitignorePath(t *testing.T) {
s, _ := newTestServer(t)
// Simulate project mode
projectRoot := t.TempDir()
projectSkillsDir := filepath.Join(projectRoot, ".skillshare", "skills")
os.MkdirAll(projectSkillsDir, 0755)
s.projectRoot = projectRoot
s.cfg.Source = projectSkillsDir
// Create tracked repo
addTrackedRepo(t, projectSkillsDir, "_team-skills")
// Write gitignore the way project install does
gitignoreDir := filepath.Join(projectRoot, ".skillshare")
gitignorePath := filepath.Join(gitignoreDir, ".gitignore")
os.WriteFile(gitignorePath, []byte(
"# BEGIN SKILLSHARE MANAGED - DO NOT EDIT\nskills/_team-skills/\n# END SKILLSHARE MANAGED\n",
), 0644)
s.registry = &config.Registry{
Skills: []config.SkillEntry{
{Name: "_team-skills", Tracked: true},
},
}
body := batchUninstallRequest{Names: []string{"_team-skills"}, Force: true}
b, _ := json.Marshal(body)
req := httptest.NewRequest(http.MethodPost, "/api/uninstall", bytes.NewReader(b))
rr := httptest.NewRecorder()
s.handleBatchUninstall(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
data, err := os.ReadFile(gitignorePath)
if err != nil {
t.Fatalf("failed to read gitignore: %v", err)
}
if strings.Contains(string(data), "skills/_team-skills/") {
t.Fatal("expected skills/_team-skills/ to be removed from .skillshare/.gitignore")
}
}
func TestHandleBatchUninstall_GlobalMode_GitignorePath(t *testing.T) {
s, src := newTestServer(t)
// Create tracked repo in global source
addTrackedRepo(t, src, "_team-skills")
// Write gitignore in source dir
gitignorePath := filepath.Join(src, ".gitignore")
os.WriteFile(gitignorePath, []byte(
"# BEGIN SKILLSHARE MANAGED - DO NOT EDIT\n_team-skills/\n# END SKILLSHARE MANAGED\n",
), 0644)
s.registry = &config.Registry{
Skills: []config.SkillEntry{
{Name: "_team-skills", Tracked: true},
},
}
body := batchUninstallRequest{Names: []string{"_team-skills"}, Force: true}
b, _ := json.Marshal(body)
req := httptest.NewRequest(http.MethodPost, "/api/uninstall", bytes.NewReader(b))
rr := httptest.NewRecorder()
s.handleBatchUninstall(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
data, err := os.ReadFile(gitignorePath)
if err != nil {
t.Fatalf("failed to read gitignore: %v", err)
}
if strings.Contains(string(data), "_team-skills/") {
t.Fatal("expected _team-skills/ to be removed from global source .gitignore")
}
}