Files
llmgw/internal/web/charts2_test.go
T
Ilker ManapandClaude Opus 5 eb9e9b348c
CI / test (push) Successful in 1m28s
Show who is using the cluster and what they are running
The dashboard could say what the cluster was made of and not who was
spending it. Usage was per key, which is the right unit for revoking
something and the wrong one for the question actually being asked: one
person holds a laptop key, a CI key and a notebook key, and adding three
rows up by eye is a chore that gets skipped.

Per person now, joined through the keys they own, and per model — counted
by the model that answered rather than the one requested, since those differ
whenever a fallback serves and what actually ran is the question. Headline
figures are stat tiles with a sparkline: a single current value is a number,
and plotting one is how a chart ends up with a single bar in it. The
sparkline says only rising, falling or flat, which is the one thing the
number cannot.

Ranked horizontal bars, because the labels are email addresses and model
names and the label is the point — the question is which person, not what
shape the distribution has. Rotated or truncated axis labels say the chart
matters more than reading it. Trimmed where drawn and complete on hover, so
the plot keeps its geometry and a reader can still find out whose row it is.

The palette is validated rather than chosen: every adjacent pair clears the
colour-vision separation threshold, and the dark values are the same hues
re-stepped for the dark surface instead of an automatic flip. Run through a
validator, because that is the only way anyone can honestly claim a palette
is colourblind-safe.

Which turned up a bug in the existing chart code. cssVar is an allowlist of
four names that silently returns --accent for anything else, and callers
wrap it in var() themselves — so every new series would have rendered in one
colour with accent-coloured text. Caught by a test written from the rule
that chart text wears text tokens, never the series colour.

A key with no owner is labelled "operator-issued keys" rather than unknown:
it predates accounts and is doing its job, and "unknown" would imply
something is wrong.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 18:38:21 +02:00

118 lines
4.2 KiB
Go

package web
import (
"strings"
"testing"
)
func TestRankedBarsLabelEveryRowDirectly(t *testing.T) {
// The label is the point of this chart: the question is which person or
// which model, not what shape the distribution is. Rotated or truncated
// axis labels would say the chart matters more than reading it.
out := string(HBarChart([]Row{
{Label: "ilker@example.com", Value: 1200},
{Label: "deniz@example.com", Value: 300},
}, "--viz-1", "tokens"))
for _, want := range []string{"ilker@example.com", "deniz@example.com", "1.2k", "300"} {
if !strings.Contains(out, want) {
t.Errorf("the chart does not show %q:\n%s", want, out)
}
}
}
func TestASmallValueStillDrawsAMark(t *testing.T) {
// A bar that rounds to zero width reads as no data rather than as a small
// number, which is a different and wrong claim.
out := string(HBarChart([]Row{
{Label: "huge", Value: 1_000_000},
{Label: "tiny", Value: 1},
}, "--viz-1", ""))
// Two paths, one per row: the tiny one is still drawn.
if n := strings.Count(out, "<path"); n != 2 {
t.Errorf("drew %d marks for two rows", n)
}
}
func TestAnEmptyChartSaysSoRatherThanRenderingNothing(t *testing.T) {
// A missing chart reads as a broken page.
out := string(HBarChart(nil, "--viz-1", ""))
if !strings.Contains(out, "Nothing recorded") {
t.Errorf("an empty series rendered as %q", out)
}
}
func TestChartTextUsesTextColoursNotTheSeriesColour(t *testing.T) {
// Values and labels wear text tokens; the mark beside them carries the
// identity. Colouring the text too makes the palette do two jobs and reads
// badly for anyone the hue separation was chosen to protect.
out := string(HBarChart([]Row{{Label: "a", Value: 5}}, "--viz-2", "req"))
for _, frag := range []string{`fill="var(--fg)"`, `fill="var(--muted)"`} {
if !strings.Contains(out, frag) {
t.Errorf("expected text in a text colour, got:\n%s", out)
}
}
if strings.Contains(out, `<text`) && strings.Contains(out, `fill="var(--viz-2)"><text`) {
t.Error("text was painted with the series colour")
}
}
func TestEveryBarCarriesItsOwnHoverTitle(t *testing.T) {
// The chart is in a browser, so it should answer a pointer. A <title> does
// that with no JavaScript, which is the right trade for a page this project
// serves from a single binary.
out := string(HBarChart([]Row{
{Label: "a", Value: 1}, {Label: "b", Value: 2},
}, "--viz-1", "tokens"))
if n := strings.Count(out, "<title>"); n != 2 {
t.Errorf("%d hover titles for two bars", n)
}
}
func TestALongLabelIsTrimmedRatherThanOverflowing(t *testing.T) {
long := strings.Repeat("a", 80) + "@example.com"
out := string(HBarChart([]Row{{Label: long, Value: 1}}, "--viz-1", ""))
// Trimmed where it is drawn, in full where it is hovered: the plot keeps
// its geometry and the reader can still find out whose row it is.
drawn := out[strings.Index(out, "<text"):strings.Index(out, "</text>")]
if strings.Contains(drawn, long) {
t.Error("a very long label was drawn in full and would overrun the plot")
}
if !strings.Contains(out, long) {
t.Error("the full label is not available anywhere, not even on hover")
}
if !strings.Contains(out, "…") {
t.Error("the label was cut without saying it had been")
}
}
func TestASparklineOfNothingIsNotDrawn(t *testing.T) {
// One point is not a trend, and a single dot invites reading one.
if got := Sparkline([]float64{5}, ""); got != "" {
t.Errorf("a one-point sparkline rendered: %q", got)
}
if got := Sparkline(nil, ""); got != "" {
t.Errorf("an empty sparkline rendered: %q", got)
}
}
func TestAFlatSparklineIsDrawnAlongTheBottom(t *testing.T) {
// No traffic is a real answer. An empty box would read as a broken chart.
out := string(Sparkline([]float64{0, 0, 0}, ""))
if !strings.Contains(out, "<polyline") {
t.Errorf("a flat series did not draw: %q", out)
}
}
func TestTheChartScalesToItsContainer(t *testing.T) {
// The dashboard is read on laptops and phones. A fixed pixel width is how a
// table ends up scrolling sideways.
out := string(HBarChart([]Row{{Label: "a", Value: 1}}, "", ""))
if !strings.Contains(out, "viewBox") {
t.Error("no viewBox, so the chart cannot scale")
}
if strings.Contains(out, `width="720"`) {
t.Error("a fixed pixel width was set on the chart")
}
}