| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "the-valley/change" |
| 9 | "the-valley/rebase" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | tip = strings.Repeat("a", 40) |
| 14 | low = strings.Repeat("b", 40) |
| 15 | high = strings.Repeat("c", 40) |
| 16 | req = strings.Repeat("d", 40) |
| 17 | ev = "0123456789abcdef" |
| 18 | ) |
| 19 | |
| 20 | // graph is an ancestry over a few named commits: each maps to its |
| 21 | // ancestors, itself included. A commit it does not know is an error. |
| 22 | func graph(g map[string][]string) Ancestry { |
| 23 | return func(a, b string) (bool, error) { |
| 24 | anc, ok := g[b] |
| 25 | if !ok { |
| 26 | return false, errors.New("missing " + b) |
| 27 | } |
| 28 | for _, x := range anc { |
| 29 | if x == a { |
| 30 | return true, nil |
| 31 | } |
| 32 | } |
| 33 | return false, nil |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestARequestIsRelatedByNameHeadOrRange(t *testing.T) { |
| 38 | m := Members{Names: map[string]bool{"c-abc": true, "alice-topic": true}, Heads: []string{low, high}} |
| 39 | g := graph(map[string][]string{ |
| 40 | tip: {tip}, |
| 41 | req: {req, high, low, tip}, // a commit on top of the family's top |
| 42 | }) |
| 43 | for _, tc := range []struct { |
| 44 | what string |
| 45 | r Request |
| 46 | want string |
| 47 | }{ |
| 48 | {"by c-<id>", Request{"c-abc", tip}, "name"}, |
| 49 | {"by an earlier version's branch", Request{"alice-topic", tip}, "name"}, |
| 50 | {"by an exact head", Request{"other", high}, "head"}, |
| 51 | {"by a range holding a layer head", Request{"other", req}, "range"}, |
| 52 | {"not at all", Request{"other", tip}, ""}, |
| 53 | } { |
| 54 | by, err := Relate(tc.r, m, tip, g) |
| 55 | if err != nil || strings.Join(by, ",") != tc.want { |
| 56 | t.Errorf("%s: related by %v, %v; want %q", tc.what, by, err, tc.want) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestARangeOnMainDoesNotRelate(t *testing.T) { |
| 62 | // A layer head the tip already reaches is not in the request's |
| 63 | // incoming range. |
| 64 | m := Members{Names: map[string]bool{}, Heads: []string{low}} |
| 65 | g := graph(map[string][]string{tip: {tip, low}, req: {req, tip, low}}) |
| 66 | if by, err := Relate(Request{"other", req}, m, tip, g); by != nil || err != nil { |
| 67 | t.Errorf("related by %v, %v; want unrelated", by, err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestAnUnreadableRequestCannotBeRelatedOrNot(t *testing.T) { |
| 72 | m := Members{Names: map[string]bool{}, Heads: []string{low}} |
| 73 | if _, err := Relate(Request{"other", req}, m, tip, graph(map[string][]string{tip: {tip}})); 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, tip, graph(nil)); err != nil || len(by) != 1 { |
| 79 | t.Errorf("related by %v, %v; want by name", by, err) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestOnlyACurrentStaleOrRejectReleases(t *testing.T) { |
| 84 | cur := func(d string) rebase.Outcome { return rebase.Outcome{Head: req, Tip: tip, Evidence: ev, Decision: d} } |
| 85 | for _, tc := range []struct { |
| 86 | what string |
| 87 | o rebase.Outcome |
| 88 | found bool |
| 89 | current bool |
| 90 | releases bool |
| 91 | }{ |
| 92 | {"no outcome", rebase.Outcome{}, false, false, false}, |
| 93 | {"stale", cur("stale"), true, true, true}, |
| 94 | {"reject", cur("reject"), true, true, true}, |
| 95 | {"malformed", rebase.ParseOutcome("not an outcome"), true, false, false}, |
| 96 | {"another head", rebase.Outcome{Head: high, Tip: tip, Evidence: ev, Decision: "stale"}, true, false, false}, |
| 97 | {"another tip", rebase.Outcome{Head: req, Tip: low, Evidence: ev, Decision: "stale"}, true, false, false}, |
| 98 | {"other evidence", rebase.Outcome{Head: req, Tip: tip, Evidence: "fedcba9876543210", Decision: "stale"}, true, false, false}, |
| 99 | {"failed", rebase.Outcome{Head: req, Tip: tip, Evidence: ev, Decision: "failed", FailedAt: "2026-10-18T00:00:00Z"}, true, true, false}, |
| 100 | {"refused", cur("refused-not-ready"), true, true, false}, |
| 101 | {"refused merge", cur("refused-merge"), true, true, false}, |
| 102 | {"land with the request still filed", cur("land"), true, true, false}, |
| 103 | } { |
| 104 | current, releases, why := Currency(tc.o, tc.found, req, tip, ev) |
| 105 | if current != tc.current || releases != tc.releases || why == "" { |
| 106 | t.Errorf("%s: current %v, releases %v (%q); want %v, %v", tc.what, current, releases, why, tc.current, tc.releases) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func record(versions, seq int, uploader string) *change.Record { |
| 112 | r := &change.Record{Schema: change.Schema, ChangeID: strings.Repeat("k", 32), Target: "main", Owner: "alice", State: change.Ready, Seq: seq, Active: versions} |
| 113 | for i := 1; i <= versions; i++ { |
| 114 | r.Versions = append(r.Versions, change.Version{N: i, Head: change.HeadPrefix + low, Title: "t", Branch: "topic", Uploader: uploader}) |
| 115 | } |
| 116 | return r |
| 117 | } |
| 118 | |
| 119 | func TestCapacityCountsTheVersionTheRestackWouldAdd(t *testing.T) { |
| 120 | // Two uploads by the principal and a cap of 2: the third is over. |
| 121 | r := record(2, 2, "restacker") |
| 122 | if c := CapacityOf(r, "restacker", 2, 100); c.Uploads != 3 || len(c.Over) != 1 { |
| 123 | t.Errorf("uploads %d, over %v; want 3 and the cap", c.Uploads, c.Over) |
| 124 | } |
| 125 | // Another principal's uploads do not count against it. |
| 126 | if c := CapacityOf(r, "other", 2, 100); c.Uploads != 1 || len(c.Over) != 0 { |
| 127 | t.Errorf("uploads %d, over %v; want 1 and nothing", c.Uploads, c.Over) |
| 128 | } |
| 129 | at := func(c *Capacity, word string) bool { |
| 130 | return len(c.Over) == 1 && strings.Contains(c.Over[0], word) |
| 131 | } |
| 132 | if c := CapacityOf(record(maxVersions-1, maxVersions-1, "alice"), "restacker", 64, 100); len(c.Over) != 0 { |
| 133 | t.Errorf("%d versions is within the limit, over %v", maxVersions, c.Over) |
| 134 | } |
| 135 | if c := CapacityOf(record(maxVersions, maxVersions, "alice"), "restacker", 64, 100); !at(c, "versions, over") { |
| 136 | t.Errorf("%d versions: over %v", maxVersions+1, c.Over) |
| 137 | } |
| 138 | if c := CapacityOf(record(1, maxSeq, "alice"), "restacker", 64, 100); !at(c, "update") { |
| 139 | t.Errorf("update %d: over %v", maxSeq+1, c.Over) |
| 140 | } |
| 141 | if c := CapacityOf(record(1, 1, "alice"), "restacker", 64, maxBytes); len(c.Over) != 0 { |
| 142 | t.Errorf("%d bytes is within the limit, over %v", maxBytes, c.Over) |
| 143 | } |
| 144 | if c := CapacityOf(record(1, 1, "alice"), "restacker", 64, maxBytes+1); !at(c, "bytes") { |
| 145 | t.Errorf("%d bytes: over %v", maxBytes+1, c.Over) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestTheNextRecordIsMeasuredWithTheNewVersion(t *testing.T) { |
| 150 | r := record(1, 1, "alice") |
| 151 | before, err := change.Encode(r) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | after := nextSize("abc", r, "restacker") |
| 156 | if after <= len(before) { |
| 157 | t.Errorf("the next record is %d bytes, not more than the %d it extends", after, len(before)) |
| 158 | } |
| 159 | } |