package tests import ( "context" "os" "os/exec" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/rockliang/poimen/workflows/action" ) func TestGitCloneAndFetch(t *testing.T) { // Create a temporary directory for the test tmpDir := t.TempDir() sourceDir := filepath.Join(tmpDir, "source") targetDir := filepath.Join(tmpDir, "target") // Initialize source repo if err := os.MkdirAll(sourceDir, 0755); err != nil { t.Fatalf("failed to create source dir: %v", err) } cmd := exec.Command("git", "init", sourceDir) if err := cmd.Run(); err != nil { t.Fatalf("git init failed: %v", err) } // Configure git user exec.Command("git", "-C", sourceDir, "config", "user.email", "test@example.com").Run() exec.Command("git", "-C", sourceDir, "config", "user.name", "Test User").Run() // Create initial commit testFile := filepath.Join(sourceDir, "test.txt") if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil { t.Fatalf("failed to create test file: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "add", "test.txt") if err := cmd.Run(); err != nil { t.Fatalf("git add failed: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "initial commit") if err := cmd.Run(); err != nil { t.Fatalf("git commit failed: %v", err) } // Test clone into empty path ctx := context.Background() err := action.CloneRepoActivity(ctx, action.CloneRepoInput{ RemoteURL: sourceDir, TargetRepoPath: targetDir, }) assert.NoError(t, err, "clone should succeed") // Verify .git exists gitDir := filepath.Join(targetDir, ".git") _, err = os.Stat(gitDir) assert.NoError(t, err, ".git directory should exist") // Verify test.txt was cloned targetTestFile := filepath.Join(targetDir, "test.txt") _, err = os.Stat(targetTestFile) assert.NoError(t, err, "test.txt should exist in target") // Add another commit to source testFile2 := filepath.Join(sourceDir, "test2.txt") if err := os.WriteFile(testFile2, []byte("test content 2"), 0644); err != nil { t.Fatalf("failed to create test2 file: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "add", "test2.txt") if err := cmd.Run(); err != nil { t.Fatalf("git add failed: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "second commit") if err := cmd.Run(); err != nil { t.Fatalf("git commit failed: %v", err) } // Test fetch on existing repo err = action.CloneRepoActivity(ctx, action.CloneRepoInput{ RemoteURL: sourceDir, TargetRepoPath: targetDir, }) assert.NoError(t, err, "fetch should succeed") } func TestGitWorktreeAdd(t *testing.T) { tmpDir := t.TempDir() sourceDir := filepath.Join(tmpDir, "source") repoDir := filepath.Join(tmpDir, "repo") // Initialize source repo if err := os.MkdirAll(sourceDir, 0755); err != nil { t.Fatalf("failed to create source dir: %v", err) } cmd := exec.Command("git", "init", sourceDir) if err := cmd.Run(); err != nil { t.Fatalf("git init failed: %v", err) } // Configure git user exec.Command("git", "-C", sourceDir, "config", "user.email", "test@example.com").Run() exec.Command("git", "-C", sourceDir, "config", "user.name", "Test User").Run() // Create initial commit testFile := filepath.Join(sourceDir, "test.txt") if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { t.Fatalf("failed to create test file: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "add", "test.txt") if err := cmd.Run(); err != nil { t.Fatalf("git add failed: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "initial") if err := cmd.Run(); err != nil { t.Fatalf("git commit failed: %v", err) } // Clone the repo ctx := context.Background() err := action.CloneRepoActivity(ctx, action.CloneRepoInput{ RemoteURL: sourceDir, TargetRepoPath: repoDir, }) assert.NoError(t, err, "clone should succeed") // Test worktree add worktreePath, err := action.GitWorktreeAddActivity(ctx, action.GitWorktreeAddInput{ RepoPath: repoDir, TaskID: "T0.1", }) assert.NoError(t, err, "worktree add should succeed") assert.NotEmpty(t, worktreePath, "worktree path should not be empty") // Verify worktree directory exists _, err = os.Stat(worktreePath) assert.NoError(t, err, "worktree directory should exist") // Verify branch exists cmd = exec.Command("git", "-C", repoDir, "branch", "--list", "task/T0.1") output, err := cmd.CombinedOutput() assert.NoError(t, err, "git branch check should succeed") assert.Contains(t, string(output), "task/T0.1", "task/T0.1 branch should exist") } func TestGitCommit(t *testing.T) { tmpDir := t.TempDir() sourceDir := filepath.Join(tmpDir, "source") repoDir := filepath.Join(tmpDir, "repo") // Initialize source repo if err := os.MkdirAll(sourceDir, 0755); err != nil { t.Fatalf("failed to create source dir: %v", err) } cmd := exec.Command("git", "init", sourceDir) if err := cmd.Run(); err != nil { t.Fatalf("git init failed: %v", err) } // Configure git user exec.Command("git", "-C", sourceDir, "config", "user.email", "test@example.com").Run() exec.Command("git", "-C", sourceDir, "config", "user.name", "Test User").Run() // Create initial commit testFile := filepath.Join(sourceDir, "test.txt") if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { t.Fatalf("failed to create test file: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "add", "test.txt") if err := cmd.Run(); err != nil { t.Fatalf("git add failed: %v", err) } cmd = exec.Command("git", "-C", sourceDir, "commit", "-m", "initial") if err := cmd.Run(); err != nil { t.Fatalf("git commit failed: %v", err) } // Clone the repo ctx := context.Background() err := action.CloneRepoActivity(ctx, action.CloneRepoInput{ RemoteURL: sourceDir, TargetRepoPath: repoDir, }) assert.NoError(t, err, "clone should succeed") // Create a worktree worktreePath, err := action.GitWorktreeAddActivity(ctx, action.GitWorktreeAddInput{ RepoPath: repoDir, TaskID: "T0.1", }) assert.NoError(t, err) // Create a new file in the worktree newFile := filepath.Join(worktreePath, "new.txt") if err := os.WriteFile(newFile, []byte("new content"), 0644); err != nil { t.Fatalf("failed to create new file: %v", err) } // Commit changes err = action.GitCommitActivity(ctx, action.GitCommitInput{ WorktreePath: worktreePath, Message: "Add new file", }) assert.NoError(t, err, "commit should succeed") // Verify commit exists cmd = exec.Command("git", "-C", worktreePath, "log", "--oneline") output, err := cmd.CombinedOutput() assert.NoError(t, err) assert.Contains(t, string(output), "Add new file", "commit message should be in log") }