-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaigit_test.go
More file actions
231 lines (201 loc) · 6.69 KB
/
aigit_test.go
File metadata and controls
231 lines (201 loc) · 6.69 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"bytes"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// --- Test helpers ---
func must(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v: %v: %s", strings.Join(args, " "), err, string(out))
}
return strings.TrimSpace(string(out))
}
func withTempRepo(t *testing.T) string {
t.Helper()
dir := t.TempDir()
runGit(t, dir, "init", "-q")
runGit(t, dir, "config", "user.name", "Test User")
runGit(t, dir, "config", "user.email", "test@example.com")
// initial commit
os.WriteFile(filepath.Join(dir, "init.txt"), []byte("init\n"), 0o644)
runGit(t, dir, "add", ".")
runGit(t, dir, "commit", "-m", "init")
return dir
}
func chdir(t *testing.T, dir string) func() {
t.Helper()
old, _ := os.Getwd()
must(t, os.Chdir(dir))
return func() { _ = os.Chdir(old) }
}
func captureOutput(t *testing.T, f func()) string {
t.Helper()
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
_ = w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
// --- Tests ---
func TestStatusCleanWorkspace(t *testing.T) {
repo := withTempRepo(t)
defer chdir(t, repo)()
// switch cwd for our git() helper
must(t, os.Chdir(repo))
out := captureOutput(t, func() {
must(t, doStatus())
})
if !strings.Contains(out, "nothing here yet, clean workspace") {
t.Fatalf("expected clean workspace message, got:\n%s", out)
}
}
func TestCheckpointListRestore(t *testing.T) {
repo := withTempRepo(t)
defer chdir(t, repo)()
must(t, os.Chdir(repo))
// Create and checkpoint v1
os.WriteFile("foo.txt", []byte("v1\n"), 0o644)
must(t, doCheckpoint("v1"))
ref, err := ckRef()
must(t, err)
sha1 := runGit(t, repo, "rev-list", "-1", ref)
// Change and checkpoint v2
os.WriteFile("foo.txt", []byte("v2\n"), 0o644)
must(t, doCheckpoint("v2"))
// Restore from first checkpoint
must(t, doRestore(sha1))
data, _ := os.ReadFile("foo.txt")
if string(data) != "v1\n" {
t.Fatalf("restore failed, foo.txt=%q", string(data))
}
// List output should include v2 or v1 subject
out := captureOutput(t, func() { must(t, doList(5, true)) })
if !(strings.Contains(out, "v1") || strings.Contains(out, "v2")) {
t.Fatalf("list missing expected subjects, got:\n%s", out)
}
}
func TestDiffOneLinerIncludesUntracked(t *testing.T) {
repo := withTempRepo(t)
defer chdir(t, repo)()
must(t, os.Chdir(repo))
os.WriteFile("newfile.txt", []byte("hello\n"), 0o644)
s, err := diffOneLiner()
must(t, err)
if !strings.Contains(s, "Add newfile.txt") {
t.Fatalf("expected untracked file in summary, got: %q", s)
}
}
func TestCheckpointDuringMerge(t *testing.T) {
repo := withTempRepo(t)
defer chdir(t, repo)()
must(t, os.Chdir(repo))
// Create a conflicting change on a branch
os.WriteFile("file.txt", []byte("base\n"), 0o644)
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "base")
runGit(t, repo, "checkout", "-b", "feature")
os.WriteFile("file.txt", []byte("feature\n"), 0o644)
runGit(t, repo, "commit", "-am", "feature change")
runGit(t, repo, "checkout", "main")
os.WriteFile("file.txt", []byte("main\n"), 0o644)
runGit(t, repo, "commit", "-am", "main change")
// Trigger a merge conflict
_ = exec.Command("git", "merge", "feature").Run()
// Now checkpoint during merge
must(t, doCheckpoint("merge progress"))
ref, err := ckRef()
must(t, err)
body := runGit(t, repo, "log", "-1", "--format=%B", ref)
if !strings.Contains(body, "Aigit-Merge: yes") {
t.Fatalf("expected merge metadata, got:\n%s", body)
}
}
func TestRemoteSyncAndApply(t *testing.T) {
repo := withTempRepo(t)
defer chdir(t, repo)()
must(t, os.Chdir(repo))
// Create a checkpoint with identifiable content
os.WriteFile("sync.txt", []byte("remote v1\n"), 0o644)
must(t, doCheckpoint("remote v1"))
// Create bare remote and add as origin
bare := filepath.Join(repo, "remote.git")
runGit(t, repo, "init", "--bare", bare)
runGit(t, repo, "remote", "add", "origin", bare)
// Push checkpoints
must(t, pushCheckpoints("origin"))
// Fetch checkpoints back and verify user appears
must(t, fetchCheckpoints("origin"))
br, err := currentBranch()
must(t, err)
users, err := listRemoteUsers("origin", br)
must(t, err)
if len(users) == 0 {
t.Fatalf("expected at least one remote user")
}
// Change local file and apply from remote (self)
os.WriteFile("sync.txt", []byte("local diverged\n"), 0o644)
uid := getUserID()
must(t, applyRemoteCheckpoint("origin", uid, ""))
data, _ := os.ReadFile("sync.txt")
if string(data) != "remote v1\n" {
t.Fatalf("apply did not restore remote content, got %q", string(data))
}
}
var noSummary = flag.Bool("no_summary", false, "skip AI summary tests")
var offline = flag.Bool("offline", false, "allow offline AI summary test using local fake")
func TestAISummaryCheckpoint(t *testing.T) {
if *noSummary { t.Skip("-no_summary set; skipping AI summary tests") }
repo := withTempRepo(t)
defer chdir(t, repo)()
must(t, os.Chdir(repo))
// Avoid any background autostart interference
t.Setenv("AIGIT_DISABLE_AUTOSTART", "1")
// Make a change
os.WriteFile("ai.txt", []byte("hello\n"), 0o644)
// If no network key, force local fake so we still exercise the AI path
if os.Getenv("OPENROUTER_API_KEY") == "" && !*offline {
t.Setenv("AIGIT_FAKE_AI_SUMMARY", "1")
}
err := maybeCheckpoint("ai", "openai/gpt-oss-20b:free")
if err != nil && *offline {
// Fallback to local fake if allowed
t.Setenv("AIGIT_FAKE_AI_SUMMARY", "1")
must(t, maybeCheckpoint("ai", "openai/gpt-oss-20b:free"))
} else {
must(t, err)
}
// Live snapshots update the live ref, not the checkpoint ref
br, err := currentBranch()
must(t, err)
liveRef := liveLocalRef(br)
subj := runGit(t, repo, "log", "-1", "--format=%s", liveRef)
if os.Getenv("AIGIT_FAKE_AI_SUMMARY") != "" {
if !strings.HasPrefix(subj, "AI: ") {
t.Fatalf("expected fake AI summary prefix, got: %q", subj)
}
} else {
if strings.TrimSpace(subj) == "" {
t.Fatalf("expected non-empty summary, got empty")
}
}
}