Files
skillshare/cmd/skillshare/update_test.go
T
Willie 0be5164159 fix(update): use the tracked checkout rule in every update and reconcile path
#474 made install.IsTrackedCheckout (a _-prefixed git repo) the rule for what
a tracked repo is, but several paths still treated any git checkout as one, so
the same directory was classified differently depending on the entry point. An
unprefixed checkout, including a submodule or worktree whose .git is a file,
could be pulled as a tracked repo from the dashboard batch stream, the CLI
--group and project-mode name resolution, and the dashboard single update's
direct-path lookup, and reconcile wrote tracked: true for it.

All of them now use IsTrackedCheckout, so an unprefixed checkout updates as a
regular skill or is skipped, while _-prefixed repos, including --into repos
such as org/_team, still update as tracked repos.

Reconcile no longer registers an unprefixed checkout as tracked or creates an
entry for it from the git remote. An existing entry that an older reconcile
flagged tracked on such a checkout is kept with its source and branch; only the
flag is cleared on the next reconcile, so no user data is removed.

Fixes #476
2026-10-07 23:10:13 +08:00

220 lines
6.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"testing"
"skillshare/internal/audit"
"skillshare/internal/install"
)
func TestIsSecurityError(t *testing.T) {
tests := []struct {
err error
want bool
}{
{nil, false},
{fmt.Errorf("connection timeout"), false},
{fmt.Errorf("skill not found"), false},
{fmt.Errorf("has uncommitted changes"), false},
// Sentinel-based detection
{fmt.Errorf("security audit failed: scan error: %w", audit.ErrBlocked), true},
{fmt.Errorf("post-update audit failed: scan error — rolled back: %w", audit.ErrBlocked), true},
{fmt.Errorf("rollback failed: permission denied: %w", audit.ErrBlocked), true},
{audit.ErrBlocked, true},
// Wrapped sentinel
{fmt.Errorf("outer: %w", fmt.Errorf("inner: %w", audit.ErrBlocked)), true},
// Non-sentinel errors with similar text should NOT match
{fmt.Errorf("security audit something"), false},
{fmt.Errorf("was rolled back successfully"), false},
}
for _, tt := range tests {
name := "nil"
if tt.err != nil {
name = tt.err.Error()
if len(name) > 60 {
name = name[:60]
}
}
t.Run(name, func(t *testing.T) {
got := isSecurityError(tt.err)
if got != tt.want {
t.Errorf("isSecurityError(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
func TestParseUpdateArgs_ThresholdAlias(t *testing.T) {
opts, showHelp, err := parseUpdateArgs([]string{"--all", "-T", "h"})
if err != nil {
t.Fatalf("parseUpdateArgs returned error: %v", err)
}
if showHelp {
t.Fatal("expected showHelp=false")
}
if opts.threshold != audit.SeverityHigh {
t.Fatalf("expected threshold=%s, got %s", audit.SeverityHigh, opts.threshold)
}
}
func TestParseUpdateArgs_InvalidThreshold(t *testing.T) {
_, showHelp, err := parseUpdateArgs([]string{"--all", "--threshold", "urgent"})
if err == nil {
t.Fatal("expected error for invalid threshold")
}
if showHelp {
t.Fatal("expected showHelp=false on invalid threshold")
}
}
// --- resolveByGlob tests ---
// setupTrackedRepo creates a fake tracked repo (_-prefixed dir with .git).
func setupTrackedRepo(t *testing.T, sourceDir, name string) {
t.Helper()
repoDir := filepath.Join(sourceDir, name)
os.MkdirAll(filepath.Join(repoDir, ".git"), 0755)
os.WriteFile(filepath.Join(repoDir, "SKILL.md"), []byte("# "+name), 0644)
}
// setupUpdatableSkill creates a skill with .skillshare-meta.json containing a Source.
func setupUpdatableSkill(t *testing.T, sourceDir, name string) {
t.Helper()
dir := filepath.Join(sourceDir, name)
os.MkdirAll(dir, 0755)
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# "+name), 0644)
store, _ := install.LoadMetadata(sourceDir)
store.Set(name, &install.MetadataEntry{Source: "github.com/test/" + name, Type: "github"})
store.Save(sourceDir)
}
func TestResolveByGlob_MatchesTrackedRepos(t *testing.T) {
src := t.TempDir()
setupTrackedRepo(t, src, "_team-auth")
setupTrackedRepo(t, src, "_team-db")
setupTrackedRepo(t, src, "_other-repo")
matches, err := resolveByGlob(src, "_team-*")
if err != nil {
t.Fatal(err)
}
if len(matches) != 2 {
t.Fatalf("expected 2 matches, got %d", len(matches))
}
for _, m := range matches {
if !m.isRepo {
t.Errorf("%s should be marked as repo", m.name)
}
}
}
func TestResolveByGlob_MatchesUpdatableSkills(t *testing.T) {
src := t.TempDir()
setupUpdatableSkill(t, src, "core-auth")
setupUpdatableSkill(t, src, "core-db")
setupUpdatableSkill(t, src, "utils")
matches, err := resolveByGlob(src, "core-*")
if err != nil {
t.Fatal(err)
}
if len(matches) != 2 {
t.Fatalf("expected 2 matches, got %d", len(matches))
}
for _, m := range matches {
if m.isRepo {
t.Errorf("%s should not be marked as repo", m.name)
}
}
}
func TestResolveByGlob_CaseInsensitive(t *testing.T) {
src := t.TempDir()
setupUpdatableSkill(t, src, "Core-Auth")
setupUpdatableSkill(t, src, "core-db")
matches, err := resolveByGlob(src, "CORE-*")
if err != nil {
t.Fatal(err)
}
if len(matches) != 2 {
t.Fatalf("expected 2 case-insensitive matches, got %d", len(matches))
}
}
func TestResolveByGlob_NoMatch(t *testing.T) {
src := t.TempDir()
setupUpdatableSkill(t, src, "utils")
matches, err := resolveByGlob(src, "core-*")
if err != nil {
t.Fatal(err)
}
if len(matches) != 0 {
t.Errorf("expected 0 matches, got %d", len(matches))
}
}
func TestResolveByGlob_SortedByName(t *testing.T) {
src := t.TempDir()
setupUpdatableSkill(t, src, "z-skill")
setupUpdatableSkill(t, src, "a-skill")
setupUpdatableSkill(t, src, "m-skill")
matches, err := resolveByGlob(src, "*-skill")
if err != nil {
t.Fatal(err)
}
if len(matches) != 3 {
t.Fatalf("expected 3 matches, got %d", len(matches))
}
if matches[0].name > matches[1].name || matches[1].name > matches[2].name {
t.Errorf("results not sorted: %s, %s, %s", matches[0].name, matches[1].name, matches[2].name)
}
}
// An unprefixed git checkout is not a tracked repo: a group walk updates it as
// the regular skill its metadata describes, beside the _-prefixed repos (#476).
func TestResolveGroupUpdatable_UnprefixedCheckoutIsRegularSkill(t *testing.T) {
src := t.TempDir()
setupTrackedRepo(t, src, "org/_team")
setupUpdatableSkill(t, src, "org/solo")
// A submodule or worktree has a .git file, not a directory.
if err := os.WriteFile(filepath.Join(src, "org", "solo", ".git"), []byte("gitdir: ../../elsewhere\n"), 0644); err != nil {
t.Fatal(err)
}
matches, err := resolveGroupUpdatable("org", src)
if err != nil {
t.Fatal(err)
}
got := map[string]updateTarget{}
for _, m := range matches {
got[filepath.ToSlash(m.name)] = m
}
if len(got) != 2 || !got["org/_team"].isRepo || got["org/solo"].isRepo || got["org/solo"].meta == nil {
t.Fatalf("matches = %+v, want org/_team as repo and org/solo as skill with metadata", matches)
}
}
func TestIsGroupDir_UnprefixedCheckoutIsGroup(t *testing.T) {
src := t.TempDir()
os.MkdirAll(filepath.Join(src, "bundle", ".git"), 0755)
setupTrackedRepo(t, src, "_team")
store, _ := install.LoadMetadata(src)
if !isGroupDir("bundle", src, store) {
t.Error("an unprefixed checkout is a group, not a tracked repo")
}
if isGroupDir("_team", src, store) {
t.Error("a _-prefixed checkout is a tracked repo, not a group")
}
}