package adminconsole

import (
	"strings"
	"testing"
)

func TestFormatDuration(t *testing.T) {
	cases := map[float64]string{
		0: "0s", 30: "30s", 59: "59s", 60: "1m", 150: "3m", 3600: "1h", 3660: "1h1m", 7800: "2h10m",
	}
	for secs, want := range cases {
		if got := FormatDuration(secs); got != want {
			t.Errorf("FormatDuration(%v) = %q, want %q", secs, got, want)
		}
	}
}

func TestMoveDurationChartEmpty(t *testing.T) {
	if got := MoveDurationChart(nil); got != "" {
		t.Errorf("empty chart = %q, want empty", got)
	}
}

func TestMoveDurationChart(t *testing.T) {
	pts := []ChartPoint{{Ordinal: 1, Min: 5, Max: 20, Avg: 10}, {Ordinal: 2, Min: 8, Max: 40, Avg: 18}, {Ordinal: 3, Min: 12, Max: 90, Avg: 30}}
	svg := string(MoveDurationChart(pts))
	for _, want := range []string{"<svg", "ln-min", "ln-avg", "ln-max", "</svg>"} {
		if !strings.Contains(svg, want) {
			t.Errorf("chart missing %q\n%s", want, svg)
		}
	}
	if n := strings.Count(svg, "<polyline"); n != 3 {
		t.Errorf("polylines = %d, want 3", n)
	}
}

func TestXTicks(t *testing.T) {
	cases := map[int][]int{1: {1}, 2: {1, 2}, 3: {1, 2, 3}, 10: {1, 5, 10}}
	for maxOrd, want := range cases {
		got := xTicks(maxOrd)
		if len(got) != len(want) {
			t.Fatalf("xTicks(%d) = %v, want %v", maxOrd, got, want)
		}
		for i := range want {
			if got[i] != want[i] {
				t.Errorf("xTicks(%d) = %v, want %v", maxOrd, got, want)
			}
		}
	}
}
