package web
import (
"regexp"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/ci"
)
// hookHref reads the href of the element carrying data-hook="name".
func hookHref(body, name string) string {
m := regexp.MustCompile(`<a[^>]*data-hook="` + name + `"[^>]*href="([^"]*)"|<a[^>]*href="([^"]*)"[^>]*data-hook="` + name + `"`).FindStringSubmatch(body)
if m == nil {
return ""
}
return m[1] + m[2]
}
// #302: a re-run's page links back to the run it repeats, and the
// original links forward to the run that repeated it; a run with
// neither shows no link.
func TestRunPageLinksARerunBothWays(t *testing.T) {
eng := fakeEngine{run: ci.Run{RunID: "run-2", Workflow: "quarry", Status: "completed"}}
srv := runPageServer(t, eng, []api.CIRun{
{RunID: "run-1", Checkin: "abc", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-08-29T01:00:00Z", RerunAs: "run-2"},
{RunID: "run-2", Checkin: "abc", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-08-29T02:00:00Z", RerunOf: "run-1"},
{RunID: "run-3", Checkin: "def", Workflow: "quarry", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-08-29T03:00:00Z"},
})
if got := hookHref(asDan(t, srv, "/r/alpha/ci/run/run-1"), "run-rerun-as"); got != "/r/alpha/ci/run/run-2" {
t.Errorf("original links forward to %q, want /r/alpha/ci/run/run-2", got)
}
body := asDan(t, srv, "/r/alpha/ci/run/run-2")
if got := hookHref(body, "run-rerun-of"); got != "/r/alpha/ci/run/run-1" {
t.Errorf("re-run links back to %q, want /r/alpha/ci/run/run-1", got)
}
body = asDan(t, srv, "/r/alpha/ci/run/run-3")
if hookHref(body, "run-rerun-of") != "" || hookHref(body, "run-rerun-as") != "" {
t.Errorf("a run nobody repeated links to a re-run")
}
}