mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
* fix(projects): save new projects in config.yaml again v0.29.0 wrote every project add, edit and remove to a machine-local registry, so a project added on one machine never reached config.yaml or the other machines that share it (#792). Most people share one config.yaml and expect a new project to follow it. config.yaml is the default again. Machine-local storage stays as an explicit choice: --machine-local on add, or projects_storage: machine-local for every new project, with --shared to override it. The new share command moves a local project into config.yaml, and localize still copies one to this machine. Edits and removals stay in the layer that owns the project; removing a local copy of a config.yaml project keeps config.yaml's version unless --shared asks to remove both, so one machine cannot silently drop a project for the others. Exclusions written by v0.29.0 are left as they are. The dashboard picks storage once, in the Add project dialog, and offers the two removal choices only where both copies exist. Moving a project between layers stays a CLI command, so the detail page keeps one job. Closes #792 * fix(projects): recheck both layers after a confirmation, sync the saved key remove and share ask before taking the config lock, then compared only the merged entry after locking. A share or localize from another terminal moves a project between layers without changing that entry, so a remove confirmed for this machine's copy could then delete config.yaml's declaration for every machine, and a confirmed share could overwrite config.yaml settings changed meanwhile. The recheck now compares the owning layer and config.yaml's declaration too (ProjectLayerState). Re-adding a project that v0.29.0 hid restores config.yaml's own key, which can be spelled differently from the folder given to add (~/app versus an absolute path). add --sync looked the project up by the given spelling and failed with "project not found" after saving it; it now syncs the key that was saved. * test(projects): make the layer tests hold on Windows The mutation-sequence test looked the shared project up by filepath.Join, but the fixture spells config.yaml's key $ROOT/app; on Windows the two spellings differ, so the lookup found nothing. It now asks the config for the key it uses. The remove save-failure test blocked the local registry, but removing a config.yaml project now writes config.yaml. On Linux the registry check still failed with ENOTDIR first, which hid the mismatch; on Windows that reads as a missing file and the save went through. The test now blocks config.yaml, as the edit save-failure test does, and its hook no longer shadows the config path with the target path. --------- Co-authored-by: sim <sim@example.invalid>
211 lines
6.4 KiB
Go
211 lines
6.4 KiB
Go
package projects
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
ssync "skillshare/internal/sync"
|
|
)
|
|
|
|
func TestServicePut_KeepsDepartingSkills(t *testing.T) {
|
|
for _, change := range []string{"drop", "disable"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
s, cfg, _, root, _ := serviceFixture(t)
|
|
manifests := serviceTargetManifests(t, cfg)
|
|
s.DetachSkillsTarget = func(path string, _ ssync.DetachOptions) (ssync.DetachReport, error) {
|
|
t.Fatalf("save detached %s", path)
|
|
return ssync.DetachReport{}, nil
|
|
}
|
|
next := cfg.Projects[root]
|
|
if change == "drop" {
|
|
next.Targets = []string{"codex"}
|
|
} else {
|
|
next.Skills = nil
|
|
}
|
|
result, err := s.Put(root, next, PutOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Registration != "updated" || result.Detach != nil {
|
|
t.Fatalf("save result: %+v", result)
|
|
}
|
|
for _, p := range manifests {
|
|
if _, err := os.Stat(p); err != nil {
|
|
t.Fatalf("save removed manifest %s: %v", p, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An agents-only project can still hold manifests from a sync made while its
|
|
// skills were on, or with exclude: ["*"]. Remove detaches them, as on main.
|
|
func TestServiceRemove_SkillsOffProjectDetaches(t *testing.T) {
|
|
path, _, dir, _ := projectCandidateFixture(t, " $ROOT/app:\n targets: [claude, codex]\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"))
|
|
cfg, err := config.LoadReadOnly()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(dir, "data"))
|
|
t.Setenv("XDG_STATE_HOME", filepath.Join(dir, "state"))
|
|
manifests := serviceTargetManifests(t, cfg)
|
|
if len(manifests) == 0 {
|
|
t.Fatal("fixture has no project targets")
|
|
}
|
|
s := NewService(path, func() (*config.Config, error) { return cfg, nil })
|
|
result, err := s.Remove(root, RemoveOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Detach == nil || len(result.Detach.Removed) != len(manifests) {
|
|
t.Fatalf("skills-off project left managed: %+v", result.Detach)
|
|
}
|
|
for _, p := range manifests {
|
|
if _, err := os.Stat(p); !os.IsNotExist(err) {
|
|
t.Fatalf("manifest retained %s: %v", p, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServiceRemove_SharedPathRetained(t *testing.T) {
|
|
s, cfg, _, root, _ := serviceFixture(t)
|
|
manifests := serviceTargetManifests(t, cfg)
|
|
shared := filepath.Join(root, ".claude", "skills")
|
|
cfg.Targets["shared"] = config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: shared}}
|
|
result, err := s.Remove(root, RemoveOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Detach == nil || len(result.Detach.Retained) != 1 || result.Detach.Retained[0] != shared {
|
|
t.Fatalf("shared path not reported: %+v", result.Detach)
|
|
}
|
|
for _, p := range manifests {
|
|
if filepath.Dir(p) == shared {
|
|
if _, err := os.Stat(p); err != nil {
|
|
t.Fatal("surviving target lost manifest", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServiceEdit_DetachesDepartingSkills(t *testing.T) {
|
|
for _, change := range []string{"drop", "disable", "rename"} {
|
|
t.Run(change, func(t *testing.T) {
|
|
s, cfg, path, root, original := serviceFixture(t)
|
|
serviceTargetManifests(t, cfg)
|
|
next := cfg.Projects[root]
|
|
want := 1
|
|
switch change {
|
|
case "drop":
|
|
next.Targets = []string{"codex"}
|
|
case "disable":
|
|
next.Skills = nil
|
|
want = 2
|
|
case "rename":
|
|
next.Name = "renamed"
|
|
want = 0
|
|
}
|
|
preview, err := s.Put(root, next, PutOptions{DryRun: true, Detach: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want > 0 && (preview.Detach == nil || len(preview.Detach.Removed) != want) {
|
|
t.Fatalf("preview lost departure: %+v", preview.Detach)
|
|
}
|
|
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
|
|
t.Fatal("preview wrote config")
|
|
}
|
|
result, err := s.Put(root, next, PutOptions{Detach: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := 0
|
|
if result.Detach != nil {
|
|
got = len(result.Detach.Removed)
|
|
}
|
|
if got != want {
|
|
t.Fatalf("detached %d manifests, want %d", got, want)
|
|
}
|
|
if result.Detach != nil {
|
|
for _, p := range result.Detach.Removed {
|
|
if _, err := os.Lstat(p); !os.IsNotExist(err) {
|
|
t.Fatalf("departing manifest retained: %s %v", p, err)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServiceEdit_SaveFailureReportsCompletedDetach(t *testing.T) {
|
|
s, cfg, path, root, original := serviceFixture(t)
|
|
serviceTargetManifests(t, cfg)
|
|
next := cfg.Projects[root]
|
|
next.Targets = []string{"codex"}
|
|
s.DetachSkillsTarget = func(target string, opts ssync.DetachOptions) (ssync.DetachReport, error) {
|
|
r, err := ssync.DetachSkillsTarget(target, opts)
|
|
if err == nil {
|
|
if err := os.Rename(path, path+".kept"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(path, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return r, err
|
|
}
|
|
result, err := s.Put(root, next, PutOptions{Detach: true})
|
|
if !errors.Is(err, ErrSave) {
|
|
t.Fatalf("want failed save: %v", err)
|
|
}
|
|
if result.Registration != "kept" || result.Detach == nil || len(result.Detach.Removed) != 1 || result.Item == nil || len(result.Item.Targets) != 2 {
|
|
t.Fatalf("partial edit misrepresented persisted state: %+v", result)
|
|
}
|
|
if got, _ := os.ReadFile(path + ".kept"); !bytes.Equal(got, original) {
|
|
t.Fatal("failed save changed config")
|
|
}
|
|
}
|
|
|
|
// An edit saved in config.yaml changes the aliases of its anchor, and Affected
|
|
// reports them, as it did before machine-local projects existed.
|
|
func TestServiceEdit_SharedAnchorReportsAliases(t *testing.T) {
|
|
path, _, dir, _ := projectCandidateFixture(t, " $ROOT/a: &shared\n targets: [claude, codex]\n skills: {}\n $ROOT/z: *shared\n")
|
|
for _, name := range []string{"a", "z"} {
|
|
if err := os.MkdirAll(filepath.Join(dir, name), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cfg, err := config.LoadReadOnly()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
serviceTargetManifests(t, cfg)
|
|
s := NewService(path, func() (*config.Config, error) { return cfg, nil })
|
|
next := cfg.Projects[filepath.Join(dir, "a")]
|
|
next.Targets = []string{"codex"}
|
|
result, err := s.Put(filepath.Join(dir, "a"), next, PutOptions{Detach: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(result.Affected) != 2 || result.Detach == nil {
|
|
t.Fatalf("shared edit hid an anchored alias: %+v", result)
|
|
}
|
|
loaded, err := config.LoadReadOnly()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(loaded.Projects, result.Expanded.Projects) {
|
|
t.Fatal("persisted view disagrees with candidate")
|
|
}
|
|
}
|