gunk-dev/the-valley

plan_test.go

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