gunk-dev/the-valley

records_test.go

sourceb9df3fe59e29main's published tip history
15.1 KiB
1package main
2
3// What the change records refuse, read from a real repository: a request
4// named for a change lands only while that change is ready at the
5// request's head, a request of any name is held back by a record that is
6// not ready at its head, and a request whose commits hold a merge is
7// refused. The records are written by hand, the way anyone holding a key
8// could write one where no hook guards the repository.
9
10import (
11 "os"
12 "strings"
13 "testing"
14 "time"
15
16 "the-valley/change"
17)
18
19// scratchRepo is a repository with a main, and a way to make commits and
20// records in it.
21type scratchRepo struct {
22 t *testing.T
23 repo string
24}
25
26func newScratchRepo(t *testing.T) *scratchRepo {
27 s := &scratchRepo{t: t, repo: t.TempDir()}
28 s.git("init", "--quiet", "--initial-branch=main")
29 s.git("commit", "--quiet", "--allow-empty", "-m", "base")
30 return s
31}
32
33func (s *scratchRepo) git(args ...string) string {
34 s.t.Helper()
35 out, err := git(s.repo, append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...)
36 if err != nil {
37 s.t.Fatal(err)
38 }
39 return strings.TrimSpace(out)
40}
41
42func (s *scratchRepo) input(stdin string, args ...string) string {
43 s.t.Helper()
44 out, err := gitInput(s.repo, []byte(stdin), append([]string{"-c", "user.name=t", "-c", "user.email=t@localhost"}, args...)...)
45 if err != nil {
46 s.t.Fatal(err)
47 }
48 return strings.TrimSpace(string(out))
49}
50
51// commit is a new commit on parents, each with a tree of its own.
52func (s *scratchRepo) commit(subject string, parents ...string) string {
53 s.t.Helper()
54 blob := s.input(subject+"\n", "hash-object", "-w", "--stdin")
55 tree := s.input("100644 blob "+blob+"\t"+subject+".txt\n", "mktree")
56 args := []string{"commit-tree", tree, "-m", subject}
57 for _, p := range parents {
58 args = append(args, "-p", p)
59 }
60 return s.git(args...)
61}
62
63// record writes a record of the change at the heads given, in the state
64// given, as the ref for id.
65func (s *scratchRepo) record(id, state string, heads ...string) {
66 s.t.Helper()
67 r := &change.Record{
68 Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state,
69 Seq: len(heads) + 1, Active: len(heads),
70 }
71 for i, h := range heads {
72 r.Versions = append(r.Versions, change.Version{
73 N: i + 1, Head: change.HeadPrefix + h, Title: "a version", Branch: "topic", Uploader: "owner",
74 })
75 }
76 data, err := change.Encode(r)
77 if err != nil {
78 s.t.Fatal(err)
79 }
80 s.recordText(change.Ref("main", id), string(data), heads...)
81}
82
83// recordText writes a record commit holding text as its record.json, at ref.
84func (s *scratchRepo) recordText(ref, text string, parents ...string) {
85 s.t.Helper()
86 blob := s.input(text, "hash-object", "-w", "--stdin")
87 tree := s.input("100644 blob "+blob+"\trecord.json\n", "mktree")
88 args := []string{"commit-tree", tree, "-m", "a change record"}
89 for _, p := range parents {
90 args = append(args, "-p", p)
91 }
92 s.git("update-ref", ref, s.git(args...))
93}
94
95const (
96 readyID = "kkkkllllmmmmnnnnooooppppqqqqrrrr"
97 draftID = "zzzzyyyyxxxxwwwwvvvvuuuuttttssss"
98 goneID = "ppppppppppppppppqqqqqqqqqqqqqqqq"
99)
100
101func TestTheRecordsRefuseARequestForAChangeThatIsNotReady(t *testing.T) {
102 s := newScratchRepo(t)
103 base := s.git("rev-parse", "HEAD")
104 v1 := s.commit("v1", base)
105 v2 := s.commit("v2", base)
106 drafted := s.commit("drafted", base)
107 child := s.commit("child of drafted", drafted)
108 loose := s.commit("loose", base)
109 s.record(readyID, change.Ready, v1, v2)
110 s.record(draftID, change.Draft, drafted)
111 // A record that does not read, and one whose ref names another change.
112 s.recordText(change.Ref("main", goneID), "{}\n", loose)
113 s.recordText(change.Ref("main", "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"),
114 strings.Replace(recordJSON(t, readyID, change.Draft, loose), readyID, draftID, 1), loose)
115
116 in := &integrator{repo: s.repo}
117 idx, err := in.readRecords()
118 if err != nil {
119 t.Fatal(err)
120 }
121 if len(idx.unreadable) != 2 {
122 t.Fatalf("unreadable = %q, want the two records that do not read", idx.unreadable)
123 }
124 if len(idx.byHead[v1]) != 1 || len(idx.byHead[v2]) != 1 || len(idx.byHead[drafted]) != 1 || len(idx.byHead[loose]) != 0 {
125 t.Fatalf("the index by head is %v", idx.byHead)
126 }
127
128 for _, c := range []struct {
129 name, change, head string
130 refused bool
131 says string
132 }{
133 {"its change's request at a version head", change.RequestName(readyID), v2, false, ""},
134 {"its change's request at an older version head", change.RequestName(readyID), v1, false, ""},
135 {"its change's request at a head no version records", change.RequestName(readyID), loose, true, "is no version of change " + readyID},
136 {"a draft's request", change.RequestName(draftID), drafted, true, "change " + draftID + " is draft, not ready"},
137 {"a request for a change with no record", change.RequestName("llllllllllllllllllllllllllllllll"), loose, true, "holds no record of it"},
138 {"a request for a change whose record does not read", change.RequestName(goneID), loose, true, "does not read"},
139 {"a branch's request at a draft's head", "topic", drafted, true, "is v1 of change " + draftID + ", which is draft, not ready"},
140 {"a request for a ready change at a draft's head", change.RequestName(readyID), drafted, true, ""},
141 {"a branch's request at a ready change's head", "topic", v1, false, ""},
142 {"a branch's request no record knows", "loose", loose, false, ""},
143 {"a branch named like a request but not one", "c-" + readyID[:31], drafted, true, "which is draft"},
144 // A line of commits above a draft's head brings the draft with it.
145 {"another name's request for a child of a draft's head", "other", child, true,
146 "the commit " + drafted[:12] + " it brings is v1 of change " + draftID + ", which is draft, not ready"},
147 } {
148 r := request{ref: requestPrefix + "/main/" + c.change, change: c.change, target: "refs/heads/main", head: c.head}
149 outcome, why, err := in.recordRefusal(r, idx, base)
150 if err != nil {
151 t.Fatalf("%s: %v", c.name, err)
152 }
153 if refused := outcome != ""; refused != c.refused || (refused && outcome != refusedNotReady) {
154 t.Errorf("%s: refused %q (%s), want refused %v", c.name, outcome, why, c.refused)
155 }
156 if !strings.Contains(why, c.says) {
157 t.Errorf("%s: refused saying %q, without %q", c.name, why, c.says)
158 }
159 }
160 // Once the draft has landed, its head is on the tip and brings nothing.
161 r := request{change: "other", target: "refs/heads/main", head: child}
162 if outcome, why, _ := in.recordRefusal(r, idx, drafted); outcome != "" {
163 t.Errorf("a draft's head already on the tip still refuses its child: %s", why)
164 }
165
166 // The final check reads every record again: the draft readied lets its
167 // head through, the ready change unreadied holds its heads back, and a
168 // record that newly names a head the pass's index did not know holds
169 // that head back too.
170 s.record(draftID, change.Ready, drafted)
171 s.record(readyID, change.Draft, v1, v2)
172 newID := "lllllllllllllllllllllllllllllllk"
173 s.record(newID, change.Draft, loose)
174 for _, c := range []struct {
175 head string
176 refused bool
177 }{{drafted, false}, {v1, true}, {loose, true}} {
178 r := request{change: "topic", target: "refs/heads/main", head: c.head}
179 outcome, why, err := in.finalRefusal(r, base)
180 if err != nil {
181 t.Fatal(err)
182 }
183 if (outcome == refusedNotReady) != c.refused {
184 t.Errorf("the final check of %s: refused %q (%s), want refused %v", c.head[:12], outcome, why, c.refused)
185 }
186 }
187}
188
189// A record that does not read may name only commits its record commit
190// reaches, so a request is flagged when a commit it brings is one of
191// those, and not otherwise.
192func TestARecordThatDoesNotReadIsReportedWhereItMayMatter(t *testing.T) {
193 s := newScratchRepo(t)
194 base := s.git("rev-parse", "HEAD")
195 named := s.commit("named", base)
196 above := s.commit("above", named)
197 elsewhere := s.commit("elsewhere", base)
198 s.recordText(change.Ref("main", draftID), "{}\n", named)
199 in := &integrator{repo: s.repo}
200 idx, err := in.readRecords()
201 if err != nil {
202 t.Fatal(err)
203 }
204 for _, c := range []struct {
205 name, head, tip string
206 flagged bool
207 }{
208 {"its own head", named, base, true},
209 {"a child of its head", above, base, true},
210 {"a head it cannot reach", elsewhere, base, false},
211 {"a child of its head, once its head is on the tip", above, named, false},
212 } {
213 r := request{change: "topic", target: "refs/heads/main", head: c.head}
214 if got := len(in.uncheckable(r, idx, c.tip)) == 1; got != c.flagged {
215 t.Errorf("%s: flagged %v, want %v", c.name, got, c.flagged)
216 }
217 if outcome, _, _ := in.recordRefusal(r, idx, c.tip); outcome != "" {
218 t.Errorf("%s: a record that does not read refused it", c.name)
219 }
220 }
221}
222
223// A withdrawn change's requests are deleted, named for it or at one of its
224// version heads, each only while the change is still withdrawn and the
225// request still where the pass found it.
226func TestAWithdrawnChangeVoidsItsRequests(t *testing.T) {
227 s := newScratchRepo(t)
228 base := s.git("rev-parse", "HEAD")
229 h := s.commit("withdrawn", base)
230 other := s.commit("other", base)
231 readied := s.commit("readied", base)
232 s.record(draftID, change.Withdrawn, h)
233 s.record(readyID, change.Withdrawn, readied)
234 ref := func(name string) string { return requestPrefix + "/main/" + name }
235 for name, head := range map[string]string{
236 change.RequestName(draftID): h, "topic": h, "unrelated": other, "moved": h, "readied": readied,
237 } {
238 s.git("update-ref", ref(name), head)
239 }
240 in := &integrator{repo: s.repo, out: devNull(t)}
241 idx, err := in.readRecords()
242 if err != nil {
243 t.Fatal(err)
244 }
245 // Between the index and the voiding, one request is approved again at
246 // another head, and one change is readied again.
247 s.git("update-ref", ref("moved"), other)
248 s.record(readyID, change.Ready, readied)
249 var requests []request
250 for _, name := range []string{change.RequestName(draftID), "topic", "unrelated", "moved", "readied"} {
251 head := h
252 switch name {
253 case "unrelated":
254 head = other
255 case "readied":
256 head = readied
257 }
258 requests = append(requests, request{ref: ref(name), change: name, target: "refs/heads/main", head: head})
259 }
260 kept := in.voidWithdrawn(requests, idx)
261 var names []string
262 for _, r := range kept {
263 names = append(names, r.change)
264 }
265 if strings.Join(names, " ") != "unrelated moved readied" {
266 t.Fatalf("kept %v, want the unrelated request, the one approved again, and the readied change's", names)
267 }
268 for name, gone := range map[string]bool{
269 change.RequestName(draftID): true, "topic": true, "unrelated": false, "moved": false, "readied": false,
270 } {
271 if _, there := gitTry(s.repo, "rev-parse", "--verify", "--quiet", ref(name)); there == gone {
272 t.Errorf("the request %s: deleted %v, want %v", name, !there, gone)
273 }
274 }
275 memo, _ := gitTry(s.repo, "cat-file", "blob", outcomePrefix+"/main/topic")
276 if fields := strings.Fields(memo); len(fields) != 4 || fields[3] != string(voidedWithdrawn) {
277 t.Errorf("the voided request's outcome record is %q", memo)
278 }
279}
280
281func recordJSON(t *testing.T, id, state, head string) string {
282 t.Helper()
283 data, err := change.Encode(&change.Record{
284 Schema: change.Schema, ChangeID: id, Target: "main", Owner: "owner", State: state, Seq: 1, Active: 1,
285 Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "a version", Branch: "topic", Uploader: "owner"}},
286 })
287 if err != nil {
288 t.Fatal(err)
289 }
290 return string(data)
291}
292
293// A record past a bound is not read, so it names no head.
294func TestARecordPastItsBoundsIsNotRead(t *testing.T) {
295 s := newScratchRepo(t)
296 head := s.commit("head", s.git("rev-parse", "HEAD"))
297 text := recordJSON(t, draftID, change.Draft, head)
298 s.recordText(change.Ref("main", draftID), text+strings.Repeat(" ", change.MaxRecordBlob), head)
299 in := &integrator{repo: s.repo}
300 idx, err := in.readRecords()
301 if err != nil {
302 t.Fatal(err)
303 }
304 if len(idx.byHead[head]) != 0 || len(idx.unreadable) != 1 || !strings.Contains(idx.unreadable[0].why, "more than the 65536") {
305 t.Fatalf("an oversized record read as %v, unreadable %q", idx.byHead, idx.unreadable)
306 }
307}
308
309func TestARequestWhoseCommitsHoldAMergeIsRefused(t *testing.T) {
310 s := newScratchRepo(t)
311 base := s.git("rev-parse", "HEAD")
312 side := s.commit("side", base)
313 line := s.commit("line", base)
314 merged := s.commit("merged", line, side)
315 above := s.commit("above", merged)
316 // main itself merged once, below where the change begins.
317 tip := s.commit("main merged", base, side)
318 onTip := s.commit("on the tip", tip)
319
320 in := &integrator{repo: s.repo}
321 for _, c := range []struct {
322 name, head string
323 refused bool
324 }{
325 {"a line of commits", line, false},
326 {"a merge at the head", merged, true},
327 {"a merge below the head", above, true},
328 {"a line above a merge main already holds", onTip, false},
329 } {
330 r := request{change: "topic", target: "refs/heads/main", head: c.head}
331 outcome, why, err := in.mergeRefusal(r, tip)
332 if err != nil {
333 t.Fatalf("%s: %v", c.name, err)
334 }
335 if refused := outcome == refusedMerge; refused != c.refused {
336 t.Errorf("%s: refused %v (%s), want %v", c.name, refused, why, c.refused)
337 }
338 }
339}
340
341// A refusal is decided again on every pass, so its memo never stands as
342// the answer: a draft readied is judged on the next pass though its head,
343// the tip and the evidence have not moved.
344func TestARefusalIsNeverTheStandingAnswer(t *testing.T) {
345 in := &integrator{}
346 memo := "h t e"
347 for outcome, stands := range map[string]bool{
348 "land": true, "stale": true, "reject": true,
349 string(refusedNotReady): false, string(refusedMerge): false,
350 } {
351 if got := in.answered(memo+" "+outcome, memo, "i", "i"); got != stands {
352 t.Errorf("a %s memo stands: %v, want %v", outcome, got, stands)
353 }
354 }
355 if in.answered("other t e land", memo, "i", "i") {
356 t.Errorf("a memo about another head stands")
357 }
358}
359
360// An answer stands only under the inputs it was reached under. An outcome
361// record with no inputs record beside it, as one written before inputs
362// were remembered, stands under none. When an input does not read, no
363// verdict stands, and a failure stands for its window as any failure does.
364func TestAnAnswerStandsOnlyUnderItsInputs(t *testing.T) {
365 now := time.Date(2026, 10, 31, 12, 0, 0, 0, time.UTC)
366 in := &integrator{clock: now}
367 memo := "h t e"
368 recent := memo + " " + failedMemo + " " + now.Add(-time.Minute).Format(time.RFC3339)
369 expired := memo + " " + failedMemo + " " + now.Add(-retryAfter).Format(time.RFC3339)
370 for _, c := range []struct {
371 name string
372 last, before, now string
373 stands bool
374 }{
375 {"a verdict under the same inputs", memo + " stale", "i", "i", true},
376 {"a verdict under other inputs", memo + " stale", "i", "j", false},
377 {"a verdict with no inputs record", memo + " stale", "", "i", false},
378 {"a verdict while an input does not read", memo + " stale", unreadInputs, unreadInputs, false},
379 {"a recent failure while an input does not read", recent, unreadInputs, unreadInputs, true},
380 {"an expired failure while an input does not read", expired, unreadInputs, unreadInputs, false},
381 {"a recent failure once the input reads", recent, unreadInputs, "i", false},
382 {"a recent failure under the same inputs", recent, "i", "i", true},
383 } {
384 if got := in.answered(c.last, memo, c.before, c.now); got != c.stands {
385 t.Errorf("%s stands: %v, want %v", c.name, got, c.stands)
386 }
387 }
388}
389
390func devNull(t *testing.T) *os.File {
391 f, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
392 if err != nil {
393 t.Fatal(err)
394 }
395 t.Cleanup(func() { f.Close() })
396 return f
397}