//go:build !online package integration import ( "os/exec" "path/filepath" "testing" "skillshare/internal/testutil" ) // Tests for pull command (git remote operations) func TestPull_NoGitRepo_ShowsError(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `) result := sb.RunCLI("pull") result.AssertSuccess(t) result.AssertOutputContains(t, "not a git repository") } func TestPull_NoRemote_ShowsError(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `) // Initialize git but no remote cmd := exec.Command("git", "init") cmd.Dir = sb.SourcePath if err := cmd.Run(); err != nil { t.Skip("git not available") } result := sb.RunCLI("pull") result.AssertSuccess(t) result.AssertOutputContains(t, "No git remote") } func TestPull_UncommittedChanges_Refuses(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `) // Create bare repo as "remote" bareRepo := filepath.Join(sb.Home, "remote.git") cmd := exec.Command("git", "init", "--bare", bareRepo) if err := cmd.Run(); err != nil { t.Skip("git not available") } // Initialize git and add remote cmd = exec.Command("git", "init") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "remote", "add", "origin", bareRepo) cmd.Dir = sb.SourcePath cmd.Run() configGitForPull(t, sb.SourcePath) // Initial commit and push cmd = exec.Command("git", "commit", "--allow-empty", "-m", "initial") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "push", "-u", "origin", "master") cmd.Dir = sb.SourcePath if err := cmd.Run(); err != nil { cmd = exec.Command("git", "push", "-u", "origin", "main") cmd.Dir = sb.SourcePath cmd.Run() } // Create uncommitted changes sb.CreateSkill("uncommitted-skill", map[string]string{"SKILL.md": "# Uncommitted"}) result := sb.RunCLI("pull") result.AssertSuccess(t) result.AssertOutputContains(t, "Local changes detected") } func TestPull_DryRun_ShowsActions(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: {} `) // Create bare repo as "remote" bareRepo := filepath.Join(sb.Home, "remote.git") cmd := exec.Command("git", "init", "--bare", bareRepo) if err := cmd.Run(); err != nil { t.Skip("git not available") } // Initialize git and add remote cmd = exec.Command("git", "init") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "remote", "add", "origin", bareRepo) cmd.Dir = sb.SourcePath cmd.Run() configGitForPull(t, sb.SourcePath) // Initial commit and push cmd = exec.Command("git", "commit", "--allow-empty", "-m", "initial") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "push", "-u", "origin", "master") cmd.Dir = sb.SourcePath if err := cmd.Run(); err != nil { cmd = exec.Command("git", "push", "-u", "origin", "main") cmd.Dir = sb.SourcePath cmd.Run() } result := sb.RunCLI("pull", "--dry-run") result.AssertSuccess(t) result.AssertOutputContains(t, "dry-run") } func TestPull_ActualPull_AndSyncs(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup() targetPath := sb.CreateTarget("claude") sb.WriteConfig(`source: ` + sb.SourcePath + ` targets: claude: path: ` + targetPath + ` `) // Create bare repo as "remote" bareRepo := filepath.Join(sb.Home, "remote.git") cmd := exec.Command("git", "init", "--bare", bareRepo) if err := cmd.Run(); err != nil { t.Skip("git not available") } // Initialize git and add remote cmd = exec.Command("git", "init") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "remote", "add", "origin", bareRepo) cmd.Dir = sb.SourcePath cmd.Run() configGitForPull(t, sb.SourcePath) // Create skill, commit, and push sb.CreateSkill("remote-skill", map[string]string{"SKILL.md": "# Remote Skill"}) cmd = exec.Command("git", "add", "-A") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "commit", "-m", "add skill") cmd.Dir = sb.SourcePath cmd.Run() cmd = exec.Command("git", "push", "-u", "origin", "master") cmd.Dir = sb.SourcePath if err := cmd.Run(); err != nil { cmd = exec.Command("git", "push", "-u", "origin", "main") cmd.Dir = sb.SourcePath cmd.Run() } // Now run pull (already up to date, but should sync) result := sb.RunCLI("pull") result.AssertSuccess(t) // Should sync to target if !sb.FileExists(filepath.Join(targetPath, "remote-skill", "SKILL.md")) { t.Error("skill should be synced to target after pull") } } // Helper function for pull tests func configGitForPull(t *testing.T, dir string) { cmd := exec.Command("git", "config", "user.email", "test@test.com") cmd.Dir = dir cmd.Run() cmd = exec.Command("git", "config", "user.name", "Test") cmd.Dir = dir cmd.Run() }