Files
skillshare/internal/sync/skill_run_test.go
T
Willie b76375dab5 refactor(sync): share the per-target skills sync step between CLI and server
After the server fixes, the CLI's collect*SyncResult functions and the
Web API loop ran the same merge/copy/symlink and prune logic twice. Both
now call sync.SyncSkillTarget and only render its result: the CLI keeps
its parallel grouping and progress output, the server stays sequential.
The stats helpers this orphaned are removed.

The server's "pruned" list is now always an array; it could be null
when a prune removed nothing.
2026-09-30 02:08:24 +08:00

60 lines
1.9 KiB
Go

package sync
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"skillshare/internal/config"
)
func skillRunSource(t *testing.T) (string, []DiscoveredSkill) {
t.Helper()
sourceDir := t.TempDir()
os.MkdirAll(filepath.Join(sourceDir, "alpha"), 0755)
os.WriteFile(filepath.Join(sourceDir, "alpha", "SKILL.md"), []byte("---\nname: alpha\n---\n# Alpha"), 0644)
skills, err := DiscoverSourceSkills(sourceDir)
if err != nil {
t.Fatal(err)
}
return sourceDir, skills
}
func TestSyncSkillTarget_MergeLinksAndPrunesOrphans(t *testing.T) {
sourceDir, skills := skillRunSource(t)
targetDir := t.TempDir()
if err := os.Symlink(filepath.Join(sourceDir, "old"), filepath.Join(targetDir, "old")); err != nil {
t.Fatal(err)
}
target := config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: targetDir}}
r := SyncSkillTarget(SkillTarget{Name: "claude", Target: target, Mode: "merge"}, skills, SkillRunOptions{Source: sourceDir})
if r.Err != nil || !slices.Equal(r.Linked, []string{"alpha"}) || !slices.Equal(r.Pruned, []string{"old"}) {
t.Fatalf("unexpected result: %+v", r)
}
}
func TestSyncSkillTarget_SymlinkConflictNeedsForce(t *testing.T) {
sourceDir, skills := skillRunSource(t)
elsewhere := t.TempDir()
linkPath := filepath.Join(t.TempDir(), "skills")
if err := os.Symlink(elsewhere, linkPath); err != nil {
t.Fatal(err)
}
target := config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: linkPath}}
st := SkillTarget{Name: "claude", Target: target, Mode: "symlink"}
r := SyncSkillTarget(st, skills, SkillRunOptions{Source: sourceDir})
if r.Err == nil || !strings.HasPrefix(r.Err.Error(), "conflict - symlink points to ") {
t.Fatalf("expected conflict error, got %+v", r)
}
r = SyncSkillTarget(st, skills, SkillRunOptions{Source: sourceDir, Force: true})
if r.Err != nil || r.SymlinkStatus != StatusConflict || CheckStatus(linkPath, sourceDir) != StatusLinked {
t.Fatalf("expected forced relink, got %+v", r)
}
}