Repository navigation
Expand file tree
/
Copy pathcarried_folder_test.go
More file actions
253 lines (240 loc) · 11.4 KB
/
Copy pathcarried_folder_test.go
File metadata and controls
253 lines (240 loc) · 11.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//go:build !windows
package main
// A SHELL RUN WORKS IN ITS FOLDER THE WAY A CONVERSATION'S RUN DOES
// (internal/session's programfolder.go): a plain folder is worked in as it is
// with the program told so on its line — where it used to end at once with
// "workspace is not a git repository" — and a repository gets a copy of its
// own on a branch of its own, which starts from the person's changes that are
// not committed and is kept with the work committed on it, checked out nowhere,
// while the person's checkout never moves.
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Agent-Field/codeaf/internal/delegate"
"github.com/Agent-Field/codeaf/internal/delegate/builtin"
"github.com/Agent-Field/codeaf/internal/provider/modelapi"
"github.com/Agent-Field/codeaf/internal/session"
)
// hostWithFolderChild is [hostWithRealChild] for the fake that works in its
// folder, and it answers what the run printed on stdout and on stderr.
func hostWithFolderChild(t *testing.T) (*carriedFunnel, *lockedBuffer, *lockedBuffer) {
t.Helper()
restore := builtin.Override([]delegate.Delegate{fakeFolderProgram()})
t.Cleanup(restore)
t.Setenv(carriedChildEnv, "folder")
t.Setenv("DO_NOT_TRACK", "1")
t.Setenv("CODEAF_NO_UPDATE_CHECK", "1")
calling := &carriedFunnel{cost: 0.001}
previousRoad, previousOut, previousErr, previousGrace := carriedModels, carriedStdout, carriedStderr, carriedGrace
carriedModels = func() (carriedRoad, error) {
return carriedRoad{completerFor: calling.completerFor, seat: "seat/model"}, nil
}
printed, said := &lockedBuffer{}, &lockedBuffer{}
carriedStdout, carriedStderr = printed, said
carriedGrace = 5 * time.Second
t.Cleanup(func() {
carriedModels, carriedStdout, carriedStderr, carriedGrace = previousRoad, previousOut, previousErr, previousGrace
})
return calling, printed, said
}
// shellRepo is a repository with one commit on `main`, the way a person's
// project stands.
func shellRepo(t *testing.T) string {
t.Helper()
repo := t.TempDir()
for _, args := range [][]string{
{"init", "-q", "-b", "main"},
{"config", "--local", "maintenance.auto", "false"},
{"config", "--local", "gc.auto", "0"},
{"-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "first"},
} {
shellGit(t, repo, args...)
}
return repo
}
// shellGit runs one git command in dir and answers what it printed.
func shellGit(t *testing.T, dir string, args ...string) string {
t.Helper()
command := exec.Command("git", args...)
command.Dir = dir
out, err := command.CombinedOutput()
if err != nil {
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
}
return strings.TrimSpace(string(out))
}
// A SHELL RUN IN A PLAIN FOLDER NO LONGER FAILS: the program is told on its
// line that it works without git, its work is left in the folder, its notes
// are moved into the run's record folder, and the last lines say so.
func TestAShellRunInAPlainFolderIsToldSoAndDoesNotFail(t *testing.T) {
_, printed, _ := hostWithFolderChild(t)
folder := t.TempDir()
err := runCarried(fakeFolderProgram(), []string{"--dir", folder, "make a file"})
if code := exitCodeOf(err); code != 0 {
t.Fatalf("the shell run left with %d (%v):\n%s", code, err, printed)
}
out := printed.String()
if !strings.Contains(out, " folder · in place") {
t.Fatalf("the program was not told it works without git:\n%s", out)
}
if _, err := os.Stat(filepath.Join(folder, "made.txt")); err != nil {
t.Fatalf("the work is not in the folder: %v", err)
}
if _, err := os.Stat(filepath.Join(folder, ".git")); !os.IsNotExist(err) {
t.Fatalf("a plain folder was made a repository: %v", err)
}
record := newestRecordOf(t, fakeFolder)
if _, err := os.Stat(filepath.Join(record, fakeFolder, "spec.md")); err != nil {
t.Fatalf("the program's notes are not in the run's record folder: %v", err)
}
if _, err := os.Stat(filepath.Join(folder, ".fake-folder")); !os.IsNotExist(err) {
t.Fatalf("the program's notes were left in the folder: %v", err)
}
want := " its work is in " + folder + ", which has no git history, so nothing was committed; its notes (.fake-folder/) are kept in " + filepath.Join(record, fakeFolder)
if !strings.Contains(out, want) {
t.Fatalf("the last lines do not say where the work is:\n%s\nwant %q", out, want)
}
}
// A SHELL RUN IN A REPOSITORY WORKS IN A COPY OF ITS OWN ON A BRANCH OF ITS
// OWN, and the person's checkout never moves: the program's work is committed
// on its branch, the copy is removed, and the last lines say how to bring it
// in.
func TestAShellRunInARepositoryWorksOnABranchOfItsOwn(t *testing.T) {
_, printed, _ := hostWithFolderChild(t)
repo := shellRepo(t)
base := shellGit(t, repo, "rev-parse", "main")
err := runCarried(fakeFolderProgram(), []string{"--dir", repo, "make a file"})
if code := exitCodeOf(err); code != 0 {
t.Fatalf("the shell run left with %d (%v):\n%s", code, err, printed)
}
out := printed.String()
branch := shellGit(t, repo, "for-each-ref", "--format=%(refname:short)", "refs/heads/task/")
if !strings.HasPrefix(branch, "task/make-a-file-") || shellGit(t, repo, "branch", "--show-current") != "main" {
t.Fatalf("the run's branch is %q and the checkout on %q, want its own branch and the person's checkout left alone", branch, shellGit(t, repo, "branch", "--show-current"))
}
if worktrees := shellGit(t, repo, "worktree", "list", "--porcelain"); strings.Count(worktrees, "worktree ") != 1 {
t.Fatalf("the run's copy was not removed:\n%s", worktrees)
}
if !strings.Contains(out, fakeFolder+" · working in "+repo+", in a copy of its own on its own branch "+branch) || !strings.Contains(out, " folder · git") {
t.Fatalf("the run did not say it works on its own branch with git:\n%s", out)
}
if tip := shellGit(t, repo, "rev-parse", "main"); tip != base {
t.Fatalf("the person's branch moved from %s to %s", base, tip)
}
if files := shellGit(t, repo, "ls-tree", "--name-only", branch); files != "made.txt" {
t.Fatalf("the run's branch holds %q, want its work and none of its notes", files)
}
if subject := shellGit(t, repo, "log", "-1", "--format=%s", branch); subject != "make a file" {
t.Fatalf("the commit of its work is %q, want the brief's words", subject)
}
if status := shellGit(t, repo, "status", "--porcelain"); status != "" {
t.Fatalf("the run left changes that are not committed:\n%s", status)
}
if !strings.Contains(out, " its work is on the branch "+branch+" in "+repo+", 1 file; your checkout was not touched, and `git -C ") {
t.Fatalf("the last lines do not say where the work is:\n%s", out)
}
}
// A SHELL RUN BESIDE CHANGES THAT ARE NOT COMMITTED STARTS FROM THEM, and
// leaves them where they are: they are the first commit on its branch, its
// work comes after them, and the person's checkout is not touched.
func TestAShellRunStartsBesideChangesThatAreNotCommitted(t *testing.T) {
_, printed, _ := hostWithFolderChild(t)
repo := shellRepo(t)
if err := os.WriteFile(filepath.Join(repo, "draft.md"), []byte("mine\n"), 0o644); err != nil {
t.Fatal(err)
}
shellGit(t, repo, "add", "draft.md")
if err := os.WriteFile(filepath.Join(repo, "credentials.json"), []byte("local input\n"), 0o600); err != nil {
t.Fatal(err)
}
err := runCarried(fakeFolderProgram(), []string{"--dir", repo, "make a file"})
if code := exitCodeOf(err); code != 0 {
t.Fatalf("the shell run left with %d (%v):\n%s", code, err, printed)
}
branch := shellGit(t, repo, "for-each-ref", "--format=%(refname:short)", "refs/heads/task/")
if files := shellGit(t, repo, "ls-tree", "--name-only", branch); files != "draft.md\nmade.txt" {
t.Fatalf("the run's branch holds %q, want the person's draft and its work", files)
}
if first := shellGit(t, repo, "log", "--format=%s", "--reverse", "main.."+branch); !strings.HasPrefix(first, "Your uncommitted changes when ") {
t.Fatalf("the branch does not open on the person's changes:\n%s", first)
}
if status := shellGit(t, repo, "status", "--porcelain"); status != "A draft.md\n?? credentials.json" || shellGit(t, repo, "branch", "--show-current") != "main" {
t.Fatalf("the person's checkout was touched: %q", status)
}
if !strings.Contains(printed.String(), "any it changes are committed as its work, and the rest stay off its branch") {
t.Fatalf("the shell receipt did not distinguish the untracked input:\n%s", printed)
}
}
// A SHELL RUN'S CHILD IS TOLD WHAT CODEAF DECIDED ABOUT ITS FOLDER: the
// program's own flags for a folder without git, its copy in place of the
// person's --dir in a repository, and — ahead of the brief — where that copy
// is.
func TestAShellRunsChildLineCarriesItsFolder(t *testing.T) {
program := fakeFolderProgram()
inv, err := delegate.Parse(program, []string{"--dir", "/r/repo/sub", "fix", "it"}, &bytes.Buffer{})
if err != nil {
t.Fatal(err)
}
copied := &session.ProgramFolder{Dir: "/tmp/copy", Repo: "/r/repo", Branch: "task/fix-it-abc123"}
child := carriedInFolder(carriedChildLine(inv), inv, copied)
if got, want := strings.Join(child, " "), fakeFolder+" --json --dir /tmp/copy "+copied.BriefNote()+"\n\n fix it"; got != want {
t.Fatalf("the child line = %q, want %q", got, want)
}
plain, err := delegate.Parse(program, []string{"--dir", "/r/plain", "fix", "it"}, &bytes.Buffer{})
if err != nil {
t.Fatal(err)
}
child = carriedInFolder(carriedChildLine(plain), plain, &session.ProgramFolder{Dir: "/r/plain"})
if got, want := strings.Join(child, " "), fakeFolder+" --json --in-place --dir /r/plain fix it"; got != want {
t.Fatalf("the child line = %q, want %q", got, want)
}
again, err := delegate.Parse(program, child[1:], &bytes.Buffer{})
if err != nil || again.Workspace != "/r/plain" || again.Brief() != "fix it" {
t.Fatalf("the child reads %+v (%v)", again, err)
}
}
// newestRecordOf is the most recent shell run's record folder for a program.
func newestRecordOf(t *testing.T, name string) string {
t.Helper()
matches, _ := filepath.Glob(filepath.Join(carriedRecordRoot(name), "*"))
if len(matches) == 0 {
t.Fatal("the shell run kept no record folder")
}
newest := matches[0]
for _, match := range matches[1:] {
if match > newest {
newest = match
}
}
return newest
}
// A SHELL RUN FINISHES ITS FOLDER BEFORE IT WAITS FOR ITS LAST PRICES. That
// wait is up to seventy seconds, a second ctrl-c during it leaves at once,
// and the folder used to be finished only after it: the repository was left
// on the program's branch with its work uncommitted and nothing said. By the
// time the model API starts closing, the work is committed on its branch and
// the copy is gone, so the branch is released before the wait.
func TestAShellRunFinishesItsFolderBeforeWaitingForPrices(t *testing.T) {
_, printed, _ := hostWithFolderChild(t)
repo := shellRepo(t)
var atClose struct{ status, files string }
previous := carriedAPIClose
carriedAPIClose = func(api *modelapi.Server) error {
atClose.status = shellGit(t, repo, "worktree", "list", "--porcelain")
atClose.files = shellGit(t, repo, "ls-tree", "--name-only", shellGit(t, repo, "for-each-ref", "--format=%(refname:short)", "refs/heads/task/"))
return previous(api)
}
t.Cleanup(func() { carriedAPIClose = previous })
err := runCarried(fakeFolderProgram(), []string{"--dir", repo, "make a file"})
if code := exitCodeOf(err); code != 0 {
t.Fatalf("the shell run left with %d (%v):\n%s", code, err, printed)
}
if strings.Count(atClose.status, "worktree ") != 1 || atClose.files != "made.txt" {
t.Fatalf("when the API began to close, the worktrees were %q and the branch held %q, want the copy gone and the work committed", atClose.status, atClose.files)
}
}