Skip to content
quarry Artifact [661b343651]
Login

Artifact [661b343651]

Artifact 661b3436519666e70c0c96f63e03613903ea12d130c947dee567053fd58450a0:


package web

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

	"craftdesign.group/quarry/internal/runnerdist"
)

func getCLI(t *testing.T, builds []runnerdist.Build, method string) (int, string) {
	t.Helper()
	srv := httptest.NewServer(HandlerWith(viewRegistry(t, nil), nil, nil,
		[]Option{WithCLIBuilds(func() []runnerdist.Build { return builds })}))
	defer srv.Close()
	req, _ := http.NewRequest(method, srv.URL+"/cli/", nil)
	resp, err := srv.Client().Do(req)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	b, _ := io.ReadAll(resp.Body)
	return resp.StatusCode, string(b)
}

// /cli/ lists every build with its link, size and digest, and the
// one-line install.
func TestCLIPageListsEveryBuild(t *testing.T) {
	builds := []runnerdist.Build{
		{Name: "quarry-linux-amd64", Size: 2048, SHA256: strings.Repeat("a", 64)},
		{Name: "quarry-darwin-arm64", Size: 4096, SHA256: strings.Repeat("b", 64)},
	}
	for _, method := range []string{"GET", "HEAD"} {
		code, html := getCLI(t, builds, method)
		if code != 200 {
			t.Errorf("FAIL: %s /cli/ answered %d, want 200", method, code)
		}
		if method == "HEAD" {
			continue
		}
		for _, b := range builds {
			if !strings.Contains(html, `href="/cli/`+b.Name+`"`) {
				t.Errorf("FAIL: no link to %s", b.Name)
			}
			if !strings.Contains(html, b.SHA256) {
				t.Errorf("FAIL: no digest for %s", b.Name)
			}
		}
		if got := strings.Count(html, `data-hook="cli-build"`); got != len(builds) {
			t.Errorf("FAIL: %d build rows, want %d", got, len(builds))
		}
		if !strings.Contains(html, `data-hook="cli-install"`) {
			t.Errorf("FAIL: no install line")
		}
		if strings.Contains(html, `data-hook="cli-none"`) {
			t.Errorf("FAIL: says there are no builds")
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestCLIPageWithNoBuilds(t *testing.T) {
	code, html := getCLI(t, nil, "GET")
	if code != 200 {
		t.Errorf("FAIL: answered %d, want 200", code)
	}
	if !strings.Contains(html, `data-hook="cli-none"`) {
		t.Errorf("FAIL: no empty-state hook")
	}
	if strings.Contains(html, `data-hook="cli-build"`) {
		t.Errorf("FAIL: build rows with no builds")
	}
	if !strings.Contains(html, `data-hook="cli-install"`) {
		t.Errorf("FAIL: no install line")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}