feat: ci #2

Merged
Noel merged 3 commits from feat/ci into main 2026-11-14 04:43:42 +00:00
16 changed files with 1983 additions and 70 deletions
Showing only changes of commit 9ebb9cdf0d - Show all commits

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
.git
.gitignore
*.md
opencode.json
+29
View File
@@ -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"]
+66
View File
@@ -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.NewWithToken(cfg.FastAPIURL, cfg.MLTimeout, cfg.ControlToken), 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)
}
}
+631
View File
@@ -0,0 +1,631 @@
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)
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 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)
}
}
}
+115
View File
@@ -0,0 +1,115 @@
package api
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
"mime"
"net/http"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/model"
)
// maxTrainBody bounds an uploaded CSV. The raw dataset is ~18 MB; 64 MB leaves
// room for a larger export without letting the API accept unbounded bodies.
const maxTrainBody = 64 << 20
// handleTrain proxies POST /api/train to FastAPI. The request is queued there,
// so this handler only validates the JSON form and relays the 202/409 response.
func (s *Server) handleTrain(w http.ResponseWriter, r *http.Request) {
trainer, ok := s.ml.(TrainClient)
if !ok {
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI training client is not configured")
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxTrainBody)
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "Content-Type must be application/json or multipart/form-data")
return
}
switch mediaType {
case "application/json":
var req ml.TrainRequest
if err := decodeJSON(r, &req); err != nil {
writeError(w, http.StatusBadRequest, "bad_request", "invalid JSON body")
return
}
if req.Path == "" {
writeError(w, http.StatusBadRequest, "bad_request", "path is required")
return
}
if req.Model == "" {
req.Model = "small"
}
if !model.Contains(ml.TrainModels, req.Model) {
writeError(w, http.StatusBadRequest, "bad_request", "model must be small or forecast")
return
}
raw, err := json.Marshal(req)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal", "failed to encode request")
return
}
s.relayTrain(r.Context(), trainer, w, "application/json", bytes.NewReader(raw))
case "multipart/form-data":
// Forward the stream unchanged: file and model fields belong to FastAPI.
s.relayTrain(r.Context(), trainer, w, r.Header.Get("Content-Type"), r.Body)
default:
writeError(w, http.StatusBadRequest, "bad_request", "Content-Type must be application/json or multipart/form-data")
}
}
// handleTrainStatus proxies GET /api/train/{job_id}.
func (s *Server) handleTrainStatus(w http.ResponseWriter, r *http.Request) {
trainer, ok := s.ml.(TrainClient)
if !ok {
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI training client is not configured")
return
}
jobID := r.PathValue("job_id")
if jobID == "" {
writeError(w, http.StatusNotFound, "not_found", "training job not found")
return
}
status, body, err := trainer.TrainStatus(r.Context(), jobID)
if err != nil {
slog.Warn("train status proxy failed", "job_id", jobID, "error", err)
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
return
}
relayMLResponse(w, status, body)
}
func (s *Server) relayTrain(ctx context.Context, trainer TrainClient, w http.ResponseWriter, contentType string, body io.Reader) {
status, raw, err := trainer.Train(ctx, contentType, body)
if err != nil {
slog.Warn("train proxy failed", "error", err)
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
return
}
relayMLResponse(w, status, raw)
}
// relayMLResponse forwards FastAPI's status and JSON body. A non-JSON upstream
// body is converted to the contract error envelope instead of leaking HTML.
func relayMLResponse(w http.ResponseWriter, status int, body []byte) {
if status < 100 || status > 599 {
writeError(w, http.StatusBadGateway, "bad_gateway", "FastAPI returned an invalid status")
return
}
body = bytes.TrimSpace(body)
if len(body) == 0 || !json.Valid(body) {
writeError(w, http.StatusBadGateway, "bad_gateway", "FastAPI returned a non-JSON response")
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_, _ = w.Write(body)
}
+285
View File
@@ -0,0 +1,285 @@
package api
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
)
func newTrainServer(t *testing.T, fastapiURL string) *httptest.Server {
t.Helper()
_, ts := newTestServer(t, ml.New(fastapiURL, 500*time.Millisecond), func(cfg *config.Config) {
cfg.FastAPIURL = fastapiURL
cfg.ControlUser = "control"
cfg.ControlPass = "secret"
})
return ts
}
func controlRequest(t *testing.T, method, url, contentType string, body io.Reader, user, pass string) (*http.Response, []byte) {
t.Helper()
req, err := http.NewRequest(method, url, body)
if err != nil {
t.Fatal(err)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
if user != "" {
req.SetBasicAuth(user, pass)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("%s %s: %v", method, url, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read %s %s: %v", method, url, err)
}
return resp, raw
}
func TestTrainJSONProxyAndControlAuth(t *testing.T) {
var calls atomic.Int32
var gotBody []byte
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/train" {
http.Error(w, "unexpected", http.StatusNotFound)
return
}
calls.Add(1)
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("FastAPI Content-Type=%q, want application/json", ct)
}
gotBody, _ = io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small","submitted_at":"2026-11-14T09:00:00Z"}`))
}))
defer fastapi.Close()
ts := newTrainServer(t, fastapi.URL)
body := `{"path":"/tmp/new.csv"}`
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "", ""); 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, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "wrong"); resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("bad auth status=%d, want 401", resp.StatusCode)
}
if got := calls.Load(); got != 0 {
t.Fatalf("FastAPI calls=%d before auth succeeded, want 0", got)
}
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(body), "control", "secret")
if resp.StatusCode != http.StatusAccepted {
t.Fatalf("train status=%d body=%s, want 202", resp.StatusCode, raw)
}
var job map[string]any
if err := json.Unmarshal(raw, &job); err != nil {
t.Fatalf("decode train response: %v", err)
}
if job["job_id"] != "job-1" || job["status"] != "queued" {
t.Fatalf("train response=%v", job)
}
var forwarded ml.TrainRequest
if err := json.Unmarshal(gotBody, &forwarded); err != nil {
t.Fatalf("decode forwarded body: %v", err)
}
if forwarded.Path != "/tmp/new.csv" || forwarded.Model != "small" {
t.Fatalf("forwarded body=%+v, want path and default model=small", forwarded)
}
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "", ""); resp.StatusCode != http.StatusUnauthorized {
t.Fatalf("status without auth=%d, want 401", resp.StatusCode)
}
}
func TestTrainStatusProxy(t *testing.T) {
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/train/job-1":
if r.Method != http.MethodGet {
t.Errorf("method=%s, want GET", r.Method)
}
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","model":"forecast","metrics":{"accuracy":0.9},"model_version":"v2","error":null}`))
default:
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
}
}))
defer fastapi.Close()
ts := newTrainServer(t, fastapi.URL)
resp, raw := controlRequest(t, http.MethodGet, ts.URL+"/api/train/job-1", "", nil, "control", "secret")
if resp.StatusCode != http.StatusOK {
t.Fatalf("status=%d body=%s, want 200", resp.StatusCode, raw)
}
var job struct {
JobID string `json:"job_id"`
Status string `json:"status"`
Metrics map[string]any `json:"metrics"`
Error any `json:"error"`
}
if err := json.Unmarshal(raw, &job); err != nil {
t.Fatalf("decode status response: %v", err)
}
if job.JobID != "job-1" || job.Status != "succeeded" || job.Metrics["accuracy"] != 0.9 {
t.Fatalf("status response=%+v", job)
}
resp, raw = controlRequest(t, http.MethodGet, ts.URL+"/api/train/missing", "", nil, "control", "secret")
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("unknown job status=%d body=%s, want 404", resp.StatusCode, raw)
}
if !json.Valid(raw) {
t.Fatalf("unknown job body is not JSON: %s", raw)
}
}
func TestTrainMultipartProxy(t *testing.T) {
var gotFile, gotModel string
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/train" {
http.Error(w, "unexpected", http.StatusNotFound)
return
}
if !strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
t.Errorf("Content-Type=%q, want multipart/form-data", r.Header.Get("Content-Type"))
}
if err := r.ParseMultipartForm(1 << 20); err != nil {
t.Errorf("parse multipart: %v", err)
http.Error(w, "bad multipart", http.StatusBadRequest)
return
}
gotModel = r.FormValue("model")
file, _, err := r.FormFile("file")
if err != nil {
t.Errorf("form file: %v", err)
} else {
raw, _ := io.ReadAll(file)
file.Close()
gotFile = string(raw)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"job_id":"job-2","status":"queued","model":"forecast","submitted_at":"2026-11-14T09:00:00Z"}`))
}))
defer fastapi.Close()
ts := newTrainServer(t, fastapi.URL)
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
part, err := mw.CreateFormFile("file", "new-data.csv")
if err != nil {
t.Fatal(err)
}
if _, err := part.Write([]byte("timestamp,count\n2026-11-01T10:00:00Z,42\n")); err != nil {
t.Fatal(err)
}
if err := mw.WriteField("model", "forecast"); err != nil {
t.Fatal(err)
}
if err := mw.Close(); err != nil {
t.Fatal(err)
}
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", mw.FormDataContentType(), &buf, "control", "secret")
if resp.StatusCode != http.StatusAccepted {
t.Fatalf("multipart status=%d body=%s, want 202", resp.StatusCode, raw)
}
if gotModel != "forecast" {
t.Errorf("forwarded model=%q, want forecast", gotModel)
}
if !strings.Contains(gotFile, "42") {
t.Errorf("forwarded file=%q, want CSV content", gotFile)
}
}
func TestTrainValidationAndUpstreamErrors(t *testing.T) {
var calls atomic.Int32
fastapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodPost {
w.WriteHeader(http.StatusConflict)
_, _ = w.Write([]byte(`{"detail":"a training job is already running"}`))
return
}
http.NotFound(w, r)
}))
defer fastapi.Close()
ts := newTrainServer(t, fastapi.URL)
for _, tc := range []struct {
name string
contentType string
body string
}{
{"unknown model", "application/json", `{"path":"/tmp/x.csv","model":"huge"}`},
{"missing path", "application/json", `{"model":"small"}`},
{"malformed json", "application/json", `{`},
{"wrong content type", "text/plain", `path=/tmp/x.csv`},
} {
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", tc.contentType, strings.NewReader(tc.body), "control", "secret")
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("%s: status=%d body=%s, want 400", tc.name, resp.StatusCode, raw)
}
}
if got := calls.Load(); got != 0 {
t.Fatalf("FastAPI calls=%d for invalid requests, want 0", got)
}
resp, raw := controlRequest(t, http.MethodPost, ts.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv","model":"forecast"}`), "control", "secret")
if resp.StatusCode != http.StatusConflict {
t.Fatalf("busy status=%d body=%s, want upstream 409", resp.StatusCode, raw)
}
if resp, _ := controlRequest(t, http.MethodPost, ts.URL+"/api/train/job-1", "application/json", strings.NewReader(`{}`), "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("POST job status=%d, want 405", resp.StatusCode)
} else if allow := resp.Header.Get("Allow"); allow != "GET" {
t.Errorf("Allow=%q, want GET", allow)
}
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train", "", nil, "control", "secret"); resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("GET train status=%d, want 405", resp.StatusCode)
}
if resp, _ := controlRequest(t, http.MethodGet, ts.URL+"/api/train/a/b", "", nil, "control", "secret"); resp.StatusCode != http.StatusNotFound {
t.Errorf("nested job path status=%d, want 404", resp.StatusCode)
}
}
func TestTrainFastAPIDownAndAuthUnconfigured(t *testing.T) {
_, down := newTestServer(t, ml.New("http://127.0.0.1:1", 50*time.Millisecond), func(cfg *config.Config) {
cfg.FastAPIURL = "http://127.0.0.1:1"
cfg.ControlUser = "control"
cfg.ControlPass = "secret"
})
resp, raw := controlRequest(t, http.MethodPost, down.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
if resp.StatusCode != http.StatusBadGateway {
t.Fatalf("train with FastAPI down status=%d body=%s, want 502", resp.StatusCode, raw)
}
resp, raw = controlRequest(t, http.MethodGet, down.URL+"/api/train/job-1", "", nil, "control", "secret")
if resp.StatusCode != http.StatusBadGateway {
t.Fatalf("status with FastAPI down=%d body=%s, want 502", resp.StatusCode, raw)
}
_, unconfigured := newTestServer(t, &stubML{}, nil)
resp, raw = controlRequest(t, http.MethodPost, unconfigured.URL+"/api/train", "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`), "control", "secret")
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("train without control env status=%d body=%s, want 503", resp.StatusCode, raw)
}
}
+28 -8
View File
@@ -5,8 +5,10 @@ package api
import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"strings"
"sync"
"time"
@@ -15,17 +17,23 @@ import (
"pulsecast/api/internal/model"
"pulsecast/api/internal/scenario"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/staffing"
"pulsecast/api/internal/store"
)
// MLClient is the FastAPI surface the handlers need. *ml.Client implements it;
// tests use a stub.
// MLClient is the FastAPI prediction surface the data handlers need.
// *ml.Client implements it; tests use a stub.
type MLClient interface {
Predict(ctx context.Context, req ml.PredictRequest) ([]model.ForecastPoint, error)
Simulate(ctx context.Context, req ml.SimulateRequest) (map[string]map[string][]model.ForecastPoint, error)
}
// TrainClient is the optional FastAPI training surface. Keeping it separate
// lets prediction-only stubs (and the cached fallback) work unchanged.
type TrainClient interface {
Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error)
TrainStatus(ctx context.Context, jobID string) (int, []byte, error)
}
// Server holds process-wide state. One instance per pod.
type Server struct {
cfg config.Config
@@ -36,11 +44,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.
@@ -77,6 +85,8 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/staffing", s.handleStaffing)
mux.HandleFunc("GET /api/scenario", s.handleScenarioGet)
mux.HandleFunc("POST /api/scenario", s.requireControlAuth(s.handleScenarioPost))
mux.HandleFunc("POST /api/train", s.requireControlAuth(s.handleTrain))
mux.HandleFunc("GET /api/train/{job_id}", s.requireControlAuth(s.handleTrainStatus))
mux.HandleFunc("GET /api/stream", s.handleStream)
return s.recoverer(s.withCORS(s.routeCheck(mux)))
@@ -92,12 +102,16 @@ var methodTable = map[string][]string{
"/api/forecast": {"GET"},
"/api/staffing": {"GET"},
"/api/scenario": {"GET", "POST"},
"/api/train": {"POST"},
"/api/stream": {"GET"},
}
func (s *Server) routeCheck(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
methods, ok := methodTable[r.URL.Path]
if !ok && isTrainJobPath(r.URL.Path) {
methods, ok = []string{"GET"}, true
}
if !ok {
writeError(w, http.StatusNotFound, "not_found", "route not found")
return
@@ -111,6 +125,12 @@ func (s *Server) routeCheck(next http.Handler) http.Handler {
})
}
// isTrainJobPath matches exactly one job id after /api/train/.
func isTrainJobPath(path string) bool {
jobID, found := strings.CutPrefix(path, "/api/train/")
return found && jobID != "" && !strings.Contains(jobID, "/")
}
func joinMethods(methods []string) string {
out := ""
for i, method := range methods {
+18 -15
View File
@@ -9,28 +9,31 @@ import (
// Config is the process configuration.
type Config struct {
Addr string
DataDir string
FastAPIURL string
ControlUser string
ControlPass string
Heartbeat time.Duration
MLTimeout time.Duration
Addr string
DataDir string
FastAPIURL string
ControlUser string
ControlPass string
ControlToken string
Heartbeat time.Duration
MLTimeout time.Duration
}
// Load reads configuration from env with local-development defaults.
// CONTROL_USER / CONTROL_PASS have no defaults: POST /api/scenario fails
// closed when they are unset.
// closed when they are unset. CONTROL_TOKEN is optional and, when set, is
// forwarded to FastAPI's internal /train endpoints as a Bearer token.
func Load() Config {
port := getenv("PORT", "8080")
return Config{
Addr: ":" + port,
DataDir: getenv("DATA_DIR", "testdata/data"),
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
ControlUser: os.Getenv("CONTROL_USER"),
ControlPass: os.Getenv("CONTROL_PASS"),
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
Addr: ":" + port,
DataDir: getenv("DATA_DIR", "testdata/data"),
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
ControlUser: os.Getenv("CONTROL_USER"),
ControlPass: os.Getenv("CONTROL_PASS"),
ControlToken: os.Getenv("CONTROL_TOKEN"),
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
}
}
+74
View File
@@ -0,0 +1,74 @@
package config
import (
"testing"
"time"
)
func TestLoadDefaults(t *testing.T) {
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "CONTROL_TOKEN", "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")
}
if cfg.ControlToken != "" {
t.Errorf("ControlToken=%q, want empty by default", cfg.ControlToken)
}
}
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("CONTROL_TOKEN", "token-123")
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.ControlToken != "token-123" {
t.Fatalf("ControlToken=%q, want token-123", cfg.ControlToken)
}
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)
}
}
+109 -28
View File
@@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
@@ -18,20 +19,34 @@ import (
// DefaultTimeout is the contract limit for a live prediction.
const DefaultTimeout = 1500 * time.Millisecond
// controlTimeout bounds training submissions and status reads. Uploads are
// larger than prediction payloads, so they get more than DefaultTimeout.
const controlTimeout = 30 * time.Second
// Client talks to the FastAPI ML service.
type Client struct {
baseURL string
http *http.Client
baseURL string
http *http.Client
control *http.Client
controlToken string
}
// New returns a client for baseURL (for example http://localhost:8000).
func New(baseURL string, timeout time.Duration) *Client {
return NewWithToken(baseURL, timeout, "")
}
// NewWithToken is New plus the Bearer token FastAPI's internal /train
// endpoints expect. An empty token sends no Authorization header.
func NewWithToken(baseURL string, timeout time.Duration, controlToken string) *Client {
if timeout <= 0 {
timeout = DefaultTimeout
}
return &Client{
baseURL: strings.TrimRight(baseURL, "/"),
http: &http.Client{Timeout: timeout},
baseURL: strings.TrimRight(baseURL, "/"),
http: &http.Client{Timeout: timeout},
control: &http.Client{Timeout: controlTimeout},
controlToken: controlToken,
}
}
@@ -52,39 +67,52 @@ 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.
// TrainRequest is the JSON form of POST /train.
type TrainRequest struct {
Path string `json:"path"`
Model string `json:"model"`
}
// TrainModels are the accepted training targets from CONTRACT.md §3.
var TrainModels = []string{"small", "forecast"}
// 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
@@ -95,30 +123,83 @@ func (c *Client) Simulate(ctx context.Context, req SimulateRequest) (map[string]
return out.Forecasts, nil
}
// Train proxies POST /train. body is forwarded verbatim (JSON or multipart),
// and the upstream status and response are returned unchanged for the Go
// handler to relay. Training itself runs in FastAPI; this call only queues it.
func (c *Client) Train(ctx context.Context, contentType string, body io.Reader) (int, []byte, error) {
return c.controlRequest(ctx, http.MethodPost, "/train", contentType, body)
}
// TrainStatus proxies GET /train/{job_id}.
func (c *Client) TrainStatus(ctx context.Context, jobID string) (int, []byte, error) {
return c.controlRequest(ctx, http.MethodGet, "/train/"+url.PathEscape(jobID), "", nil)
}
// controlRequest performs a training/control request and returns the raw
// upstream status and body. Non-2xx responses are not errors: the proxy
// forwards them so clients see FastAPI's 400/404/409 semantics.
func (c *Client) controlRequest(ctx context.Context, method, path, contentType string, body io.Reader) (int, []byte, error) {
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, body)
if err != nil {
return 0, nil, fmt.Errorf("ml: build request: %w", err)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
req.Header.Set("Accept", "application/json")
if c.controlToken != "" {
req.Header.Set("Authorization", "Bearer "+c.controlToken)
}
resp, err := c.control.Do(req)
if err != nil {
return 0, nil, fmt.Errorf("ml: %s: %w", path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
if err != nil {
return 0, nil, fmt.Errorf("ml: read %s: %w", path, err)
}
return resp.StatusCode, raw, nil
}
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
}
+219
View File
@@ -0,0 +1,219 @@
package ml
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"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)
}
}
func TestTrainForwardsBodyContentTypeAndStatus(t *testing.T) {
var gotMethod, gotPath, gotType, gotBody string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod, gotPath = r.Method, r.URL.Path
gotType = r.Header.Get("Content-Type")
raw, _ := io.ReadAll(r.Body)
gotBody = string(raw)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued","model":"small"}`))
}))
defer srv.Close()
status, body, err := New(srv.URL, time.Second).Train(context.Background(), "application/json",
strings.NewReader(`{"path":"/tmp/new.csv","model":"small"}`))
if err != nil {
t.Fatalf("Train: %v", err)
}
if status != http.StatusAccepted {
t.Fatalf("status=%d, want 202", status)
}
if !strings.Contains(string(body), `"job_id":"job-1"`) {
t.Fatalf("body=%s", body)
}
if gotMethod != http.MethodPost || gotPath != "/train" {
t.Fatalf("request=%s %s, want POST /train", gotMethod, gotPath)
}
if gotType != "application/json" || !strings.Contains(gotBody, "/tmp/new.csv") {
t.Fatalf("content-type=%q body=%q", gotType, gotBody)
}
}
func TestControlRequestsForwardBearerToken(t *testing.T) {
var got []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = append(got, r.Header.Get("Authorization"))
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodPost {
w.WriteHeader(http.StatusAccepted)
}
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued"}`))
}))
defer srv.Close()
client := NewWithToken(srv.URL, time.Second, "token-123")
if _, _, err := client.Train(context.Background(), "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`)); err != nil {
t.Fatalf("Train: %v", err)
}
if _, _, err := client.TrainStatus(context.Background(), "job-1"); err != nil {
t.Fatalf("TrainStatus: %v", err)
}
for i, auth := range got {
if auth != "Bearer token-123" {
t.Errorf("request %d Authorization=%q, want Bearer token-123", i, auth)
}
}
}
func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method=%s, want GET", r.Method)
}
if r.URL.Path == "/train/job-1" {
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"succeeded","error":null}`))
return
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail":"job not found"}`))
}))
defer srv.Close()
client := New(srv.URL, time.Second)
status, body, err := client.TrainStatus(context.Background(), "job-1")
if err != nil {
t.Fatalf("TrainStatus: %v", err)
}
if status != http.StatusOK || !strings.Contains(string(body), `"succeeded"`) {
t.Fatalf("status=%d body=%s", status, body)
}
status, body, err = client.TrainStatus(context.Background(), "missing")
if err != nil {
t.Fatalf("TrainStatus missing: %v", err)
}
if status != http.StatusNotFound || !strings.Contains(string(body), "job not found") {
t.Fatalf("status=%d body=%s, want forwarded 404", status, body)
}
}
+19 -19
View File
@@ -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.
+50
View File
@@ -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)
}
}
+133
View File
@@ -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)
}
}
}
+138
View File
@@ -0,0 +1,138 @@
package staffing
import (
"strings"
"testing"
"time"
"pulsecast/api/internal/model"
)
func weekPoints(t *testing.T, values map[int]float64) []model.ForecastPoint {
t.Helper()
return weekPointsForDay(t, -1, values)
}
// weekPointsForDay builds seven days of store-hour points. Hour overrides apply
// only to day (0 = Monday), or to every day when day is negative.
func weekPointsForDay(t *testing.T, day int, 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 d := 0; d < 7; d++ {
for hour := 0; hour < model.StoreHours.Close-model.StoreHours.Open; hour++ {
v := 25.0
if override, ok := values[hour]; ok && (day < 0 || day == d) {
v = override
}
points = append(points, model.ForecastPoint{
T: start.AddDate(0, 0, d).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) {
// One 19:00 peak on Monday only; the rest of the week stays flat at 25.
points := weekPointsForDay(t, 0, map[int]float64{9: 1000})
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")
}
}
+65
View File
@@ -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())
}
}