package web
import (
"net/http/httptest"
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/ops"
)
// A VERDICT IS OPEN, ITS RUN IS NOT.
//
// A reader without the CI letter sees each check's name and state on a
// landing, and no run to follow: the ids and links are withheld
// (RFC 0030, Reading CI on a public repository), the checks are not.
// Asserted on hooks, not words: the claim is which ROW is drawn and
// whether it links.
func hiddenServer(t *testing.T, ix *index.Index, letters string) *httptest.Server {
t.Helper()
r := ops.NewRegistry()
deps := api.Deps{Version: "v",
Repos: api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: ix}}),
Checks: fakeChecks{required: map[string][]string{"trunk": {"test"}}},
// A green run EXISTS: the point is that the reader is not told
// about it, and is not told it does not exist either.
CI: fakeCI{runs: []api.CIRun{{RunID: "run-1", Workflow: "test", Pipeline: "ci/test.json", Status: "success", Started: "2026-09-21"}}}}
if err := api.RegisterAll(r, deps); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(HandlerWith(r, nil, nil,
[]Option{WithSessions(fakeSessions{user: "dan"}), WithCaps(stubCaps{letters: letters})}))
t.Cleanup(srv.Close)
return srv
}
func TestARecordPageShowsTheChecksButNoRunLinkWithoutTheCILetter(t *testing.T) {
ix := landingIndex(t, false)
page := "/r/alpha/landing/" + strings.Repeat("a1", 6)
body := asDan(t, hiddenServer(t, ix, "i"), page)
for hook, want := range map[string]bool{
`data-hook="check-row" data-state="pass"`: true, // the verdict is drawn
`data-hook="check-missing"`: false, // and is not "no run yet"
"/ci/run/": false, // with no run to follow
"run-1": false,
} {
if got := strings.Contains(body, hook); got != want {
t.Errorf("FAIL without the CI letter: page contains %s = %v, want %v", hook, got, want)
}
}
body = asDan(t, hiddenServer(t, ix, "i8"), page)
for hook, want := range map[string]bool{
`data-hook="check-row" data-state="pass"`: true,
"/ci/run/run-1": true,
} {
if got := strings.Contains(body, hook); got != want {
t.Errorf("FAIL with the CI letter: page contains %s = %v, want %v", hook, got, want)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// The summary reads the rows it is given: a reader whose run ids were
// withheld still has a headline about the checks, not a hidden state.
func TestChecksSummaryReadsTheRowsOfAReaderWithoutRunIDs(t *testing.T) {
d := landingDetail{Required: []string{"test"}, ChecksHidden: true,
Checks: []ciRunRow{{Workflow: "test", Pipeline: "ci/test.json", Status: "completed"}}}
if s := d.ChecksSummary(); s.State == "hidden" {
t.Errorf("FAIL: a reader with rows got the hidden state")
}
if got := d.MissingChecks(); len(got) != 0 {
t.Errorf("FAIL: a required check with a row read as missing: %v", got)
}
d.Checks = nil
if got := d.MissingChecks(); len(got) != 1 {
t.Errorf("FAIL: a required check with no row read as %v, want [test]", got)
}
if !t.Failed() {
t.Log("ok")
}
}