mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
In copy mode, sync --force can push source over target, but collect skipped every file already in source, so edits made directly in a copy-mode target could not be pulled back. collect --force now overwrites those files; identical ones are still skipped. Collect also replaced collected files with symlinks regardless of mode, turning copy-mode targets into symlinks. Copy-mode targets now keep their files, in the CLI and the extras TUI. With flatten, two target files sharing a basename map to one source file; the second is now reported instead of overwriting the first under --force. --from paths are matched after expanding ~ and cleaning, so a spelling that differs from config no longer drops the target's mode. Refs #291
1022 lines
30 KiB
Go
1022 lines
30 KiB
Go
package sync
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// setupExtrasTest creates source and target directories under a temp root.
|
|
// It writes the given files (relative paths) into the source directory.
|
|
func setupExtrasTest(t *testing.T, files map[string]string) (srcDir, tgtDir string) {
|
|
t.Helper()
|
|
tmp := t.TempDir()
|
|
srcDir = filepath.Join(tmp, "extras-source")
|
|
tgtDir = filepath.Join(tmp, "extras-target")
|
|
os.MkdirAll(srcDir, 0755)
|
|
os.MkdirAll(tgtDir, 0755)
|
|
for rel, content := range files {
|
|
full := filepath.Join(srcDir, rel)
|
|
os.MkdirAll(filepath.Dir(full), 0755)
|
|
if err := os.WriteFile(full, []byte(content), 0644); err != nil {
|
|
t.Fatalf("write %s: %v", rel, err)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// --- DiscoverExtraFiles tests ---
|
|
|
|
func TestDiscoverExtraFiles(t *testing.T) {
|
|
src, _ := setupExtrasTest(t, map[string]string{
|
|
"flat.txt": "hello",
|
|
"nested/deep.md": "# deep",
|
|
".git/config": "should be skipped",
|
|
".git/hooks/pre-ci": "skip",
|
|
})
|
|
|
|
files, err := DiscoverExtraFiles(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Should find flat.txt and nested/deep.md, skip .git/**
|
|
if len(files) != 2 {
|
|
t.Fatalf("expected 2 files, got %d: %v", len(files), files)
|
|
}
|
|
|
|
expected := map[string]bool{
|
|
"flat.txt": true,
|
|
filepath.Join("nested", "deep.md"): true,
|
|
}
|
|
for _, f := range files {
|
|
if !expected[f] {
|
|
t.Errorf("unexpected file: %q", f)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestDiscoverExtraFiles_SkipsMetadata verifies skillshare's reserved
|
|
// .metadata.json tracking store is never discovered, so it cannot be synced or
|
|
// transformed into a target (e.g. installed-agent dirs carry one).
|
|
func TestDiscoverExtraFiles_SkipsMetadata(t *testing.T) {
|
|
src, _ := setupExtrasTest(t, map[string]string{
|
|
"agent.md": "# Agent",
|
|
".metadata.json": `{"version":1,"entries":{}}`,
|
|
})
|
|
|
|
files, err := DiscoverExtraFiles(src)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(files) != 1 || files[0] != "agent.md" {
|
|
t.Errorf("expected only [agent.md], got %v", files)
|
|
}
|
|
}
|
|
|
|
// --- SyncExtra merge mode tests ---
|
|
|
|
func TestSyncExtra_MergeMode(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"rules.md": "# Rules",
|
|
"config.yml": "key: value",
|
|
})
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 2 {
|
|
t.Errorf("expected 2 synced, got %d", result.Synced)
|
|
}
|
|
|
|
// Verify symlinks exist and point to source
|
|
for _, rel := range []string{"rules.md", "config.yml"} {
|
|
tgtPath := filepath.Join(tgt, rel)
|
|
info, lErr := os.Lstat(tgtPath)
|
|
if lErr != nil {
|
|
t.Errorf("expected file %s to exist: %v", rel, lErr)
|
|
continue
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Errorf("expected %s to be a symlink", rel)
|
|
}
|
|
dest, _ := os.Readlink(tgtPath)
|
|
absDest, _ := filepath.Abs(dest)
|
|
absSrc, _ := filepath.Abs(filepath.Join(src, rel))
|
|
if absDest != absSrc {
|
|
t.Errorf("symlink %s points to %s, expected %s", rel, absDest, absSrc)
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- SyncExtra copy mode tests ---
|
|
|
|
func TestSyncExtra_CopyMode(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"readme.txt": "hello world",
|
|
})
|
|
|
|
result, err := SyncExtra(src, tgt, "copy", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
|
|
// Verify it's a real file, not a symlink
|
|
tgtPath := filepath.Join(tgt, "readme.txt")
|
|
info, err := os.Lstat(tgtPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
t.Error("expected a real file, got a symlink")
|
|
}
|
|
|
|
content, err := os.ReadFile(tgtPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(content) != "hello world" {
|
|
t.Errorf("expected 'hello world', got %q", string(content))
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_CopyModePreservesLocalFilesOnResync(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"managed.md": "managed",
|
|
})
|
|
|
|
if _, err := SyncExtra(src, tgt, "copy", false, true, false, "", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := filepath.Join(tgt, "local.md")
|
|
if err := os.WriteFile(local, []byte("local"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
result, err := SyncExtra(src, tgt, "copy", false, true, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Pruned != 0 {
|
|
t.Errorf("expected no copy-mode prune without ownership metadata, got %d", result.Pruned)
|
|
}
|
|
if _, err := os.Stat(local); err != nil {
|
|
t.Errorf("local file must be preserved, stat err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_TransformPreservesLocalFilesOnResync(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"managed.md": "managed",
|
|
})
|
|
spec := &ExtensionSpec{Run: []string{"cat"}, OutputExt: "toml"}
|
|
|
|
if _, err := SyncExtra(src, tgt, "copy", false, true, false, "", spec); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := filepath.Join(tgt, "local.md")
|
|
if err := os.WriteFile(local, []byte("local"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
result, err := SyncExtra(src, tgt, "copy", false, true, false, "", spec)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Pruned != 0 {
|
|
t.Errorf("expected no transform prune without ownership metadata, got %d", result.Pruned)
|
|
}
|
|
if _, err := os.Stat(local); err != nil {
|
|
t.Errorf("local file must be preserved, stat err = %v", err)
|
|
}
|
|
}
|
|
|
|
// --- Conflict tests ---
|
|
|
|
func TestSyncExtra_ConflictSkipped(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"conflict.md": "from source",
|
|
})
|
|
|
|
// Pre-create a local file at the target
|
|
os.WriteFile(filepath.Join(tgt, "conflict.md"), []byte("local version"), 0644)
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Skipped != 1 {
|
|
t.Errorf("expected 1 skipped, got %d", result.Skipped)
|
|
}
|
|
if result.Synced != 0 {
|
|
t.Errorf("expected 0 synced, got %d", result.Synced)
|
|
}
|
|
|
|
// Original local content should be preserved
|
|
content, _ := os.ReadFile(filepath.Join(tgt, "conflict.md"))
|
|
if string(content) != "local version" {
|
|
t.Errorf("expected local content preserved, got %q", string(content))
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_ConflictForce(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"conflict.md": "from source",
|
|
})
|
|
|
|
// Pre-create a local file at the target
|
|
os.WriteFile(filepath.Join(tgt, "conflict.md"), []byte("local version"), 0644)
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, true, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced (force replaced), got %d", result.Synced)
|
|
}
|
|
if result.Skipped != 0 {
|
|
t.Errorf("expected 0 skipped, got %d", result.Skipped)
|
|
}
|
|
|
|
// Should now be a symlink
|
|
info, _ := os.Lstat(filepath.Join(tgt, "conflict.md"))
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("expected symlink after force replace")
|
|
}
|
|
}
|
|
|
|
// --- Nested directories test ---
|
|
|
|
func TestSyncExtra_NestedDirectories(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
filepath.Join("a", "b", "deep.md"): "deep content",
|
|
})
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
|
|
deepTarget := filepath.Join(tgt, "a", "b", "deep.md")
|
|
info, err := os.Lstat(deepTarget)
|
|
if err != nil {
|
|
t.Fatalf("expected nested file to exist: %v", err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("expected symlink for nested file")
|
|
}
|
|
}
|
|
|
|
// --- Empty source test ---
|
|
|
|
func TestSyncExtra_EmptySource(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{})
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 0 {
|
|
t.Errorf("expected 0 synced for empty source, got %d", result.Synced)
|
|
}
|
|
}
|
|
|
|
// --- Source does not exist test ---
|
|
|
|
func TestSyncExtra_SourceNotExist(t *testing.T) {
|
|
tgt := t.TempDir()
|
|
_, err := SyncExtra("/nonexistent/extras/source", tgt, "merge", false, false, false, "", nil)
|
|
if err == nil {
|
|
t.Error("expected error for non-existent source")
|
|
}
|
|
}
|
|
|
|
// --- Dry run test ---
|
|
|
|
func TestSyncExtra_DryRun(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"alpha.md": "a",
|
|
"beta.md": "b",
|
|
})
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", true, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 2 {
|
|
t.Errorf("expected 2 synced in dry-run count, got %d", result.Synced)
|
|
}
|
|
|
|
// Verify no files were actually created
|
|
entries, _ := os.ReadDir(tgt)
|
|
if len(entries) != 0 {
|
|
t.Errorf("expected empty target in dry-run, got %d entries", len(entries))
|
|
}
|
|
}
|
|
|
|
// TestSyncExtraTransform_DryRunConflict verifies that a transform-mode dry-run
|
|
// reports an existing target (under the transformed name agent.md → agent.toml)
|
|
// as a skip that needs --force, instead of claiming it would sync. The dry-run
|
|
// never spawns the extension (see TestSyncExtraTransform_DryRunNoSpawn), so it
|
|
// can't compare content and conservatively treats any real file as a conflict —
|
|
// and writes nothing.
|
|
func TestSyncExtraTransform_DryRunConflict(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"agent.md": "# Agent"})
|
|
spec := &ExtensionSpec{Run: []string{"cat"}, OutputExt: "toml"}
|
|
|
|
// Pre-create a real file at the transformed target name.
|
|
conflictPath := filepath.Join(tgt, "agent.toml")
|
|
os.WriteFile(conflictPath, []byte("local version"), 0644)
|
|
|
|
result, err := SyncExtra(src, tgt, "copy", true /* dryRun */, false /* force */, false, "", spec)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Skipped != 1 {
|
|
t.Errorf("expected 1 skipped on dry-run conflict, got %d", result.Skipped)
|
|
}
|
|
if result.Synced != 0 {
|
|
t.Errorf("expected 0 synced on dry-run conflict, got %d", result.Synced)
|
|
}
|
|
|
|
// Dry-run must not touch the conflicting file.
|
|
content, _ := os.ReadFile(conflictPath)
|
|
if string(content) != "local version" {
|
|
t.Errorf("dry-run modified the target, got %q", string(content))
|
|
}
|
|
}
|
|
|
|
// --- Idempotent test ---
|
|
|
|
func TestSyncExtra_Idempotent(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"stable.md": "content",
|
|
})
|
|
|
|
// First sync
|
|
r1, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r1.Synced != 1 {
|
|
t.Fatalf("first sync: expected 1 synced, got %d", r1.Synced)
|
|
}
|
|
|
|
// Second sync — should still report synced (already correct)
|
|
r2, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r2.Synced != 1 {
|
|
t.Errorf("second sync: expected 1 synced (idempotent), got %d", r2.Synced)
|
|
}
|
|
if r2.Skipped != 0 {
|
|
t.Errorf("second sync: expected 0 skipped, got %d", r2.Skipped)
|
|
}
|
|
}
|
|
|
|
// --- Prune orphans test ---
|
|
|
|
func TestSyncExtra_PrunesOrphans(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"keep.md": "keep this",
|
|
"remove.md": "will be removed from source",
|
|
})
|
|
|
|
// First sync both files
|
|
_, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Remove "remove.md" from source
|
|
os.Remove(filepath.Join(src, "remove.md"))
|
|
|
|
// Sync again — should prune orphan
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Pruned != 1 {
|
|
t.Errorf("expected 1 pruned, got %d", result.Pruned)
|
|
}
|
|
|
|
// Verify orphan was removed
|
|
if _, err := os.Lstat(filepath.Join(tgt, "remove.md")); !os.IsNotExist(err) {
|
|
t.Error("expected orphan symlink to be removed")
|
|
}
|
|
|
|
// Verify kept file still exists
|
|
if _, err := os.Lstat(filepath.Join(tgt, "keep.md")); err != nil {
|
|
t.Error("expected keep.md to still exist")
|
|
}
|
|
}
|
|
|
|
// --- CollectExtraFiles tests ---
|
|
|
|
func TestCollectExtraFiles(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
|
|
// Create target with a local file
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("# Rule 1"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", false, false, false, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Collected != 1 {
|
|
t.Errorf("Collected = %d, want 1", result.Collected)
|
|
}
|
|
|
|
// Verify file moved to source
|
|
data, err := os.ReadFile(filepath.Join(sourceDir, "rule1.md"))
|
|
if err != nil {
|
|
t.Fatalf("Read source file: %v", err)
|
|
}
|
|
if string(data) != "# Rule 1" {
|
|
t.Errorf("Source content = %q, want %q", string(data), "# Rule 1")
|
|
}
|
|
|
|
// Verify target is now a symlink
|
|
info, err := os.Lstat(filepath.Join(targetDir, "rule1.md"))
|
|
if err != nil {
|
|
t.Fatalf("Lstat target: %v", err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("Target should be a symlink after collect")
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_DryRun(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("# Rule 1"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", true, false, false, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles dry run: %v", err)
|
|
}
|
|
|
|
if result.Collected != 1 {
|
|
t.Errorf("Collected = %d, want 1", result.Collected)
|
|
}
|
|
|
|
// Verify file NOT moved (dry run)
|
|
if _, err := os.Stat(filepath.Join(sourceDir, "rule1.md")); !os.IsNotExist(err) {
|
|
t.Error("Source file should not exist in dry run")
|
|
}
|
|
|
|
// Verify target is still a regular file
|
|
info, err := os.Lstat(filepath.Join(targetDir, "rule1.md"))
|
|
if err != nil {
|
|
t.Fatalf("Lstat target: %v", err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
t.Error("Target should NOT be a symlink in dry run")
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_SkipsExisting(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
|
|
// Create both source and target with same file
|
|
os.MkdirAll(sourceDir, 0755)
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(sourceDir, "rule1.md"), []byte("source version"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("target version"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", false, false, false, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Skipped != 1 {
|
|
t.Errorf("Skipped = %d, want 1", result.Skipped)
|
|
}
|
|
if result.Collected != 0 {
|
|
t.Errorf("Collected = %d, want 0", result.Collected)
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_ForceOverwritesExisting(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(sourceDir, 0755)
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(sourceDir, "rule1.md"), []byte("source version"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("target version"), 0644)
|
|
|
|
if _, err := CollectExtraFiles(sourceDir, targetDir, "", false, true, false, ""); err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
data, _ := os.ReadFile(filepath.Join(sourceDir, "rule1.md"))
|
|
if string(data) != "target version" {
|
|
t.Errorf("Source content = %q, want %q", string(data), "target version")
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_ForceSkipsIdentical(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(sourceDir, 0755)
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(sourceDir, "rule1.md"), []byte("same"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("same"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "copy", false, true, false, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Collected != 0 || result.Skipped != 1 {
|
|
t.Errorf("Collected/Skipped = %d/%d, want 0/1", result.Collected, result.Skipped)
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_CopyModeKeepsTargetFile(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(targetDir, 0755)
|
|
os.WriteFile(filepath.Join(targetDir, "rule1.md"), []byte("# Rule 1"), 0644)
|
|
|
|
if _, err := CollectExtraFiles(sourceDir, targetDir, "copy", false, false, false, ""); err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
info, err := os.Lstat(filepath.Join(targetDir, "rule1.md"))
|
|
if err != nil {
|
|
t.Fatalf("Lstat target: %v", err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
t.Error("Target should stay a regular file in copy mode")
|
|
}
|
|
}
|
|
|
|
// --- Symlink mode (entire directory) test ---
|
|
|
|
func TestSyncExtra_SymlinkMode(t *testing.T) {
|
|
src, _ := setupExtrasTest(t, map[string]string{
|
|
"file.txt": "content",
|
|
})
|
|
|
|
tmp := t.TempDir()
|
|
tgt := filepath.Join(tmp, "extras-link")
|
|
|
|
result, err := SyncExtra(src, tgt, "symlink", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
|
|
// Verify target is a symlink to the source directory
|
|
info, err := os.Lstat(tgt)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("expected target to be a symlink")
|
|
}
|
|
|
|
dest, _ := os.Readlink(tgt)
|
|
absDest, _ := filepath.Abs(dest)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if absDest != absSrc {
|
|
t.Errorf("symlink points to %s, expected %s", absDest, absSrc)
|
|
}
|
|
|
|
// Verify files are accessible through the symlink
|
|
content, err := os.ReadFile(filepath.Join(tgt, "file.txt"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(content) != "content" {
|
|
t.Errorf("expected 'content', got %q", string(content))
|
|
}
|
|
}
|
|
|
|
// --- Flatten tests ---
|
|
|
|
func TestSyncExtra_FlattenMerge(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"sub1/a.md": "# A from sub1",
|
|
"sub2/b.md": "# B from sub2",
|
|
"root.md": "# Root",
|
|
})
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 3 {
|
|
t.Errorf("expected 3 synced, got %d", result.Synced)
|
|
}
|
|
for _, name := range []string{"a.md", "b.md", "root.md"} {
|
|
info, lErr := os.Lstat(filepath.Join(tgt, name))
|
|
if lErr != nil {
|
|
t.Errorf("expected %s in target root: %v", name, lErr)
|
|
continue
|
|
}
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Errorf("expected %s to be a symlink", name)
|
|
}
|
|
}
|
|
if _, err := os.Stat(filepath.Join(tgt, "sub1")); !os.IsNotExist(err) {
|
|
t.Error("sub1/ should not exist in target when flatten is true")
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenCopy(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"deep/nested/file.md": "# Deep",
|
|
})
|
|
result, err := SyncExtra(src, tgt, "copy", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
tgtFile := filepath.Join(tgt, "file.md")
|
|
info, err := os.Lstat(tgtFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
t.Error("expected real file in copy mode, got symlink")
|
|
}
|
|
content, _ := os.ReadFile(tgtFile)
|
|
if string(content) != "# Deep" {
|
|
t.Errorf("content = %q, want %q", string(content), "# Deep")
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenCollision(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"sub1/conflict.md": "# From sub1",
|
|
"sub2/conflict.md": "# From sub2",
|
|
})
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
if result.Skipped != 1 {
|
|
t.Errorf("expected 1 skipped (collision), got %d", result.Skipped)
|
|
}
|
|
if len(result.Warnings) != 1 {
|
|
t.Errorf("expected 1 warning, got %d", len(result.Warnings))
|
|
}
|
|
link := filepath.Join(tgt, "conflict.md")
|
|
dest, _ := os.Readlink(link)
|
|
absDest, _ := filepath.Abs(dest)
|
|
absSrc, _ := filepath.Abs(filepath.Join(src, "sub1", "conflict.md"))
|
|
if absDest != absSrc {
|
|
t.Errorf("symlink points to %s, expected %s (first file should win)", absDest, absSrc)
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenPrune(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"sub/keep.md": "# Keep",
|
|
})
|
|
_, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
orphanSrc := filepath.Join(src, "old", "removed.md")
|
|
os.MkdirAll(filepath.Dir(orphanSrc), 0755)
|
|
os.WriteFile(orphanSrc, []byte("old"), 0644)
|
|
os.Symlink(orphanSrc, filepath.Join(tgt, "removed.md"))
|
|
os.RemoveAll(filepath.Join(src, "old"))
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Pruned != 1 {
|
|
t.Errorf("expected 1 pruned, got %d", result.Pruned)
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(tgt, "keep.md")); err != nil {
|
|
t.Error("keep.md should still exist")
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(tgt, "removed.md")); !os.IsNotExist(err) {
|
|
t.Error("removed.md should have been pruned")
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenDryRun(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"sub/file.md": "content",
|
|
})
|
|
result, err := SyncExtra(src, tgt, "merge", true, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced in dry-run, got %d", result.Synced)
|
|
}
|
|
entries, _ := os.ReadDir(tgt)
|
|
if len(entries) != 0 {
|
|
t.Errorf("expected empty target in dry-run, got %d entries", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenDryRunCollision(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"a/same.md": "# A",
|
|
"b/same.md": "# B",
|
|
})
|
|
result, err := SyncExtra(src, tgt, "merge", true, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Synced != 1 {
|
|
t.Errorf("expected 1 synced, got %d", result.Synced)
|
|
}
|
|
if result.Skipped != 1 {
|
|
t.Errorf("expected 1 skipped (collision), got %d", result.Skipped)
|
|
}
|
|
if len(result.Warnings) != 1 {
|
|
t.Errorf("expected 1 collision warning in dry-run, got %d", len(result.Warnings))
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_FlattenIdempotent(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"sub/file.md": "content",
|
|
})
|
|
r1, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r1.Synced != 1 {
|
|
t.Fatalf("first sync: expected 1 synced, got %d", r1.Synced)
|
|
}
|
|
r2, err := SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r2.Synced != 1 {
|
|
t.Errorf("second sync: expected 1 synced (idempotent), got %d", r2.Synced)
|
|
}
|
|
if r2.Skipped != 0 {
|
|
t.Errorf("second sync: expected 0 skipped, got %d", r2.Skipped)
|
|
}
|
|
}
|
|
|
|
// --- Flatten collect tests ---
|
|
|
|
func TestCollectExtraFiles_Flatten(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(filepath.Join(sourceDir, "sub"), 0755)
|
|
os.MkdirAll(targetDir, 0755)
|
|
|
|
// Existing synced file: source/sub/existing.md → target/existing.md (flat symlink)
|
|
srcFile := filepath.Join(sourceDir, "sub", "existing.md")
|
|
os.WriteFile(srcFile, []byte("# Existing"), 0644)
|
|
os.Symlink(srcFile, filepath.Join(targetDir, "existing.md"))
|
|
|
|
// New local file in target (not from source)
|
|
os.WriteFile(filepath.Join(targetDir, "new-agent.md"), []byte("# New Agent"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", false, false, true, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Collected != 1 {
|
|
t.Errorf("Collected = %d, want 1", result.Collected)
|
|
}
|
|
|
|
// Verify new file placed in source root (not subdirectory)
|
|
collected := filepath.Join(sourceDir, "new-agent.md")
|
|
if _, err := os.Stat(collected); err != nil {
|
|
t.Errorf("expected new-agent.md in source root: %v", err)
|
|
}
|
|
|
|
// Verify target is now a symlink
|
|
info, _ := os.Lstat(filepath.Join(targetDir, "new-agent.md"))
|
|
if info.Mode()&os.ModeSymlink == 0 {
|
|
t.Error("target should be a symlink after collect")
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_FlattenSkipsExisting(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(sourceDir, 0755)
|
|
os.MkdirAll(targetDir, 0755)
|
|
|
|
os.WriteFile(filepath.Join(sourceDir, "conflict.md"), []byte("source version"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "conflict.md"), []byte("target version"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", false, false, true, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Skipped != 1 {
|
|
t.Errorf("Skipped = %d, want 1", result.Skipped)
|
|
}
|
|
if result.Collected != 0 {
|
|
t.Errorf("Collected = %d, want 0", result.Collected)
|
|
}
|
|
}
|
|
|
|
func TestCollectExtraFiles_ForceFlattenCollisionKeepsFirst(t *testing.T) {
|
|
sourceDir := filepath.Join(t.TempDir(), "source")
|
|
targetDir := filepath.Join(t.TempDir(), "target")
|
|
os.MkdirAll(filepath.Join(targetDir, "a"), 0755)
|
|
os.MkdirAll(filepath.Join(targetDir, "b"), 0755)
|
|
os.WriteFile(filepath.Join(targetDir, "a", "x.md"), []byte("A"), 0644)
|
|
os.WriteFile(filepath.Join(targetDir, "b", "x.md"), []byte("B"), 0644)
|
|
|
|
result, err := CollectExtraFiles(sourceDir, targetDir, "", false, true, true, "")
|
|
if err != nil {
|
|
t.Fatalf("CollectExtraFiles: %v", err)
|
|
}
|
|
|
|
if result.Collected != 1 || len(result.Errors) != 1 {
|
|
t.Errorf("Collected/Errors = %d/%d, want 1/1", result.Collected, len(result.Errors))
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(sourceDir, "x.md"))
|
|
if string(data) != "A" {
|
|
t.Errorf("Source content = %q, want %q", string(data), "A")
|
|
}
|
|
}
|
|
|
|
// --- FlattenRel tests ---
|
|
|
|
func TestFlattenRel_NotFlatten(t *testing.T) {
|
|
seen := make(map[string]bool)
|
|
tgt, ok := FlattenRel("sub/file.md", false, seen)
|
|
if !ok || tgt != "sub/file.md" {
|
|
t.Errorf("got (%s, %v), want (sub/file.md, true)", tgt, ok)
|
|
}
|
|
}
|
|
|
|
func TestFlattenRel_Flatten(t *testing.T) {
|
|
seen := make(map[string]bool)
|
|
tgt, ok := FlattenRel("sub/file.md", true, seen)
|
|
if !ok || tgt != "file.md" {
|
|
t.Errorf("got (%s, %v), want (file.md, true)", tgt, ok)
|
|
}
|
|
}
|
|
|
|
func TestFlattenRel_Collision(t *testing.T) {
|
|
seen := make(map[string]bool)
|
|
FlattenRel("a/file.md", true, seen)
|
|
_, ok := FlattenRel("b/file.md", true, seen)
|
|
if ok {
|
|
t.Error("expected collision (false), got true")
|
|
}
|
|
}
|
|
|
|
// --- CheckSyncStatus tests ---
|
|
|
|
func TestCheckSyncStatus_Synced(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"rule.md": "# Rule"})
|
|
SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "merge", false, ""); s != "synced" {
|
|
t.Errorf("expected synced, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_MergeRelativeSymlinkSynced(t *testing.T) {
|
|
projectRoot := t.TempDir()
|
|
src := filepath.Join(projectRoot, ".skillshare", "extras", "rules")
|
|
tgt := filepath.Join(projectRoot, ".cursor", "rules")
|
|
if err := os.MkdirAll(src, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(tgt, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(src, "rule.md"), []byte("# Rule"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := SyncExtra(src, tgt, "merge", false, false, false, projectRoot, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link, err := os.Readlink(filepath.Join(tgt, "rule.md"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if filepath.IsAbs(link) {
|
|
t.Fatalf("expected relative symlink in project mode, got %q", link)
|
|
}
|
|
|
|
files, _ := DiscoverExtraFiles(src)
|
|
if s := CheckSyncStatus(files, src, tgt, "merge", false, ""); s != "synced" {
|
|
t.Errorf("expected synced for relative project symlink, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_Drift(t *testing.T) {
|
|
src, _ := setupExtrasTest(t, map[string]string{"rule.md": "# Rule"})
|
|
tgt := filepath.Join(t.TempDir(), "empty")
|
|
os.MkdirAll(tgt, 0755)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "merge", false, ""); s != "drift" {
|
|
t.Errorf("expected drift, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_FlattenSynced(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"sub/file.md": "content"})
|
|
SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "merge", true, ""); s != "synced" {
|
|
t.Errorf("expected synced, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_FlattenDrift(t *testing.T) {
|
|
src, _ := setupExtrasTest(t, map[string]string{"sub/file.md": "content"})
|
|
tgt := filepath.Join(t.TempDir(), "empty")
|
|
os.MkdirAll(tgt, 0755)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "merge", true, ""); s != "drift" {
|
|
t.Errorf("expected drift, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_FlattenCollisionNotDrift(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{
|
|
"a/same.md": "# A",
|
|
"b/same.md": "# B",
|
|
})
|
|
SyncExtra(src, tgt, "merge", false, false, true, "", nil)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "merge", true, ""); s != "synced" {
|
|
t.Errorf("expected synced (collision skipped), got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_CopyMode(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"file.md": "content"})
|
|
SyncExtra(src, tgt, "copy", false, false, false, "", nil)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "copy", false, ""); s != "synced" {
|
|
t.Errorf("expected synced, got %s", s)
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncStatus_CopyModeDetectsContentDrift(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"file.md": "content"})
|
|
SyncExtra(src, tgt, "copy", false, false, false, "", nil)
|
|
if err := os.WriteFile(filepath.Join(tgt, "file.md"), []byte("changed"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "copy", false, ""); s != "drift" {
|
|
t.Errorf("expected drift after copy content changed, got %s", s)
|
|
}
|
|
}
|
|
|
|
// TestCheckSyncStatus_TransformOutputExt is a regression test: a transform
|
|
// extension renames output (.md → .toml), so status must compare against the
|
|
// transformed filename. Without outputExt the checker looks for the source
|
|
// name and wrongly reports drift right after a successful sync.
|
|
func TestCheckSyncStatus_TransformOutputExt(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"agent.md": "# Agent"})
|
|
spec := &ExtensionSpec{Run: []string{"cat"}, OutputExt: "toml"}
|
|
SyncExtra(src, tgt, "copy", false, false, false, "", spec)
|
|
files, _ := DiscoverExtraFiles(src)
|
|
absSrc, _ := filepath.Abs(src)
|
|
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "copy", false, "toml"); s != "synced" {
|
|
t.Errorf("expected synced with outputExt, got %s", s)
|
|
}
|
|
if s := CheckSyncStatus(files, absSrc, tgt, "copy", false, ""); s != "drift" {
|
|
t.Errorf("expected drift without outputExt (looks for .md), got %s", s)
|
|
}
|
|
}
|