Files
Willie f8d768a256 fix(projects): say where a removed project's MCP and hook settings go (#955)
Removing a project keeps its mcp.projects and hooks.projects entries on
purpose: they are separate lists that no project command edits, and the
dashboard lists such a folder on its own. But nothing said so. The CLI
warned "mcp.projects still contains root X" with no next step, and the
dashboard dialog only mentioned it for a machine-local project, so a
removed project's MCP settings looked like a cleanup bug.

The CLI warning now names the settings and where to remove them. The
remove dialog says they stay and that removing the leftover row deletes
them, except when only this machine's copy goes and config.yaml's
project remains. project.md, ui.md and the skill reference describe it.
2026-11-18 22:10:07 +08:00

324 lines
10 KiB
Go

package projects
import (
"bytes"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"gopkg.in/yaml.v3"
"skillshare/internal/config"
ssync "skillshare/internal/sync"
)
func serviceFixture(t *testing.T) (*Service, *config.Config, string, string, []byte) {
t.Helper()
path, cfg, dir, original := projectCandidateFixture(t, " $ROOT/app:\n targets: [claude, codex]\n skills: {}\n agents: {}\n")
root := filepath.Join(dir, "app")
if err := os.MkdirAll(root, 0755); err != nil {
t.Fatal(err)
}
t.Setenv("XDG_STATE_HOME", filepath.Join(dir, "state"))
// Expansion excludes missing roots, so reload after creating the folder.
cfg, err := config.LoadReadOnly()
if err != nil {
t.Fatal(err)
}
cfg.Source = filepath.Join(dir, "source")
if err := os.MkdirAll(cfg.Source, 0755); err != nil {
t.Fatal(err)
}
t.Setenv("XDG_DATA_HOME", filepath.Join(dir, "data"))
t.Setenv("XDG_STATE_HOME", filepath.Join(dir, "state"))
return NewService(path, func() (*config.Config, error) { return cfg, nil }), cfg, path, root, original
}
func TestServiceRemove_InvalidCandidateBeforeDetach(t *testing.T) {
path, _, dir, original := projectCandidateFixture(t, " $ROOT/app:\n targets: [claude]\n skills: {}\n $ROOT/missing:\n targets: [claude]\n skills: {include: [\"[\"]}\n")
root := filepath.Join(dir, "app")
if err := os.MkdirAll(root, 0755); err != nil {
t.Fatal(err)
}
cfg, err := config.LoadReadOnly()
if err != nil {
t.Fatal(err)
}
s := NewService(path, func() (*config.Config, error) { return cfg, nil })
calls := 0
s.DetachSkillsTarget = func(string, ssync.DetachOptions) (ssync.DetachReport, error) {
calls++
return ssync.DetachReport{}, nil
}
_, err = s.Remove(root, RemoveOptions{})
if err == nil {
t.Fatal("invalid removal candidate accepted")
}
if calls != 0 {
t.Fatalf("invalid removal detached %d targets before validation", calls)
}
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
t.Fatal("invalid removal changed disk")
}
if _, ok := cfg.Projects[root]; !ok {
t.Fatal("invalid removal changed memory")
}
}
func TestServiceRemove_LaterFailurePreservesReport(t *testing.T) {
s, cfg, path, root, original := serviceFixture(t)
manifests := serviceTargetManifests(t, cfg)
calls := 0
s.DetachSkillsTarget = func(path string, opts ssync.DetachOptions) (ssync.DetachReport, error) {
calls++
if calls == 2 {
return ssync.DetachReport{Retained: []string{path}}, errors.New("denied")
}
return ssync.DetachSkillsTarget(path, opts)
}
result, err := s.Remove(root, RemoveOptions{})
if err == nil || !strings.Contains(err.Error(), "denied") {
t.Fatalf("want later failure: %v", err)
}
if result.Registration != "kept" || result.Detach == nil || len(result.Detach.Removed) != 1 || len(result.Detach.Retained) != 1 {
t.Fatalf("completed detach report lost: %+v", result)
}
if _, err := os.Stat(result.Detach.Removed[0]); !os.IsNotExist(err) {
t.Fatalf("first detach did not remove manifest: %v", err)
}
remaining := 0
for _, manifest := range manifests {
if _, err := os.Stat(manifest); err == nil {
remaining++
}
}
if remaining != 1 {
t.Fatalf("later target changed after failure: %d manifests remain", remaining)
}
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
t.Fatal("partial removal changed disk")
}
if _, ok := cfg.Projects[root]; !ok {
t.Fatal("partial removal changed memory")
}
}
// Removing a config.yaml project writes config.yaml, so blocking that file
// makes the save fail after the detach.
func TestServiceRemove_SaveFailureKeepsMemory(t *testing.T) {
s, cfg, path, root, original := serviceFixture(t)
manifests := serviceTargetManifests(t, cfg)
calls := 0
s.DetachSkillsTarget = func(target string, opts ssync.DetachOptions) (ssync.DetachReport, error) {
detached, err := ssync.DetachSkillsTarget(target, opts)
if err != nil {
return detached, err
}
calls++
if calls == 1 {
if err := os.Rename(path, path+".kept"); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(path, 0755); err != nil {
t.Fatal(err)
}
}
return detached, nil
}
result, err := s.Remove(root, RemoveOptions{})
if !errors.Is(err, ErrSave) {
t.Fatalf("want persistence failure: %v", err)
}
if result.Registration != "kept" || result.Detach == nil || len(result.Detach.Removed) != 2 {
t.Fatalf("save failure lost completed detach: %+v", result)
}
for _, manifest := range manifests {
if _, err := os.Stat(manifest); !os.IsNotExist(err) {
t.Fatalf("completed detach not retained after save failure: %v", err)
}
}
if _, ok := cfg.Projects[root]; !ok {
t.Fatal("save failure removed in-memory registration")
}
if got, _ := os.ReadFile(path + ".kept"); !bytes.Equal(got, original) {
t.Fatal("save failure changed disk")
}
}
func TestServiceRemove_AgentFilesUnchanged(t *testing.T) {
s, cfg, _, root, _ := serviceFixture(t)
agents := filepath.Join(root, ".claude", "agents", "review.md")
if err := os.MkdirAll(filepath.Dir(agents), 0755); err != nil {
t.Fatal(err)
}
content := []byte("agent content\n")
if err := os.WriteFile(agents, content, 0644); err != nil {
t.Fatal(err)
}
result, err := s.Remove(root, RemoveOptions{})
if err != nil {
t.Fatal(err)
}
if result.Registration != "removed" || len(result.Expanded.Projects) != 0 || len(cfg.Projects) != 1 {
t.Fatalf("removal state: %+v, caller projects %v", result, cfg.Projects)
}
if got, _ := os.ReadFile(agents); !bytes.Equal(got, content) {
t.Fatal("removal changed agent files")
}
}
func TestServicePut_NoOpAndPreviewDoNotWrite(t *testing.T) {
s, cfg, path, root, original := serviceFixture(t)
old := cfg.Projects[root]
result, err := s.Put(root, old, PutOptions{})
if err != nil {
t.Fatal(err)
}
if len(result.Affected) != 0 || result.Item == nil || result.Expanded == cfg {
t.Fatalf("no-op result: %+v", result)
}
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
t.Fatal("no-op wrote config")
}
next := old
next.Name = "renamed"
preview, err := s.Put(root, next, PutOptions{DryRun: true})
if err != nil {
t.Fatal(err)
}
if len(preview.Affected) != 1 || preview.Item.Name != "renamed" || preview.Expanded.Targets["renamed@claude"].ProjectRoot() != root {
t.Fatalf("preview not expanded from candidate: %+v", preview)
}
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
t.Fatal("preview wrote config")
}
if cfg.Projects[root].Name != "" {
t.Fatal("preview changed caller")
}
applied, err := s.Put(root, next, PutOptions{})
if err != nil {
t.Fatal(err)
}
if applied.Item.Name != preview.Item.Name || applied.Expanded.Targets["renamed@claude"].ProjectRoot() != root {
t.Fatalf("apply diverged from preview: %+v", applied)
}
disk, err := config.LoadReadOnly()
if err != nil {
t.Fatal(err)
}
if disk.Projects[root].Name != "renamed" || cfg.Projects[root].Name != "" {
t.Fatal("put did not isolate caller and persist candidate")
}
}
func TestServiceRemove_PreviewAndLeftoverRoots(t *testing.T) {
s, cfg, path, root, original := serviceFixture(t)
mcpRoot := root + string(filepath.Separator) + "."
hooksRoot := root + string(filepath.Separator)
var extra struct {
MCP *config.MCPConfig `yaml:"mcp"`
Hooks yaml.Node `yaml:"hooks"`
}
if err := yaml.Unmarshal([]byte("mcp:\n projects:\n "+mcpRoot+": {servers: {}}\nhooks:\n projects:\n "+hooksRoot+": {entries: {}}\n"), &extra); err != nil {
t.Fatal(err)
}
cfg.MCP = extra.MCP
cfg.Hooks = extra.Hooks
calls := 0
s.DetachSkillsTarget = func(path string, opts ssync.DetachOptions) (ssync.DetachReport, error) {
calls++
if !opts.DryRun {
t.Fatal("preview detached files")
}
return ssync.DetachReport{Retained: []string{path}}, nil
}
result, err := s.Remove(root, RemoveOptions{DryRun: true})
if err != nil {
t.Fatal(err)
}
if calls != 2 || result.Registration != "kept" || len(result.Detach.Retained) != 2 || len(result.Warnings) != 2 || result.Warnings[0] != "MCP settings for "+mcpRoot+" stay in mcp.projects; remove them on the dashboard's Projects page or in config.yaml" || result.Warnings[1] != "Hook settings for "+hooksRoot+" stay in hooks.projects; remove them on the dashboard's Projects page or in config.yaml" {
t.Fatalf("preview report: %+v", result)
}
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
t.Fatal("preview wrote config")
}
if len(cfg.Projects) != 1 || len(result.Expanded.Projects) != 0 {
t.Fatal("preview state incorrect")
}
}
func TestServiceList(t *testing.T) {
s, _, _, root, _ := serviceFixture(t)
listed, err := s.List()
if err != nil {
t.Fatal(err)
}
if len(listed) != 1 || listed[0].Root != root || listed[0].Name != "app" || listed[0].Missing {
t.Fatalf("list: %+v", listed)
}
if listed[0].Skills == nil || listed[0].Agents == nil {
t.Fatalf("list resources: %+v", listed[0])
}
}
func TestServiceShow(t *testing.T) {
s, _, _, root, _ := serviceFixture(t)
shown, err := s.Show("app")
if err != nil {
t.Fatal(err)
}
if shown.Root != root || shown.Skills == nil || shown.Agents == nil {
t.Fatalf("show: %+v", shown)
}
_, err = s.Show("unknown")
if !errors.Is(err, ErrNotFound) {
t.Fatalf("unknown: %v", err)
}
}
func serviceTargetManifests(t *testing.T, cfg *config.Config) []string {
t.Helper()
var paths []string
for _, target := range cfg.Targets {
path := filepath.Join(target.SkillsConfig().Path, ssync.ManifestFile)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
paths = append(paths, path)
}
return paths
}
func TestServicePut_RejectsDuplicateNormalizedSharedRoots(t *testing.T) {
_, cfg, path, root, original := serviceFixture(t)
entry := cfg.Projects[root]
entry.Name = "primary"
cfg.Projects[root] = entry
entry.Name = "secondary"
cfg.Projects[root+string(filepath.Separator)+"."] = entry
if _, err := config.PrepareSave(path, cfg); err == nil {
t.Fatal("duplicate normalized roots accepted")
}
if raw, _ := os.ReadFile(path); !bytes.Equal(raw, original) {
t.Fatal("invalid candidate changed shared YAML")
}
}
// Windows CI runs this with native case-folding path identities.
func TestServiceExactKeysWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("native Windows path identity")
}
_, cfg, path, root, _ := serviceFixture(t)
entry := cfg.Projects[root]
entry.Name = "case-alias"
cfg.Projects[strings.ToUpper(root)] = entry
if _, err := config.PrepareSave(path, cfg); err == nil {
t.Fatal("case-equivalent roots accepted")
}
}