Skip to content
quarry Artifact [d76d37f796]
Login

Artifact [d76d37f796]

Artifact d76d37f796c2046dcbab2a73aefee8e4c9778335aef54fc655447685a37425d8:


package web

import (
	"strings"
	"testing"
)

// A line is drawn only when it has something to say, and it is scaled
// to its own peak so the shape is the change rather than the absolute —
// the absolute is the number it sits beside.
func TestOpsLineDrawsOnlyWhenItSaysSomething(t *testing.T) {
	if got := lineSVG(nil, "x"); got != "" {
		t.Errorf("no series drew %q", got)
	}
	if got := lineSVG([]int{3}, "x"); got != "" {
		t.Errorf("a single point drew %q", got)
	}
	// All zero: a flat line at the floor invites a reading that is not there.
	if got := lineSVG([]int{0, 0, 0, 0}, "x"); got != "" {
		t.Errorf("an empty window drew %q", got)
	}
	svg := string(lineSVG([]int{0, 5, 0}, "store subprocesses running, last 3m"))
	if !strings.Contains(svg, "<polyline") {
		t.Fatalf("no polyline: %s", svg)
	}
	// The peak touches the top of the box and the floor sits at the
	// bottom, so a reader sees the full swing of the window.
	if !strings.Contains(svg, "0.0,24.00") || !strings.Contains(svg, "50.0,2.00") {
		t.Errorf("series is not scaled to its own peak: %s", svg)
	}
	// Named, not silent: the shape is the only content, so it carries
	// the label a reader hovers and a screen reader announces.
	if !strings.Contains(svg, `aria-label="store subprocesses running, last 3m"`) ||
		!strings.Contains(svg, "<title>store subprocesses running, last 3m</title>") {
		t.Errorf("the line is unlabelled: %s", svg)
	}
}

// The caption names what each line measures, because neither line is a
// history of the number above it on that card.
func TestOpsCardsNameWhatTheirLineMeasures(t *testing.T) {
	v := opsView{LiveSubprocess: 5, PeakSubprocess: 9,
		SubprocessSeries: []int{1, 4, 2}, RenderSeries: []int{0, 2, 1},
		SeriesWindowSeconds: 180, Window: "3 recent renders"}
	var buf strings.Builder
	if err := opsPage.Execute(&buf, pageData{Title: "Operations", Ops: v}); err != nil {
		t.Fatal(err)
	}
	body := buf.String()
	if strings.Count(body, "<polyline") != 2 {
		t.Fatalf("want 2 lines, got %d", strings.Count(body, "<polyline"))
	}
	for _, want := range []string{"line: running, last 3m", "line: renders, last 3m"} {
		if !strings.Contains(body, want) {
			t.Errorf("caption missing %q", want)
		}
	}
	// No series, no caption claiming a scale, and no empty box.
	var plain strings.Builder
	if err := opsPage.Execute(&plain, pageData{Title: "Operations",
		Ops: opsView{LiveSubprocess: 0, Window: "no renders"}}); err != nil {
		t.Fatal(err)
	}
	if b := plain.String(); strings.Contains(b, "<polyline") || strings.Contains(b, "line: ") {
		t.Error("a forge with no window drew a line or claimed one")
	}
}