aboutsummaryrefslogtreecommitdiff
path: root/internal/render/svg_test.go
blob: 075cb726b46bfddf8576cef217a61ead5e616b99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package render

import (
	"encoding/xml"
	"math"
	"regexp"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/lukaszkasprzak/prognosis/internal/openmeteo"
)

// svgView builds hours starting at 12:00, so a 24-hour view crosses midnight.
func svgView(hours int) View {
	v := View{Label: "Krakow, PL", Sun: map[string][2]string{}}
	start := time.Date(2026, 8, 27, 12, 0, 0, 0, time.UTC)
	for i := 0; i < hours; i++ {
		when := start.Add(time.Duration(i) * time.Hour)
		v.Rows = append(v.Rows, openmeteo.Row{
			When: when,
			Code: 3,
			Vals: map[string]float64{
				"temperature_2m":            15 + 6*math.Sin(float64(i)/4),
				"apparent_temperature":      14 + 6*math.Sin(float64(i)/4),
				"precipitation":             float64(i%7) * 0.1,
				"precipitation_probability": float64(i % 100),
				"relative_humidity_2m":      50 + float64(i%40),
			},
		})
		v.Sun[when.Format("2006-01-02")] = [2]string{"05:47", "19:35"}
	}
	return v
}

func svgOf(t *testing.T, hours int) string {
	t.Helper()
	out := SVG(svgView(hours), testConfig())
	if out == "" {
		t.Fatal("SVG produced nothing")
	}
	return out
}

func TestSVGIsWellFormedXML(t *testing.T) {
	out := svgOf(t, 24)
	if err := xml.Unmarshal([]byte(out), new(any)); err != nil {
		t.Fatalf("not well-formed XML: %v", err)
	}
	if !strings.HasPrefix(out, "<svg xmlns=") {
		t.Error("missing the SVG namespace, so viewers will refuse it")
	}
}

// A place name is user input and can carry characters XML reserves.
func TestSVGEscapesTheLabel(t *testing.T) {
	v := svgView(6)
	v.Label = `Foo & <Bar>`
	out := SVG(v, testConfig())
	if strings.Contains(out, "<Bar>") || strings.Contains(out, "& ") {
		t.Errorf("label was not escaped:\n%s", out)
	}
	if err := xml.Unmarshal([]byte(out), new(any)); err != nil {
		t.Fatalf("unescaped label broke the XML: %v", err)
	}
}

func TestSVGEmptyViewProducesNothing(t *testing.T) {
	if got := SVG(View{}, testConfig()); got != "" {
		t.Fatalf("expected empty output for no rows, got %d bytes", len(got))
	}
}

// One point per hour, on every series.
func TestSVGSeriesHaveAPointPerHour(t *testing.T) {
	const hours = 18
	out := svgOf(t, hours)
	polylines := regexp.MustCompile(`<polyline points="([^"]+)"`).FindAllStringSubmatch(out, -1)
	if len(polylines) < 4 {
		t.Fatalf("expected at least four series, found %d", len(polylines))
	}
	for i, m := range polylines {
		if n := len(strings.Fields(m[1])); n != hours {
			t.Errorf("series %d has %d points, want %d", i, n, hours)
		}
	}
}

// Night shading is what makes a meteogram readable; without sun data there must
// be none rather than a guess.
func TestSVGNightBandsFollowSunData(t *testing.T) {
	withSun := svgOf(t, 24)
	if !strings.Contains(withSun, `fill="#eef1f6"`) {
		t.Error("no night bands drawn despite sunrise/sunset being known")
	}
	v := svgView(24)
	v.Sun = map[string][2]string{}
	if strings.Contains(SVG(v, testConfig()), `fill="#eef1f6"`) {
		t.Error("night bands drawn without sun data; that is a guess")
	}
}

func TestIsNight(t *testing.T) {
	sun := map[string][2]string{"2026-08-27": {"05:47", "19:35"}}
	at := func(h int) time.Time { return time.Date(2026, 8, 27, h, 0, 0, 0, time.UTC) }
	for _, c := range []struct {
		hour int
		want bool
	}{{0, true}, {5, true}, {6, false}, {12, false}, {19, false}, {20, true}, {23, true}} {
		if got := isNight(at(c.hour), sun); got != c.want {
			t.Errorf("%02d:00 night=%v, want %v", c.hour, got, c.want)
		}
	}
	// An unknown date must not be shaded on a guess.
	if isNight(time.Date(2030, 1, 1, 3, 0, 0, 0, time.UTC), sun) {
		t.Error("shaded a date with no sun data")
	}
}

// The rain panel carries millimetres and percent. Without the right-hand axis
// the dashed probability line is read against the millimetre scale.
func TestSVGRainPanelHasBothAxes(t *testing.T) {
	out := svgOf(t, 24)
	if !strings.Contains(out, "100%") || !strings.Contains(out, "50%") {
		t.Errorf("no percentage axis for the probability line:\n%s", out)
	}
}

func TestSVGDatesAreBoldAndHoursDoNotCollide(t *testing.T) {
	out := svgOf(t, 30) // crosses midnight
	if !strings.Contains(out, `font-weight="bold"`) {
		t.Error("date labels are not bold")
	}

	// Collect the x of every axis label, and check none sits on top of a date.
	type label struct {
		x    float64
		bold bool
	}
	var labels []label
	re := regexp.MustCompile(`<text x="([0-9.]+)" y="\d+" text-anchor="middle"( font-weight="bold")?`)
	for _, m := range re.FindAllStringSubmatch(out, -1) {
		x, _ := strconv.ParseFloat(m[1], 64)
		labels = append(labels, label{x: x, bold: m[2] != ""})
	}
	if len(labels) < 4 {
		t.Fatalf("expected several axis labels, found %d", len(labels))
	}
	for _, a := range labels {
		if !a.bold {
			continue
		}
		for _, b := range labels {
			if !b.bold && math.Abs(a.x-b.x) < 20 {
				t.Errorf("an hour label at x=%.0f collides with the date at x=%.0f", b.x, a.x)
			}
		}
	}
}

func TestSVGHasALegendForTheTwoTemperatureSeries(t *testing.T) {
	out := svgOf(t, 12)
	if strings.Count(out, ">temp<") == 0 || strings.Count(out, ">feels<") == 0 {
		t.Errorf("the dashed series is unexplained without a legend:\n%s", out)
	}
}

func TestSVGDrawsDayBoundaries(t *testing.T) {
	crossing := svgOf(t, 30)
	if !strings.Contains(crossing, `stroke="#9aa3ad"`) {
		t.Error("no midnight rule drawn on a view that crosses midnight")
	}
	if strings.Contains(svgOf(t, 6), `stroke="#9aa3ad"`) {
		t.Error("midnight rule drawn on a six-hour view that never reaches midnight")
	}
}

// Axis labels must land on round numbers, and the count must follow the step.
func TestNiceTicks(t *testing.T) {
	for _, c := range []struct{ lo, hi float64 }{{9.4, 22.6}, {0, 1}, {-4.2, 3.1}, {990, 1030}} {
		lo, hi, lines := niceTicks(c.lo, c.hi, 6)
		if lo > c.lo || hi < c.hi {
			t.Errorf("niceTicks(%v,%v) = %v,%v — must contain the range", c.lo, c.hi, lo, hi)
		}
		if lines < 2 || lines > 8 {
			t.Errorf("niceTicks(%v,%v) wants %d lines", c.lo, c.hi, lines)
		}
		step := (hi - lo) / float64(lines-1)
		for i := 0; i < lines; i++ {
			v := lo + step*float64(i)
			if math.Abs(v-math.Round(v*100)/100) > 1e-9 {
				t.Errorf("tick %v is not a round value", v)
			}
		}
	}
}

// The meteogram needs its own fields whatever the table shows.
func TestSVGFieldsCoverEveryPanel(t *testing.T) {
	for _, f := range []string{
		"temperature_2m", "apparent_temperature", "precipitation",
		"precipitation_probability", "relative_humidity_2m",
	} {
		found := false
		for _, have := range SVGFields {
			if have == f {
				found = true
			}
		}
		if !found {
			t.Errorf("SVGFields is missing %q, so that panel would be empty", f)
		}
	}
}