blob: ae291e058ad921786fa3c9cf2af97585a8966954 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001package loop
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
Earl Lee2e463fb2025-04-17 11:22:22 -070011)
12
13// TestGitCommitTracking tests the git commit tracking functionality
14func 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
Philip Zeyliger49edc922025-05-14 09:45:45 -070056 // Note: The initial commit will be tagged as sketch-base later
Earl Lee2e463fb2025-04-17 11:22:22 -070057
58 // Create agent with the temp repo
59 agent := &Agent{
Philip Zeyliger49edc922025-05-14 09:45:45 -070060 workingDir: tempDir,
61 repoRoot: tempDir, // Set repoRoot to same as workingDir for this test
Philip Zeyliger49edc922025-05-14 09:45:45 -070062 subscribers: []chan *AgentMessage{},
63 config: AgentConfig{
64 SessionID: "test-session",
65 InDocker: false,
66 },
67 history: []AgentMessage{},
Philip Zeyligerf2872992025-05-22 10:35:28 -070068 gitState: AgentGitState{
69 seenCommits: make(map[string]bool),
70 },
Philip Zeyliger49edc922025-05-14 09:45:45 -070071 }
72
73 // Create sketch-base-test-session tag at current HEAD to serve as the base commit
74 cmd = exec.Command("git", "tag", "-f", "sketch-base-test-session", "HEAD")
75 cmd.Dir = tempDir
76 if err := cmd.Run(); err != nil {
77 t.Fatalf("Failed to create sketch-base tag: %v", err)
Earl Lee2e463fb2025-04-17 11:22:22 -070078 }
79
80 // Make a new commit
81 if err := os.WriteFile(testFile, []byte("updated content\n"), 0o644); err != nil {
82 t.Fatalf("Failed to update file: %v", err)
83 }
84
85 cmd = exec.Command("git", "add", "test.txt")
86 cmd.Dir = tempDir
87 if err := cmd.Run(); err != nil {
88 t.Fatalf("Failed to add updated file: %v", err)
89 }
90
91 cmd = exec.Command("git", "commit", "-m", "Second commit\n\nThis commit has a multi-line message\nwith details about the changes.")
92 cmd.Dir = tempDir
93 if err := cmd.Run(); err != nil {
94 t.Fatalf("Failed to create second commit: %v", err)
95 }
96
97 // Call handleGitCommits and verify we get a commit message
98 ctx := context.Background()
Philip Zeyliger49edc922025-05-14 09:45:45 -070099 _, gitErr := agent.handleGitCommits(ctx)
100 if gitErr != nil {
101 t.Fatalf("handleGitCommits failed: %v", gitErr)
Earl Lee2e463fb2025-04-17 11:22:22 -0700102 }
103
104 // Check if we received a commit message
Philip Zeyliger9373c072025-05-01 10:27:01 -0700105 agent.mu.Lock()
106 if len(agent.history) == 0 {
107 agent.mu.Unlock()
108 t.Fatal("No commit message was added to history")
109 }
110 commitMsg := agent.history[len(agent.history)-1]
111 agent.mu.Unlock()
Earl Lee2e463fb2025-04-17 11:22:22 -0700112
113 // Verify the commit message
114 if commitMsg.Type != CommitMessageType {
115 t.Errorf("Expected message type %s, got %s", CommitMessageType, commitMsg.Type)
116 }
117
118 if len(commitMsg.Commits) < 1 {
119 t.Fatalf("Expected at least 1 commit, got %d", len(commitMsg.Commits))
120 }
121
122 // Find the second commit
123 var commit *GitCommit
124 found := false
125 for _, c := range commitMsg.Commits {
126 if strings.HasPrefix(c.Subject, "Second commit") {
127 commit = c
128 found = true
129 break
130 }
131 }
132
133 if !found {
134 t.Fatalf("Could not find 'Second commit' in commits")
135 }
136 if !strings.HasPrefix(commit.Subject, "Second commit") {
137 t.Errorf("Expected commit subject 'Second commit', got '%s'", commit.Subject)
138 }
139
140 if !strings.Contains(commit.Body, "multi-line message") {
141 t.Errorf("Expected body to contain 'multi-line message', got '%s'", commit.Body)
142 }
143
144 // Test with many commits
145 if testing.Short() {
146 t.Skip("Skipping multiple commits test in short mode")
147 }
148
Philip Zeyliger9373c072025-05-01 10:27:01 -0700149 // Skip the multiple commits test in short mode
150 if testing.Short() {
151 t.Log("Skipping multiple commits test in short mode")
152 return
153 }
154
155 // Make multiple commits - reduce from 110 to 20 for faster tests
156 // 20 is enough to verify the functionality without the time penalty
157 for i := range 20 {
158 newContent := fmt.Appendf(nil, "content update %d\n", i)
Earl Lee2e463fb2025-04-17 11:22:22 -0700159 if err := os.WriteFile(testFile, newContent, 0o644); err != nil {
160 t.Fatalf("Failed to update file: %v", err)
161 }
162
163 cmd = exec.Command("git", "add", "test.txt")
164 cmd.Dir = tempDir
165 if err := cmd.Run(); err != nil {
166 t.Fatalf("Failed to add updated file: %v", err)
167 }
168
169 cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("Commit %d", i+3))
170 cmd.Dir = tempDir
171 if err := cmd.Run(); err != nil {
172 t.Fatalf("Failed to create commit %d: %v", i+3, err)
173 }
174 }
175
Philip Zeyliger9373c072025-05-01 10:27:01 -0700176 // Reset the seen commits map
Philip Zeyligerf2872992025-05-22 10:35:28 -0700177 agent.gitState.seenCommits = make(map[string]bool)
Earl Lee2e463fb2025-04-17 11:22:22 -0700178
Philip Zeyliger9373c072025-05-01 10:27:01 -0700179 // Call handleGitCommits again - it should show up to 20 commits (or whatever git defaults to)
Philip Zeyliger49edc922025-05-14 09:45:45 -0700180 _, handleErr := agent.handleGitCommits(ctx)
181 if handleErr != nil {
182 t.Fatalf("handleGitCommits failed: %v", handleErr)
Earl Lee2e463fb2025-04-17 11:22:22 -0700183 }
184
185 // Check if we received a commit message
Philip Zeyliger9373c072025-05-01 10:27:01 -0700186 agent.mu.Lock()
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700187 commitMsg = agent.history[len(agent.history)-1]
Philip Zeyliger9373c072025-05-01 10:27:01 -0700188 agent.mu.Unlock()
Earl Lee2e463fb2025-04-17 11:22:22 -0700189
Philip Zeyliger9373c072025-05-01 10:27:01 -0700190 // We should have our commits
191 if len(commitMsg.Commits) < 5 {
192 t.Errorf("Expected at least 5 commits, but only got %d", len(commitMsg.Commits))
Earl Lee2e463fb2025-04-17 11:22:22 -0700193 }
194
Philip Zeyliger9373c072025-05-01 10:27:01 -0700195 t.Logf("Received %d commits total", len(commitMsg.Commits))
Earl Lee2e463fb2025-04-17 11:22:22 -0700196}
197
198// TestParseGitLog tests the parseGitLog function
199func TestParseGitLog(t *testing.T) {
200 tests := []struct {
201 name string
202 input string
203 expected []GitCommit
204 }{
205 {
206 name: "Empty input",
207 input: "",
208 expected: []GitCommit{},
209 },
210 {
211 name: "Single commit",
212 input: "abcdef1234567890\x00Initial commit\x00This is the first commit\x00",
213 expected: []GitCommit{
214 {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"},
215 },
216 },
217 {
218 name: "Multiple commits",
219 input: "abcdef1234567890\x00Initial commit\x00This is the first commit\x00" +
220 "fedcba0987654321\x00Second commit\x00This is the second commit\x00" +
221 "123456abcdef7890\x00Third commit\x00This is the third commit\x00",
222 expected: []GitCommit{
223 {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"},
224 {Hash: "fedcba0987654321", Subject: "Second commit", Body: "This is the second commit"},
225 {Hash: "123456abcdef7890", Subject: "Third commit", Body: "This is the third commit"},
226 },
227 },
228 {
229 name: "Commit with multi-line body",
230 input: "abcdef1234567890\x00Commit with multi-line body\x00This is a commit\nwith a multi-line\nbody message\x00",
231 expected: []GitCommit{
232 {Hash: "abcdef1234567890", Subject: "Commit with multi-line body", Body: "This is a commit\nwith a multi-line\nbody message"},
233 },
234 },
235 {
236 name: "Commit with empty body",
237 input: "abcdef1234567890\x00Commit with empty body\x00\x00",
238 expected: []GitCommit{
239 {Hash: "abcdef1234567890", Subject: "Commit with empty body", Body: ""},
240 },
241 },
242 {
243 name: "Empty parts removed",
244 input: "\x00abcdef1234567890\x00Initial commit\x00This is the first commit\x00\x00",
245 expected: []GitCommit{
246 {Hash: "abcdef1234567890", Subject: "Initial commit", Body: "This is the first commit"},
247 },
248 },
249 }
250
251 for _, tt := range tests {
252 t.Run(tt.name, func(t *testing.T) {
253 actual := parseGitLog(tt.input)
254
255 if len(actual) != len(tt.expected) {
256 t.Fatalf("Expected %d commits, got %d", len(tt.expected), len(actual))
257 }
258
259 for i, commit := range actual {
260 expected := tt.expected[i]
261 if commit.Hash != expected.Hash || commit.Subject != expected.Subject || commit.Body != expected.Body {
262 t.Errorf("Commit %d doesn't match:\nExpected: %+v\nGot: %+v", i, expected, commit)
263 }
264 }
265 })
266 }
267}