| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | package loop |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // TestGitCommitTracking tests the git commit tracking functionality |
| 14 | func TestGitCommitTracking(t *testing.T) { |
| 15 | // Create a temporary directory for our test git repo |
| 16 | tempDir := t.TempDir() // Automatically cleaned up when the test completes |
| 17 | |
| 18 | // Initialize a git repo in the temp directory |
| 19 | cmd := exec.Command("git", "init") |
| 20 | cmd.Dir = tempDir |
| 21 | if err := cmd.Run(); err != nil { |
| 22 | t.Fatalf("Failed to initialize git repo: %v", err) |
| 23 | } |
| 24 | |
| 25 | // Configure git user for commits |
| 26 | cmd = exec.Command("git", "config", "user.name", "Test User") |
| 27 | cmd.Dir = tempDir |
| 28 | if err := cmd.Run(); err != nil { |
| 29 | t.Fatalf("Failed to configure git user name: %v", err) |
| 30 | } |
| 31 | |
| 32 | cmd = exec.Command("git", "config", "user.email", "test@example.com") |
| 33 | cmd.Dir = tempDir |
| 34 | if err := cmd.Run(); err != nil { |
| 35 | t.Fatalf("Failed to configure git user email: %v", err) |
| 36 | } |
| 37 | |
| 38 | // Make an initial commit |
| 39 | testFile := filepath.Join(tempDir, "test.txt") |
| 40 | if err := os.WriteFile(testFile, []byte("initial content\n"), 0o644); err != nil { |
| 41 | t.Fatalf("Failed to write file: %v", err) |
| 42 | } |
| 43 | |
| 44 | cmd = exec.Command("git", "add", "test.txt") |
| 45 | cmd.Dir = tempDir |
| 46 | if err := cmd.Run(); err != nil { |
| 47 | t.Fatalf("Failed to add file: %v", err) |
| 48 | } |
| 49 | |
| 50 | cmd = exec.Command("git", "commit", "-m", "Initial commit") |
| 51 | cmd.Dir = tempDir |
| 52 | if err := cmd.Run(); err != nil { |
| 53 | t.Fatalf("Failed to create initial commit: %v", err) |
| 54 | } |
| 55 | |
| 56 | // Get the initial commit hash |
| 57 | cmd = exec.Command("git", "rev-parse", "HEAD") |
| 58 | cmd.Dir = tempDir |
| 59 | initialCommitOutput, err := cmd.Output() |
| 60 | if err != nil { |
| 61 | t.Fatalf("Failed to get initial commit hash: %v", err) |
| 62 | } |
| 63 | initialCommit := strings.TrimSpace(string(initialCommitOutput)) |
| 64 | |
| 65 | // Create agent with the temp repo |
| 66 | agent := &Agent{ |
| 67 | workingDir: tempDir, |
| 68 | repoRoot: tempDir, // Set repoRoot to same as workingDir for this test |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 69 | seenCommits: make(map[string]bool), |
| 70 | initialCommit: initialCommit, |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 71 | subscribers: []chan *AgentMessage{}, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Make a new commit |
| 75 | if err := os.WriteFile(testFile, []byte("updated content\n"), 0o644); err != nil { |
| 76 | t.Fatalf("Failed to update file: %v", err) |
| 77 | } |
| 78 | |
| 79 | cmd = exec.Command("git", "add", "test.txt") |
| 80 | cmd.Dir = tempDir |
| 81 | if err := cmd.Run(); err != nil { |
| 82 | t.Fatalf("Failed to add updated file: %v", err) |
| 83 | } |
| 84 | |
| 85 | cmd = exec.Command("git", "commit", "-m", "Second commit\n\nThis commit has a multi-line message\nwith details about the changes.") |
| 86 | cmd.Dir = tempDir |
| 87 | if err := cmd.Run(); err != nil { |
| 88 | t.Fatalf("Failed to create second commit: %v", err) |
| 89 | } |
| 90 | |
| 91 | // Call handleGitCommits and verify we get a commit message |
| 92 | ctx := context.Background() |
| 93 | _, err = agent.handleGitCommits(ctx) |
| 94 | if err != nil { |
| 95 | t.Fatalf("handleGitCommits failed: %v", err) |
| 96 | } |
| 97 | |
| 98 | // Check if we received a commit message |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 99 | agent.mu.Lock() |
| 100 | if len(agent.history) == 0 { |
| 101 | agent.mu.Unlock() |
| 102 | t.Fatal("No commit message was added to history") |
| 103 | } |
| 104 | commitMsg := agent.history[len(agent.history)-1] |
| 105 | agent.mu.Unlock() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 106 | |
| 107 | // Verify the commit message |
| 108 | if commitMsg.Type != CommitMessageType { |
| 109 | t.Errorf("Expected message type %s, got %s", CommitMessageType, commitMsg.Type) |
| 110 | } |
| 111 | |
| 112 | if len(commitMsg.Commits) < 1 { |
| 113 | t.Fatalf("Expected at least 1 commit, got %d", len(commitMsg.Commits)) |
| 114 | } |
| 115 | |
| 116 | // Find the second commit |
| 117 | var commit *GitCommit |
| 118 | found := false |
| 119 | for _, c := range commitMsg.Commits { |
| 120 | if strings.HasPrefix(c.Subject, "Second commit") { |
| 121 | commit = c |
| 122 | found = true |
| 123 | break |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if !found { |
| 128 | t.Fatalf("Could not find 'Second commit' in commits") |
| 129 | } |
| 130 | if !strings.HasPrefix(commit.Subject, "Second commit") { |
| 131 | t.Errorf("Expected commit subject 'Second commit', got '%s'", commit.Subject) |
| 132 | } |
| 133 | |
| 134 | if !strings.Contains(commit.Body, "multi-line message") { |
| 135 | t.Errorf("Expected body to contain 'multi-line message', got '%s'", commit.Body) |
| 136 | } |
| 137 | |
| 138 | // Test with many commits |
| 139 | if testing.Short() { |
| 140 | t.Skip("Skipping multiple commits test in short mode") |
| 141 | } |
| 142 | |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 143 | // Skip the multiple commits test in short mode |
| 144 | if testing.Short() { |
| 145 | t.Log("Skipping multiple commits test in short mode") |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | // Make multiple commits - reduce from 110 to 20 for faster tests |
| 150 | // 20 is enough to verify the functionality without the time penalty |
| 151 | for i := range 20 { |
| 152 | newContent := fmt.Appendf(nil, "content update %d\n", i) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 153 | if err := os.WriteFile(testFile, newContent, 0o644); err != nil { |
| 154 | t.Fatalf("Failed to update file: %v", err) |
| 155 | } |
| 156 | |
| 157 | cmd = exec.Command("git", "add", "test.txt") |
| 158 | cmd.Dir = tempDir |
| 159 | if err := cmd.Run(); err != nil { |
| 160 | t.Fatalf("Failed to add updated file: %v", err) |
| 161 | } |
| 162 | |
| 163 | cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("Commit %d", i+3)) |
| 164 | cmd.Dir = tempDir |
| 165 | if err := cmd.Run(); err != nil { |
| 166 | t.Fatalf("Failed to create commit %d: %v", i+3, err) |
| 167 | } |
| 168 | } |
| 169 | |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 170 | // Reset the seen commits map |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 171 | agent.seenCommits = make(map[string]bool) |
| 172 | |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 173 | // Call handleGitCommits again - it should show up to 20 commits (or whatever git defaults to) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 174 | _, err = agent.handleGitCommits(ctx) |
| 175 | if err != nil { |
| 176 | t.Fatalf("handleGitCommits failed: %v", err) |
| 177 | } |
| 178 | |
| 179 | // Check if we received a commit message |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 180 | agent.mu.Lock() |
| Philip Zeyliger | b7c5875 | 2025-05-01 10:10:17 -0700 | [diff] [blame] | 181 | commitMsg = agent.history[len(agent.history)-1] |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 182 | agent.mu.Unlock() |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 183 | |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 184 | // We should have our commits |
| 185 | if len(commitMsg.Commits) < 5 { |
| 186 | t.Errorf("Expected at least 5 commits, but only got %d", len(commitMsg.Commits)) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| Philip Zeyliger | 9373c07 | 2025-05-01 10:27:01 -0700 | [diff] [blame] | 189 | t.Logf("Received %d commits total", len(commitMsg.Commits)) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // TestParseGitLog tests the parseGitLog function |
| 193 | func TestParseGitLog(t *testing.T) { |
| 194 | tests := []struct { |
| 195 | name string |
| 196 | input string |
| 197 | expected []GitCommit |
| 198 | }{ |
| 199 | { |
| 200 | name: "Empty input", |
| 201 | input: "", |
| 202 | expected: []GitCommit{}, |
| 203 | }, |
| 204 | { |
| 205 | name: "Single commit", |
| 206 | input: "abcdef1234567890\x00Initial commit\x00This is the first commit\x00", |
| 207 | expected: []GitCommit{ |
| 208 | {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"}, |
| 209 | }, |
| 210 | }, |
| 211 | { |
| 212 | name: "Multiple commits", |
| 213 | input: "abcdef1234567890\x00Initial commit\x00This is the first commit\x00" + |
| 214 | "fedcba0987654321\x00Second commit\x00This is the second commit\x00" + |
| 215 | "123456abcdef7890\x00Third commit\x00This is the third commit\x00", |
| 216 | expected: []GitCommit{ |
| 217 | {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"}, |
| 218 | {Hash: "fedcba0987654321", Subject: "Second commit", Body: "This is the second commit"}, |
| 219 | {Hash: "123456abcdef7890", Subject: "Third commit", Body: "This is the third commit"}, |
| 220 | }, |
| 221 | }, |
| 222 | { |
| 223 | name: "Commit with multi-line body", |
| 224 | input: "abcdef1234567890\x00Commit with multi-line body\x00This is a commit\nwith a multi-line\nbody message\x00", |
| 225 | expected: []GitCommit{ |
| 226 | {Hash: "abcdef1234567890", Subject: "Commit with multi-line body", Body: "This is a commit\nwith a multi-line\nbody message"}, |
| 227 | }, |
| 228 | }, |
| 229 | { |
| 230 | name: "Commit with empty body", |
| 231 | input: "abcdef1234567890\x00Commit with empty body\x00\x00", |
| 232 | expected: []GitCommit{ |
| 233 | {Hash: "abcdef1234567890", Subject: "Commit with empty body", Body: ""}, |
| 234 | }, |
| 235 | }, |
| 236 | { |
| 237 | name: "Empty parts removed", |
| 238 | input: "\x00abcdef1234567890\x00Initial commit\x00This is the first commit\x00\x00", |
| 239 | expected: []GitCommit{ |
| 240 | {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"}, |
| 241 | }, |
| 242 | }, |
| 243 | } |
| 244 | |
| 245 | for _, tt := range tests { |
| 246 | t.Run(tt.name, func(t *testing.T) { |
| 247 | actual := parseGitLog(tt.input) |
| 248 | |
| 249 | if len(actual) != len(tt.expected) { |
| 250 | t.Fatalf("Expected %d commits, got %d", len(tt.expected), len(actual)) |
| 251 | } |
| 252 | |
| 253 | for i, commit := range actual { |
| 254 | expected := tt.expected[i] |
| 255 | if commit.Hash != expected.Hash || commit.Subject != expected.Subject || commit.Body != expected.Body { |
| 256 | t.Errorf("Commit %d doesn't match:\nExpected: %+v\nGot: %+v", i, expected, commit) |
| 257 | } |
| 258 | } |
| 259 | }) |
| 260 | } |
| 261 | } |