Files
api-go/internal/api/api_test.go
T

957 lines
30 KiB
Go

package api
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"
"pulsecast/api/internal/cache"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/model"
"pulsecast/api/internal/sse"
)
const fixtureBootstrap = "../../testdata/data/bootstrap.json"
type stubFetcher struct {
mu sync.RWMutex
payload model.Bootstrap
err error
refreshFn func(context.Context) (model.Bootstrap, error)
}
func (s *stubFetcher) Bootstrap(ctx context.Context) (model.Bootstrap, error) {
s.mu.RLock()
fn := s.refreshFn
payload, err := s.payload, s.err
s.mu.RUnlock()
if fn != nil {
return fn(ctx)
}
if err != nil {
return model.Bootstrap{}, err
}
return payload, nil
}
func (s *stubFetcher) fail(err error) {
s.mu.Lock()
s.err = err
s.mu.Unlock()
}
func (s *stubFetcher) recover(payload model.Bootstrap) {
s.mu.Lock()
s.payload, s.err = payload, nil
s.mu.Unlock()
}
func loadFixtureBootstrap(t *testing.T) model.Bootstrap {
t.Helper()
raw, err := os.ReadFile(fixtureBootstrap)
if err != nil {
t.Fatalf("read fixture: %v", err)
}
var payload model.Bootstrap
if err := json.Unmarshal(raw, &payload); err != nil {
t.Fatalf("decode fixture: %v", err)
}
return payload
}
func newFixtureCache(t *testing.T) (*cache.Cache, *stubFetcher) {
t.Helper()
payload := loadFixtureBootstrap(t)
fetcher := &stubFetcher{payload: payload}
snapshots := cache.New(fetcher, cache.Options{RefreshInterval: time.Hour, Timeout: time.Second})
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("prime cache: %v", err)
}
return snapshots, fetcher
}
type stubML struct {
predict func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error)
simulate func(context.Context, ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
train func(context.Context, string, io.Reader) (int, []byte, error)
trainStatus func(context.Context, string) (int, []byte, error)
}
func (s *stubML) Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error) {
if s.predict == nil {
return nil, errors.New("ml unavailable")
}
return s.predict(ctx, req)
}
func (s *stubML) Simulate(ctx context.Context, req ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error) {
if s.simulate == nil {
return nil, errors.New("ml unavailable")
}
return s.simulate(ctx, req)
}
func (s *stubML) Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error) {
if s.train == nil {
return 0, nil, errors.New("ml unavailable")
}
return s.train(ctx, contentType, body)
}
func (s *stubML) TrainStatus(ctx context.Context, jobID string) (int, []byte, error) {
if s.trainStatus == nil {
return 0, nil, errors.New("ml unavailable")
}
return s.trainStatus(ctx, jobID)
}
func newTestServer(t *testing.T, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
t.Helper()
snapshots, _ := newFixtureCache(t)
return newTestServerWithCache(t, snapshots, client, mutate)
}
func newTestServerWithCache(t *testing.T, snapshots *cache.Cache, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
t.Helper()
cfg := config.Config{
Addr: ":0",
FastAPIURL: "http://127.0.0.1:1",
Heartbeat: time.Hour,
MLTimeout: 100 * time.Millisecond,
}
if mutate != nil {
mutate(&cfg)
}
hub := sse.NewHub(cfg.Heartbeat)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go hub.Run(ctx)
srv := New(cfg, snapshots, client, hub)
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
return srv, ts
}
func forecastPoints(n int) []model.ForecastPoint {
start := time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC)
points := make([]model.ForecastPoint, n)
for i := range points {
points[i] = model.ForecastPoint{
T: start.Add(time.Duration(i) * time.Hour),
V: float64(10 + i),
Lo: float64(5 + i),
Hi: float64(15 + i),
}
}
return points
}
// weekForecast builds seven complete store-hour days (10:00-20:00 Berlin),
// which is what the staffing planner consumes.
func weekForecast(t *testing.T) []model.ForecastPoint {
t.Helper()
loc, err := time.LoadLocation(model.StoreHours.TZ)
if err != nil {
t.Fatal(err)
}
start := time.Date(2026, 11, 16, model.StoreHours.Open, 0, 0, 0, loc)
var points []model.ForecastPoint
for day := 0; day < 7; day++ {
for hour := 0; hour < model.StoreHours.Close-model.StoreHours.Open; hour++ {
points = append(points, model.ForecastPoint{
T: start.AddDate(0, 0, day).Add(time.Duration(hour) * time.Hour).UTC(),
V: 25,
})
}
}
return points
}
func getJSON(t *testing.T, url string, dst any) *http.Response {
t.Helper()
resp, err := http.Get(url)
if err != nil {
t.Fatalf("GET %s: %v", url, err)
}
defer resp.Body.Close()
if dst != nil {
if err := json.NewDecoder(resp.Body).Decode(dst); err != nil {
t.Fatalf("decode GET %s: %v", url, err)
}
}
return resp
}
func TestHealthAndReady(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var health map[string]bool
resp := getJSON(t, ts.URL+"/api/healthz", &health)
if resp.StatusCode != http.StatusOK || !health["ok"] {
t.Fatalf("healthz: status=%d body=%v", resp.StatusCode, health)
}
var ready map[string]bool
resp = getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusOK {
t.Fatalf("readyz status=%d body=%v", resp.StatusCode, ready)
}
if !ready["ok"] || !ready["data"] || !ready["model"] {
t.Fatalf("readyz body=%v", ready)
}
}
func TestReadyzWithoutSnapshot(t *testing.T) {
cfg := config.Config{Addr: ":0", FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
fetcher := &stubFetcher{err: errors.New("fastapi unavailable")}
snapshots := cache.New(fetcher, cache.Options{RefreshInterval: time.Hour, Timeout: time.Second})
srv := New(cfg, snapshots, &stubML{}, sse.NewHub(time.Hour))
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
var ready map[string]bool
resp := getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("readyz status=%d, want 503", resp.StatusCode)
}
if ready["ok"] || ready["data"] || ready["model"] {
t.Fatalf("readyz body=%v, want all false", ready)
}
// One failed bootstrap attempt must not make the cache ready.
if err := snapshots.Refresh(context.Background()); err == nil {
t.Fatal("expected the bootstrap refresh to fail")
}
resp = getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusServiceUnavailable || ready["ok"] {
t.Fatalf("readyz after failed refresh: status=%d body=%v", resp.StatusCode, ready)
}
}
func TestReadyzSurvivesFastAPIFailureAfterFirstSnapshot(t *testing.T) {
snapshots, fetcher := newFixtureCache(t)
cfg := config.Config{Addr: ":0", FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
srv := New(cfg, snapshots, &stubML{}, sse.NewHub(time.Hour))
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
var ready map[string]bool
resp := getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusOK || !ready["ok"] || !ready["data"] || !ready["model"] {
t.Fatalf("readyz status=%d body=%v", resp.StatusCode, ready)
}
fetcher.fail(errors.New("connection refused"))
if err := snapshots.Refresh(context.Background()); err == nil {
t.Fatal("expected the refresh to fail")
}
resp = getJSON(t, ts.URL+"/api/readyz", &ready)
if resp.StatusCode != http.StatusOK || !ready["ok"] || !ready["data"] || !ready["model"] {
t.Fatalf("readyz after FastAPI failure: status=%d body=%v, want 200 with the last snapshot", resp.StatusCode, ready)
}
var meta model.Meta
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.Cache.Failures != 1 || meta.Cache.LastError == nil ||
!strings.Contains(*meta.Cache.LastError, "connection refused") {
t.Fatalf("cache status=%+v", meta.Cache)
}
if meta.Cache.LastSuccessAt == nil || meta.Cache.NextRetryAt == nil {
t.Fatalf("cache timestamps missing: %+v", meta.Cache)
}
// The context endpoint comes from the snapshot; it is now stale.
var ctx model.ContextResponse
getJSON(t, ts.URL+"/api/context", &ctx)
if !ctx.Cached {
t.Error("context cached=false after a failed refresh, want true")
}
// A later success clears the failure state.
fetcher.recover(loadFixtureBootstrap(t))
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("recovery refresh: %v", err)
}
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.Cache.Failures != 0 || meta.Cache.LastError != nil || meta.Cache.NextRetryAt != nil {
t.Fatalf("cache status after recovery=%+v", meta.Cache)
}
getJSON(t, ts.URL+"/api/context", &ctx)
if ctx.Cached {
t.Error("context cached=true after a successful refresh, want false")
}
}
func TestMetaSensorsAndVersionHeaders(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var meta model.Meta
resp := getJSON(t, ts.URL+"/api/meta", &meta)
if resp.StatusCode != http.StatusOK {
t.Fatalf("meta status=%d", resp.StatusCode)
}
if meta.Coverage.Sensors != 8 || meta.ModelVersion == "" || meta.ContextVersion == "" {
t.Fatalf("meta=%+v", meta)
}
if meta.Cache.Source != "fastapi" || meta.Cache.LastSuccessAt == nil ||
meta.Cache.LastError != nil || meta.Cache.NextRetryAt != nil || meta.Cache.Failures != 0 {
t.Fatalf("meta cache=%+v", meta.Cache)
}
for _, header := range []string{"X-Data-Version", "X-Model-Version", "X-Context-Version"} {
if resp.Header.Get(header) == "" {
t.Errorf("missing %s header", header)
}
}
var sensors []model.Sensor
resp = getJSON(t, ts.URL+"/api/sensors", &sensors)
if resp.StatusCode != http.StatusOK {
t.Fatalf("sensors status=%d", resp.StatusCode)
}
if len(sensors) != 8 {
t.Fatalf("got %d sensors, want 8", len(sensors))
}
for i := 1; i < len(sensors); i++ {
if sensors[i-1].ID >= sensors[i].ID {
t.Fatalf("sensors not sorted by id: %q then %q", sensors[i-1].ID, sensors[i].ID)
}
}
if sensors[0].ID != "hn-bahnhof-01" || sensors[0].Battery == nil {
t.Fatalf("unexpected first sensor: %+v", sensors[0])
}
}
func TestDataEndpointsFollowSnapshotRefresh(t *testing.T) {
snapshots, fetcher := newFixtureCache(t)
_, ts := newTestServerWithCache(t, snapshots, &stubML{}, nil)
var sensors []model.Sensor
getJSON(t, ts.URL+"/api/sensors", &sensors)
if len(sensors) != 8 {
t.Fatalf("sensors=%d, want 8", len(sensors))
}
updated := loadFixtureBootstrap(t)
updated.DataVersion = "2026-11-14T05:00:00Z"
updated.ContextVersion = "2026-11-14T05:00:00Z"
updated.Sensors = updated.Sensors[:1]
updated.Series = map[string][]model.Point{
updated.Sensors[0].ID: {{T: time.Date(2026, 11, 1, 10, 0, 0, 0, time.UTC), V: 42}},
}
fetcher.recover(updated)
if err := snapshots.Refresh(context.Background()); err != nil {
t.Fatalf("refresh: %v", err)
}
sensors = nil
resp := getJSON(t, ts.URL+"/api/sensors", &sensors)
if len(sensors) != 1 || sensors[0].ID != updated.Sensors[0].ID {
t.Fatalf("sensors after refresh=%+v", sensors)
}
if got := resp.Header.Get("X-Data-Version"); got != updated.DataVersion {
t.Errorf("X-Data-Version=%q, want %q", got, updated.DataVersion)
}
var series struct {
Points []model.Point `json:"points"`
}
getJSON(t, ts.URL+"/api/series?sensor="+updated.Sensors[0].ID+"&step=15m", &series)
if len(series.Points) != 1 || series.Points[0].V != 42 {
t.Fatalf("series after refresh=%+v", series.Points)
}
var ctx model.ContextResponse
getJSON(t, ts.URL+"/api/context", &ctx)
if ctx.UpdatedAt.IsZero() || ctx.Cached {
t.Fatalf("context after refresh=%+v", ctx)
}
var meta model.Meta
getJSON(t, ts.URL+"/api/meta", &meta)
if meta.DataVersion != updated.DataVersion || meta.Cache.Failures != 0 {
t.Fatalf("meta after refresh=%+v", meta)
}
}
func TestSeriesRangeStepAndErrors(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var body struct {
Sensor string `json:"sensor"`
Step string `json:"step"`
Points []model.Point `json:"points"`
}
resp := getJSON(t, ts.URL+"/api/series?sensor=hn-bahnhof-01&from=2026-11-01T00:00:00Z&to=2026-11-01T23:59:59Z&step=15m", &body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("series 15m status=%d", resp.StatusCode)
}
if body.Step != "15m" || len(body.Points) != 96 {
t.Fatalf("series 15m step=%q points=%d, want 96", body.Step, len(body.Points))
}
body.Points = nil
resp = getJSON(t, ts.URL+"/api/series?sensor=hn-bahnhof-01&from=2026-11-01T00:00:00Z&to=2026-11-01T23:59:59Z&step=1h", &body)
if resp.StatusCode != http.StatusOK || len(body.Points) != 24 {
t.Fatalf("series 1h status=%d points=%d, want 24", resp.StatusCode, len(body.Points))
}
cases := []struct {
path string
status int
}{
{"/api/series?sensor=does-not-exist", http.StatusNotFound},
{"/api/series?sensor=hn-bahnhof-01&step=5m", http.StatusBadRequest},
{"/api/series", http.StatusBadRequest},
{"/api/series?sensor=hn-bahnhof-01&from=not-a-time", http.StatusBadRequest},
{"/api/series?sensor=hn-bahnhof-01&from=2026-11-02T00:00:00Z&to=2026-11-01T00:00:00Z", http.StatusBadRequest},
}
for _, tc := range cases {
var errBody model.ErrorBody
resp := getJSON(t, ts.URL+tc.path, &errBody)
if resp.StatusCode != tc.status {
t.Errorf("%s: status=%d, want %d", tc.path, resp.StatusCode, tc.status)
}
if errBody.Error.Code == "" {
t.Errorf("%s: missing error envelope: %+v", tc.path, errBody)
}
}
}
func TestContextEndpoint(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var ctx model.ContextResponse
resp := getJSON(t, ts.URL+"/api/context", &ctx)
if resp.StatusCode != http.StatusOK {
t.Fatalf("context status=%d", resp.StatusCode)
}
if len(ctx.Weather.Forecast) != 168 {
t.Errorf("weather forecast points=%d, want 168", len(ctx.Weather.Forecast))
}
if len(ctx.Holidays) == 0 {
t.Error("expected holidays")
}
if ctx.UpdatedAt.IsZero() {
t.Error("expected updated_at")
}
if ctx.Cached {
t.Error("cached=true while the last snapshot refresh succeeded, want false")
}
}
func TestForecastServesLastGoodWhenMLIsDown(t *testing.T) {
live := forecastPoints(168)
var down bool
var mu sync.Mutex
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
mu.Lock()
defer mu.Unlock()
if down {
return nil, errors.New("connection refused")
}
return live, nil
}}
_, ts := newTestServer(t, client, nil)
var forecast model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &forecast)
if resp.StatusCode != http.StatusOK {
t.Fatalf("live forecast status=%d", resp.StatusCode)
}
if forecast.Cached || len(forecast.Points) != 168 {
t.Fatalf("live forecast cached=%v points=%d, want false/168", forecast.Cached, len(forecast.Points))
}
mu.Lock()
down = true
mu.Unlock()
var fallback model.ForecastResponse
resp = getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &fallback)
if resp.StatusCode != http.StatusOK {
t.Fatalf("fallback status=%d", resp.StatusCode)
}
if !fallback.Cached {
t.Error("cached=false, want true when FastAPI is down")
}
if len(fallback.Points) != 168 || fallback.Points[0].T != forecast.Points[0].T ||
fallback.Points[0].V != forecast.Points[0].V {
t.Fatalf("fallback points do not match the last good forecast: %+v", fallback.Points[0])
}
if fallback.Scenario != "base" || fallback.DataVersion == "" || fallback.ModelVersion == "" {
t.Fatalf("forecast=%+v", fallback)
}
}
func TestForecastWithoutLastGoodFails503(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var errBody model.ErrorBody
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=7d", &errBody)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status=%d, want 503", resp.StatusCode)
}
if errBody.Error.Code != "forecast_unavailable" {
t.Fatalf("error=%+v", errBody)
}
}
func TestForecastTimeoutServesLastGood(t *testing.T) {
live := forecastPoints(48)
var mu sync.Mutex
slow := false
client := &stubML{predict: func(ctx context.Context, _ ml.PredictRequest) ([]model.ForecastPoint, error) {
mu.Lock()
block := slow
mu.Unlock()
if block {
<-ctx.Done()
return nil, ctx.Err()
}
return live, nil
}}
_, ts := newTestServer(t, client, func(cfg *config.Config) { cfg.MLTimeout = 20 * time.Millisecond })
var primed model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-stadtgalerie-01&horizon=48h&scenario=sunny-saturday-market", &primed)
if resp.StatusCode != http.StatusOK || primed.Cached {
t.Fatalf("prime forecast status=%d cached=%v", resp.StatusCode, primed.Cached)
}
mu.Lock()
slow = true
mu.Unlock()
start := time.Now()
var forecast model.ForecastResponse
resp = getJSON(t, ts.URL+"/api/forecast?sensor=hn-stadtgalerie-01&horizon=48h&scenario=sunny-saturday-market", &forecast)
if resp.StatusCode != http.StatusOK {
t.Fatalf("forecast status=%d", resp.StatusCode)
}
if !forecast.Cached || len(forecast.Points) != 48 {
t.Fatalf("cached=%v points=%d, want cached 48 points", forecast.Cached, len(forecast.Points))
}
if elapsed := time.Since(start); elapsed > 2*time.Second {
t.Fatalf("fallback took %s, want <2s", elapsed)
}
}
func TestForecastLivePath(t *testing.T) {
point := model.ForecastPoint{T: time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC), V: 42, Lo: 40, Hi: 44}
var gotReq ml.PredictRequest
client := &stubML{predict: func(_ context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error) {
gotReq = req
return []model.ForecastPoint{point}, nil
}}
_, ts := newTestServer(t, client, nil)
var forecast model.ForecastResponse
resp := getJSON(t, ts.URL+"/api/forecast?sensor=hn-bahnhof-01&horizon=1h&scenario=rainy-saturday", &forecast)
if resp.StatusCode != http.StatusOK {
t.Fatalf("forecast status=%d", resp.StatusCode)
}
if forecast.Cached {
t.Error("cached=true, want false for a live prediction")
}
if len(forecast.Points) != 1 || forecast.Points[0].V != 42 {
t.Fatalf("points=%+v", forecast.Points)
}
if gotReq.Sensor != "hn-bahnhof-01" || gotReq.Horizon != "1h" || gotReq.Scenario != "rainy-saturday" {
t.Fatalf("predict request=%+v", gotReq)
}
}
func TestForecastValidation(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
for _, tc := range []struct {
path string
status int
}{
{"/api/forecast", http.StatusBadRequest},
{"/api/forecast?sensor=nope", http.StatusNotFound},
{"/api/forecast?sensor=hn-bahnhof-01&horizon=2h", http.StatusBadRequest},
{"/api/forecast?sensor=hn-bahnhof-01&scenario=party", http.StatusBadRequest},
} {
resp := getJSON(t, ts.URL+tc.path, nil)
if resp.StatusCode != tc.status {
t.Errorf("%s: status=%d, want %d", tc.path, resp.StatusCode, tc.status)
}
}
}
func TestStaffingPlan(t *testing.T) {
points := weekForecast(t)
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
return points, nil
}}
_, ts := newTestServer(t, client, nil)
start := time.Now()
var plan model.StaffingResponse
resp := getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=standard", &plan)
if resp.StatusCode != http.StatusOK {
t.Fatalf("staffing status=%d", resp.StatusCode)
}
if elapsed := time.Since(start); elapsed > 2*time.Second {
t.Errorf("plan took %s, want <2s", elapsed)
}
if plan.Assumptions.VisitorsPerStaffHour != 25 || plan.Assumptions.ShiftLengthH != 4 {
t.Fatalf("assumptions=%+v", plan.Assumptions)
}
if len(plan.Hours) != 70 {
t.Errorf("hours=%d, want 70 (7 days x 10 store hours)", len(plan.Hours))
}
if len(plan.Shifts) != 21 {
t.Errorf("shifts=%d, want 21 (3 blocks x 7 days)", len(plan.Shifts))
}
if plan.Summary.CoveragePct != 100 {
t.Errorf("coverage=%v, want 100", plan.Summary.CoveragePct)
}
if plan.Summary.CostEUR != 2240 || plan.Summary.FlatScheduleCostEUR != 2240 {
t.Errorf("summary=%+v", plan.Summary)
}
if plan.Cached {
t.Error("cached=true, want false for a live plan")
}
if len(plan.Why) == 0 {
t.Error("expected a non-empty why list")
}
for _, hour := range plan.Hours {
if hour.Coverage != "ok" && hour.Coverage != "over" && hour.Coverage != "under" {
t.Fatalf("invalid coverage label %q", hour.Coverage)
}
}
}
func TestStaffingFallsBackToLastGoodForecast(t *testing.T) {
points := weekForecast(t)
var mu sync.Mutex
down := false
client := &stubML{predict: func(context.Context, ml.PredictRequest) ([]model.ForecastPoint, error) {
mu.Lock()
defer mu.Unlock()
if down {
return nil, errors.New("connection refused")
}
return points, nil
}}
_, ts := newTestServer(t, client, nil)
var live model.StaffingResponse
resp := getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=standard", &live)
if resp.StatusCode != http.StatusOK || live.Cached {
t.Fatalf("live staffing status=%d cached=%v", resp.StatusCode, live.Cached)
}
mu.Lock()
down = true
mu.Unlock()
// Different standard -> different plan key, so the handler must rebuild
// from the last good 7d forecast.
var fallback model.StaffingResponse
resp = getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=premium", &fallback)
if resp.StatusCode != http.StatusOK {
t.Fatalf("fallback staffing status=%d", resp.StatusCode)
}
if !fallback.Cached {
t.Error("cached=false, want true when built from the last good forecast")
}
if len(fallback.Hours) != 70 || len(fallback.Shifts) != 21 {
t.Fatalf("fallback plan hours=%d shifts=%d", len(fallback.Hours), len(fallback.Shifts))
}
}
func TestStaffingUnavailableWithoutForecast(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
var errBody model.ErrorBody
resp := getJSON(t, ts.URL+"/api/staffing?sensor=hn-bahnhof-01&scenario=base&standard=standard", &errBody)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status=%d, want 503", resp.StatusCode)
}
if errBody.Error.Code != "staffing_unavailable" {
t.Fatalf("error=%+v", errBody)
}
}
func TestStaffingValidation(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
for _, tc := range []struct {
path string
status int
}{
{"/api/staffing", http.StatusBadRequest},
{"/api/staffing?sensor=nope", http.StatusNotFound},
{"/api/staffing?sensor=hn-bahnhof-01&standard=luxury", http.StatusBadRequest},
{"/api/staffing?sensor=hn-bahnhof-01&scenario=party", http.StatusBadRequest},
} {
resp := getJSON(t, ts.URL+tc.path, nil)
if resp.StatusCode != tc.status {
t.Errorf("%s: status=%d, want %d", tc.path, resp.StatusCode, tc.status)
}
}
}
func TestScenarioAuthAndValidation(t *testing.T) {
client := &stubML{}
_, ts := newTestServer(t, client, func(cfg *config.Config) {
cfg.ControlUser = "control"
cfg.ControlPass = "secret"
})
post := func(body string, user, pass string) *http.Response {
req, err := http.NewRequest(http.MethodPost, ts.URL+"/api/scenario", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
if user != "" {
req.SetBasicAuth(user, pass)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return resp
}
valid := `{"scenario":"sunny-saturday-market","weather":{"temp_delta_c":4,"precip_mm":0},"event":{"type":"market","uplift":0.2}}`
if resp := post(valid, "", ""); resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("no auth status=%d, want 401", resp.StatusCode)
} else if resp.Header.Get("WWW-Authenticate") == "" {
t.Error("missing WWW-Authenticate header")
}
if resp := post(valid, "control", "wrong"); resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("bad password status=%d, want 401", resp.StatusCode)
}
if resp := post(`{"scenario":"nope"}`, "control", "secret"); resp.StatusCode != http.StatusBadRequest {
t.Fatalf("bad enum status=%d, want 400", resp.StatusCode)
}
if resp := post(`{"scenario":"base","weather":{"precip_mm":-1}}`, "control", "secret"); resp.StatusCode != http.StatusBadRequest {
t.Fatalf("negative precip status=%d, want 400", resp.StatusCode)
}
if resp := post(`{`, "control", "secret"); resp.StatusCode != http.StatusBadRequest {
t.Fatalf("malformed JSON status=%d, want 400", resp.StatusCode)
}
resp := post(valid, "control", "secret")
if resp.StatusCode != http.StatusOK {
t.Fatalf("valid POST status=%d, want 200", resp.StatusCode)
}
var state model.ScenarioState
getJSON(t, ts.URL+"/api/scenario", &state)
if state.Version != 2 || state.Scenario.Scenario != "sunny-saturday-market" {
t.Fatalf("scenario state=%+v", state)
}
if state.Scenario.Weather.TempDeltaC != 4 || state.Scenario.Event.Uplift != 0.2 {
t.Fatalf("scenario overrides lost: %+v", state.Scenario)
}
}
func TestScenarioAuthUnconfigured(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
resp, err := http.Post(ts.URL+"/api/scenario", "application/json", strings.NewReader(`{"scenario":"base"}`))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("status=%d, want 503 when control auth is unset", resp.StatusCode)
}
}
func TestScenarioBroadcastOverSSE(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, func(cfg *config.Config) {
cfg.ControlUser = "control"
cfg.ControlPass = "secret"
cfg.Heartbeat = 20 * time.Millisecond
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("stream status=%d", resp.StatusCode)
}
if got := resp.Header.Get("Content-Type"); !strings.HasPrefix(got, "text/event-stream") {
t.Fatalf("Content-Type=%q", got)
}
if resp.Header.Get("X-Accel-Buffering") != "no" {
t.Errorf("X-Accel-Buffering=%q", resp.Header.Get("X-Accel-Buffering"))
}
reader := bufio.NewReader(resp.Body)
event := readEvent(t, reader)
if event.name != "scenario" || event.id != "1" {
t.Fatalf("initial event=%+v, want scenario id 1", event)
}
body := `{"scenario":"rainy-saturday"}`
postReq, err := http.NewRequest(http.MethodPost, ts.URL+"/api/scenario", bytes.NewBufferString(body))
if err != nil {
t.Fatal(err)
}
postReq.SetBasicAuth("control", "secret")
postReq.Header.Set("Content-Type", "application/json")
postResp, err := http.DefaultClient.Do(postReq)
if err != nil {
t.Fatal(err)
}
postResp.Body.Close()
if postResp.StatusCode != http.StatusOK {
t.Fatalf("scenario POST status=%d", postResp.StatusCode)
}
deadline := time.Now().Add(3 * time.Second)
for {
if time.Now().After(deadline) {
t.Fatal("timed out waiting for scenario event")
}
event := readEvent(t, reader)
if event.name == "scenario" {
if event.id != "2" {
t.Fatalf("scenario event id=%q, want 2", event.id)
}
var state model.ScenarioState
if err := json.Unmarshal([]byte(event.data), &state); err != nil {
t.Fatalf("decode event data: %v", err)
}
if state.Scenario.Scenario != "rainy-saturday" || state.Version != 2 {
t.Fatalf("event state=%+v", state)
}
break
}
if event.name != "ping" {
t.Fatalf("unexpected event=%+v", event)
}
}
}
type sseFrame struct {
name string
id string
data string
}
func readEvent(t *testing.T, reader *bufio.Reader) sseFrame {
t.Helper()
var frame sseFrame
for {
line, err := reader.ReadString('\n')
if err != nil {
t.Fatalf("read SSE: %v", err)
}
line = strings.TrimSuffix(strings.TrimSuffix(line, "\n"), "\r")
if line == "" {
if frame.name != "" || frame.data != "" {
return frame
}
continue
}
switch {
case strings.HasPrefix(line, "event: "):
frame.name = strings.TrimPrefix(line, "event: ")
case strings.HasPrefix(line, "id: "):
frame.id = strings.TrimPrefix(line, "id: ")
case strings.HasPrefix(line, "data: "):
frame.data = strings.TrimPrefix(line, "data: ")
}
}
}
func TestRoutingCORNAndErrors(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, nil)
req, _ := http.NewRequest(http.MethodOptions, ts.URL+"/api/scenario", nil)
req.Header.Set("Origin", "https://pulse.noelsabia.dev")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatalf("OPTIONS status=%d, want 204", resp.StatusCode)
}
if resp.Header.Get("Access-Control-Allow-Origin") != "https://pulse.noelsabia.dev" {
t.Errorf("missing CORS origin header: %v", resp.Header)
}
var errBody model.ErrorBody
resp = getJSON(t, ts.URL+"/api/nope", &errBody)
if resp.StatusCode != http.StatusNotFound || errBody.Error.Code != "not_found" {
t.Fatalf("unknown route status=%d body=%+v", resp.StatusCode, errBody)
}
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
t.Errorf("unknown route Content-Type=%q", ct)
}
req, _ = http.NewRequest(http.MethodPost, ts.URL+"/api/meta", strings.NewReader("{}"))
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("POST /api/meta status=%d, want 405 (body=%s)", resp.StatusCode, body)
}
if allow := resp.Header.Get("Allow"); allow != "GET" {
t.Errorf("Allow=%q, want GET", allow)
}
}
func TestSSEConcurrentClients(t *testing.T) {
srv, _ := newTestServer(t, &stubML{}, func(cfg *config.Config) { cfg.Heartbeat = time.Hour })
const clients = 120
channels := make([]<-chan sse.Event, clients)
cancels := make([]func(), clients)
for i := 0; i < clients; i++ {
channels[i], cancels[i] = srv.hub.Subscribe()
}
defer func() {
for _, cancel := range cancels {
cancel()
}
}()
if got := srv.hub.Clients(); got != clients {
t.Fatalf("clients=%d, want %d", got, clients)
}
srv.hub.Broadcast(sse.Event{Name: "scenario", ID: 7, Data: []byte(fmt.Sprintf(`{"version":%d}`, 7))})
for i, ch := range channels {
select {
case event := <-ch:
if event.Name != "scenario" || event.ID != 7 {
t.Fatalf("client %d event=%+v", i, event)
}
case <-time.After(2 * time.Second):
t.Fatalf("client %d did not receive the broadcast", i)
}
}
}