package web
import (
"bytes"
"strings"
"testing"
"craftdesign.group/quarry/internal/ci"
)
// A landing is the page people sit on while CI runs, so the card has to
// move on its own — and say nothing is moving when nothing is. Both
// halves matter: a card that never subscribes leaves the reader
// reloading, and one that always subscribes holds a stream open on
// every settled record forever.
func TestLandingCardIsLiveOnlyWhileSomethingRuns(t *testing.T) {
card := func(status string) string {
det := &landingDetail{}
det.State = "open"
det.ID = "abcdef123456"
det.Checks = []ciRunRow{{
RunID: "run1", Workflow: "ci/quarry.json", Status: status,
Tail: []ci.LogChunk{{Seq: 1, Stream: "out", Data: "go test ./..."}},
TailStep: "test",
}}
var buf bytes.Buffer
if err := noHooks.template(landingPage).ExecuteTemplate(&buf, "landing-card",
pageData{Repo: "alpha", Landing: det}); err != nil {
t.Fatalf("landing-card does not render standalone, so the stream cannot push it: %v", err)
}
return buf.String()
}
running := card("running")
if !strings.Contains(running, "/r/alpha/landing.sse/abcdef123456") {
t.Error("a running check does not subscribe; the reader is left reloading")
}
// The running step's own output rides on the card — the question
// while you wait is what it is doing, not merely that it is.
if !strings.Contains(running, "go test ./...") {
t.Error("the running step's log is not on the card")
}
// ...and it is not dressed as a failure.
if strings.Contains(running, "l-checkfail") || strings.Contains(running, "failed</span>") {
t.Error("a running check renders as a failed one")
}
if done := card("succeeded"); strings.Contains(done, "landing.sse") {
t.Error("a settled check still subscribes; that stream never ends")
}
}
// The no-script half of the same promise: reload while it moves, never
// once it has settled. A page that always refreshes is a page nobody
// can read.
func TestNoScriptRefreshOnlyWhileRunning(t *testing.T) {
if !strings.Contains(baseHTML, `{{if .RefreshSecs}}<noscript><meta http-equiv="refresh"`) {
t.Error("the no-script refresh is missing, or is not inside <noscript> " +
"(a scripted client would reload underneath its own stream)")
}
}