mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Add SyncAgents() dispatcher supporting all three sync modes: - merge: per-file symlinks with local file preservation - symlink: whole directory symlink to agent source - copy: file copy with content-diff detection Add CheckAgentCollisions() to detect agents that flatten to the same filename from different subdirectories. Add PruneOrphanAgentLinks() for merge mode and PruneOrphanAgentCopies() for copy mode to clean up stale agent files in targets. Add CollectAgents() to copy non-symlinked .md files from target back to agent source directory. 18 tests covering all modes, collision detection, pruning, and collect.
459 lines
13 KiB
Go
459 lines
13 KiB
Go
package sync
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"skillshare/internal/resource"
|
|
)
|
|
|
|
func TestCheckAgentCollisions_NoCollision(t *testing.T) {
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", RelPath: "tutor.md"},
|
|
{FlatName: "reviewer.md", RelPath: "reviewer.md"},
|
|
}
|
|
collisions := CheckAgentCollisions(agents)
|
|
if len(collisions) != 0 {
|
|
t.Errorf("expected 0 collisions, got %d", len(collisions))
|
|
}
|
|
}
|
|
|
|
func TestCheckAgentCollisions_HasCollision(t *testing.T) {
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "helper.md", RelPath: "a/helper.md"},
|
|
{FlatName: "helper.md", RelPath: "b/helper.md"},
|
|
}
|
|
collisions := CheckAgentCollisions(agents)
|
|
if len(collisions) != 1 {
|
|
t.Fatalf("expected 1 collision, got %d", len(collisions))
|
|
}
|
|
if collisions[0].FlatName != "helper.md" {
|
|
t.Errorf("collision FlatName = %q", collisions[0].FlatName)
|
|
}
|
|
}
|
|
|
|
func TestSyncAgentsToTarget_NewLinks(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
// Create agent source files
|
|
os.WriteFile(filepath.Join(sourceDir, "tutor.md"), []byte("# Tutor"), 0644)
|
|
os.WriteFile(filepath.Join(sourceDir, "reviewer.md"), []byte("# Reviewer"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: filepath.Join(sourceDir, "tutor.md")},
|
|
{FlatName: "reviewer.md", AbsPath: filepath.Join(sourceDir, "reviewer.md")},
|
|
}
|
|
|
|
result, err := SyncAgentsToTarget(agents, targetDir, false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgentsToTarget: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 2 {
|
|
t.Errorf("expected 2 linked, got %d", len(result.Linked))
|
|
}
|
|
|
|
// Verify symlinks exist
|
|
for _, name := range []string{"tutor.md", "reviewer.md"} {
|
|
linkPath := filepath.Join(targetDir, name)
|
|
info, err := os.Lstat(linkPath)
|
|
if err != nil {
|
|
t.Errorf("expected symlink %s to exist", name)
|
|
continue
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Errorf("expected %s to be a symlink", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSyncAgentsToTarget_ExistingSymlinkCorrect(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
srcFile := filepath.Join(sourceDir, "tutor.md")
|
|
os.WriteFile(srcFile, []byte("# Tutor"), 0644)
|
|
|
|
// Pre-create correct symlink
|
|
os.Symlink(srcFile, filepath.Join(targetDir, "tutor.md"))
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: srcFile},
|
|
}
|
|
|
|
result, err := SyncAgentsToTarget(agents, targetDir, false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgentsToTarget: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked (existing correct), got %d", len(result.Linked))
|
|
}
|
|
if len(result.Updated) != 0 {
|
|
t.Errorf("expected 0 updated, got %d", len(result.Updated))
|
|
}
|
|
}
|
|
|
|
func TestSyncAgentsToTarget_LocalFileSkipped(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
srcFile := filepath.Join(sourceDir, "tutor.md")
|
|
os.WriteFile(srcFile, []byte("# Tutor source"), 0644)
|
|
|
|
// Pre-create local file (not a symlink)
|
|
os.WriteFile(filepath.Join(targetDir, "tutor.md"), []byte("# Local tutor"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: srcFile},
|
|
}
|
|
|
|
result, err := SyncAgentsToTarget(agents, targetDir, false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgentsToTarget: %v", err)
|
|
}
|
|
|
|
if len(result.Skipped) != 1 {
|
|
t.Errorf("expected 1 skipped, got %d", len(result.Skipped))
|
|
}
|
|
}
|
|
|
|
func TestSyncAgentsToTarget_ForceReplacesLocal(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
srcFile := filepath.Join(sourceDir, "tutor.md")
|
|
os.WriteFile(srcFile, []byte("# Tutor source"), 0644)
|
|
|
|
os.WriteFile(filepath.Join(targetDir, "tutor.md"), []byte("# Local"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: srcFile},
|
|
}
|
|
|
|
result, err := SyncAgentsToTarget(agents, targetDir, false, true)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgentsToTarget: %v", err)
|
|
}
|
|
|
|
if len(result.Updated) != 1 {
|
|
t.Errorf("expected 1 updated, got %d", len(result.Updated))
|
|
}
|
|
|
|
// Should now be a symlink
|
|
info, _ := os.Lstat(filepath.Join(targetDir, "tutor.md"))
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("expected symlink after force")
|
|
}
|
|
}
|
|
|
|
func TestPruneOrphanAgentLinks(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
// Create source file and active symlink
|
|
srcFile := filepath.Join(sourceDir, "active.md")
|
|
os.WriteFile(srcFile, []byte("# Active"), 0644)
|
|
os.Symlink(srcFile, filepath.Join(targetDir, "active.md"))
|
|
|
|
// Create orphan symlink
|
|
orphanSrc := filepath.Join(sourceDir, "orphan.md")
|
|
os.WriteFile(orphanSrc, []byte("# Orphan"), 0644)
|
|
os.Symlink(orphanSrc, filepath.Join(targetDir, "orphan.md"))
|
|
|
|
// Create non-symlink file (should not be removed)
|
|
os.WriteFile(filepath.Join(targetDir, "local.md"), []byte("# Local"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "active.md"},
|
|
}
|
|
|
|
removed, err := PruneOrphanAgentLinks(targetDir, agents, false)
|
|
if err != nil {
|
|
t.Fatalf("PruneOrphanAgentLinks: %v", err)
|
|
}
|
|
|
|
if len(removed) != 1 {
|
|
t.Fatalf("expected 1 removed, got %d: %v", len(removed), removed)
|
|
}
|
|
if removed[0] != "orphan.md" {
|
|
t.Errorf("expected orphan.md removed, got %q", removed[0])
|
|
}
|
|
|
|
// local.md should still exist
|
|
if _, err := os.Stat(filepath.Join(targetDir, "local.md")); err != nil {
|
|
t.Error("local.md should not be removed")
|
|
}
|
|
}
|
|
|
|
func TestPruneOrphanAgentLinks_NonExistentDir(t *testing.T) {
|
|
removed, err := PruneOrphanAgentLinks("/nonexistent/path", nil, false)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(removed) != 0 {
|
|
t.Errorf("expected 0 removed, got %d", len(removed))
|
|
}
|
|
}
|
|
|
|
func TestCollectAgents(t *testing.T) {
|
|
targetDir := t.TempDir()
|
|
agentSourceDir := t.TempDir()
|
|
|
|
// Create a local (non-symlink) agent file in target
|
|
os.WriteFile(filepath.Join(targetDir, "new-agent.md"), []byte("# New agent"), 0644)
|
|
|
|
// Create a symlink (should be skipped)
|
|
srcFile := filepath.Join(agentSourceDir, "existing.md")
|
|
os.WriteFile(srcFile, []byte("# Existing"), 0644)
|
|
os.Symlink(srcFile, filepath.Join(targetDir, "existing.md"))
|
|
|
|
// Create README (should be skipped)
|
|
os.WriteFile(filepath.Join(targetDir, "README.md"), []byte("# Readme"), 0644)
|
|
|
|
// Create non-md file (should be skipped)
|
|
os.WriteFile(filepath.Join(targetDir, "config.yaml"), []byte("key: val"), 0644)
|
|
|
|
collectDir := t.TempDir()
|
|
collected, err := CollectAgents(targetDir, collectDir, false, nil)
|
|
if err != nil {
|
|
t.Fatalf("CollectAgents: %v", err)
|
|
}
|
|
|
|
if len(collected) != 1 {
|
|
t.Fatalf("expected 1 collected, got %d: %v", len(collected), collected)
|
|
}
|
|
if collected[0] != "new-agent.md" {
|
|
t.Errorf("collected = %q, want %q", collected[0], "new-agent.md")
|
|
}
|
|
|
|
// Verify file was copied
|
|
data, err := os.ReadFile(filepath.Join(collectDir, "new-agent.md"))
|
|
if err != nil {
|
|
t.Fatalf("collected file not found: %v", err)
|
|
}
|
|
if string(data) != "# New agent" {
|
|
t.Errorf("collected content = %q", string(data))
|
|
}
|
|
}
|
|
|
|
// --- Symlink mode tests ---
|
|
|
|
func TestSyncAgents_SymlinkMode_NewDir(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
os.WriteFile(filepath.Join(sourceDir, "tutor.md"), []byte("# Tutor"), 0644)
|
|
|
|
targetDir := filepath.Join(t.TempDir(), "agents")
|
|
|
|
result, err := SyncAgents(nil, sourceDir, targetDir, "symlink", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents symlink: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked, got %d", len(result.Linked))
|
|
}
|
|
|
|
// targetDir should be a symlink to sourceDir
|
|
info, err := os.Lstat(targetDir)
|
|
if err != nil {
|
|
t.Fatalf("Lstat: %v", err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("expected targetDir to be a symlink")
|
|
}
|
|
}
|
|
|
|
func TestSyncAgents_SymlinkMode_AlreadyCorrect(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
parentDir := t.TempDir()
|
|
targetDir := filepath.Join(parentDir, "agents")
|
|
|
|
os.Symlink(sourceDir, targetDir)
|
|
|
|
result, err := SyncAgents(nil, sourceDir, targetDir, "symlink", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents symlink: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked (already correct), got %d", len(result.Linked))
|
|
}
|
|
if len(result.Updated) != 0 {
|
|
t.Errorf("expected 0 updated, got %d", len(result.Updated))
|
|
}
|
|
}
|
|
|
|
func TestSyncAgents_SymlinkMode_RealDirSkipped(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir() // real directory
|
|
|
|
result, err := SyncAgents(nil, sourceDir, targetDir, "symlink", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents symlink: %v", err)
|
|
}
|
|
|
|
if len(result.Skipped) != 1 {
|
|
t.Errorf("expected 1 skipped, got %d", len(result.Skipped))
|
|
}
|
|
}
|
|
|
|
// --- Copy mode tests ---
|
|
|
|
func TestSyncAgents_CopyMode_NewFiles(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(sourceDir, "tutor.md"), []byte("# Tutor"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: filepath.Join(sourceDir, "tutor.md")},
|
|
}
|
|
|
|
result, err := SyncAgents(agents, sourceDir, targetDir, "copy", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents copy: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked (new copy), got %d", len(result.Linked))
|
|
}
|
|
|
|
// Verify it's a real file, not a symlink
|
|
info, _ := os.Lstat(filepath.Join(targetDir, "tutor.md"))
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
t.Error("copy mode should create real files, not symlinks")
|
|
}
|
|
|
|
data, _ := os.ReadFile(filepath.Join(targetDir, "tutor.md"))
|
|
if string(data) != "# Tutor" {
|
|
t.Errorf("content = %q", string(data))
|
|
}
|
|
}
|
|
|
|
func TestSyncAgents_CopyMode_SameContent(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(sourceDir, "tutor.md"), []byte("# Same"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "tutor.md"), []byte("# Same"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: filepath.Join(sourceDir, "tutor.md")},
|
|
}
|
|
|
|
result, err := SyncAgents(agents, sourceDir, targetDir, "copy", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents copy: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked (same content), got %d", len(result.Linked))
|
|
}
|
|
if len(result.Updated) != 0 {
|
|
t.Errorf("expected 0 updated, got %d", len(result.Updated))
|
|
}
|
|
}
|
|
|
|
func TestSyncAgents_CopyMode_DifferentContent(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(sourceDir, "tutor.md"), []byte("# New"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "tutor.md"), []byte("# Old"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "tutor.md", AbsPath: filepath.Join(sourceDir, "tutor.md")},
|
|
}
|
|
|
|
result, err := SyncAgents(agents, sourceDir, targetDir, "copy", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents copy: %v", err)
|
|
}
|
|
|
|
if len(result.Updated) != 1 {
|
|
t.Errorf("expected 1 updated, got %d", len(result.Updated))
|
|
}
|
|
|
|
data, _ := os.ReadFile(filepath.Join(targetDir, "tutor.md"))
|
|
if string(data) != "# New" {
|
|
t.Errorf("content = %q, want %q", string(data), "# New")
|
|
}
|
|
}
|
|
|
|
func TestPruneOrphanAgentCopies(t *testing.T) {
|
|
targetDir := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(targetDir, "active.md"), []byte("# Active"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "orphan.md"), []byte("# Orphan"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "README.md"), []byte("# Readme"), 0644) // conventional, skip
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "active.md"},
|
|
}
|
|
|
|
removed, err := PruneOrphanAgentCopies(targetDir, agents, false)
|
|
if err != nil {
|
|
t.Fatalf("PruneOrphanAgentCopies: %v", err)
|
|
}
|
|
|
|
if len(removed) != 1 || removed[0] != "orphan.md" {
|
|
t.Errorf("expected [orphan.md] removed, got %v", removed)
|
|
}
|
|
|
|
// README.md should still exist
|
|
if _, err := os.Stat(filepath.Join(targetDir, "README.md")); err != nil {
|
|
t.Error("README.md should not be removed")
|
|
}
|
|
}
|
|
|
|
// --- Dispatch tests ---
|
|
|
|
func TestSyncAgents_DefaultIsMerge(t *testing.T) {
|
|
sourceDir := t.TempDir()
|
|
targetDir := t.TempDir()
|
|
|
|
os.WriteFile(filepath.Join(sourceDir, "a.md"), []byte("# A"), 0644)
|
|
|
|
agents := []resource.DiscoveredResource{
|
|
{FlatName: "a.md", AbsPath: filepath.Join(sourceDir, "a.md")},
|
|
}
|
|
|
|
result, err := SyncAgents(agents, sourceDir, targetDir, "", false, false)
|
|
if err != nil {
|
|
t.Fatalf("SyncAgents default: %v", err)
|
|
}
|
|
|
|
if len(result.Linked) != 1 {
|
|
t.Errorf("expected 1 linked, got %d", len(result.Linked))
|
|
}
|
|
|
|
// Should be a symlink (merge mode)
|
|
info, _ := os.Lstat(filepath.Join(targetDir, "a.md"))
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("default mode should create symlinks (merge)")
|
|
}
|
|
}
|
|
|
|
func TestCollectAgents_DryRun(t *testing.T) {
|
|
targetDir := t.TempDir()
|
|
os.WriteFile(filepath.Join(targetDir, "agent.md"), []byte("# Agent"), 0644)
|
|
|
|
collectDir := t.TempDir()
|
|
collected, err := CollectAgents(targetDir, collectDir, true, nil)
|
|
if err != nil {
|
|
t.Fatalf("CollectAgents dry-run: %v", err)
|
|
}
|
|
|
|
if len(collected) != 1 {
|
|
t.Fatalf("expected 1 collected in dry-run, got %d", len(collected))
|
|
}
|
|
|
|
// File should NOT exist (dry-run)
|
|
if _, err := os.Stat(filepath.Join(collectDir, "agent.md")); err == nil {
|
|
t.Error("file should not exist in dry-run")
|
|
}
|
|
}
|