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,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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user