package api
import (
"context"
"testing"
"time"
"craftdesign.group/quarry/internal/obs"
"craftdesign.group/quarry/internal/ops"
)
// The four charts share one axis, so ops.status sends every series the
// same length, the minute the last one is for, which minutes a process
// was running in, and the errors binned on the same clock.
func TestOpsStatusSendsTheHourOnOneAxis(t *testing.T) {
resetFindings()
defer resetFindings()
var asked time.Time
d := Deps{Version: "v", Repos: NewRepos(nil),
ErrorMinutes: func(end time.Time, n int) ([]int, error) {
asked = end
out := make([]int, n)
out[n-2] = 3
return out, nil
}}
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
res, err := r.Invoke(ops.WithUser(context.Background(), "admin"), "ops.status", opsStatusIn{Limit: 5})
if err != nil {
t.Fatal(err)
}
s := res.(opsStatusOut).Signals
end, perr := time.Parse(time.RFC3339, s.End)
if perr != nil || !end.Equal(asked) {
t.Fatalf("FAIL: signals end %q, errors asked for %v (%v)", s.End, asked, perr)
}
n := obs.SignalMinutes
for name, got := range map[string]int{"renders": len(s.Renders), "slow": len(s.Slow), "busy": len(s.Busy),
"errors": len(s.Errors), "up": len(s.Up), "minutes": s.Minutes} {
if got != n {
t.Errorf("FAIL: %s has %d bins, want %d", name, got, n)
}
}
if s.Errors[n-2] != 3 {
t.Errorf("FAIL: errors %v did not come from the minute binning", s.Errors)
}
if !s.Up[n-1] {
t.Error("FAIL: the current minute is marked as no process running")
}
if !t.Failed() {
t.Log("ok")
}
}