package web
import (
"regexp"
"strings"
"testing"
)
type maintRow = struct {
Store string `json:"store"`
Task string `json:"task"`
State string `json:"state"`
WaitingOn []string `json:"waitingOn"`
AgeMs int64 `json:"ageMs"`
Overdue bool `json:"overdue"`
Last *struct {
AgoMs int64 `json:"agoMs"`
DurationMs int64 `json:"durationMs"`
Error string `json:"error"`
Aborted bool `json:"aborted"`
Pack *maintPack `json:"pack"`
} `json:"last"`
}
type maintPack = struct {
Before uint64 `json:"before"`
After uint64 `json:"after"`
Reclaimed int `json:"reclaimed"`
Nothing bool `json:"nothing"`
}
func maintPage(t *testing.T, rows ...maintRow) string {
t.Helper()
var d pageData
d.Ops.BudgetMs = 500
d.Ops.Maintenance = rows
var buf strings.Builder
if err := noHooks.template(opsPage).ExecuteTemplate(&buf, "content", d); err != nil {
t.Fatal(err)
}
return buf.String()
}
// The maintenance section carries one row per store and task, with the
// state, what a waiting task waits on, whether it is overdue, and the
// last run's duration and error as data hooks.
func TestMaintenanceSectionDrawsOneRowPerTask(t *testing.T) {
last := maintLast(60_000, 4_200, "boom")
body := maintPage(t,
maintRow{Store: "tcl", Task: "file history", State: "waiting", WaitingOn: []string{"deploy running", "memory pressure"}, AgeMs: 90_000_000, Overdue: true},
maintRow{Store: "sqlite", Task: "search check", State: "idle", Last: last},
maintRow{Store: "pkgsrc", Task: "warm reads", State: "queued"},
)
if n := strings.Count(body, `data-hook="maint-task"`); n != 3 {
t.Errorf("FAIL: drew %d task rows, want 3", n)
}
for _, want := range []string{
`id="maintenance"`,
`data-store="tcl"`, `data-task="file history"`, `data-state="waiting"`, `data-overdue="true"`,
`data-hook="maint-waiting-on"`, `data-hook="maint-overdue"`,
`data-state="idle"`, `data-hook="maint-last"`, `data-duration-ms="4200"`, `data-hook="maint-error"`,
`data-state="queued"`,
} {
if !strings.Contains(body, want) {
t.Errorf("FAIL: the section is missing %s", want)
}
}
if m := regexp.MustCompile(`data-overdue="true"`).FindAllString(body, -1); len(m) != 1 {
t.Errorf("FAIL: %d overdue rows, want 1", len(m))
}
if !t.Failed() {
t.Log("ok")
}
}
// No tasks, no section: a forge without a maintenance lane draws nothing.
func TestMaintenanceSectionIsAbsentWithoutTasks(t *testing.T) {
if strings.Contains(maintPage(t), `id="maintenance"`) {
t.Errorf("FAIL: an empty maintenance section was drawn")
}
if !t.Failed() {
t.Log("ok")
}
}
func maintLast(ago, dur int64, errText string) *struct {
AgoMs int64 `json:"agoMs"`
DurationMs int64 `json:"durationMs"`
Error string `json:"error"`
Aborted bool `json:"aborted"`
Pack *maintPack `json:"pack"`
} {
return &struct {
AgoMs int64 `json:"agoMs"`
DurationMs int64 `json:"durationMs"`
Error string `json:"error"`
Aborted bool `json:"aborted"`
Pack *maintPack `json:"pack"`
}{AgoMs: ago, DurationMs: dur, Error: errText}
}
// A finished pack's row carries the before and after sizes as data; an
// aborted one says so and offers the retry; a running or queued pack
// offers no second request. The form to start one names the store.
func TestMaintenancePackRows(t *testing.T) {
done := maintLast(1000, 2000, "")
done.Pack = &maintPack{Before: 3 << 20, After: 2 << 20}
aborted := maintLast(1000, 2000, "")
aborted.Aborted = true
body := maintPage(t,
maintRow{Store: "tcl", Task: "pack", State: "idle", Last: done},
maintRow{Store: "sqlite", Task: "pack", State: "idle", Last: aborted},
maintRow{Store: "pkgsrc", Task: "pack", State: "running"},
maintRow{Store: "fossil", Task: "pack", State: "queued"},
)
for _, want := range []string{
`data-hook="maint-pack" data-before="3145728" data-after="2097152"`,
`data-hook="maint-aborted"`,
} {
if !strings.Contains(body, want) {
t.Errorf("FAIL: the section is missing %s", want)
}
}
if n := strings.Count(body, `data-hook="maint-pack-form"`); n != 2 {
t.Errorf("FAIL: %d per-row pack buttons, want 2 (the idle rows only)", n)
}
if n := strings.Count(body, `data-hook="maint-aborted"`); n != 1 {
t.Errorf("FAIL: %d aborted marks, want 1", n)
}
if !t.Failed() {
t.Log("ok")
}
}