Files
skm/internal/core/agents_test.go
T
johannesandClaude Sonnet 5.5 df8d11d8cb feat: pin agent definitions next to skills
A source can install agents (agents/<name>.md files with a frontmatter
name) alongside or instead of skills.

- manifest: optional "agents" selection; lock: "agents" map
- add --agent / --all-agents, remove --agent, list shows the kind
- check/update diff agents like skills (files, not directories)
- dashboard lists and removes agents
- home-manager module: agentTargets (default .claude/agents) links <name>.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-28 13:42:45 +01:00

228 lines
7.3 KiB
Go

package core
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"skill-manager/internal/lock"
"skill-manager/internal/manifest"
)
// withAgents adds pstack/agents/<name>.md files ("name" or "name:body") to a fixture root.
func withAgents(t *testing.T, root string, agents ...string) string {
t.Helper()
dir := filepath.Join(root, "pstack", "agents")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
for _, a := range agents {
name, body, _ := strings.Cut(a, ":")
text := "---\nname: " + name + "\ndescription: x\n---\n" + body + "\n"
if err := os.WriteFile(filepath.Join(dir, name+".md"), []byte(text), 0o644); err != nil {
t.Fatal(err)
}
}
return root
}
func agentStore(t *testing.T) (Store, *fakeFetcher) {
f := &fakeFetcher{repos: map[string]*fakeRepo{
"github:cursor/plugins": {head: "r1", revs: map[string]string{
"r1": withAgents(t, fixture(t, "tdd"), "comment-sicko:v1", "poteto-agent"),
}},
}}
return Store{Dir: t.TempDir(), Fetcher: f}, f
}
// agents builds manifest entries; "name=alias" renames.
func agents(items ...string) []manifest.Skill {
var out []manifest.Skill
for _, n := range items {
name, as, _ := strings.Cut(n, "=")
out = append(out, manifest.Skill{Name: name, As: as})
}
return out
}
func lockedAgents(t *testing.T, s Store, key string) map[string]string {
t.Helper()
l, err := lock.Load(filepath.Join(s.Dir, LockFile))
if err != nil {
t.Fatal(err)
}
return l.Sources[key].Agents
}
func TestAddAgentsOnly(t *testing.T) {
s, _ := agentStore(t)
key, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko")})
if err != nil {
t.Fatal(err)
}
want := map[string]string{"comment-sicko": "pstack/agents/comment-sicko.md"}
if got := lockedAgents(t, s, key); !reflect.DeepEqual(got, want) {
t.Errorf("agents = %v, want %v", got, want)
}
list, err := s.List()
if err != nil {
t.Fatal(err)
}
if len(list) != 1 || !list[0].Agent || list[0].Name != "comment-sicko" {
t.Errorf("list = %+v, want just the agent", list)
}
}
func TestAddAgentAliasThenAll(t *testing.T) {
ctx := context.Background()
s, _ := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko=sicko"), Skills: pick("tdd")}); err != nil {
t.Fatal(err)
}
if got := lockedAgents(t, s, "cursor/plugins"); got["sicko"] != "pstack/agents/comment-sicko.md" {
t.Errorf("agents = %v", got)
}
// Installing every agent replaces the list with "*".
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", AllAgents: true}); err != nil {
t.Fatal(err)
}
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 2 {
t.Errorf("agents = %v, want both", got)
}
}
func TestAddNothingListsAgents(t *testing.T) {
s, _ := agentStore(t)
_, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins"})
var sel *NoSelectionError
if !errors.As(err, &sel) || len(sel.AvailableAgents) != 2 {
t.Fatalf("err = %v, want a NoSelectionError listing both agents", err)
}
}
func TestAddUnknownAgent(t *testing.T) {
s, _ := agentStore(t)
_, err := s.Add(context.Background(), AddRequest{URL: "github:cursor/plugins", Agents: agents("nope")})
if err == nil || !strings.Contains(err.Error(), `no agent "nope"`) {
t.Fatalf("err = %v", err)
}
}
func TestAgentModifiedUpstream(t *testing.T) {
ctx := context.Background()
s, f := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko", "poteto-agent")}); err != nil {
t.Fatal(err)
}
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko:v2", "poteto-agent"))
sts := check(t, s)
if got, want := changes(sts[0]), []string{"↑comment-sicko"}; !reflect.DeepEqual(got, want) {
t.Fatalf("changes = %v, want %v", got, want)
}
ch := sts[0].Changes[0]
if !ch.Agent || ch.Label() != "agent comment-sicko" {
t.Errorf("change = %+v", ch)
}
d, err := Diff(ctx, ch)
if err != nil || !strings.Contains(d, "-v1") || !strings.Contains(d, "+v2") {
t.Errorf("diff = %q, err %v", d, err)
}
if err := s.Apply(sts); err != nil {
t.Fatal(err)
}
if got := check(t, s); got[0].Outdated() {
t.Errorf("still outdated after apply: %v", changes(got[0]))
}
}
func TestAgentAddedUpstreamWithStar(t *testing.T) {
ctx := context.Background()
s, f := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", AllAgents: true}); err != nil {
t.Fatal(err)
}
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko:v1", "poteto-agent", "new-one"))
sts := check(t, s)
if got, want := changes(sts[0]), []string{"+new-one"}; !reflect.DeepEqual(got, want) {
t.Fatalf("changes = %v, want %v", got, want)
}
// An added agent is a file, so the diff compares against an empty file.
d, err := Diff(ctx, sts[0].Changes[0])
if err != nil || !strings.Contains(d, "+name: new-one") {
t.Errorf("diff = %q, err %v", d, err)
}
}
func TestAgentVanishesUpstream(t *testing.T) {
ctx := context.Background()
s, f := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("poteto-agent")}); err != nil {
t.Fatal(err)
}
f.repos["github:cursor/plugins"].push("r2", withAgents(t, fixture(t, "tdd"), "comment-sicko"))
sts, err := s.Check(ctx)
if err != nil {
t.Fatal(err)
}
if sts[0].Err == nil || !strings.Contains(sts[0].Err.Error(), `no agent "poteto-agent"`) {
t.Fatalf("status err = %v, want the missing agent reported", sts[0].Err)
}
}
func TestRemoveAgent(t *testing.T) {
ctx := context.Background()
s, _ := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko", "poteto-agent")}); err != nil {
t.Fatal(err)
}
if _, err := s.Remove("comment-sicko"); err == nil {
t.Error("Remove should only find skills")
}
if _, err := s.RemoveAgent("poteto-agent"); err != nil {
t.Fatal(err)
}
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 1 {
t.Errorf("agents = %v, want one left", got)
}
// The last entry takes the source with it.
if _, err := s.RemoveAgent("comment-sicko"); err != nil {
t.Fatal(err)
}
m, err := manifest.Load(filepath.Join(s.Dir, ManifestFile))
if err != nil || len(m.Sources) != 0 {
t.Errorf("manifest = %+v, err %v; want the empty source dropped", m, err)
}
}
func TestRemoveSkillKeepsSourceWithAgents(t *testing.T) {
ctx := context.Background()
s, _ := agentStore(t)
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Skills: pick("tdd"), Agents: agents("comment-sicko")}); err != nil {
t.Fatal(err)
}
if _, err := s.Remove("tdd"); err != nil {
t.Fatal(err)
}
if got := lockedAgents(t, s, "cursor/plugins"); len(got) != 1 {
t.Errorf("agents = %v, want the agent kept", got)
}
}
func TestAgentNameCollisionAcrossSources(t *testing.T) {
ctx := context.Background()
s, f := agentStore(t)
f.repos["github:other/plugins"] = &fakeRepo{head: "r1", revs: map[string]string{"r1": withAgents(t, fixture(t, "x"), "comment-sicko")}}
if _, err := s.Add(ctx, AddRequest{URL: "github:cursor/plugins", Agents: agents("comment-sicko")}); err != nil {
t.Fatal(err)
}
_, err := s.Add(ctx, AddRequest{URL: "github:other/plugins", Agents: agents("comment-sicko")})
if err == nil || !strings.Contains(err.Error(), `agent "comment-sicko" comes from both`) {
t.Fatalf("err = %v, want an agent collision", err)
}
}