-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathe2e_test.go
More file actions
211 lines (179 loc) · 5.26 KB
/
e2e_test.go
File metadata and controls
211 lines (179 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/roborev-dev/roborev/internal/config"
"github.com/roborev-dev/roborev/internal/daemon"
"github.com/roborev-dev/roborev/internal/storage"
"github.com/roborev-dev/roborev/internal/testenv"
)
// TestE2EEnqueueAndReview tests the full flow of enqueueing and reviewing a commit
func TestE2EEnqueueAndReview(t *testing.T) {
// Skip if not in a git repo (CI might not have one)
if _, err := exec.Command("git", "rev-parse", "--git-dir").Output(); err != nil {
t.Skip("Not in a git repo, skipping e2e test")
}
// Setup temp DB
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
db, err := storage.Open(dbPath)
if err != nil {
t.Fatalf("Failed to open test DB: %v", err)
}
defer db.Close()
// Create a mock server
cfg := config.DefaultConfig()
server := daemon.NewServer(db, cfg, "")
// Create test HTTP server
mux := http.NewServeMux()
// Add handlers manually (simulating the server)
mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) {
queued, running, done, failed, canceled, applied, rebased, _ := db.GetJobCounts()
status := storage.DaemonStatus{
QueuedJobs: queued,
RunningJobs: running,
CompletedJobs: done,
FailedJobs: failed,
CanceledJobs: canceled,
AppliedJobs: applied,
RebasedJobs: rebased,
MaxWorkers: 4,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
})
ts := httptest.NewServer(mux)
defer ts.Close()
// Test status endpoint
resp, err := http.Get(ts.URL + "/api/status")
if err != nil {
t.Fatalf("Status request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
var status storage.DaemonStatus
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
t.Fatalf("Failed to decode status: %v", err)
}
if status.MaxWorkers != 4 {
t.Errorf("Expected MaxWorkers 4, got %d", status.MaxWorkers)
}
_ = server // Avoid unused variable
}
// TestDatabaseIntegration tests the full database workflow
func TestDatabaseIntegration(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
db, err := storage.Open(dbPath)
if err != nil {
t.Fatalf("Failed to open test DB: %v", err)
}
defer db.Close()
// Simulate full workflow
repo, err := db.GetOrCreateRepo("/tmp/test-repo")
if err != nil {
t.Fatalf("GetOrCreateRepo failed: %v", err)
}
commit, err := db.GetOrCreateCommit(repo.ID, "abc123", "Test Author", "Test commit message", time.Now())
if err != nil {
t.Fatalf("GetOrCreateCommit failed: %v", err)
}
job, err := db.EnqueueJob(storage.EnqueueOpts{RepoID: repo.ID, CommitID: commit.ID, GitRef: "abc123", Agent: "codex"})
if err != nil {
t.Fatalf("EnqueueJob failed: %v", err)
}
// Verify initial state
queued, running, done, _, _, _, _, _ := db.GetJobCounts()
if queued != 1 {
t.Errorf("Expected 1 queued job, got %d", queued)
}
_ = running
_ = done
// Claim the job
claimed, err := db.ClaimJob("test-worker")
if err != nil {
t.Fatalf("ClaimJob failed: %v", err)
}
if claimed == nil {
t.Fatal("ClaimJob returned nil")
}
if claimed.ID != job.ID {
t.Error("Claimed wrong job")
}
// Verify running state
_, running, _, _, _, _, _, _ = db.GetJobCounts()
if running != 1 {
t.Errorf("Expected 1 running job, got %d", running)
}
// Complete the job
err = db.CompleteJob(job.ID, "codex", "test prompt", "This commit looks good!")
if err != nil {
t.Fatalf("CompleteJob failed: %v", err)
}
// Verify completed state
queued, running, done, _, _, _, _, _ = db.GetJobCounts()
if done != 1 {
t.Errorf("Expected 1 completed job, got %d", done)
}
_ = queued
_ = running
// Fetch the review
review, err := db.GetReviewByCommitSHA("abc123")
if err != nil {
t.Fatalf("GetReviewByCommitSHA failed: %v", err)
}
if !strings.Contains(review.Output, "looks good") {
t.Errorf("Review output doesn't contain expected text: %s", review.Output)
}
// Add a comment
resp, err := db.AddComment(commit.ID, "human-reviewer", "Agreed, LGTM!")
if err != nil {
t.Fatalf("AddComment failed: %v", err)
}
if resp.Response != "Agreed, LGTM!" {
t.Errorf("Comment not saved correctly")
}
// Verify comment can be fetched
comments, err := db.GetCommentsForCommitSHA("abc123")
if err != nil {
t.Fatalf("GetCommentsForCommitSHA failed: %v", err)
}
if len(comments) != 1 {
t.Errorf("Expected 1 comment, got %d", len(comments))
}
}
// TestConfigPersistence tests config save/load
func TestConfigPersistence(t *testing.T) {
testenv.SetDataDir(t)
// Save custom config
cfg := config.DefaultConfig()
cfg.DefaultAgent = "claude-code"
cfg.MaxWorkers = 8
cfg.ReviewContextCount = 5
err := config.SaveGlobal(cfg)
if err != nil {
t.Fatalf("SaveGlobal failed: %v", err)
}
// Load it back
loaded, err := config.LoadGlobal()
if err != nil {
t.Fatalf("LoadGlobal failed: %v", err)
}
if loaded.DefaultAgent != "claude-code" {
t.Errorf("DefaultAgent not persisted correctly")
}
if loaded.MaxWorkers != 8 {
t.Errorf("MaxWorkers not persisted correctly")
}
if loaded.ReviewContextCount != 5 {
t.Errorf("ReviewContextCount not persisted correctly")
}
}