package web
import (
"net/http/httptest"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/ci"
"craftdesign.group/quarry/internal/ops"
)
func rerunServer(t *testing.T, letters string, runs []api.CIRun, asked *[]string) *httptest.Server {
t.Helper()
ix := landingIndex(t, false)
r := ops.NewRegistry()
deps := api.Deps{Version: "v",
Repos: api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: ix}}),
CI: fakeCI{runs: runs},
CIRerun: func(repo, runID, by string) (string, error) {
*asked = append(*asked, repo+"/"+runID+"/"+by)
return "run-new", nil
}}
if err := api.RegisterAll(r, deps); err != nil {
t.Fatal(err)
}
eng := fakeEngine{run: ci.Run{RunID: "run-1", Workflow: "quarry", Status: "running"}}
srv := httptest.NewServer(HandlerWith(r, nil, nil, []Option{
WithSessions(fakeSessions{user: "dan", roles: map[string]ops.Auth{"alpha": ops.Admin}}),
WithCaps(stubCaps{letters: letters}), WithEngine(eng)},
Verify(func(string, string, string, ops.Auth) api.Decision { return api.Denied })))
t.Cleanup(srv.Close)
return srv
}
// #499: a run made by ci.rerun and its original name each other on
// their pages. (The page's own Re-run goes through ci.trigger.)
func TestARerunAndItsOriginalNameEachOther(t *testing.T) {
runs := []api.CIRun{
{RunID: "run-1", Checkin: "abc", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "running", Started: "2026-08-29T01:00:00Z"},
{RunID: "run-2", Checkin: "abc", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-08-29T01:00:00Z", RerunAs: "run-3"},
{RunID: "run-3", Checkin: "abc", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-08-29T02:00:00Z", RerunOf: "run-2"},
}
var asked []string
admin := rerunServer(t, "ia8", runs, &asked)
if got := len(hookAttrs(asDan(t, admin, "/r/alpha/ci/run/run-2"), "run-rerun-as")); got != 1 {
t.Errorf("FAIL: the original shows %d rerun-as links, want 1", got)
}
if got := len(hookAttrs(asDan(t, admin, "/r/alpha/ci/run/run-3"), "run-rerun-of")); got != 1 {
t.Errorf("FAIL: the rerun shows %d rerun-of links, want 1", got)
}
if !t.Failed() {
t.Log("ok")
}
}