Files
skillshare/internal/server/handler_projects_test.go
Willie f9be84c0f5 feat(projects): manage project folders from the global config
People with many repositories had to choose between listing every
project skills folder as a custom target or running project mode in
each repo. A `projects:` key in the global config now declares a folder
once with the tools used there, and sync treats each one as a
`<project>@<tool>` target with its own mode and filters.

- Expand `projects:` into virtual targets at load time and strip them
  on save; tools that share a project skills folder are written once
- Skip a project whose folder is missing with a warning instead of
  failing the whole sync; `collect` and `target` ignore project targets
- Add /api/projects (list, save, remove, convert) and detect ordinary
  targets that already point inside a project so they can be converted
- Add a Projects page to the dashboard with Skills, Agents and MCP tabs
  per project, replacing the Projects tab on the MCP page
- Extend Select with a multi-select mode for picking a project's targets

Refs #286
2026-09-21 01:52:34 +08:00

107 lines
3.6 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/config"
)
func projectRequest(t *testing.T, s *Server, method, url, body string) *httptest.ResponseRecorder {
t.Helper()
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(method, url, strings.NewReader(body)))
return rr
}
func addProject(t *testing.T, s *Server, root string) {
t.Helper()
if err := os.MkdirAll(root, 0755); err != nil {
t.Fatal(err)
}
body := `{"create":true,"root":"` + root + `","targets":["claude","codex"],"skills":{"mode":"copy","include":["team-*"],"exclude":[]}}`
if rr := projectRequest(t, s, http.MethodPut, "/api/projects", body); rr.Code != http.StatusOK {
t.Fatalf("add project: %d %s", rr.Code, rr.Body)
}
}
func TestSaveProject_AddsTargetsThatOnlyTheProjectScopeLists(t *testing.T) {
s, src := newTestServer(t)
root := filepath.Join(filepath.Dir(src), "app")
addProject(t, s, root)
var listed struct {
Targets []targetItem `json:"targets"`
}
json.Unmarshal(projectRequest(t, s, http.MethodGet, "/api/targets?scope=projects", "").Body.Bytes(), &listed)
if len(listed.Targets) != 2 || listed.Targets[0].Project != root {
t.Fatalf("project targets %+v", listed.Targets)
}
json.Unmarshal(projectRequest(t, s, http.MethodGet, "/api/targets", "").Body.Bytes(), &listed)
if len(listed.Targets) != 0 {
t.Fatalf("own targets %+v", listed.Targets)
}
}
func TestSaveProject_Rejected(t *testing.T) {
s, src := newTestServer(t)
root := filepath.Join(filepath.Dir(src), "app")
addProject(t, s, root)
for name, body := range map[string]string{
"already declared": `{"create":true,"root":"` + root + `","targets":["claude"]}`,
"folder not found": `{"create":true,"root":"` + root + `-gone","targets":["claude"]}`,
"unknown target": `{"root":"` + root + `","targets":["nope"]}`,
"invalid mode": `{"root":"` + root + `","targets":["claude"],"skills":{"mode":"mirror"}}`,
} {
if rr := projectRequest(t, s, http.MethodPut, "/api/projects", body); rr.Code == http.StatusOK {
t.Errorf("%s: saved", name)
}
}
}
func TestProjectTargets_CannotBeEditedAsTargets(t *testing.T) {
s, src := newTestServer(t)
addProject(t, s, filepath.Join(filepath.Dir(src), "app"))
for _, method := range []string{http.MethodPatch, http.MethodDelete} {
if rr := projectRequest(t, s, method, "/api/targets/app@claude", `{"mode":"merge"}`); rr.Code != http.StatusBadRequest {
t.Errorf("%s: %d %s", method, rr.Code, rr.Body)
}
}
}
func TestRemoveProject_DropsItsTargets(t *testing.T) {
s, src := newTestServer(t)
root := filepath.Join(filepath.Dir(src), "app")
addProject(t, s, root)
if rr := projectRequest(t, s, http.MethodDelete, "/api/projects?root="+root, ""); rr.Code != http.StatusOK {
t.Fatalf("%d %s", rr.Code, rr.Body)
}
if len(s.cfg.Projects) != 0 || len(s.cfg.Targets) != 0 {
t.Fatalf("projects %v, targets %v", s.cfg.Projects, s.cfg.Targets)
}
}
func TestConvertProject_MovesTargetsUnderProjectsWithTheSameFolders(t *testing.T) {
root := filepath.Join(t.TempDir(), "legacy")
skills := filepath.Join(root, ".claude", "skills")
s, _ := newTestServerWithTargets(t, map[string]string{"old-claude": skills})
if rr := projectRequest(t, s, http.MethodPost, "/api/projects/convert", `{"root":"`+root+`"}`); rr.Code != http.StatusOK {
t.Fatalf("%d %s", rr.Code, rr.Body)
}
if _, kept := s.cfg.Targets["old-claude"]; kept {
t.Error("old-claude is still a target")
}
converted := s.cfg.Targets[config.ProjectTargetName("legacy", "claude")]
if got := converted.SkillsConfig().Path; got != skills {
t.Errorf("skills path %q, want %q", got, skills)
}
}