Skip to content
quarry Artifact [493aabb7d8]
Login

Artifact [493aabb7d8]

Artifact 493aabb7d82cc4388451d275945bf16ea9375845c05443ecbcfcd436c9d3819c:


package web

import (
	"context"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/ops"
)

// #367: the record page shows every merge run landing.get answers, not
// only the one mergeCiRun names. The card test alone missed the page
// dropping mergeRuns between the op and the template.
func TestTheLandingPageShowsEveryMergeRun(t *testing.T) {
	type getIn struct {
		Repo string `json:"repo"`
		ID   string `json:"id"`
	}
	type row struct {
		ID     string `json:"id"`
		Number int    `json:"number"`
		Branch string `json:"branch"`
		Target string `json:"target"`
		Title  string `json:"title"`
		State  string `json:"state"`
	}
	type run struct {
		RunID         string `json:"runId"`
		Pipeline      string `json:"pipeline"`
		Status        string `json:"status"`
		InheritedFrom string `json:"inheritedFrom,omitempty"`
	}
	type getOut struct {
		Landing    row    `json:"landing"`
		MergeCI    string `json:"mergeCi"`
		MergeCIRun string `json:"mergeCiRun"`
		MergeRuns  []run  `json:"mergeRuns"`
	}
	r := ops.NewRegistry()
	if err := ops.Register(r, ops.Op[getIn, getOut]{
		Name: "landing.get", Doc: "test stub", Kind: ops.Query, Auth: ops.Anonymous,
		Route: "GET /api/v1/r/{repo}/landing/{id}",
		Run: func(context.Context, getIn) (getOut, error) {
			return getOut{Landing: row{ID: "aaaa1111", Number: 7, Branch: "b", Target: "trunk", Title: "Add x", State: "accepted"},
				MergeCI: "ci-pass", MergeCIRun: "rrrrrrrrrrrr",
				MergeRuns: []run{{RunID: "qqqqqqqqqqqq", Pipeline: "ci/quarry.json", Status: "completed", InheritedFrom: "qqqqqqqqqqqq"},
					{RunID: "rrrrrrrrrrrr", Pipeline: "ci/race.json", Status: "completed", InheritedFrom: "rrrrrrrrrrrr"}}}, nil
		},
	}); err != nil {
		t.Fatal(err)
	}
	type ver struct {
		Version string `json:"version"`
	}
	if err := ops.Register(r, ops.Op[struct{}, ver]{
		Name: "meta.version", Doc: "test stub", Kind: ops.Query, Auth: ops.Anonymous, Route: "GET /api/v1/version",
		Run: func(context.Context, struct{}) (ver, error) { return ver{Version: "t"}, nil },
	}); err != nil {
		t.Fatal(err)
	}
	verify := Verify(func(repo, user, pass string, need ops.Auth) api.Decision { return api.Allowed })
	srv := httptest.NewServer(HandlerWith(r, nil, nil, nil, verify))
	t.Cleanup(srv.Close)
	resp, err := http.Get(srv.URL + "/r/alpha/landing/aaaa1111")
	if err != nil {
		t.Fatal(err)
	}
	b, _ := io.ReadAll(resp.Body)
	resp.Body.Close()
	body := string(b)
	if n := strings.Count(body, `data-hook="merge-run"`); n != 2 {
		t.Errorf("FAIL: %d merge runs on the page, want 2 (status %d): %.600s", n, resp.StatusCode, body)
	}
	for _, want := range []string{`/ci/run/qqqqqqqqqqqq"`, `data-inherited="rrrrrrrrrrrr"`} {
		if !strings.Contains(body, want) {
			t.Errorf("FAIL: the page lacks %s", want)
		}
	}
}