Files
skillshare/internal/server/handler_agent_diff_test.go
T
Willie 7af9224896 fix(diff): list only what sync prunes, and have doctor follow it (#942)
Three previews disagreed with what sync does to a link no skill or
agent maps to.

The agent diff, in the CLI and the dashboard, listed every orphan link
as a removal. Agent sync prunes a link only in merge mode, and only when
it is broken or points into the source, so a live link to another
location was listed forever. Both diffs now ask the function prune uses.

The CLI diff listed no orphan links at all for merge-mode skills, so a
skill removed from the source showed nothing until sync pruned its
link. It now lists the links sync.OrphanLink says prune removes.

doctor called a broken link "did not create, kept" whenever OwnsLink was
false. Prune also removes a broken link under a name the manifest
records in any form once no skill maps to it, so doctor told the user to
delete by hand a link that sync removes. It now reports those as broken
and suggests sync. An empty source counts as "every entry is an orphan";
only a failed discovery turns the check off.

Closes #939
Closes #940
Closes #941
2026-11-17 13:38:31 +08:00

186 lines
6.1 KiB
Go

package server
import (
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
"skillshare/internal/resource"
"skillshare/internal/sync"
)
func TestComputeAgentTargetDiff_MissingInTarget(t *testing.T) {
targetDir := t.TempDir()
agents := []resource.DiscoveredResource{
{FlatName: "helper.md", AbsPath: "/src/helper.md", RelPath: "helper.md"},
}
items := computeAgentTargetDiff("claude", targetDir, t.TempDir(), config.ResourceTargetConfig{}, agents)
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Action != "link" {
t.Errorf("expected action 'link', got %q", items[0].Action)
}
if items[0].Kind != "agent" {
t.Errorf("expected kind 'agent', got %q", items[0].Kind)
}
}
func TestComputeAgentTargetDiff_OrphanSymlink(t *testing.T) {
targetDir := t.TempDir()
os.Symlink("/nonexistent/old.md", filepath.Join(targetDir, "orphan.md"))
items := computeAgentTargetDiff("claude", targetDir, t.TempDir(), config.ResourceTargetConfig{}, nil)
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Action != "prune" {
t.Errorf("expected action 'prune', got %q", items[0].Action)
}
}
func TestComputeAgentTargetDiff_LocalFile(t *testing.T) {
targetDir := t.TempDir()
os.WriteFile(filepath.Join(targetDir, "local.md"), []byte("# Local"), 0644)
items := computeAgentTargetDiff("claude", targetDir, t.TempDir(), config.ResourceTargetConfig{}, nil)
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Action != "local" {
t.Errorf("expected action 'local', got %q", items[0].Action)
}
}
func TestComputeAgentTargetDiff_SymlinkPointsElsewhere(t *testing.T) {
sourceDir := t.TempDir()
otherDir := t.TempDir()
targetDir := t.TempDir()
srcFile := filepath.Join(sourceDir, "agent.md")
os.WriteFile(srcFile, []byte("# Agent"), 0644)
otherFile := filepath.Join(otherDir, "agent.md")
os.WriteFile(otherFile, []byte("# Other"), 0644)
// Symlink points to otherFile, not srcFile
os.Symlink(otherFile, filepath.Join(targetDir, "agent.md"))
agents := []resource.DiscoveredResource{
{FlatName: "agent.md", AbsPath: srcFile, RelPath: "agent.md"},
}
items := computeAgentTargetDiff("claude", targetDir, sourceDir, config.ResourceTargetConfig{}, agents)
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Action != "update" {
t.Errorf("expected action 'update', got %q", items[0].Action)
}
if items[0].Reason != "symlink points elsewhere" {
t.Errorf("expected reason 'symlink points elsewhere', got %q", items[0].Reason)
}
}
func TestComputeAgentTargetDiff_InSync(t *testing.T) {
sourceDir := t.TempDir()
targetDir := t.TempDir()
srcFile := filepath.Join(sourceDir, "agent.md")
os.WriteFile(srcFile, []byte("# Agent"), 0644)
os.Symlink(srcFile, filepath.Join(targetDir, "agent.md"))
agents := []resource.DiscoveredResource{
{FlatName: "agent.md", AbsPath: srcFile, RelPath: "agent.md"},
}
items := computeAgentTargetDiff("claude", targetDir, sourceDir, config.ResourceTargetConfig{}, agents)
if len(items) != 0 {
t.Fatalf("expected 0 items (in sync), got %d", len(items))
}
}
func extensionTargetFixture(t *testing.T) (targetDir string, agents []resource.DiscoveredResource) {
t.Helper()
srcDir, targetDir := t.TempDir(), t.TempDir()
srcFile := filepath.Join(srcDir, "reviewer.md")
os.WriteFile(srcFile, []byte("# Reviewer"), 0644)
agents = []resource.DiscoveredResource{{FlatName: "reviewer.md", AbsPath: srcFile, RelPath: "reviewer.md"}}
spec := &sync.ExtensionSpec{Run: []string{"cat"}, Dir: srcDir, Name: "id", OutputExt: "toml"}
if _, err := sync.SyncAgentsTransform(agents, srcDir, targetDir, "copy", spec, false, false); err != nil {
t.Fatal(err)
}
return targetDir, agents
}
func TestComputeAgentTargetDiff_ExtensionOutputInSync(t *testing.T) {
targetDir, agents := extensionTargetFixture(t)
items := computeAgentTargetDiff("codex", targetDir, t.TempDir(), config.ResourceTargetConfig{Extension: "x"}, agents)
if len(items) != 0 {
t.Fatalf("expected 0 items (converted output in sync), got %+v", items)
}
}
func TestComputeAgentTargetDiff_ExtensionOutputOutdated(t *testing.T) {
targetDir, agents := extensionTargetFixture(t)
os.WriteFile(agents[0].AbsPath, []byte("# Reviewer v2"), 0644)
items := computeAgentTargetDiff("codex", targetDir, t.TempDir(), config.ResourceTargetConfig{Extension: "x"}, agents)
if len(items) != 1 || items[0].Action != "link" {
t.Fatalf("expected 1 link item, got %+v", items)
}
}
func TestComputeAgentTargetDiff_ExtensionOrphanOutput(t *testing.T) {
targetDir, _ := extensionTargetFixture(t)
items := computeAgentTargetDiff("codex", targetDir, t.TempDir(), config.ResourceTargetConfig{Extension: "x"}, nil)
if len(items) != 1 || items[0].Action != "prune" || items[0].Skill != "reviewer.toml" {
t.Fatalf("expected prune of reviewer.toml, got %+v", items)
}
}
func TestComputeAgentTargetDiff_ExcludedAgentIsNotPending(t *testing.T) {
agents := []resource.DiscoveredResource{
{FlatName: "helper.md", AbsPath: "/src/helper.md", RelPath: "helper.md"},
}
items := computeAgentTargetDiff("claude", t.TempDir(), t.TempDir(), config.ResourceTargetConfig{Exclude: []string{"helper*"}}, agents)
if len(items) != 0 {
t.Fatalf("expected 0 items for an excluded agent, got %+v", items)
}
}
// A live link to a file outside the source survives an agent sync, so the diff
// must not count it as a removal.
func TestComputeAgentTargetDiff_KeepsLiveLinkToAnotherLocation(t *testing.T) {
sourceDir, targetDir, other := t.TempDir(), t.TempDir(), t.TempDir()
theirs := filepath.Join(other, "theirs.md")
if err := os.WriteFile(theirs, []byte("# Theirs"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(theirs, filepath.Join(targetDir, "theirs.md")); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
if err := os.Symlink(filepath.Join(other, "gone.md"), filepath.Join(targetDir, "broken.md")); err != nil {
t.Fatal(err)
}
items := computeAgentTargetDiff("claude", targetDir, sourceDir, config.ResourceTargetConfig{}, nil)
if len(items) != 1 || items[0].Skill != "broken.md" || items[0].Action != "prune" {
t.Fatalf("items = %+v, want only the broken link pruned", items)
}
}