mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
Prefixed naming rewrites name: in every copy to the entry name, so the target folder and name: always agree. Requiring name: to equal the source folder only skipped skills it could sync: a tracked repo that is itself a skill lives in _<repo>, so its root SKILL.md could never pass. Only standard naming now requires the match; name format rules still apply to both. Sync lists the skills named this way, so a user looking for _ooux in the target finds ooux. The dashboard's skipped-skills hint always blamed folders that end in the same name, even when skills were skipped for their name. The diff API now reports naming skips apart from collisions and the hint names the cause. Refs #530
189 lines
6.9 KiB
Go
189 lines
6.9 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
)
|
|
|
|
func TestPrefixedTargetName(t *testing.T) {
|
|
tests := []struct {
|
|
repo string // RepoRelPath; empty means outside a tracked repo
|
|
name string
|
|
want string
|
|
}{
|
|
{"_emil-design", "prototype", "emil-design-prototype"},
|
|
{"_mattpocock-skills", "prototype", "mattpocock-skills-prototype"},
|
|
{"_bmad", "bmad-ux", "bmad-ux"},
|
|
{"_bmad", "bmad", "bmad"},
|
|
{"", "my-skill", "my-skill"},
|
|
{"org/_My_Team..v2", "dev", "my-team-v2-dev"},
|
|
{"_--", "dev", "dev"},
|
|
{"_技能", "dev", "技能-dev"},
|
|
{"_Café", "dev", "café-dev"},
|
|
}
|
|
for _, tt := range tests {
|
|
skill := DiscoveredSkill{IsInRepo: tt.repo != "", RepoRelPath: tt.repo}
|
|
if got := PrefixedTargetName(skill, tt.name); got != tt.want {
|
|
t.Errorf("PrefixedTargetName(%q, %q) = %q, want %q", tt.repo, tt.name, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolveTargetSkillsForTarget_BOMFrontmatter(t *testing.T) {
|
|
dir := t.TempDir()
|
|
skill := createTempSkill(t, dir, "notepad-skill", "notepad-skill")
|
|
content := "\ufeff---\nname: notepad-skill\n---\n# notepad-skill"
|
|
if err := os.WriteFile(filepath.Join(skill.SourcePath, "SKILL.md"), []byte(content), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, naming := range []string{"standard", "prefixed"} {
|
|
resolution, err := ResolveTargetSkillsForTarget("claude", config.ResourceTargetConfig{TargetNaming: naming}, []DiscoveredSkill{skill})
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", naming, err)
|
|
}
|
|
if len(resolution.Skills) != 1 || resolution.Skills[0].TargetName != "notepad-skill" {
|
|
t.Errorf("%s: skills = %+v, want notepad-skill kept", naming, resolution.Skills)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestResolveTargetSkillsForTarget_PrefixedNaming(t *testing.T) {
|
|
dir := t.TempDir()
|
|
inRepo := func(s DiscoveredSkill, repo string) DiscoveredSkill {
|
|
s.IsInRepo, s.RepoRelPath = true, repo
|
|
return s
|
|
}
|
|
longRepo, longName := strings.Repeat("a", 40), strings.Repeat("b", 30)
|
|
skills := []DiscoveredSkill{
|
|
inRepo(createTempSkill(t, dir, "_emil-design/skills/prototype", "prototype"), "_emil-design"),
|
|
inRepo(createTempSkill(t, dir, "_mattpocock-skills/skills/engineering/prototype", "prototype"), "_mattpocock-skills"),
|
|
inRepo(createTempSkill(t, dir, "_bmad/skills/bmad-ux", "bmad-ux"), "_bmad"),
|
|
createTempSkill(t, dir, "my-skill", "my-skill"),
|
|
inRepo(createTempSkill(t, dir, "_"+longRepo+"/"+longName, longName), "_"+longRepo),
|
|
inRepo(createTempSkill(t, dir, "_emil-design/skills/odd", "not-odd"), "_emil-design"),
|
|
inRepo(createTempSkill(t, dir, "_ooux", "ooux"), "_ooux"),
|
|
}
|
|
|
|
resolution, err := ResolveTargetSkillsForTarget("claude", config.ResourceTargetConfig{TargetNaming: "prefixed"}, skills)
|
|
if err != nil {
|
|
t.Fatalf("ResolveTargetSkillsForTarget() error = %v", err)
|
|
}
|
|
|
|
var got []string
|
|
for _, s := range resolution.Skills {
|
|
got = append(got, s.TargetName)
|
|
}
|
|
// name: need not match the folder: the copy's name: is rewritten anyway.
|
|
want := []string{"emil-design-prototype", "mattpocock-skills-prototype", "bmad-ux", "my-skill", "emil-design-not-odd", "ooux"}
|
|
if strings.Join(got, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("TargetNames = %v, want %v", got, want)
|
|
}
|
|
if len(resolution.Warnings) != 1 {
|
|
t.Fatalf("Warnings = %v, want only the over-long name", resolution.Warnings)
|
|
}
|
|
if !strings.Contains(resolution.Warnings[0], "longer than 64") {
|
|
t.Errorf("over-long warning = %q", resolution.Warnings[0])
|
|
}
|
|
}
|
|
|
|
func TestPrintResolutionSummary_ListsSkillsNamedApartFromFolder(t *testing.T) {
|
|
dir := t.TempDir()
|
|
var skills []DiscoveredSkill
|
|
for i := range 7 {
|
|
skills = append(skills, createTempSkill(t, dir, fmt.Sprintf("dir-%d", i), fmt.Sprintf("name-%d", i)))
|
|
}
|
|
summary := func(naming string) string {
|
|
var out bytes.Buffer
|
|
prev := DiagOutput
|
|
DiagOutput = &out
|
|
defer func() { DiagOutput = prev }()
|
|
resolution, err := ResolveTargetSkillsForTarget("claude", config.ResourceTargetConfig{TargetNaming: naming}, skills)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
printResolutionSummary(resolution)
|
|
return out.String()
|
|
}
|
|
|
|
t.Run("prefixed lists the first few", func(t *testing.T) {
|
|
want := " 7 skill(s) use their SKILL.md name instead of their folder name:\n" +
|
|
" dir-0 → name-0\n dir-1 → name-1\n dir-2 → name-2\n dir-3 → name-3\n dir-4 → name-4\n" +
|
|
" ... and 2 more\n"
|
|
if got := summary("prefixed"); got != want {
|
|
t.Fatalf("summary =\n%s\nwant\n%s", got, want)
|
|
}
|
|
})
|
|
t.Run("standard skips them instead", func(t *testing.T) {
|
|
if got := summary("standard"); strings.Contains(got, "instead of their folder name") {
|
|
t.Fatalf("standard listed skills it skips:\n%s", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestResolveTargetSkillsForTarget_PrefixedNamingSkipsRemainingCollisions(t *testing.T) {
|
|
dir := t.TempDir()
|
|
repoSkill := createTempSkill(t, dir, "_a/b-c", "b-c")
|
|
repoSkill.IsInRepo, repoSkill.RepoRelPath = true, "_a"
|
|
skills := []DiscoveredSkill{repoSkill, createTempSkill(t, dir, "a-b-c", "a-b-c")}
|
|
|
|
resolution, err := ResolveTargetSkillsForTarget("claude", config.ResourceTargetConfig{TargetNaming: "prefixed"}, skills)
|
|
if err != nil {
|
|
t.Fatalf("ResolveTargetSkillsForTarget() error = %v", err)
|
|
}
|
|
if len(resolution.Skills) != 0 || len(resolution.Collisions) != 1 || resolution.Collisions[0].Name != "a-b-c" {
|
|
t.Fatalf("Skills = %v, Collisions = %v; want both excluded under a-b-c", resolution.Skills, resolution.Collisions)
|
|
}
|
|
}
|
|
|
|
func TestCopySkillToTarget_RemovesCopyWhenNameRewriteFails(t *testing.T) {
|
|
src := t.TempDir() // no SKILL.md, so the name rewrite cannot read it
|
|
if err := os.WriteFile(filepath.Join(src, "notes.md"), []byte("x"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := filepath.Join(t.TempDir(), "repo-skill")
|
|
|
|
if err := copySkillToTarget(src, dst, "repo-skill", nil); err == nil {
|
|
t.Fatal("expected the rewrite error")
|
|
}
|
|
// A copy left behind without a manifest entry would look user-owned and
|
|
// never be refreshed again.
|
|
if _, err := os.Lstat(dst); !os.IsNotExist(err) {
|
|
t.Fatalf("copy left behind: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCopySkillToTarget_RewritesReadOnlySkillFile(t *testing.T) {
|
|
if os.Getuid() == 0 {
|
|
t.Skip("a read-only file is still writable as root")
|
|
}
|
|
src := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(src, "SKILL.md"), []byte("---\nname: skill\n---\n"), 0o444); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dst := filepath.Join(t.TempDir(), "repo-skill")
|
|
|
|
if err := copySkillToTarget(src, dst, "repo-skill", nil); err != nil {
|
|
t.Fatalf("copySkillToTarget: %v", err)
|
|
}
|
|
info, err := os.Stat(filepath.Join(dst, "SKILL.md"))
|
|
if err != nil || info.Mode().Perm() != 0o444 {
|
|
t.Fatalf("copied SKILL.md mode = %v, err = %v; want the source's 0444 kept", info, err)
|
|
}
|
|
}
|
|
|
|
func TestKeptLocalWarning_CapsTheNameList(t *testing.T) {
|
|
names := []string{"a", "b", "c", "d", "e", "f", "g"}
|
|
got := keptLocalWarning(names)
|
|
want := "kept local: a, b, c, d, e ... and 2 more (sync --force replaces them)"
|
|
if got != want {
|
|
t.Fatalf("keptLocalWarning = %q, want %q", got, want)
|
|
}
|
|
}
|