internal/httpd/buildpages_test.go
333 lines · 11661 bytes
1package httpd
2
3import (
4 "gitbay.org/gitbay/internal/control"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/store"
9 "gitbay.org/gitbay/internal/web"
10)
11
12func testRepoPage() repoPage {
13 var p repoPage
14 p.Repo = store.Repo{OwnerName: "krz", Name: "gitbay", DefaultBranch: "main"}
15 p.Host = "gitbay.org"
16 return p
17}
18
19// The build pages read their data from the build commands' JSON now, not
20// from store.Build. A renamed field would be a blank cell rather than a
21// compile error, so render both pages and look for the values.
22func TestBuildsPageRendersCommandOutput(t *testing.T) {
23 var sb strings.Builder
24 builds := []control.BuildOut{{
25 Number: 60, Job: "build", Status: "success",
26 SHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b",
27 Ref: "cli-coverage", CreatedAt: "2026-08-28T04:42:54Z",
28 }}
29 jobs := []control.JobOut{{Name: "build"}, {Name: "nightly", Schedule: "0 3 * * *"}}
30 filter := buildFilter{}
31 err := web.Render(&sb, "builds.html", struct {
32 repoPage
33 Builds []control.BuildOut
34 Jobs []control.JobOut
35 Runs []buildRun
36 Filter buildFilter
37 Facets []facetGroup
38 Refs []string
39 Older string
40 CanWrite bool
41 Notice string
42 }{
43 testRepoPage(), builds, jobs, groupRuns(builds), filter, nil,
44 distinctRefs(builds, filter.Ref), "", true, "",
45 })
46 if err != nil {
47 t.Fatalf("render: %v", err)
48 }
49 out := sb.String()
50 for _, want := range []string{
51 "build", "success", "cli-coverage", "ff6271a9d4",
52 `value="build"`, `value="nightly"`, "schedule 0 3 * * *",
53 } {
54 if !strings.Contains(out, want) {
55 t.Errorf("builds.html missing %q", want)
56 }
57 }
58}
59
60// A writer sees the instruction to add CI; a reader without push access
61// sees only the fact, since the instruction is not theirs to act on
62// (#270).
63func TestBuildsEmptyStateOmitsInstructionForReaders(t *testing.T) {
64 var sb strings.Builder
65 filter := buildFilter{}
66 err := web.Render(&sb, "builds.html", struct {
67 repoPage
68 Builds []control.BuildOut
69 Jobs []control.JobOut
70 Runs []buildRun
71 Filter buildFilter
72 Facets []facetGroup
73 Refs []string
74 Older string
75 CanWrite bool
76 Notice string
77 }{
78 testRepoPage(), nil, nil, nil, filter, nil, nil, "", false, "",
79 })
80 if err != nil {
81 t.Fatalf("render: %v", err)
82 }
83 out := sb.String()
84 if !strings.Contains(out, "no builds") {
85 t.Error(`missing "no builds"`)
86 }
87 if strings.Contains(out, "ci.yml") {
88 t.Error("a reader without push access should not see the push instruction")
89 }
90}
91
92// A writer with zero builds gets the one instruction the page can give
93// them (#270).
94func TestBuildsEmptyStateShowsInstructionForWriters(t *testing.T) {
95 var sb strings.Builder
96 filter := buildFilter{}
97 err := web.Render(&sb, "builds.html", struct {
98 repoPage
99 Builds []control.BuildOut
100 Jobs []control.JobOut
101 Runs []buildRun
102 Filter buildFilter
103 Facets []facetGroup
104 Refs []string
105 Older string
106 CanWrite bool
107 Notice string
108 }{
109 testRepoPage(), nil, nil, nil, filter, nil, nil, "", true, "",
110 })
111 if err != nil {
112 t.Fatalf("render: %v", err)
113 }
114 out := sb.String()
115 if !strings.Contains(out, "no builds") {
116 t.Error(`missing "no builds"`)
117 }
118 if !strings.Contains(out, "push a commit with a .gitbay/ci.yml") {
119 t.Error("a writer with no builds should see the push instruction")
120 }
121}
122
123func TestBuildPageRendersCommandOutput(t *testing.T) {
124 var sb strings.Builder
125 err := web.Render(&sb, "build.html", buildView{
126 repoPage: testRepoPage(),
127 Build: control.BuildOut{
128 Number: 60, Job: "build", Status: "success",
129 SHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b", Ref: "cli-coverage",
130 CreatedAt: "2026-08-28T04:42:54Z", FinishedAt: "2026-08-28T04:43:06Z",
131 },
132 Log: "step 1 ok",
133 CanWrite: true,
134 })
135 if err != nil {
136 t.Fatalf("render: %v", err)
137 }
138 out := sb.String()
139 for _, want := range []string{
140 "Build 60", "success", "build on cli-coverage", "ff6271a9d4",
141 "2026-08-28 04:42", "2026-08-28 04:43", "step 1 ok",
142 } {
143 if !strings.Contains(out, want) {
144 t.Errorf("build.html missing %q", want)
145 }
146 }
147 // A finished build offers no cancel control, even to a writer.
148 if strings.Contains(out, "/cancel") {
149 t.Error("build.html offers cancel on a finished build")
150 }
151}
152
153// The count under the heading said "15 runs" while listing 23 builds
154// (#240). It names both numbers now, so neither is mistaken for the other.
155func TestBuildsPageCountsBuildsAndRuns(t *testing.T) {
156 builds := []control.BuildOut{
157 {Number: 4, Job: "instances", Status: "success", SHA: "aaa", Ref: "main", CreatedAt: "2026-09-20T06:00:00Z"},
158 {Number: 3, Job: "instances", Status: "success", SHA: "aaa", Ref: "main", CreatedAt: "2026-09-19T06:00:00Z"},
159 {Number: 2, Job: "lint", Status: "success", SHA: "aaa", Ref: "main", CreatedAt: "2026-09-12T11:20:03Z"},
160 {Number: 1, Job: "unit", Status: "success", SHA: "aaa", Ref: "main", CreatedAt: "2026-09-12T11:20:03Z"},
161 }
162 var sb strings.Builder
163 filter := buildFilter{}
164 err := web.Render(&sb, "builds.html", struct {
165 repoPage
166 Builds []control.BuildOut
167 Jobs []control.JobOut
168 Runs []buildRun
169 Filter buildFilter
170 Facets []facetGroup
171 Refs []string
172 Older string
173 CanWrite bool
174 Notice string
175 }{
176 testRepoPage(), builds, nil, groupRuns(builds), filter, nil,
177 distinctRefs(builds, filter.Ref), "", true, "",
178 })
179 if err != nil {
180 t.Fatalf("render: %v", err)
181 }
182 if !strings.Contains(sb.String(), "4 builds in 3 runs") {
183 t.Errorf("builds.html count line: %q", sb.String())
184 }
185}
186
187// A run row led with a ten-character sha and nothing said what the commit
188// was (#241). The subject leads now, the sha follows as metadata, and a
189// build whose commit is gone falls back to the sha alone.
190func TestBuildsPageLeadsWithTheSubject(t *testing.T) {
191 builds := []control.BuildOut{
192 {Number: 2, Job: "unit", Status: "success", SHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b",
193 Ref: "main", CreatedAt: "2026-09-20T06:00:00Z", Subject: "runner: cap a build's container"},
194 {Number: 1, Job: "unit", Status: "failure", SHA: "aa11bb22cc33dd44ee55ff6677889900aabbccdd",
195 Ref: "main", CreatedAt: "2026-09-19T06:00:00Z"},
196 }
197 out := renderBuilds(t, builds, buildFilter{}, "?cursor=abc")
198 if !strings.Contains(out, "runner: cap a build's container") {
199 t.Errorf("builds.html does not lead with the subject:\n%s", out)
200 }
201 if !strings.Contains(out, "<code>ff6271a9d4</code>") {
202 t.Errorf("builds.html drops the sha from the metadata line:\n%s", out)
203 }
204 // The commit is gone, so the sha is all there is to name the row by.
205 if !strings.Contains(out, ">aa11bb22cc</a>") {
206 t.Errorf("a subjectless run does not fall back to its sha:\n%s", out)
207 }
208}
209
210// Filtering appeared to raise the run count because the list was capped
211// at 50 with the window unstated (#244). The page is paged now: the count
212// says it counts this page, and the link to the next one carries every
213// filter.
214func TestBuildsPagePagesAndSaysSo(t *testing.T) {
215 builds := []control.BuildOut{{Number: 1, Job: "unit", Status: "failure", SHA: "aaa", Ref: "main", CreatedAt: "2026-09-19T06:00:00Z"}}
216 out := renderBuilds(t, builds, buildFilter{Status: "failure"}, "?cursor=abc&status=failure")
217 if !strings.Contains(out, "1 build in 1 run on this page") {
218 t.Errorf("count line does not name the page:\n%s", out)
219 }
220 if !strings.Contains(out, `href="?cursor=abc&status=failure"`) {
221 t.Errorf("pager link missing or drops the filter:\n%s", out)
222 }
223 // With everything on one page the count is the whole count.
224 if out := renderBuilds(t, builds, buildFilter{}, ""); strings.Contains(out, "on this page") {
225 t.Errorf("an unpaged listing still hedges the count:\n%s", out)
226 }
227}
228
229func TestOlderBuildsCarriesFilters(t *testing.T) {
230 if got := olderBuilds(buildFilter{Ref: "main"}, ""); got != "" {
231 t.Errorf("no next cursor should mean no link, got %q", got)
232 }
233 got := olderBuilds(buildFilter{Ref: "feature/x", Status: "failure", Job: "unit"}, "c1")
234 for _, want := range []string{"cursor=c1", "ref=feature%2Fx", "status=failure", "job=unit"} {
235 if !strings.Contains(got, want) {
236 t.Errorf("olderBuilds = %q, missing %q", got, want)
237 }
238 }
239}
240
241func renderBuilds(t *testing.T, builds []control.BuildOut, filter buildFilter, older string) string {
242 t.Helper()
243 var sb strings.Builder
244 err := web.Render(&sb, "builds.html", struct {
245 repoPage
246 Builds []control.BuildOut
247 Jobs []control.JobOut
248 Runs []buildRun
249 Filter buildFilter
250 Facets []facetGroup
251 Refs []string
252 Older string
253 CanWrite bool
254 Notice string
255 }{
256 testRepoPage(), builds, nil, groupRuns(builds), filter, nil,
257 distinctRefs(builds, filter.Ref), older, true, "",
258 })
259 if err != nil {
260 t.Fatalf("render: %v", err)
261 }
262 return sb.String()
263}
264
265// A failed build's page folds its log by step, opens the step that
266// failed and links to it; no JavaScript (#266).
267func TestBuildPageFoldsStepsAndOpensFailure(t *testing.T) {
268 b := control.BuildOut{Number: 61, Job: "test", Status: "failure",
269 SHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b", Ref: "main",
270 CreatedAt: "2026-08-28T04:42:54Z", FinishedAt: "2026-08-28T04:53:50Z", DurationS: 656,
271 Steps: []string{"go build ./...", "go test ./..."}, FailedStep: 2, FailedReason: "exit 1"}
272 log := "$ git clone x (ff6271a9d4)\n$ go build ./...\n$ go test ./...\n--- FAIL: TestCLI\nstep 2/2 failed: exit 1\n"
273 v := buildView{repoPage: testRepoPage(), Build: b, Log: log, Duration: "10m56s"}
274 v.Steps, v.Failed = logSteps(log, b)
275 var sb strings.Builder
276 if err := web.Render(&sb, "build.html", v); err != nil {
277 t.Fatalf("render: %v", err)
278 }
279 out := sb.String()
280 for _, want := range []string{
281 `<details class="difffold buildstep" id="failed" open>`,
282 "step 2/2", "<code>go test ./...</code>", `href="#failed"`, "Jump to failure",
283 "ran 10m56s", "--- FAIL: TestCLI",
284 } {
285 if !strings.Contains(out, want) {
286 t.Errorf("build.html missing %q", want)
287 }
288 }
289 if n := strings.Count(out, `class="difffold buildstep"`); n != 3 {
290 t.Errorf("%d step folds, want 3 (setup and two steps)", n)
291 }
292 if n := strings.Count(out, `id="failed"`); n != 1 {
293 t.Errorf("%d failed anchors, want 1", n)
294 }
295}
296
297// A multi-line step's fold summary shows only its first line, as build
298// show does; the whole step is in the log (#266).
299func TestBuildPageStepSummaryIsFirstLine(t *testing.T) {
300 step := "set -e\ngo test ./..."
301 b := control.BuildOut{Number: 62, Job: "test", Status: "failure",
302 SHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b", Ref: "main",
303 CreatedAt: "2026-08-28T04:42:54Z", FinishedAt: "2026-08-28T04:43:54Z", DurationS: 60,
304 Steps: []string{step}, FailedStep: 1, FailedReason: "exit 1"}
305 log := "$ git clone x (ff6271a9d4)\n$ " + step + "\n--- FAIL: TestCLI\nstep 1/1 failed: exit 1\n"
306 v := buildView{repoPage: testRepoPage(), Build: b, Log: log, Duration: "1m0s"}
307 v.Steps, v.Failed = logSteps(log, b)
308 var sb strings.Builder
309 if err := web.Render(&sb, "build.html", v); err != nil {
310 t.Fatalf("render: %v", err)
311 }
312 out := sb.String()
313 if !strings.Contains(out, "<code>set -e</code>") {
314 t.Errorf("summary does not show the step's first line:\n%s", out)
315 }
316 if strings.Contains(out, "<code>set -e\ngo test") {
317 t.Errorf("summary shows the whole multi-line step")
318 }
319}
320
321// The Live note says the page updates itself, in plain words, rather
322// than the more technical "streams here" (#271).
323func TestBuildPageLiveNoteSaysItUpdatesItself(t *testing.T) {
324 var sb strings.Builder
325 if err := web.Render(&sb, "build.html", buildView{
326 repoPage: testRepoPage(), Live: true,
327 }); err != nil {
328 t.Fatalf("render: %v", err)
329 }
330 if !strings.Contains(sb.String(), "This page updates itself") {
331 t.Error(`Live note does not say the page updates itself`)
332 }
333}