mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
The analyze numbers were drifting from what tools really see. Symlink-mode targets expose the whole source folder, so .skillignore'd skills still load but were left out; merge/copy targets often hold skills the user dropped in by hand that skillshare never counted; and chars/4 undercounted CJK text by roughly four times. Route the CLI, TUI, web handler and sync summary through one sync.TargetSkills so the three copies of the filter logic cannot disagree again. Disabled skills stay in the discovery with Disabled=true and are priced for symlink targets; unmanaged folders in a target are reported as Local. Token estimates now charge one token per wide rune. The dashboard keeps the chosen target in the URL so target and project pages can deep-link into the analysis, lists project targets after own targets with their tool icon, and tags local/disabled rows. Select gives single-select icons a key to silence a React warning.
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package sync
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"skillshare/internal/config"
|
|
)
|
|
|
|
func writeAnalyzeSkill(t *testing.T, root, name, body string) {
|
|
t.Helper()
|
|
dir := filepath.Join(root, name)
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content := "---\nname: " + name + "\ndescription: about " + name + "\n---\n" + body
|
|
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func skillTarget(path, mode string) config.TargetConfig {
|
|
return config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: path, Mode: mode}}
|
|
}
|
|
|
|
func namesOf(skills []DiscoveredSkill) map[string]DiscoveredSkill {
|
|
m := map[string]DiscoveredSkill{}
|
|
for _, s := range skills {
|
|
m[s.FlatName] = s
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestEstimateTokens_WideRunesCostOneEach(t *testing.T) {
|
|
if got := EstimateTokens("abcdefgh"); got != 2 {
|
|
t.Errorf("ascii: got %d, want 2", got)
|
|
}
|
|
if got := EstimateTokens("中文字"); got != 3 {
|
|
t.Errorf("cjk: got %d, want 3", got)
|
|
}
|
|
}
|
|
|
|
func TestTargetSkills_SymlinkKeepsDisabledMergeDropsIt(t *testing.T) {
|
|
src := t.TempDir()
|
|
writeAnalyzeSkill(t, src, "kept", "body")
|
|
writeAnalyzeSkill(t, src, "off", "body")
|
|
os.WriteFile(filepath.Join(src, ".skillignore"), []byte("off\n"), 0644)
|
|
|
|
discovered, err := DiscoverSourceSkillsForAnalyze(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
symlinked, err := TargetSkills("claude", skillTarget(src, "symlink"), "merge", src, discovered)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := namesOf(symlinked); len(got) != 2 || !got["off"].Disabled || got["off"].BodyTokens == 0 {
|
|
t.Errorf("symlink target: want kept + measured disabled off, got %+v", symlinked)
|
|
}
|
|
|
|
merged, err := TargetSkills("claude", skillTarget(t.TempDir(), "merge"), "merge", src, discovered)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := namesOf(merged); len(got) != 1 || got["kept"].FlatName == "" {
|
|
t.Errorf("merge target: want only kept, got %+v", merged)
|
|
}
|
|
}
|
|
|
|
func TestTargetSkills_MergeAddsUnmanagedLocalSkills(t *testing.T) {
|
|
src, tgt := t.TempDir(), t.TempDir()
|
|
writeAnalyzeSkill(t, src, "linked", "body")
|
|
if err := os.Symlink(filepath.Join(src, "linked"), filepath.Join(tgt, "linked")); err != nil {
|
|
t.Skip("symlinks unavailable:", err)
|
|
}
|
|
writeAnalyzeSkill(t, tgt, "mine", "手寫的 body")
|
|
writeAnalyzeSkill(t, tgt, "copied", "body")
|
|
if err := WriteManifest(tgt, &Manifest{Managed: map[string]string{"copied": "sha"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
discovered, err := DiscoverSourceSkillsForAnalyze(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := TargetSkills("claude", skillTarget(tgt, "merge"), "merge", src, discovered)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
byName := namesOf(got)
|
|
if len(byName) != 2 || byName["linked"].Local || !byName["mine"].Local || byName["mine"].BodyTokens == 0 {
|
|
t.Errorf("want linked (source) + mine (local, measured), not copied; got %+v", got)
|
|
}
|
|
}
|