Files
skillshare/internal/server/handler_uninstall_test.go
T
Willie 0c010c55f4 fix: wire custom sources into server reload and gitignore paths
reloadConfig now updates s.cfg.Source and s.cfg.AgentsSource from the
refreshed ProjectConfig so project UI handlers see the latest source
directories after a config edit.

Add ProjectGitignoreTarget helper that computes the correct .gitignore
directory and entry prefix based on whether the source path is inside
.skillshare/ or a custom location. All gitignore write and remove
operations (reconcile, install, uninstall, server handlers) now use
this helper instead of hardcoded 'skills/' prefix.

Fix nil projectCfg panics in three server tests that simulate project
mode by setting projectRoot without projectCfg.
2026-05-20 16:12:02 +08:00

93 lines
2.7 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
"skillshare/internal/install"
)
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.projectCfg = &config.ProjectConfig{}
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.skillsStore = install.NewMetadataStore()
s.skillsStore.Set("_team-skills", &install.MetadataEntry{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.skillsStore = install.NewMetadataStore()
s.skillsStore.Set("_team-skills", &install.MetadataEntry{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")
}
}