Files
skillshare/internal/server/handler_sync_test.go
T
Willie a570670a92 fix: registry cleanup after uninstall and sync, UI polish
- Fix UI batch uninstall not removing nested skill registry entries:
  handler used flat names (__) but registry stores path names (/),
  now tracks exact RelPath from discovered skills for matching

- Add stale entry pruning to ReconcileGlobalSkills and
  ReconcileProjectSkills using a live set built during disk walk,
  so skills deleted from source are removed from registry

- Trigger registry reconcile after sync in both UI handler (via defer
  for early-return safety) and CLI (global + project mode), writing
  warnings to stderr to avoid corrupting --json output

- UI: fix BatchUninstallPage search treating plain text as exact-match
  glob (now substring search when no * or ? present), display nested
  skill paths with / instead of __ in confirm and result dialogs

- UI: hide redundant 'X repo(s) and Y skill(s) already up to date'
  line on UpdatePage when everything is already current

- Fix test isolation: set XDG_CONFIG_HOME in server test helpers to
  prevent host agents dir leaking into test results, skip chmod-based
  oplog test when running as root

- Add README badge for Awesome Codex CLI
2026-04-01 22:30:39 +08:00

108 lines
3.1 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
)
func TestHandleSync_MergeMode(t *testing.T) {
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
s, src := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
addSkill(t, src, "alpha")
body := `{"dryRun":false}`
req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(body))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []map[string]any `json:"results"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Results) != 1 {
t.Fatalf("expected 1 sync result, got %d", len(resp.Results))
}
if resp.Results[0]["target"] != "claude" {
t.Errorf("expected target 'claude', got %v", resp.Results[0]["target"])
}
}
func TestHandleSync_IgnoredSkillNotPrunedFromRegistry(t *testing.T) {
tgtPath := filepath.Join(t.TempDir(), "claude-skills")
s, src := newTestServerWithTargets(t, map[string]string{"claude": tgtPath})
// Create a skill with install metadata (so it appears in registry)
addSkill(t, src, "kept-skill")
addSkillMeta(t, src, "kept-skill", "github.com/user/kept")
// Create another skill that will be ignored
addSkill(t, src, "ignored-skill")
addSkillMeta(t, src, "ignored-skill", "github.com/user/ignored")
// Add .skillignore to exclude the second skill
os.WriteFile(filepath.Join(src, ".skillignore"), []byte("ignored-skill\n"), 0644)
// Pre-populate registry with both entries and persist to disk
// (server auto-reloads registry from disk on each request)
s.registry = &config.Registry{
Skills: []config.SkillEntry{
{Name: "kept-skill", Source: "github.com/user/kept"},
{Name: "ignored-skill", Source: "github.com/user/ignored"},
},
}
if err := s.registry.Save(s.cfg.RegistryDir); err != nil {
t.Fatalf("failed to save registry: %v", err)
}
// Run sync (non-dry-run)
body := `{"dryRun":false}`
req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(body))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
// Both entries should survive — ignored skill still exists on disk
if len(s.registry.Skills) != 2 {
names := make([]string, len(s.registry.Skills))
for i, sk := range s.registry.Skills {
names[i] = sk.Name
}
t.Fatalf("expected 2 registry entries after sync, got %d: %v", len(s.registry.Skills), names)
}
}
func TestHandleSync_NoTargets(t *testing.T) {
s, _ := newTestServer(t) // no targets configured
body := `{}`
req := httptest.NewRequest(http.MethodPost, "/api/sync", strings.NewReader(body))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
Results []any `json:"results"`
}
json.Unmarshal(rr.Body.Bytes(), &resp)
if len(resp.Results) != 0 {
t.Errorf("expected 0 results for no targets, got %d", len(resp.Results))
}
}