gunk-dev/the-valley

plan_test.go

sourceaabc2b3ae680main's published tip history
8.0 KiB
1package main
2
3import (
4 "encoding/json"
5 "errors"
6 "math"
7 "strings"
8 "testing"
9
10 "the-valley/change"
11 "the-valley/rebase"
12)
13
14var (
15 tip = strings.Repeat("a", 40)
16 low = strings.Repeat("b", 40)
17 high = strings.Repeat("c", 40)
18 req = strings.Repeat("d", 40)
19 ev = "0123456789abcdef"
20)
21
22// commits is what each request head would bring to the target, for a
23// few named commits. A head it does not know is an error.
24func commits(in map[string][]string) Incoming {
25 return func(head string) (map[string]bool, error) {
26 list, ok := in[head]
27 if !ok {
28 return nil, errors.New("missing " + head)
29 }
30 set := map[string]bool{}
31 for _, c := range list {
32 set[c] = true
33 }
34 return set, nil
35 }
36}
37
38func TestARequestIsRelatedByNameHeadOrRange(t *testing.T) {
39 m := Members{Names: map[string]bool{"c-abc": true, "alice-topic": true}, Heads: []string{low, high}}
40 in := commits(map[string][]string{
41 tip: {},
42 req: {req, high, low}, // a commit on top of the family's top
43 })
44 for _, tc := range []struct {
45 what string
46 r Request
47 want string
48 }{
49 {"by c-<id>", Request{"c-abc", tip}, "name"},
50 {"by an earlier version's branch", Request{"alice-topic", tip}, "name"},
51 {"by an exact head", Request{"other", high}, "head"},
52 {"by a range holding a layer head", Request{"other", req}, "range"},
53 {"not at all", Request{"other", tip}, ""},
54 } {
55 by, err := Relate(tc.r, m, in)
56 if err != nil || strings.Join(by, ",") != tc.want {
57 t.Errorf("%s: related by %v, %v; want %q", tc.what, by, err, tc.want)
58 }
59 }
60}
61
62func TestARangeOnMainDoesNotRelate(t *testing.T) {
63 // A layer head the tip already reaches is not in the request's
64 // incoming range.
65 m := Members{Names: map[string]bool{}, Heads: []string{low}}
66 if by, err := Relate(Request{"other", req}, m, commits(map[string][]string{req: {req}})); by != nil || err != nil {
67 t.Errorf("related by %v, %v; want unrelated", by, err)
68 }
69}
70
71func TestAnUnreadableRequestCannotBeRelatedOrNot(t *testing.T) {
72 m := Members{Names: map[string]bool{}, Heads: []string{low}}
73 if _, err := Relate(Request{"other", req}, m, commits(nil)); err == nil {
74 t.Error("a request whose head cannot be read was judged unrelated")
75 }
76 // A name match needs no ancestry, so it answers even then.
77 m.Names["c-abc"] = true
78 if by, err := Relate(Request{"c-abc", req}, m, commits(nil)); err != nil || len(by) != 1 {
79 t.Errorf("related by %v, %v; want by name", by, err)
80 }
81}
82
83// A request for an older version of a change holds the change's family,
84// whatever its name: by that version's exact head, and by a range that
85// holds it and not the active head. The older head is no layer's head.
86func TestARequestForAnOlderVersionIsRelated(t *testing.T) {
87 id := strings.Repeat("k", 32)
88 older, active, above := strings.Repeat("e", 40), strings.Repeat("f", 40), strings.Repeat("9", 40)
89 r := &change.Record{Schema: change.Schema, ChangeID: id, Target: "main", Owner: "alice", State: change.Ready, Seq: 2, Active: 2,
90 Versions: []change.Version{
91 {N: 1, Head: change.HeadPrefix + older, Title: "t", Branch: "topic", Uploader: "alice"},
92 {N: 2, Head: change.HeadPrefix + active, Title: "t", Branch: "topic", Uploader: "alice"},
93 }}
94 m := membersOf(&rebase.Family{Layers: []*rebase.Layer{{Name: "kkkk", ID: id, Branch: "topic", Head: active, Record: r}}})
95 in := commits(map[string][]string{above: {above, older}})
96 for _, tc := range []struct {
97 r Request
98 want string
99 }{
100 {Request{"arbitrary", older}, "head"},
101 {Request{"arbitrary", above}, "range"},
102 } {
103 if by, err := Relate(tc.r, m, in); err != nil || strings.Join(by, ",") != tc.want {
104 t.Errorf("a request at %s: related by %v, %v; want %q", short(tc.r.Head), by, err, tc.want)
105 }
106 }
107}
108
109func TestOnlyACurrentStaleOrRejectReleases(t *testing.T) {
110 cur := func(d string) rebase.Outcome { return rebase.Outcome{Head: req, Tip: tip, Evidence: ev, Decision: d} }
111 for _, tc := range []struct {
112 what string
113 o rebase.Outcome
114 found bool
115 current bool
116 releases bool
117 }{
118 {"no outcome", rebase.Outcome{}, false, false, false},
119 {"stale", cur("stale"), true, true, true},
120 {"reject", cur("reject"), true, true, true},
121 {"malformed", rebase.ParseOutcome("not an outcome"), true, false, false},
122 {"another head", rebase.Outcome{Head: high, Tip: tip, Evidence: ev, Decision: "stale"}, true, false, false},
123 {"another tip", rebase.Outcome{Head: req, Tip: low, Evidence: ev, Decision: "stale"}, true, false, false},
124 {"other evidence", rebase.Outcome{Head: req, Tip: tip, Evidence: "fedcba9876543210", Decision: "stale"}, true, false, false},
125 {"failed", rebase.Outcome{Head: req, Tip: tip, Evidence: ev, Decision: "failed", FailedAt: "2026-10-18T00:00:00Z"}, true, true, false},
126 {"refused", cur("refused-not-ready"), true, true, false},
127 {"refused merge", cur("refused-merge"), true, true, false},
128 {"land with the request still filed", cur("land"), true, true, false},
129 } {
130 current, releases, why := Currency(tc.o, tc.found, req, tip, ev)
131 if current != tc.current || releases != tc.releases || why == "" {
132 t.Errorf("%s: current %v, releases %v (%q); want %v, %v", tc.what, current, releases, why, tc.current, tc.releases)
133 }
134 }
135}
136
137func record(versions, seq int, uploader string) *change.Record {
138 r := &change.Record{Schema: change.Schema, ChangeID: strings.Repeat("k", 32), Target: "main", Owner: "alice", State: change.Ready, Seq: seq, Active: versions}
139 for i := 1; i <= versions; i++ {
140 r.Versions = append(r.Versions, change.Version{N: i, Head: change.HeadPrefix + low, Title: "t", Branch: "topic", Uploader: uploader})
141 }
142 return r
143}
144
145func TestCapacityCountsTheVersionTheRestackWouldAdd(t *testing.T) {
146 // Two uploads by the principal and a cap of 2: the third is over.
147 r := record(2, 2, "restacker")
148 if c := CapacityOf(r, "restacker", 2, 100); c.Uploads != 3 || len(c.Over) != 1 {
149 t.Errorf("uploads %d, over %v; want 3 and the cap", c.Uploads, c.Over)
150 }
151 // Another principal's uploads do not count against it.
152 if c := CapacityOf(r, "other", 2, 100); c.Uploads != 1 || len(c.Over) != 0 {
153 t.Errorf("uploads %d, over %v; want 1 and nothing", c.Uploads, c.Over)
154 }
155 at := func(c *Capacity, word string) bool {
156 return len(c.Over) == 1 && strings.Contains(c.Over[0], word)
157 }
158 if c := CapacityOf(record(maxVersions-1, maxVersions-1, "alice"), "restacker", 64, 100); len(c.Over) != 0 {
159 t.Errorf("%d versions is within the limit, over %v", maxVersions, c.Over)
160 }
161 if c := CapacityOf(record(maxVersions, maxVersions, "alice"), "restacker", 64, 100); !at(c, "versions, over") {
162 t.Errorf("%d versions: over %v", maxVersions+1, c.Over)
163 }
164 if c := CapacityOf(record(1, maxSeq, "alice"), "restacker", 64, 100); !at(c, "update") {
165 t.Errorf("update %d: over %v", maxSeq+1, c.Over)
166 }
167 if c := CapacityOf(record(1, 1, "alice"), "restacker", 64, maxBytes); len(c.Over) != 0 {
168 t.Errorf("%d bytes is within the limit, over %v", maxBytes, c.Over)
169 }
170 if c := CapacityOf(record(1, 1, "alice"), "restacker", 64, maxBytes+1); !at(c, "bytes") {
171 t.Errorf("%d bytes: over %v", maxBytes+1, c.Over)
172 }
173}
174
175// The schema puts no upper bound on the cap, so a cap no int holds is no
176// practical cap rather than a refusal. Only what the schema itself refuses
177// is refused.
178func TestACapTooLargeToCountIsNoPracticalCap(t *testing.T) {
179 for _, tc := range []struct {
180 stated json.Number
181 want int
182 }{
183 {"1", 1},
184 {"64", 64},
185 {"9223372036854775807", math.MaxInt},
186 {"9223372036854775808", math.MaxInt},
187 {"100000000000000000000000000000", math.MaxInt},
188 } {
189 if got, err := CapOf(tc.stated); err != nil || got != tc.want {
190 t.Errorf("a cap of %s counts as %d, %v; want %d", tc.stated, got, err, tc.want)
191 }
192 }
193 for _, stated := range []json.Number{"", "0", "-1", "-9223372036854775809", "1.5", "1e3"} {
194 if got, err := CapOf(stated); err == nil {
195 t.Errorf("a cap of %q counts as %d; want it refused", stated, got)
196 }
197 }
198}
199
200func TestTheNextRecordIsMeasuredWithTheNewVersion(t *testing.T) {
201 r := record(1, 1, "alice")
202 before, err := change.Encode(r)
203 if err != nil {
204 t.Fatal(err)
205 }
206 after := nextSize("abc", r, "restacker")
207 if after <= len(before) {
208 t.Errorf("the next record is %d bytes, not more than the %d it extends", after, len(before))
209 }
210}