mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +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>
234 lines
7.4 KiB
Go
234 lines
7.4 KiB
Go
package projects
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
ssync "skillshare/internal/sync"
|
|
)
|
|
|
|
func reload(t *testing.T, path string) (*Service, *config.Config) {
|
|
t.Helper()
|
|
loaded, err := config.LoadReadOnlyPath(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewService(path, func() (*config.Config, error) { return loaded, nil }), loaded
|
|
}
|
|
|
|
// An edit of a machine-local project must not republish config.yaml.
|
|
func TestLocalEditNeverWritesSharedConfig(t *testing.T) {
|
|
s, cfg, path, root, original := serviceFixture(t)
|
|
if _, err := s.Localize(root, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, cfg = reload(t, path)
|
|
next := cfg.Projects[root]
|
|
next.Name = "local-name"
|
|
result, err := s.Put(root, next, PutOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
|
|
t.Fatal("local edit rewrote shared config")
|
|
}
|
|
if result.Registration != "updated" || result.Item.Name != "local-name" || result.Item.Origin != OriginLocal || !result.Item.SharedCopy {
|
|
t.Fatalf("local edit result: %+v", result.Item)
|
|
}
|
|
}
|
|
|
|
// An edit of a config.yaml project is saved in config.yaml.
|
|
func TestSharedEditWritesSharedConfig(t *testing.T) {
|
|
s, cfg, path, root, _ := serviceFixture(t)
|
|
next := cfg.Projects[root]
|
|
next.Name = "renamed"
|
|
if _, err := s.Put(root, next, PutOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); !bytes.Contains(got, []byte("name: renamed")) {
|
|
t.Fatalf("shared edit missing from config.yaml:\n%s", got)
|
|
}
|
|
local, _ := config.LocalProjectsPath(path)
|
|
if _, err := os.Stat(local); !os.IsNotExist(err) {
|
|
t.Fatalf("shared edit created local state: %v", err)
|
|
}
|
|
}
|
|
|
|
// Removing this machine's copy brings back the config.yaml declaration.
|
|
func TestRemoveLocalCopyKeepsSharedDeclaration(t *testing.T) {
|
|
s, cfg, path, root, original := serviceFixture(t)
|
|
if _, err := s.Localize(root, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, cfg = reload(t, path)
|
|
result, err := s.Remove(root, RemoveOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); !bytes.Equal(got, original) {
|
|
t.Fatal("removing the local copy rewrote config.yaml")
|
|
}
|
|
if result.Remaining == nil || result.Remaining.Origin != OriginShared {
|
|
t.Fatalf("remaining declaration: %+v", result.Remaining)
|
|
}
|
|
_, loaded := reload(t, path)
|
|
if origin, _ := loaded.ProjectOrigin(root); origin != OriginShared || !config.SameManagedProject(loaded.Projects[root], cfg.Projects[root]) {
|
|
t.Fatalf("shared declaration did not apply again: %s %+v", origin, loaded.Projects)
|
|
}
|
|
}
|
|
|
|
// Removing with Shared drops both declarations.
|
|
func TestRemoveSharedDropsBothLayers(t *testing.T) {
|
|
s, _, path, root, _ := serviceFixture(t)
|
|
if _, err := s.Localize(root, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, _ = reload(t, path)
|
|
if _, err := s.Remove(root, RemoveOptions{Shared: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, loaded := reload(t, path)
|
|
if len(loaded.Projects) != 0 || loaded.HasSharedProject(root) {
|
|
t.Fatalf("project survived: %+v", loaded.Projects)
|
|
}
|
|
}
|
|
|
|
// Share moves a local project into config.yaml and drops the local copy.
|
|
func TestShareMovesProjectIntoConfig(t *testing.T) {
|
|
s, cfg, path, root, _ := serviceFixture(t)
|
|
p := cfg.Projects[root]
|
|
p.Name = "machine"
|
|
if _, err := s.Localize(root, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, cfg = reload(t, path)
|
|
if _, err := s.Put(root, p, PutOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, _ = reload(t, path)
|
|
if _, err := s.Share(root, ShareOptions{}); !errors.Is(err, ErrSharedDiffers) {
|
|
t.Fatalf("different shared settings replaced without confirmation: %v", err)
|
|
}
|
|
result, err := s.Share(root, ShareOptions{Replace: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Registration != "updated" || result.Item.Origin != OriginShared {
|
|
t.Fatalf("share result: %+v", result.Item)
|
|
}
|
|
_, loaded := reload(t, path)
|
|
if origin, _ := loaded.ProjectOrigin(root); origin != OriginShared || loaded.Projects[root].Name != "machine" {
|
|
t.Fatalf("shared project: %s %+v", origin, loaded.Projects[root])
|
|
}
|
|
if local, _ := config.LocalProjectsPath(path); func() bool { b, _ := os.ReadFile(local); return bytes.Contains(b, []byte("machine")) }() {
|
|
t.Fatal("local copy kept after share")
|
|
}
|
|
}
|
|
|
|
// A new project follows projects_storage unless the caller picks a layer.
|
|
func TestPutCreateStorage(t *testing.T) {
|
|
for _, tc := range []struct{ setting, storage, want string }{{"", "", OriginShared}, {"machine-local", "", OriginLocal}, {"machine-local", OriginShared, OriginShared}, {"", OriginLocal, OriginLocal}} {
|
|
t.Run(tc.setting+"/"+tc.storage, func(t *testing.T) {
|
|
s, cfg, path, root, _ := serviceFixture(t)
|
|
cfg.ProjectsStorage = tc.setting
|
|
other := filepath.Join(filepath.Dir(root), "other")
|
|
if err := os.MkdirAll(other, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := s.Put(other, config.ManagedProject{Targets: []string{"claude"}, Skills: &config.ResourceTargetConfig{}}, PutOptions{Create: true, Storage: tc.storage})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, loaded := reload(t, path)
|
|
if origin, _ := loaded.ProjectOrigin(other); origin != tc.want || result.Item.Origin != tc.want {
|
|
t.Fatalf("origin %s (result %s), want %s", origin, result.Item.Origin, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLocalMutationRejectsStaleSharedBeforeDetach(t *testing.T) {
|
|
for _, remove := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "edit", true: "remove"}[remove], func(t *testing.T) {
|
|
s, cfg, path, root, original := serviceFixture(t)
|
|
if err := os.WriteFile(path, append(original, []byte("# newer shared settings\n")...), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
calls := 0
|
|
s.DetachSkillsTarget = func(string, ssync.DetachOptions) (ssync.DetachReport, error) {
|
|
calls++
|
|
return ssync.DetachReport{}, nil
|
|
}
|
|
var err error
|
|
if remove {
|
|
_, err = s.Remove(root, RemoveOptions{})
|
|
} else {
|
|
p := cfg.Projects[root]
|
|
p.Name = "renamed"
|
|
_, err = s.Put(root, p, PutOptions{Detach: true})
|
|
}
|
|
if err == nil || calls != 0 {
|
|
t.Fatalf("stale operation: error=%v detach calls=%d", err, calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLocalRegistrationPrivateState(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("Unix permission bits")
|
|
}
|
|
s, _, path, root, _ := serviceFixture(t)
|
|
release, err := LockConfig(context.Background(), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer release()
|
|
if _, err := s.Localize(root, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local, err := config.LocalProjectsPath(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for p, want := range map[string]os.FileMode{local: 0600, filepath.Dir(local): 0700} {
|
|
info, err := os.Stat(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode().Perm() != want {
|
|
t.Fatalf("%s has %o, want %o", p, info.Mode().Perm(), want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLocalNoopEmptyFiltersKeepsSharedOrigin(t *testing.T) {
|
|
s, cfg, path, root, _ := serviceFixture(t)
|
|
p := cfg.Projects[root]
|
|
p.Skills.Include = []string{}
|
|
// This shape is emitted by the supported raw shared editor.
|
|
cfg.Projects[root] = p
|
|
p.Skills = cloneProjectResource(p.Skills)
|
|
p.Skills.Include = nil
|
|
result, err := s.Put(root, p, PutOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(result.Affected) != 0 || result.Item.Origin != "shared" {
|
|
t.Fatalf("no-op changed provenance: %+v", result)
|
|
}
|
|
local, err := config.LocalProjectsPath(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(local); !os.IsNotExist(err) {
|
|
t.Fatalf("no-op created local state: %v", err)
|
|
}
|
|
}
|