feat: API skeleton, SSE, staffing and tests
This commit is contained in:
1 parent
907164f972
commit
f284969a4b
14 files changed
+1355
-54
No files matched your search
@@ -0,0 +1,4 @@
|
||||
.git
|
||||
.gitignore
|
||||
*.md
|
||||
opencode.json
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM golang:1.27-alpine AS build
|
||||
WORKDIR /src
|
||||
|
||||
# Dependencies are only the standard library today; the explicit copy keeps
|
||||
# the module cache layer stable when that changes.
|
||||
COPY go.mod ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath -ldflags="-s -w" \
|
||||
-o /out/api ./cmd/api
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=build /out/api /app/api
|
||||
# Baked fallback data lives in the image; there is no runtime data fetch.
|
||||
COPY --from=build /src/testdata/data /app/data
|
||||
|
||||
ENV DATA_DIR=/app/data \
|
||||
PORT=8080
|
||||
|
||||
EXPOSE 8080
|
||||
USER nonroot:nonroot
|
||||
|
||||
ENTRYPOINT ["/app/api"]
|
||||
@@ -0,0 +1,66 @@
|
||||
// Command api serves the PulseCast Go API, SSE stream and cached fallback.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"pulsecast/api/internal/api"
|
||||
"pulsecast/api/internal/config"
|
||||
"pulsecast/api/internal/ml"
|
||||
"pulsecast/api/internal/sse"
|
||||
"pulsecast/api/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.Load()
|
||||
|
||||
// A partial dataset is not fatal: the process starts and /api/readyz reports
|
||||
// 503 until the baked artifacts are complete, instead of crash-looping.
|
||||
st, err := store.Load(cfg.DataDir)
|
||||
if err != nil {
|
||||
slog.Error("baked data is incomplete", "dir", cfg.DataDir, "error", err)
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
hub := sse.NewHub(cfg.Heartbeat)
|
||||
go hub.Run(ctx)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: cfg.Addr,
|
||||
Handler: api.New(cfg, st, ml.New(cfg.FastAPIURL, cfg.MLTimeout), hub).Handler(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
serveErr := make(chan error, 1)
|
||||
go func() {
|
||||
slog.Info("api listening", "addr", cfg.Addr, "data_dir", cfg.DataDir, "fastapi", cfg.FastAPIURL)
|
||||
serveErr <- server.ListenAndServe()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
slog.Info("shutting down")
|
||||
case err := <-serveErr:
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("server stopped", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
slog.Error("graceful shutdown failed", "error", err)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -9,13 +9,13 @@ import (
|
||||
|
||||
// Config is the process configuration.
|
||||
type Config struct {
|
||||
Addr string
|
||||
DataDir string
|
||||
FastAPIURL string
|
||||
Addr string
|
||||
DataDir string
|
||||
FastAPIURL string
|
||||
ControlUser string
|
||||
ControlPass string
|
||||
Heartbeat time.Duration
|
||||
MLTimeout time.Duration
|
||||
Heartbeat time.Duration
|
||||
MLTimeout time.Duration
|
||||
}
|
||||
|
||||
// Load reads configuration from env with local-development defaults.
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadDefaults(t *testing.T) {
|
||||
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "SSE_HEARTBEAT", "ML_TIMEOUT"} {
|
||||
t.Setenv(key, "")
|
||||
}
|
||||
cfg := Load()
|
||||
if cfg.Addr != ":8080" {
|
||||
t.Errorf("Addr=%q", cfg.Addr)
|
||||
}
|
||||
if cfg.DataDir != "testdata/data" {
|
||||
t.Errorf("DataDir=%q", cfg.DataDir)
|
||||
}
|
||||
if cfg.FastAPIURL != "http://localhost:8000" {
|
||||
t.Errorf("FastAPIURL=%q", cfg.FastAPIURL)
|
||||
}
|
||||
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
|
||||
t.Errorf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
if cfg.ControlAuthConfigured() {
|
||||
t.Error("control auth should be unconfigured by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromEnv(t *testing.T) {
|
||||
t.Setenv("PORT", "9999")
|
||||
t.Setenv("DATA_DIR", "/data")
|
||||
t.Setenv("FASTAPI_URL", "http://ml:8000")
|
||||
t.Setenv("CONTROL_USER", "user")
|
||||
t.Setenv("CONTROL_PASS", "pass")
|
||||
t.Setenv("SSE_HEARTBEAT", "5s")
|
||||
t.Setenv("ML_TIMEOUT", "2s")
|
||||
|
||||
cfg := Load()
|
||||
if cfg.Addr != ":9999" || cfg.DataDir != "/data" || cfg.FastAPIURL != "http://ml:8000" {
|
||||
t.Fatalf("cfg=%+v", cfg)
|
||||
}
|
||||
if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
if !cfg.ControlAuthConfigured() {
|
||||
t.Fatal("control auth should be configured")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationAcceptsPlainSeconds(t *testing.T) {
|
||||
t.Setenv("SSE_HEARTBEAT", "7")
|
||||
t.Setenv("ML_TIMEOUT", "3")
|
||||
cfg := Load()
|
||||
if cfg.Heartbeat != 7*time.Second || cfg.MLTimeout != 3*time.Second {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationFallsBackOnInvalidValue(t *testing.T) {
|
||||
t.Setenv("SSE_HEARTBEAT", "soon")
|
||||
t.Setenv("ML_TIMEOUT", "-1s")
|
||||
cfg := Load()
|
||||
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
|
||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||
}
|
||||
}
|
||||
+40
-24
@@ -52,39 +52,43 @@ type WeatherInput struct {
|
||||
|
||||
// SimulateRequest mirrors CONTRACT.md §3 POST /simulate.
|
||||
type SimulateRequest struct {
|
||||
Scenario string `json:"scenario"`
|
||||
Scenario string `json:"scenario"`
|
||||
Weather model.WeatherInput `json:"weather"`
|
||||
Sensors string `json:"sensors"`
|
||||
Sensors string `json:"sensors"`
|
||||
}
|
||||
|
||||
// Predict calls POST /predict and returns the forecast points.
|
||||
// Predict calls POST /predict and returns the forecast points. The contract
|
||||
// response is {"points":[...]}; a bare array is accepted as well.
|
||||
func (c *Client) Predict(ctx context.Context, req PredictRequest) ([]model.ForecastPoint, error) {
|
||||
raw, err := c.postRaw(ctx, "/predict", req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out struct {
|
||||
Points []model.ForecastPoint `json:"points"`
|
||||
}
|
||||
if err := c.post(ctx, "/predict", req, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out.Points == nil {
|
||||
// The contract says "the same points array"; accept a bare array too.
|
||||
var points []model.ForecastPoint
|
||||
if err := c.post(ctx, "/predict", req, &points); err == nil && len(points) > 0 {
|
||||
out.Points = points
|
||||
if err := json.Unmarshal(raw, &out); err == nil && out.Points != nil {
|
||||
if len(out.Points) == 0 {
|
||||
return nil, fmt.Errorf("ml: predict returned no points")
|
||||
}
|
||||
return out.Points, nil
|
||||
}
|
||||
if len(out.Points) == 0 {
|
||||
return nil, fmt.Errorf("ml: predict returned no points")
|
||||
|
||||
var points []model.ForecastPoint
|
||||
if err := json.Unmarshal(raw, &points); err == nil && len(points) > 0 {
|
||||
return points, nil
|
||||
}
|
||||
return out.Points, nil
|
||||
return nil, fmt.Errorf("ml: predict returned no points")
|
||||
}
|
||||
|
||||
// Simulate calls POST /simulate and returns forecasts keyed
|
||||
// sensor -> horizon -> points.
|
||||
func (c *Client) Simulate(ctx context.Context, req SimulateRequest) (map[string]map[string][]model.ForecastPoint, error) {
|
||||
var out struct {
|
||||
Scenario string `json:"scenario"`
|
||||
Generated string `json:"generated_at"`
|
||||
Forecasts map[string]map[string][]model.ForecastPoint `json:"forecasts"`
|
||||
Scenario string `json:"scenario"`
|
||||
Generated string `json:"generated_at"`
|
||||
Forecasts map[string]map[string][]model.ForecastPoint `json:"forecasts"`
|
||||
}
|
||||
if err := c.post(ctx, "/simulate", req, &out); err != nil {
|
||||
return nil, err
|
||||
@@ -96,29 +100,41 @@ func (c *Client) Simulate(ctx context.Context, req SimulateRequest) (map[string]
|
||||
}
|
||||
|
||||
func (c *Client) post(ctx context.Context, path string, body, dst any) error {
|
||||
raw, err := c.postRaw(ctx, path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal(raw, dst); err != nil {
|
||||
return fmt.Errorf("ml: decode %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) postRaw(ctx context.Context, path string, body any) ([]byte, error) {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ml: encode request: %w", err)
|
||||
return nil, fmt.Errorf("ml: encode request: %w", err)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+path, bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return fmt.Errorf("ml: build request: %w", err)
|
||||
return nil, fmt.Errorf("ml: build request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ml: %s: %w", path, err)
|
||||
return nil, fmt.Errorf("ml: %s: %w", path, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
||||
return fmt.Errorf("ml: %s: status %d: %s", path, resp.StatusCode, strings.TrimSpace(string(snippet)))
|
||||
return nil, fmt.Errorf("ml: %s: status %d: %s", path, resp.StatusCode, strings.TrimSpace(string(snippet)))
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(resp.Body, 32<<20)).Decode(dst); err != nil {
|
||||
return fmt.Errorf("ml: decode %s: %w", path, err)
|
||||
raw, err = io.ReadAll(io.LimitReader(resp.Body, 32<<20))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ml: read %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
return raw, nil
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package ml
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPredictObjectResponseAndRequest(t *testing.T) {
|
||||
var got PredictRequest
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/predict" {
|
||||
t.Errorf("path=%q", r.URL.Path)
|
||||
}
|
||||
if r.Header.Get("Content-Type") != "application/json" {
|
||||
t.Errorf("Content-Type=%q", r.Header.Get("Content-Type"))
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
|
||||
t.Errorf("decode request: %v", err)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"points":[{"t":"2026-11-16T09:00:00Z","v":12.3,"lo":8.1,"hi":17.2}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
client := New(srv.URL+"/", time.Second)
|
||||
points, err := client.Predict(context.Background(), PredictRequest{
|
||||
Sensor: "hn-bahnhof-01", Horizon: "1h", Scenario: "base",
|
||||
Weather: WeatherInput{TempC: 9, PrecipMM: 0}, IsHoliday: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Predict: %v", err)
|
||||
}
|
||||
if len(points) != 1 || points[0].V != 12.3 || points[0].Lo != 8.1 {
|
||||
t.Fatalf("points=%+v", points)
|
||||
}
|
||||
if got.Sensor != "hn-bahnhof-01" || got.Horizon != "1h" || got.Weather.TempC != 9 {
|
||||
t.Fatalf("request=%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPredictBareArrayResponse(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`[{"t":"2026-11-16T09:00:00Z","v":1,"lo":0.5,"hi":1.5}]`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
points, err := New(srv.URL, time.Second).Predict(context.Background(), PredictRequest{})
|
||||
if err != nil {
|
||||
t.Fatalf("Predict: %v", err)
|
||||
}
|
||||
if len(points) != 1 || points[0].V != 1 {
|
||||
t.Fatalf("points=%+v", points)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPredictRejectsEmptyAndErrorStatus(t *testing.T) {
|
||||
empty := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"points":[]}`))
|
||||
}))
|
||||
defer empty.Close()
|
||||
if _, err := New(empty.URL, time.Second).Predict(context.Background(), PredictRequest{}); err == nil {
|
||||
t.Fatal("expected an error for an empty points array")
|
||||
}
|
||||
|
||||
boom := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, "nope", http.StatusInternalServerError)
|
||||
}))
|
||||
defer boom.Close()
|
||||
if _, err := New(boom.URL, time.Second).Predict(context.Background(), PredictRequest{}); err == nil {
|
||||
t.Fatal("expected an error for HTTP 500")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPredictTimeout(t *testing.T) {
|
||||
slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
_, _ = w.Write([]byte(`{"points":[{"t":"2026-11-16T09:00:00Z","v":1,"lo":0,"hi":2}]}`))
|
||||
}))
|
||||
defer slow.Close()
|
||||
|
||||
client := New(slow.URL, 20*time.Millisecond)
|
||||
start := time.Now()
|
||||
if _, err := client.Predict(context.Background(), PredictRequest{}); err == nil {
|
||||
t.Fatal("expected a timeout error")
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("timeout took %s", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimulate(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req SimulateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
t.Errorf("decode: %v", err)
|
||||
}
|
||||
if req.Scenario != "rainy-saturday" || req.Sensors != "all" {
|
||||
t.Errorf("request=%+v", req)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"scenario":"rainy-saturday","generated_at":"2026-11-14T09:12:00Z","forecasts":{"hn-bahnhof-01":{"7d":[{"t":"2026-11-16T09:00:00Z","v":5,"lo":4,"hi":6}]}}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
forecasts, err := New(srv.URL, time.Second).Simulate(context.Background(), SimulateRequest{
|
||||
Scenario: "rainy-saturday", Sensors: "all",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Simulate: %v", err)
|
||||
}
|
||||
points := forecasts["hn-bahnhof-01"]["7d"]
|
||||
if len(points) != 1 || points[0].V != 5 {
|
||||
t.Fatalf("forecasts=%+v", forecasts)
|
||||
}
|
||||
if _, ok := forecasts["missing"]; ok {
|
||||
t.Fatal("unexpected sensor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUsesDefaultTimeout(t *testing.T) {
|
||||
client := New("http://example.invalid", 0)
|
||||
if client.http.Timeout != DefaultTimeout {
|
||||
t.Fatalf("timeout=%s, want %s", client.http.Timeout, DefaultTimeout)
|
||||
}
|
||||
}
|
||||
+19
-19
@@ -6,9 +6,9 @@ import "time"
|
||||
|
||||
// Horizons and enums from CONTRACT.md §1.
|
||||
var (
|
||||
Horizons = []string{"15m", "1h", "48h", "7d"}
|
||||
Scenarios = []string{"base", "sunny-saturday-market", "rainy-saturday", "weekday-event"}
|
||||
Standards = []string{"relaxed", "standard", "premium"}
|
||||
Horizons = []string{"15m", "1h", "48h", "7d"}
|
||||
Scenarios = []string{"base", "sunny-saturday-market", "rainy-saturday", "weekday-event"}
|
||||
Standards = []string{"relaxed", "standard", "premium"}
|
||||
)
|
||||
|
||||
// Contains reports whether v is in list.
|
||||
@@ -56,12 +56,12 @@ type Coverage struct {
|
||||
|
||||
// Metrics is the model card headline block exposed by GET /api/meta.
|
||||
type Metrics struct {
|
||||
Model string `json:"model"`
|
||||
MAE float64 `json:"mae"`
|
||||
WAPE float64 `json:"wape"`
|
||||
R2 float64 `json:"r2"`
|
||||
BaselineMAE float64 `json:"baseline_mae"`
|
||||
BaselineWAPE float64 `json:"baseline_wape"`
|
||||
Model string `json:"model"`
|
||||
MAE float64 `json:"mae"`
|
||||
WAPE float64 `json:"wape"`
|
||||
R2 float64 `json:"r2"`
|
||||
BaselineMAE float64 `json:"baseline_mae"`
|
||||
BaselineWAPE float64 `json:"baseline_wape"`
|
||||
}
|
||||
|
||||
// Meta is GET /api/meta.
|
||||
@@ -196,11 +196,11 @@ type StoreHoursSpec struct {
|
||||
|
||||
// StaffingHour is one hour of the plan.
|
||||
type StaffingHour struct {
|
||||
T time.Time `json:"t"`
|
||||
Footfall float64 `json:"footfall"`
|
||||
RequiredStaff int `json:"required_staff"`
|
||||
ScheduledStaff int `json:"scheduled_staff"`
|
||||
Coverage string `json:"coverage"`
|
||||
T time.Time `json:"t"`
|
||||
Footfall float64 `json:"footfall"`
|
||||
RequiredStaff int `json:"required_staff"`
|
||||
ScheduledStaff int `json:"scheduled_staff"`
|
||||
Coverage string `json:"coverage"`
|
||||
}
|
||||
|
||||
// Shift is one contiguous shift block.
|
||||
@@ -213,11 +213,11 @@ type Shift struct {
|
||||
|
||||
// StaffingSummary is the plan headline block.
|
||||
type StaffingSummary struct {
|
||||
CoveragePct float64 `json:"coverage_pct"`
|
||||
CostEUR int `json:"cost_eur"`
|
||||
FlatScheduleCostEUR int `json:"flat_schedule_cost_eur"`
|
||||
OverstaffedHours int `json:"overstaffed_hours"`
|
||||
UnderstaffedHours int `json:"understaffed_hours"`
|
||||
CoveragePct float64 `json:"coverage_pct"`
|
||||
CostEUR int `json:"cost_eur"`
|
||||
FlatScheduleCostEUR int `json:"flat_schedule_cost_eur"`
|
||||
OverstaffedHours int `json:"overstaffed_hours"`
|
||||
UnderstaffedHours int `json:"understaffed_hours"`
|
||||
}
|
||||
|
||||
// StaffingResponse is GET /api/staffing.
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package scenario
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"pulsecast/api/internal/model"
|
||||
)
|
||||
|
||||
func TestNewAndSet(t *testing.T) {
|
||||
state := New()
|
||||
initial := state.Get()
|
||||
if initial.Version != 1 || initial.Scenario.Scenario != "base" {
|
||||
t.Fatalf("initial state=%+v", initial)
|
||||
}
|
||||
if initial.At.IsZero() {
|
||||
t.Fatal("initial timestamp is zero")
|
||||
}
|
||||
|
||||
next := state.Set(model.ScenarioInput{
|
||||
Scenario: "sunny-saturday-market",
|
||||
Weather: model.WeatherInput{TempDeltaC: 4, PrecipMM: 0},
|
||||
Event: model.EventInput{Type: "market", Uplift: 0.2},
|
||||
})
|
||||
if next.Version != 2 || next.Scenario.Scenario != "sunny-saturday-market" {
|
||||
t.Fatalf("next state=%+v", next)
|
||||
}
|
||||
if got := state.Get(); got.Version != 2 || got.Scenario.Weather.TempDeltaC != 4 {
|
||||
t.Fatalf("Get after Set=%+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentSetProducesUniqueVersions(t *testing.T) {
|
||||
state := New()
|
||||
const writers = 50
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(writers)
|
||||
for i := 0; i < writers; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
state.Set(model.ScenarioInput{Scenario: "base"})
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if got := state.Get().Version; got != writers+1 {
|
||||
t.Fatalf("version=%d, want %d", got, writers+1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package sse
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBroadcastDeliversToAllAndUnsubscribe(t *testing.T) {
|
||||
hub := NewHub(time.Hour)
|
||||
first, cancelFirst := hub.Subscribe()
|
||||
second, cancelSecond := hub.Subscribe()
|
||||
defer cancelSecond()
|
||||
|
||||
if got := hub.Clients(); got != 2 {
|
||||
t.Fatalf("clients=%d, want 2", got)
|
||||
}
|
||||
|
||||
hub.Broadcast(Event{Name: "scenario", ID: 3, Data: []byte(`{"version":3}`)})
|
||||
for i, ch := range []<-chan Event{first, second} {
|
||||
select {
|
||||
case event := <-ch:
|
||||
if event.Name != "scenario" || event.ID != 3 || string(event.Data) != `{"version":3}` {
|
||||
t.Fatalf("client %d event=%+v", i, event)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("client %d timed out", i)
|
||||
}
|
||||
}
|
||||
|
||||
cancelFirst()
|
||||
if got := hub.Clients(); got != 1 {
|
||||
t.Fatalf("clients after unsubscribe=%d, want 1", got)
|
||||
}
|
||||
hub.Broadcast(Event{Name: "scenario", ID: 4})
|
||||
select {
|
||||
case event := <-second:
|
||||
if event.ID != 4 {
|
||||
t.Fatalf("second client event=%+v", event)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("second client timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlowClientDropsInsteadOfBlocking(t *testing.T) {
|
||||
hub := NewHub(time.Hour)
|
||||
slow, cancel := hub.Subscribe()
|
||||
defer cancel()
|
||||
|
||||
// A subscriber that never reads must not stall the broadcaster.
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
for i := 1; i <= 20; i++ {
|
||||
hub.Broadcast(Event{Name: "scenario", ID: i})
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Broadcast blocked on a slow client")
|
||||
}
|
||||
|
||||
// The channel buffer is 16, so the first 16 events survive and the rest
|
||||
// are dropped. State is versioned and re-readable, so a drop is safe.
|
||||
for i := 1; i <= 16; i++ {
|
||||
select {
|
||||
case event := <-slow:
|
||||
if event.ID != i {
|
||||
t.Fatalf("buffered event %d has id=%d", i, event.ID)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("expected event %d to be buffered", i)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case event := <-slow:
|
||||
t.Fatalf("unexpected extra event %+v", event)
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartbeat(t *testing.T) {
|
||||
hub := NewHub(10 * time.Millisecond)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go hub.Run(ctx)
|
||||
|
||||
events, unsubscribe := hub.Subscribe()
|
||||
defer unsubscribe()
|
||||
|
||||
select {
|
||||
case event := <-events:
|
||||
if event.Name != "ping" {
|
||||
t.Fatalf("event=%+v, want ping", event)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(event.Data, &payload); err != nil {
|
||||
t.Fatalf("decode ping: %v", err)
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, payload["t"]); err != nil {
|
||||
t.Fatalf("ping time %q: %v", payload["t"], err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("no heartbeat within 1s")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManyClients(t *testing.T) {
|
||||
hub := NewHub(time.Hour)
|
||||
const clients = 120
|
||||
|
||||
channels := make([]<-chan Event, clients)
|
||||
cancels := make([]func(), clients)
|
||||
for i := range channels {
|
||||
channels[i], cancels[i] = hub.Subscribe()
|
||||
defer cancels[i]()
|
||||
}
|
||||
|
||||
hub.Broadcast(Event{Name: "scenario", ID: 9})
|
||||
for i, ch := range channels {
|
||||
select {
|
||||
case event := <-ch:
|
||||
if event.ID != 9 {
|
||||
t.Fatalf("client %d event=%+v", i, event)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("client %d timed out", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package staffing
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pulsecast/api/internal/model"
|
||||
)
|
||||
|
||||
func weekPoints(t *testing.T, values map[int]float64) []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++ {
|
||||
v := 25.0
|
||||
if override, ok := values[hour]; ok {
|
||||
v = override
|
||||
}
|
||||
points = append(points, model.ForecastPoint{
|
||||
T: start.AddDate(0, 0, day).Add(time.Duration(hour) * time.Hour).UTC(),
|
||||
V: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
return points
|
||||
}
|
||||
|
||||
func TestBuildStandardWeek(t *testing.T) {
|
||||
points := weekPoints(t, nil)
|
||||
ctx := model.ContextData{
|
||||
Holidays: []model.Holiday{{Date: "2026-11-18", Name: "Testtag"}},
|
||||
}
|
||||
plan, err := Build("hn-bahnhof-01", "base", "standard", points, ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Build: %v", err)
|
||||
}
|
||||
|
||||
if len(plan.Hours) != 70 {
|
||||
t.Fatalf("hours=%d, want 70", len(plan.Hours))
|
||||
}
|
||||
if len(plan.Shifts) != 21 {
|
||||
t.Fatalf("shifts=%d, want 21", len(plan.Shifts))
|
||||
}
|
||||
if plan.Assumptions.VisitorsPerStaffHour != 25 {
|
||||
t.Errorf("visitors per staff=%d", plan.Assumptions.VisitorsPerStaffHour)
|
||||
}
|
||||
for _, hour := range plan.Hours {
|
||||
// ceil(25 / 25 * 1.15) = 2
|
||||
if hour.RequiredStaff != 2 || hour.ScheduledStaff != 2 {
|
||||
t.Fatalf("hour %s required=%d scheduled=%d, want 2/2", hour.T, hour.RequiredStaff, hour.ScheduledStaff)
|
||||
}
|
||||
if hour.Coverage != "ok" {
|
||||
t.Fatalf("hour %s coverage=%q", hour.T, hour.Coverage)
|
||||
}
|
||||
}
|
||||
// 7 days * 10 hours * 2 staff * 16 EUR.
|
||||
if plan.Summary.CostEUR != 2240 {
|
||||
t.Errorf("cost=%d, want 2240", plan.Summary.CostEUR)
|
||||
}
|
||||
if plan.Summary.FlatScheduleCostEUR != 2240 {
|
||||
t.Errorf("flat cost=%d, want 2240", plan.Summary.FlatScheduleCostEUR)
|
||||
}
|
||||
if plan.Summary.CoveragePct != 100 || plan.Summary.OverstaffedHours != 0 || plan.Summary.UnderstaffedHours != 0 {
|
||||
t.Errorf("summary=%+v", plan.Summary)
|
||||
}
|
||||
if len(plan.Why) == 0 || !strings.Contains(strings.Join(plan.Why, "\n"), "Testtag") {
|
||||
t.Errorf("why=%v, want the holiday driver", plan.Why)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildShiftBlocksAndPeak(t *testing.T) {
|
||||
points := weekPoints(t, map[int]float64{9: 1000}) // last store hour is a peak
|
||||
plan, err := Build("hn-bahnhof-01", "base", "standard", points, model.ContextData{})
|
||||
if err != nil {
|
||||
t.Fatalf("Build: %v", err)
|
||||
}
|
||||
|
||||
if len(plan.Shifts) != 21 {
|
||||
t.Fatalf("shifts=%d, want 21", len(plan.Shifts))
|
||||
}
|
||||
first := plan.Shifts[0]
|
||||
if first.Date != "2026-11-16" || first.Start != "10:00" || first.End != "14:00" || first.Staff != 2 {
|
||||
t.Errorf("first shift=%+v", first)
|
||||
}
|
||||
last := plan.Shifts[2]
|
||||
if last.Start != "18:00" || last.End != "20:00" || last.Staff != 46 {
|
||||
t.Errorf("peak shift=%+v, want 18:00-20:00 staff 46", last)
|
||||
}
|
||||
// The peak block overstaffs 18:00 while 19:00 is exactly covered.
|
||||
if plan.Summary.OverstaffedHours != 1 {
|
||||
t.Errorf("overstaffed=%d, want 1", plan.Summary.OverstaffedHours)
|
||||
}
|
||||
if plan.Summary.UnderstaffedHours != 0 {
|
||||
t.Errorf("understaffed=%d, want 0", plan.Summary.UnderstaffedHours)
|
||||
}
|
||||
if plan.Summary.CoveragePct != 100 {
|
||||
t.Errorf("coverage=%v, want 100", plan.Summary.CoveragePct)
|
||||
}
|
||||
wantCost := 6*10*2*16 + (4*2+4*2+2*46)*16
|
||||
if plan.Summary.CostEUR != wantCost {
|
||||
t.Errorf("cost=%d, want %d", plan.Summary.CostEUR, wantCost)
|
||||
}
|
||||
maxRequired := 46
|
||||
wantFlat := maxRequired * 70 * 16
|
||||
if plan.Summary.FlatScheduleCostEUR != wantFlat {
|
||||
t.Errorf("flat cost=%d, want %d", plan.Summary.FlatScheduleCostEUR, wantFlat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildUnknownStandard(t *testing.T) {
|
||||
if _, err := Build("s", "base", "luxury", weekPoints(t, nil), model.ContextData{}); err == nil {
|
||||
t.Fatal("expected an error for an unknown standard")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRequiresACompleteStoreDay(t *testing.T) {
|
||||
points := []model.ForecastPoint{{
|
||||
T: time.Date(2026, 11, 16, 2, 0, 0, 0, time.UTC),
|
||||
V: 10,
|
||||
}}
|
||||
if _, err := Build("s", "base", "standard", points, model.ContextData{}); err == nil {
|
||||
t.Fatal("expected an error without store-hour points")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadFixtures(t *testing.T) {
|
||||
st, err := Load("../../testdata/data")
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if !st.Ready() {
|
||||
t.Fatal("Ready()=false for the fixture dataset")
|
||||
}
|
||||
if st.Len() != 8 {
|
||||
t.Fatalf("Len()=%d, want 8", st.Len())
|
||||
}
|
||||
|
||||
sensors := st.Sensors()
|
||||
for i := 1; i < len(sensors); i++ {
|
||||
if sensors[i-1].ID >= sensors[i].ID {
|
||||
t.Fatalf("sensors not sorted: %q then %q", sensors[i-1].ID, sensors[i].ID)
|
||||
}
|
||||
}
|
||||
if _, ok := st.Sensor("hn-bahnhof-01"); !ok {
|
||||
t.Fatal("missing hn-bahnhof-01")
|
||||
}
|
||||
if _, ok := st.Sensor("nope"); ok {
|
||||
t.Fatal("unexpected sensor nope")
|
||||
}
|
||||
|
||||
if points, ok := st.Series("hn-bahnhof-01"); !ok || len(points) == 0 {
|
||||
t.Fatalf("series: ok=%v points=%d", ok, len(points))
|
||||
}
|
||||
if points, ok := st.Forecast("hn-bahnhof-01", "sunny-saturday-market", "7d"); !ok || len(points) != 168 {
|
||||
t.Fatalf("forecast: ok=%v points=%d", ok, len(points))
|
||||
}
|
||||
if _, ok := st.Forecast("hn-bahnhof-01", "base", "2h"); ok {
|
||||
t.Fatal("unexpected horizon 2h")
|
||||
}
|
||||
if ctx := st.Context(); ctx.UpdatedAt.IsZero() || len(ctx.Weather.Forecast) == 0 || len(ctx.Holidays) == 0 {
|
||||
t.Fatalf("context incomplete: %+v", ctx)
|
||||
}
|
||||
|
||||
data, modelVersion, context := st.Versions()
|
||||
if data == "" || modelVersion == "" || context == "" {
|
||||
t.Fatalf("versions: data=%q model=%q context=%q", data, modelVersion, context)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMissingDirectoryIsPartial(t *testing.T) {
|
||||
st, err := Load(t.TempDir())
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for an empty data dir")
|
||||
}
|
||||
if st == nil {
|
||||
t.Fatal("Load returned a nil store; handlers need a usable store")
|
||||
}
|
||||
if st.Ready() {
|
||||
t.Fatal("Ready()=true without data")
|
||||
}
|
||||
if st.Len() != 0 {
|
||||
t.Fatalf("Len()=%d, want 0", st.Len())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user