package web
import (
"net/http/httptest"
"regexp"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/runnerdist"
)
// A REPOSITORY'S RUNNERS SECTION HANDS OUT THE RUNNER.
//
// It used to say "run quarry runner", which meant building quarry from
// source — jva could not, and a repository's owners should never have
// to. Now the section links one download per platform the forge
// carries, and names the command with this repository's URL.
func TestSettingsRunnersLinksADownloadPerPlatform(t *testing.T) {
r := ops.NewRegistry()
if err := api.RegisterAll(r, api.Deps{Version: "v",
Repos: api.NewRepos(map[string]*api.Repo{"alpha": {Index: landingIndex(t, false)}})}); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(HandlerWith(r, nil, nil, []Option{
WithSessions(fakeSessions{user: "dan", roles: map[string]ops.Auth{"alpha": ops.Admin}}),
WithCaps(stubCaps{letters: "a8i"}),
WithEngine(&mintRecorder{tokens: tokensFixture()}),
}))
t.Cleanup(srv.Close)
page := asDan(t, srv, "/r/alpha/settings")
if !regexp.MustCompile(`data-hook="runner-downloads"`).MatchString(page) {
t.Fatalf("FAIL: settings has no runner downloads ")
}
links := regexp.MustCompile(`href="(/runner/[^"]+)"[^>]*data-hook="runner-download"`).FindAllStringSubmatch(page, -1)
if len(links) != len(runnerdist.Platforms) {
t.Errorf("FAIL: %d download links, want one per platform (%d)", len(links), len(runnerdist.Platforms))
}
for i, p := range runnerdist.Platforms {
if i < len(links) && links[i][1] != "/runner/"+p.File() {
t.Errorf("FAIL: link %d is %s, want /runner/%s", i, links[i][1], p.File())
}
}
if !regexp.MustCompile(`href="/runner/SHA256SUMS"[^>]*data-hook="runner-sums"`).MatchString(page) {
t.Error("FAIL: no checksum link beside the downloads")
}
// A repository adopting CI has no ci/runner.json on trunk yet, and the
// runner exits without one unless told its task types (jva, #1182).
if !regexp.MustCompile(`data-hook="runner-tasks">--tasks alpha\.`).MatchString(page) {
t.Error("FAIL: the page does not say how to name task types before ci/runner.json is on trunk")
}
if !regexp.MustCompile(`data-hook="runner-command">[^<]*/r/alpha<`).MatchString(page) {
t.Error("FAIL: the runner command does not name this repository's URL")
}
}