Skip to content
quarry Artifact [709b00b9c4]
Login

Artifact [709b00b9c4]

Artifact 709b00b9c4467ccf9260bdf517a2680446f83d7c12aaaf0b19c8a72a3e5be34e:


package web

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

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

func ciBoxServer(t *testing.T, plane *memCaps) (*httptest.Server, *editRecorder) {
	t.Helper()
	rec := &editRecorder{}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v", Registry: rec,
		Repos:  api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: landingIndex(t, false)}}),
		Roster: capsRoster{plane}}); 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(plane)}))
	t.Cleanup(srv.Close)
	return srv, rec
}

// The visibility section draws one box, with its presence marker, under
// the public preset only, checked when visitors hold 8.
func TestVisibilityDrawsTheCIBoxUnderThePublicPreset(t *testing.T) {
	plane := &memCaps{stored: map[string]string{"dan": "a"}}
	srv, _ := ciBoxServer(t, plane)
	draws := func() (drawn, checked bool) {
		body := asDan(t, srv, "/r/alpha/settings")
		i := strings.Index(body, `name="cipublic"`)
		if i < 0 {
			return false, false
		}
		tag := body[strings.LastIndex(body[:i], "<input"):]
		tag = tag[:strings.Index(tag, ">")]
		return strings.Contains(body, `name="has" value="cipublic"`), strings.Contains(tag, "checked")
	}
	if drawn, checked := draws(); !drawn || checked {
		t.Errorf("FAIL: public preset: drawn with marker=%v checked=%v, want drawn and unchecked", drawn, checked)
	}
	plane.cats = map[string]string{"alpha/nobody": "gjorzh28"}
	if drawn, checked := draws(); !drawn || !checked {
		t.Errorf("FAIL: nobody holding 8: drawn=%v checked=%v, want both", drawn, checked)
	}
	plane.cats = map[string]string{"alpha/nobody": ""}
	if drawn, _ := draws(); drawn {
		t.Errorf("FAIL: the box is drawn where visitors read nothing")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The box goes through repo.update as ciPublic, read only when the form
// drew it, and only for the public preset; a preset save keeps the 8.
func TestVisibilityFormCarriesTheCIBox(t *testing.T) {
	plane := &memCaps{stored: map[string]string{"dan": "a"}}
	srv, rec := ciBoxServer(t, plane)
	post := func(f url.Values) {
		t.Helper()
		if resp := postAsDan(t, srv, "/r/alpha/settings/visibility", f); resp.StatusCode >= 400 {
			t.Fatalf("FAIL: the visibility form answered %d", resp.StatusCode)
		}
	}
	post(url.Values{"visibility": {"Public project"}, "has": {"cipublic"}, "cipublic": {"on"}})
	if rec.count() != 1 || rec.last().CIPublic == nil || !*rec.last().CIPublic {
		t.Fatalf("FAIL: a ticked box did not arrive as on: %d patches", rec.count())
	}
	post(url.Values{"visibility": {"Public project"}, "has": {"cipublic"}})
	if rec.count() != 2 || rec.last().CIPublic == nil || *rec.last().CIPublic {
		t.Errorf("FAIL: a drawn, unticked box did not arrive as off")
	}
	n := rec.count()
	post(url.Values{"visibility": {"Public project"}})
	if rec.count() != n {
		t.Errorf("FAIL: a form that did not draw the box wrote it")
	}
	post(url.Values{"visibility": {"Members only"}, "has": {"cipublic"}, "cipublic": {"on"}})
	if rec.count() != n {
		t.Errorf("FAIL: the box was written beside a preset visitors cannot read")
	}
	plane.cats = map[string]string{"alpha/nobody": "gjorzh28"}
	post(url.Values{"visibility": {"Public project"}})
	if got := plane.cat("alpha", "nobody"); !strings.Contains(got, "8") {
		t.Errorf("FAIL: saving the preset again dropped the 8: %q", got)
	}
	// A reset from Custom resets: the 8 goes with the rest.
	plane.cats = map[string]string{"alpha/nobody": "o8", "alpha/anonymous": "o"}
	post(url.Values{"visibility": {"Public project"}})
	if got := plane.cat("alpha", "nobody"); strings.Contains(got, "8") {
		t.Errorf("FAIL: a preset chosen from Custom kept the 8: %q", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The failing line of a red check is log output: drawn for a reader
// with the CI letter, never for one without it.
func TestAFailedChecksLogLineNeedsTheCILetter(t *testing.T) {
	ix := landingIndex(t, false)
	build := func(letters string) string {
		t.Helper()
		r := ops.NewRegistry()
		if err := api.RegisterAll(r, api.Deps{Version: "v",
			Repos:  api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: ix}}),
			Checks: fakeChecks{required: map[string][]string{"trunk": {"test"}}},
			CI: fakeCI{runs: []api.CIRun{
				{RunID: "runfailfailfail0", Workflow: "test", Pipeline: "ci/test.json", Status: "failed", Started: "2026-08-29"}}}}); err != nil {
			t.Fatal(err)
		}
		store := fakeLogStore{chunks: []ci.LogChunk{
			{Seq: 1, Stream: "err", Data: "--- FAIL: TestChurn\n", Step: "test"},
			{Seq: 2, Stream: "marker", Data: "failed", Step: "test"}}}
		srv := httptest.NewServer(HandlerWith(r, nil, nil,
			[]Option{WithSessions(fakeSessions{user: "dan"}), WithCaps(stubCaps{letters: letters}), WithCILogs(store)}))
		t.Cleanup(srv.Close)
		return asDan(t, srv, "/r/alpha/landing/"+strings.Repeat("a1", 6))
	}
	without, with := build("i"), build("i8")
	if !strings.Contains(without, `data-hook="check-row" data-state="fail"`) {
		t.Errorf("FAIL: the red check is not drawn for a reader without 8")
	}
	if strings.Contains(without, `data-hook="check-fail-line"`) {
		t.Errorf("FAIL: a reader without 8 was shown the failing log line")
	}
	if !strings.Contains(with, `data-hook="check-fail-line"`) {
		t.Errorf("FAIL: a reader with 8 lost the failing log line")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}