| 1 | package pipeline |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | |
| 9 | "the-valley/flow/internal/event" |
| 10 | "the-valley/flow/internal/valley" |
| 11 | ) |
| 12 | |
| 13 | // TestChangeLedgerSharedByTwoDashboards has two dashboards on one session, |
| 14 | // each with its own ChangeLedger on the session's file, loaded before |
| 15 | // either saw the change, take turns observing it. Each transition is |
| 16 | // reported by whichever sees it first, and never by the other. |
| 17 | func TestChangeLedgerSharedByTwoDashboards(t *testing.T) { |
| 18 | path := filepath.Join(t.TempDir(), ChangeLedgerFile) |
| 19 | a, err := LoadChangeLedger(path) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | b, err := LoadChangeLedger(path) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | observe := observer(t) |
| 28 | v1 := valley.Version{N: 1, Head: "h1", Landing: valley.NotLanded} |
| 29 | v2 := valley.Version{N: 2, Head: "h2", Landing: valley.NotLanded, Uploader: "restacker"} |
| 30 | |
| 31 | observe("a's first look", a, change(v1)) |
| 32 | observe("b's first look, after a's", b, change(v1)) |
| 33 | |
| 34 | requested := v1 |
| 35 | requested.Request = &valley.Request{Ref: "refs/valley/requests/nxvp"} |
| 36 | observe("a sees the approval", a, change(requested), event.ChangeApproved) |
| 37 | observe("b sees it after a", b, change(requested)) |
| 38 | |
| 39 | stale := requested |
| 40 | stale.Outcome = &valley.Outcome{Decision: "stale"} |
| 41 | observe("b sees it go stale", b, change(stale), event.ChangeStale) |
| 42 | observe("a sees it after b", a, change(stale)) |
| 43 | |
| 44 | observe("a sees the restacker's version", a, change(stale, v2), event.ChangeNewVersion) |
| 45 | observe("b sees it after a", b, change(stale, v2)) |
| 46 | |
| 47 | rejected := v2 |
| 48 | rejected.Request, rejected.Outcome = &valley.Request{Ref: "refs/valley/requests/nxvp"}, &valley.Outcome{Decision: "rejected"} |
| 49 | observe("a sees v2 approved and rejected", a, change(stale, rejected), event.ChangeApproved, event.ChangeRejected) |
| 50 | observe("b, with a listing read before a's, sees v1 stale", b, change(stale)) |
| 51 | |
| 52 | // A third dashboard, started now, reads what the two recorded. |
| 53 | c, err := LoadChangeLedger(path) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | observe("a restarted dashboard", c, change(stale, rejected)) |
| 58 | } |
| 59 | |
| 60 | // TestChangeLedgerOlderListing has two dashboards on one session, where |
| 61 | // the second holds a listing read before the one the first recorded a |
| 62 | // later version from. What the older listing says of the superseded |
| 63 | // version's request is old news, and reports nothing; the first landing |
| 64 | // either listing shows is the change's only one. |
| 65 | func TestChangeLedgerOlderListing(t *testing.T) { |
| 66 | observe := observer(t) |
| 67 | path := filepath.Join(t.TempDir(), ChangeLedgerFile) |
| 68 | a, err := LoadChangeLedger(path) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | b, err := LoadChangeLedger(path) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | v1 := valley.Version{N: 1, Head: "h1", Landing: valley.NotLanded} |
| 77 | v2 := valley.Version{N: 2, Head: "h2", Landing: valley.NotLanded, Uploader: "restacker"} |
| 78 | with := func(v valley.Version, decision string) valley.Version { |
| 79 | v.Request, v.Outcome = &valley.Request{Ref: "refs/valley/requests/nxvp"}, &valley.Outcome{Decision: decision} |
| 80 | return v |
| 81 | } |
| 82 | requested := v1 |
| 83 | requested.Request = &valley.Request{Ref: "refs/valley/requests/nxvp"} |
| 84 | |
| 85 | observe("a's first look", a, change(v1)) |
| 86 | observe("a sees the restacker's version", a, change(v1, v2), event.ChangeNewVersion) |
| 87 | observe("b, with a listing where v1 is approved", b, change(requested)) |
| 88 | observe("b, with a listing where v1 went stale", b, change(with(v1, "stale"))) |
| 89 | observe("b, with a listing where v1 was rejected", b, change(with(v1, "rejected"))) |
| 90 | withdrawn := change(requested) |
| 91 | withdrawn.State = valley.StateWithdrawn |
| 92 | observe("b, with a listing where the change was withdrawn at v1", b, withdrawn) |
| 93 | observe("a sees v2 approved and stale", a, change(v1, with(v2, "stale")), event.ChangeApproved, event.ChangeStale) |
| 94 | observe("b, with a listing where v1 failed", b, change(with(v1, "failed"))) |
| 95 | |
| 96 | // A landing is news from any listing, once. |
| 97 | landed := v1 |
| 98 | landed.Landing = valley.Landed |
| 99 | observe("b, with a listing where v1 landed", b, change(landed), event.ChangeIntegrated) |
| 100 | observe("a sees v1 landed under v2", a, change(landed, with(v2, "stale"))) |
| 101 | |
| 102 | l, err := LoadChangeLedger(filepath.Join(t.TempDir(), ChangeLedgerFile)) |
| 103 | if err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | observe("a first look", l, change(v1)) |
| 107 | observe("a newer listing shows v1 landed under v2", l, change(landed, v2), event.ChangeNewVersion, event.ChangeIntegrated) |
| 108 | observe("an older listing shows v1 landed", l, change(landed)) |
| 109 | } |
| 110 | |
| 111 | // observer returns a function that has l observe ch, as the change nxvp in |
| 112 | // the valley's repo site, whose version 1 this session recorded, and |
| 113 | // checks the transitions it reports, at step. |
| 114 | func observer(t *testing.T) func(step string, l *ChangeLedger, ch valley.Change, want ...string) { |
| 115 | own := func(v valley.Version) bool { return v.N == 1 } |
| 116 | return func(step string, l *ChangeLedger, ch valley.Change, want ...string) { |
| 117 | t.Helper() |
| 118 | ts, err := l.Observe("site", ch, own) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | var got []string |
| 123 | for _, tr := range ts { |
| 124 | got = append(got, tr.Type) |
| 125 | } |
| 126 | if !slices.Equal(got, want) { |
| 127 | t.Errorf("%s: transitions %q, want %q", step, got, want) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // change is the ready change nxvp with versions vs, the last active. |
| 133 | func change(vs ...valley.Version) valley.Change { |
| 134 | return valley.Change{ChangeID: "nxvp-id", Handle: "nxvp", State: valley.StateReady, Active: vs[len(vs)-1].N, Versions: vs} |
| 135 | } |
| 136 | |
| 137 | // TestChangeLedgerLandingBeforeANewVersion: a version that landed is the |
| 138 | // change's landing even when another version became active before the |
| 139 | // ledger saw it land. |
| 140 | func TestChangeLedgerLandingBeforeANewVersion(t *testing.T) { |
| 141 | l, err := LoadChangeLedger(filepath.Join(t.TempDir(), ChangeLedgerFile)) |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | own := func(valley.Version) bool { return false } |
| 146 | commit := "c1" |
| 147 | v1 := valley.Version{N: 1, Head: "h1", Landing: valley.NotLanded, Request: &valley.Request{Ref: "r"}} |
| 148 | if _, err := l.Observe("site", valley.Change{ChangeID: "x", State: valley.StateReady, Active: 1, Versions: []valley.Version{v1}}, own); err != nil { |
| 149 | t.Fatal(err) |
| 150 | } |
| 151 | v1.Landing, v1.LandedCommit = valley.Landed, &commit |
| 152 | v2 := valley.Version{N: 2, Head: "h2", Landing: valley.NotLanded} |
| 153 | ts, err := l.Observe("site", valley.Change{ChangeID: "x", State: valley.StateReady, Active: 2, Versions: []valley.Version{v1, v2}}, own) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | var got []string |
| 158 | for _, tr := range ts { |
| 159 | got = append(got, fmt.Sprintf("%s v%d %s", tr.Type, tr.Version.N, tr.Commit)) |
| 160 | } |
| 161 | if want := []string{event.ChangeNewVersion + " v2 ", event.ChangeIntegrated + " v1 c1"}; !slices.Equal(got, want) { |
| 162 | t.Errorf("transitions %q, want %q", got, want) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // TestChangeLedgerChecks: a version's checks are reported once per verdict. |
| 167 | // A re-run that goes pending and fails again is no news; a new version's |
| 168 | // failure is; a listing read before the one that recorded a later version |
| 169 | // reports nothing of an older version's checks. |
| 170 | func TestChangeLedgerChecks(t *testing.T) { |
| 171 | l, err := LoadChangeLedger(filepath.Join(t.TempDir(), ChangeLedgerFile)) |
| 172 | if err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | observe := observer(t) |
| 176 | at := func(v valley.Version, state string) valley.Version { |
| 177 | v.Checks = &valley.Checks{State: state, Required: []valley.Check{{Name: "web-unit", State: state}}} |
| 178 | return v |
| 179 | } |
| 180 | v1 := valley.Version{N: 1, Head: "h1", Landing: valley.NotLanded} |
| 181 | v2 := valley.Version{N: 2, Head: "h2", Landing: valley.NotLanded} |
| 182 | |
| 183 | observe("the first look", l, change(v1)) |
| 184 | observe("pending", l, change(at(v1, valley.ChecksPending))) |
| 185 | observe("failed", l, change(at(v1, valley.ChecksFailed)), event.ChangeChecksFailed) |
| 186 | observe("re-run, pending", l, change(at(v1, valley.ChecksPending))) |
| 187 | observe("failed again", l, change(at(v1, valley.ChecksFailed))) |
| 188 | observe("v2 failed", l, change(v1, at(v2, valley.ChecksFailed)), event.ChangeNewVersion, event.ChangeChecksFailed) |
| 189 | observe("an older listing's v1 passed", l, change(at(v1, valley.ChecksPassed))) |
| 190 | observe("v2 passed", l, change(v1, at(v2, valley.ChecksPassed)), event.ChangeChecksPassed) |
| 191 | draft := change(v1, at(v2, valley.ChecksFailed)) |
| 192 | draft.State = valley.StateDraft |
| 193 | observe("a draft's checks", l, draft) |
| 194 | } |