feat: API skeleton, SSE, staffing and tests

This commit is contained in:
Noel Sabia committed 2026-11-14 04:23:43 +01:00
1 parent 907164f972
commit f284969a4b
14 files changed
+1355 -54

No files matched your search

+615
View File
@@ -0,0 +1,615 @@
package api
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/model"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/store"
)
const fixtureDir = "../../testdata/data"
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)
}
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 loadFixtureStore(t *testing.T) *store.Store {
t.Helper()
st, err := store.Load(fixtureDir)
if err != nil {
t.Fatalf("load fixtures: %v", err)
}
return st
}
func newTestServer(t *testing.T, client MLClient, mutate func(*config.Config)) (*Server, *httptest.Server) {
t.Helper()
cfg := config.Config{
Addr: ":0",
DataDir: fixtureDir,
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, loadFixtureStore(t), client, hub)
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
return srv, ts
}
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 TestReadyzWithoutData(t *testing.T) {
cfg := config.Config{Addr: ":0", DataDir: t.TempDir(), FastAPIURL: "http://127.0.0.1:1", Heartbeat: time.Hour, MLTimeout: 50 * time.Millisecond}
st, err := store.Load(cfg.DataDir)
if err != nil {
t.Logf("expected load error: %v", err)
}
srv := New(cfg, st, &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)
}
}
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)
}
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 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")
}
}
func TestForecastFallsBackWhenMLIsDown(t *testing.T) {
_, ts := newTestServer(t, &stubML{}, 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("forecast status=%d", resp.StatusCode)
}
if !forecast.Cached {
t.Error("cached=false, want true when FastAPI is down")
}
if len(forecast.Points) != 168 {
t.Fatalf("points=%d, want 168", len(forecast.Points))
}
if forecast.Scenario != "base" || forecast.DataVersion == "" || forecast.ModelVersion == "" {
t.Fatalf("forecast=%+v", forecast)
}
}
func TestForecastTimeoutFallsBack(t *testing.T) {
client := &stubML{predict: func(ctx context.Context, _ ml.PredictRequest) ([]model.ForecastPoint, error) {
<-ctx.Done()
return nil, ctx.Err()
}}
_, ts := newTestServer(t, client, func(cfg *config.Config) { cfg.MLTimeout = 20 * time.Millisecond })
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) {
_, ts := newTestServer(t, &stubML{}, 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 <= 0 || plan.Summary.FlatScheduleCostEUR <= 0 {
t.Errorf("summary=%+v", plan.Summary)
}
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 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)
}
}
}
+5 -6
View File
@@ -15,7 +15,6 @@ import (
"pulsecast/api/internal/model"
"pulsecast/api/internal/scenario"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/staffing"
"pulsecast/api/internal/store"
)
@@ -36,11 +35,11 @@ type Server struct {
loc *time.Location
now func() time.Time
mu sync.RWMutex
sim map[string]map[string][]model.ForecastPoint // sensor -> horizon -> points
simFor string // scenario the sim belongs to
simGen int // invalidates staffing cache
staffing map[string]model.StaffingResponse
mu sync.RWMutex
sim map[string]map[string][]model.ForecastPoint // sensor -> horizon -> points
simFor string // scenario the sim belongs to
simGen int // invalidates staffing cache
staffing map[string]model.StaffingResponse
}
// New builds a server. hub must be non-nil; main runs hub.Run.