Files
skillshare/internal/server/handler_uninstall_test.go
T
Willie 20ae89a838 fix(server): stop dropping git and metadata errors in check, uninstall and batch targets
The dashboard check reimplemented the CLI's per-repo check but fetched
without the HTTPS token and ignored git status errors. Private tracked
repos could fail in the dashboard while `skillshare check` worked, and a
repo whose status could not be read showed as up to date. Both check
endpoints now call check.ParallelCheckRepos, the same code the CLI uses.
The JSON and SSE response shape does not change.

Batch uninstall treated a failed dirty check as clean and moved the
repo to trash. Without force, that item now fails with the git error
instead. This is stricter than the CLI, which only warns and continues.

Batch target updates ignored metadata save failures and still reported
success, so an override could silently disappear on the next reload.
They now return 500.
2026-09-30 01:34:58 +08:00

154 lines
5.1 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
"skillshare/internal/install"
)
// Issue #190: a disabled skill (listed in .skillignore) must still be
// uninstallable via the UI. The list handler uses DiscoverSourceSkillsAll
// (includes disabled skills), so the uninstall handler must match it —
// otherwise the skill shows in the UI but uninstall reports "skill not found".
func TestHandleBatchUninstall_DisabledSkill(t *testing.T) {
s, src := newTestServer(t)
addSkill(t, src, "disabled-skill")
// Disable it the way the toggle handler does: add to .skillignore.
os.WriteFile(filepath.Join(src, ".skillignore"), []byte("disabled-skill\n"), 0644)
s.skillsStore = install.NewMetadataStore()
body := batchUninstallRequest{Names: []string{"disabled-skill"}, 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())
}
var resp struct {
Results []batchUninstallItemResult `json:"results"`
Summary batchUninstallSummary `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Summary.Succeeded != 1 || resp.Summary.Failed != 0 {
t.Fatalf("expected 1 succeeded / 0 failed, got %+v (results: %+v)", resp.Summary, resp.Results)
}
if _, err := os.Stat(filepath.Join(src, "disabled-skill")); !os.IsNotExist(err) {
t.Fatal("expected disabled-skill directory to be removed from source")
}
}
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")
}
}
func TestHandleBatchUninstall_DirtyCheckErrorBlocksRepo(t *testing.T) {
s, src := newTestServer(t)
repoDir := addTrackedRepoWithBrokenIndex(t, src, "_team")
b, _ := json.Marshal(batchUninstallRequest{Names: []string{"_team"}})
req := httptest.NewRequest(http.MethodPost, "/api/uninstall", bytes.NewReader(b))
rr := httptest.NewRecorder()
s.handleBatchUninstall(rr, req)
var resp struct {
Summary batchUninstallSummary `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v (%s)", err, rr.Body.String())
}
if resp.Summary.Failed != 1 || resp.Summary.Succeeded != 0 {
t.Fatalf("expected 1 failed / 0 succeeded, got %+v", resp.Summary)
}
if _, err := os.Stat(repoDir); err != nil {
t.Fatalf("expected repo to stay in place: %v", err)
}
}