201 lines
6.8 KiB
Go
201 lines
6.8 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"pulsecast/api/internal/ml"
|
|
"pulsecast/api/internal/model"
|
|
)
|
|
|
|
type stubPlayground struct {
|
|
*stubML
|
|
fn func(context.Context, ml.PlaygroundRequest) (ml.PlaygroundResponse, error)
|
|
}
|
|
|
|
func (s *stubPlayground) PlaygroundPredict(ctx context.Context, req ml.PlaygroundRequest) (ml.PlaygroundResponse, error) {
|
|
if s.fn == nil {
|
|
return ml.PlaygroundResponse{}, errors.New("ml unavailable")
|
|
}
|
|
return s.fn(ctx, req)
|
|
}
|
|
|
|
func playgroundResponse() ml.PlaygroundResponse {
|
|
return ml.PlaygroundResponse{
|
|
Sensor: "hn-bahnhof-01",
|
|
Horizon: "48h",
|
|
Scenario: "base",
|
|
Points: []model.ForecastPoint{{
|
|
T: time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC), V: 12.3, Lo: 8.1, Hi: 17.2,
|
|
}},
|
|
DataVersion: "data-1",
|
|
ModelVersion: "model-1",
|
|
ContextVersion: "context-1",
|
|
}
|
|
}
|
|
|
|
type playgroundJSON struct {
|
|
Sensor string `json:"sensor"`
|
|
Horizon string `json:"horizon"`
|
|
Scenario string `json:"scenario"`
|
|
Points []model.ForecastPoint `json:"points"`
|
|
DataVersion string `json:"data_version"`
|
|
ModelVersion string `json:"model_version"`
|
|
ContextVersion string `json:"context_version"`
|
|
WeatherSource string `json:"weather_source"`
|
|
Request struct {
|
|
TempC *float64 `json:"temp_c"`
|
|
PrecipMM *float64 `json:"precip_mm"`
|
|
IsHoliday *bool `json:"is_holiday"`
|
|
} `json:"request"`
|
|
}
|
|
|
|
func TestPlaygroundForecastDefaults(t *testing.T) {
|
|
var got ml.PlaygroundRequest
|
|
client := &stubPlayground{
|
|
stubML: &stubML{},
|
|
fn: func(_ context.Context, req ml.PlaygroundRequest) (ml.PlaygroundResponse, error) {
|
|
got = req
|
|
return playgroundResponse(), nil
|
|
},
|
|
}
|
|
_, ts := newTestServer(t, client, nil)
|
|
|
|
var payload playgroundJSON
|
|
resp := getJSON(t, ts.URL+"/api/playground/forecast?sensor=hn-bahnhof-01", &payload)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status=%d", resp.StatusCode)
|
|
}
|
|
if got.Horizon != "48h" || got.Scenario != "base" {
|
|
t.Fatalf("defaults horizon=%q scenario=%q", got.Horizon, got.Scenario)
|
|
}
|
|
if got.Weather != nil || got.IsHoliday != nil {
|
|
t.Fatalf("unexpected overrides: weather=%+v holiday=%v", got.Weather, got.IsHoliday)
|
|
}
|
|
if payload.WeatherSource != "forecast" {
|
|
t.Errorf("weather_source=%q", payload.WeatherSource)
|
|
}
|
|
if payload.ModelVersion != "model-1" || payload.DataVersion != "data-1" || payload.ContextVersion != "context-1" {
|
|
t.Fatalf("versions=%+v", payload)
|
|
}
|
|
if resp.Header.Get("X-Model-Version") != "model-1" {
|
|
t.Errorf("X-Model-Version=%q", resp.Header.Get("X-Model-Version"))
|
|
}
|
|
if len(payload.Points) != 1 || payload.Points[0].V != 12.3 {
|
|
t.Fatalf("points=%+v", payload.Points)
|
|
}
|
|
}
|
|
|
|
func TestPlaygroundForecastOverrides(t *testing.T) {
|
|
var got ml.PlaygroundRequest
|
|
client := &stubPlayground{
|
|
stubML: &stubML{},
|
|
fn: func(_ context.Context, req ml.PlaygroundRequest) (ml.PlaygroundResponse, error) {
|
|
got = req
|
|
return playgroundResponse(), nil
|
|
},
|
|
}
|
|
_, ts := newTestServer(t, client, nil)
|
|
|
|
var payload playgroundJSON
|
|
resp := getJSON(t, ts.URL+"/api/playground/forecast?sensor=hn-bahnhof-01&temp_c=29&precip_mm=0&is_holiday=true", &payload)
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status=%d", resp.StatusCode)
|
|
}
|
|
if got.Weather == nil || got.Weather.TempC == nil || *got.Weather.TempC != 29 {
|
|
t.Fatalf("weather=%+v", got.Weather)
|
|
}
|
|
if got.Weather.PrecipMM == nil || *got.Weather.PrecipMM != 0 {
|
|
t.Fatalf("precip=%+v", got.Weather.PrecipMM)
|
|
}
|
|
if got.IsHoliday == nil || !*got.IsHoliday {
|
|
t.Fatalf("holiday=%+v", got.IsHoliday)
|
|
}
|
|
if payload.WeatherSource != "override" {
|
|
t.Errorf("weather_source=%q", payload.WeatherSource)
|
|
}
|
|
if payload.Request.TempC == nil || *payload.Request.TempC != 29 {
|
|
t.Errorf("request echo temp=%+v", payload.Request.TempC)
|
|
}
|
|
if payload.Request.IsHoliday == nil || !*payload.Request.IsHoliday {
|
|
t.Errorf("request echo holiday=%+v", payload.Request.IsHoliday)
|
|
}
|
|
}
|
|
|
|
func TestPlaygroundForecastValidation(t *testing.T) {
|
|
_, ts := newTestServer(t, &stubPlayground{stubML: &stubML{}}, nil)
|
|
cases := []struct {
|
|
path string
|
|
status int
|
|
code string
|
|
}{
|
|
{"/api/playground/forecast", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=nope", http.StatusNotFound, "not_found"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&horizon=2h", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&scenario=party", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&temp_c=hot", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&temp_c=51", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&temp_c=NaN", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&precip_mm=-1", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&precip_mm=51", http.StatusBadRequest, "bad_request"},
|
|
{"/api/playground/forecast?sensor=hn-bahnhof-01&is_holiday=maybe", http.StatusBadRequest, "bad_request"},
|
|
}
|
|
for _, tc := range cases {
|
|
var body model.ErrorBody
|
|
resp := getJSON(t, ts.URL+tc.path, &body)
|
|
if resp.StatusCode != tc.status {
|
|
t.Errorf("%s: status=%d, want %d", tc.path, resp.StatusCode, tc.status)
|
|
}
|
|
if body.Error.Code != tc.code {
|
|
t.Errorf("%s: code=%q, want %q", tc.path, body.Error.Code, tc.code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlaygroundForecastUpstreamFailure(t *testing.T) {
|
|
client := &stubPlayground{
|
|
stubML: &stubML{},
|
|
fn: func(context.Context, ml.PlaygroundRequest) (ml.PlaygroundResponse, error) {
|
|
return ml.PlaygroundResponse{}, errors.New("connection refused")
|
|
},
|
|
}
|
|
_, ts := newTestServer(t, client, nil)
|
|
|
|
var body model.ErrorBody
|
|
resp := getJSON(t, ts.URL+"/api/playground/forecast?sensor=hn-bahnhof-01", &body)
|
|
if resp.StatusCode != http.StatusBadGateway {
|
|
t.Fatalf("status=%d, want 502", resp.StatusCode)
|
|
}
|
|
if body.Error.Code != "ml_unavailable" {
|
|
t.Fatalf("error=%+v", body.Error)
|
|
}
|
|
}
|
|
|
|
func TestPlaygroundForecastMethodNotAllowed(t *testing.T) {
|
|
_, ts := newTestServer(t, &stubPlayground{stubML: &stubML{}}, nil)
|
|
resp, err := http.Post(ts.URL+"/api/playground/forecast", "application/json", strings.NewReader("{}"))
|
|
if err != nil {
|
|
t.Fatalf("POST: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Fatalf("status=%d, want 405", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestPlaygroundForecastMissingMLClient(t *testing.T) {
|
|
_, ts := newTestServer(t, &stubML{}, nil)
|
|
var body model.ErrorBody
|
|
resp := getJSON(t, ts.URL+"/api/playground/forecast?sensor=hn-bahnhof-01", &body)
|
|
if resp.StatusCode != http.StatusBadGateway {
|
|
t.Fatalf("status=%d, want 502", resp.StatusCode)
|
|
}
|
|
if body.Error.Code != "ml_unavailable" {
|
|
t.Fatalf("error=%+v", body.Error)
|
|
}
|
|
}
|