bd05edfb9c
- Frontend: index.html, style.css, app.js - Dunkles Kontrast-Design mit Team-Farben (Rot/Grün) - Vollbild-Toggle, Responsive Design - Echtzeit-Updates über WebSocket + Polling-Fallback - Tastatur-Shortcuts (R/G für Punkte, N für nächste Runde) - Rundenverlauf mit Farbcodierung - Go-Integration: - frontend.go: embed.FS für statische Dateien - websocket.go: WebSocket-Handler (/ws) für State-Broadcast - httpapi.go: broadcastState() nach jeder Mutation, /ws-Route - main.go: api.SetupFrontend() für Frontend-Einbettung - Frontend unter '/' verfügbar, API unter '/api/' - Tests: 17 End-to-End-Tests bestanden - Alle HTTP-Endpunkte funktionsfähig - Frontend-Assets korrekt eingebettet - Persistenz (state.json) funktioniert
361 lines
8.6 KiB
Go
361 lines
8.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.jlserver.de/goSportpunktetafel/internal/logik"
|
|
)
|
|
|
|
func newTestServer() (*Server, *logik.Scoreboard) {
|
|
sb := logik.NewScoreboard()
|
|
s := NewServer("127.0.0.1:0", sb, nil)
|
|
return s, sb
|
|
}
|
|
|
|
func TestHandleState(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamGreen)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/state", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
|
|
var response map[string]interface{}
|
|
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if int(response["red_score"].(float64)) != 2 {
|
|
t.Errorf("red_score = %v, want 2", response["red_score"])
|
|
}
|
|
if int(response["green_score"].(float64)) != 1 {
|
|
t.Errorf("green_score = %v, want 1", response["green_score"])
|
|
}
|
|
if int(response["current_round"].(float64)) != 1 {
|
|
t.Errorf("current_round = %v, want 1", response["current_round"])
|
|
}
|
|
}
|
|
|
|
func TestHandleState_WithRounds(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamRed)
|
|
sb.NextRound()
|
|
|
|
sb.Score(logik.TeamGreen)
|
|
sb.Score(logik.TeamGreen)
|
|
sb.NextRound()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/state", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
|
|
var response map[string]interface{}
|
|
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if int(response["current_round"].(float64)) != 3 {
|
|
t.Errorf("current_round = %v, want 3", response["current_round"])
|
|
}
|
|
|
|
rounds := response["rounds"].([]interface{})
|
|
if len(rounds) != 2 {
|
|
t.Fatalf("len(rounds) = %d, want 2", len(rounds))
|
|
}
|
|
|
|
r1 := rounds[0].(map[string]interface{})
|
|
if int(r1["Red"].(float64)) != 3 {
|
|
t.Errorf("round 1 red = %v, want 3", r1["Red"])
|
|
}
|
|
|
|
r2 := rounds[1].(map[string]interface{})
|
|
if int(r2["Green"].(float64)) != 2 {
|
|
t.Errorf("round 2 green = %v, want 2", r2["Green"])
|
|
}
|
|
}
|
|
|
|
func TestHandleScore_Red(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/score?team=red", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
if sb.RedScore != 1 {
|
|
t.Errorf("RedScore = %d, want 1", sb.RedScore)
|
|
}
|
|
}
|
|
|
|
func TestHandleScore_Green(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/score?team=green", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
if sb.GreenScore != 1 {
|
|
t.Errorf("GreenScore = %d, want 1", sb.GreenScore)
|
|
}
|
|
}
|
|
|
|
func TestHandleScore_InvalidTeam(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/score?team=blue", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusBadRequest)
|
|
}
|
|
}
|
|
|
|
func TestHandleScore_WrongMethod(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/score?team=red", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestHandleNextRound(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamRed)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/next", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
if sb.CurrentRound != 2 {
|
|
t.Errorf("CurrentRound = %d, want 2", sb.CurrentRound)
|
|
}
|
|
if sb.RedScore != 0 {
|
|
t.Errorf("RedScore after next = %d, want 0", sb.RedScore)
|
|
}
|
|
if sb.GreenScore != 0 {
|
|
t.Errorf("GreenScore after next = %d, want 0", sb.GreenScore)
|
|
}
|
|
|
|
var response map[string]interface{}
|
|
json.NewDecoder(w.Body).Decode(&response)
|
|
if int(response["round"].(float64)) != 2 {
|
|
t.Errorf("response round = %v, want 2", response["round"])
|
|
}
|
|
}
|
|
|
|
func TestHandleReset(t *testing.T) {
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamGreen)
|
|
sb.Score(logik.TeamGreen)
|
|
sb.NextRound()
|
|
sb.Score(logik.TeamRed)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/reset", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
if sb.CurrentRound != 1 {
|
|
t.Errorf("CurrentRound = %d, want 1", sb.CurrentRound)
|
|
}
|
|
if sb.RedScore != 0 {
|
|
t.Errorf("RedScore = %d, want 0", sb.RedScore)
|
|
}
|
|
if sb.GreenScore != 0 {
|
|
t.Errorf("GreenScore = %d, want 0", sb.GreenScore)
|
|
}
|
|
if len(sb.Rounds) != 0 {
|
|
t.Errorf("len(Rounds) = %d, want 0", len(sb.Rounds))
|
|
}
|
|
}
|
|
|
|
func TestHandleReset_WrongMethod(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/reset", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestHandleNextRound_WrongMethod(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/next", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestHandleState_WrongMethod(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/state", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestHandleAPI(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("Status = %d, want %d", w.Code, http.StatusOK)
|
|
}
|
|
|
|
body, err := io.ReadAll(w.Body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read body: %v", err)
|
|
}
|
|
|
|
if !contains(string(body), `"service"`) {
|
|
t.Errorf("Response missing service field: %s", string(body))
|
|
}
|
|
if !contains(string(body), `"sportscore"`) {
|
|
t.Errorf("Response missing sportscore value: %s", string(body))
|
|
}
|
|
}
|
|
|
|
func TestHandleAPI_ContentType(t *testing.T) {
|
|
s, _ := newTestServer()
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
contentType := w.Header().Get("Content-Type")
|
|
if contentType != "application/json" {
|
|
t.Errorf("Content-Type = %q, want %q", contentType, "application/json")
|
|
}
|
|
}
|
|
|
|
func TestEndToEnd(t *testing.T) {
|
|
// Simulate a complete game
|
|
s, sb := newTestServer()
|
|
defer s.Close()
|
|
|
|
// Round 1: Red scores 3, Green scores 1
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamRed)
|
|
sb.Score(logik.TeamGreen)
|
|
sb.Score(logik.TeamRed)
|
|
|
|
// Check state
|
|
req := httptest.NewRequest(http.MethodGet, "/api/state", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
var state map[string]interface{}
|
|
json.NewDecoder(w.Body).Decode(&state)
|
|
if int(state["red_score"].(float64)) != 3 {
|
|
t.Errorf("After round 1: red_score = %v, want 3", state["red_score"])
|
|
}
|
|
|
|
// Next round
|
|
req = httptest.NewRequest(http.MethodPost, "/api/next", nil)
|
|
w = httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if sb.CurrentRound != 2 {
|
|
t.Errorf("After next: CurrentRound = %d, want 2", sb.CurrentRound)
|
|
}
|
|
}
|
|
|
|
func TestPersistCallback(t *testing.T) {
|
|
sb := logik.NewScoreboard()
|
|
saved := false
|
|
var savedScore int
|
|
|
|
s := NewServer("127.0.0.1:0", sb, func(state interface{}) error {
|
|
saved = true
|
|
savedScore = state.(*logik.Scoreboard).RedScore
|
|
return nil
|
|
})
|
|
defer s.Close()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/score?team=red", nil)
|
|
w := httptest.NewRecorder()
|
|
s.mux.ServeHTTP(w, req)
|
|
|
|
if !saved {
|
|
t.Error("persist callback was not called")
|
|
}
|
|
if savedScore != 1 {
|
|
t.Errorf("savedScore = %d, want 1", savedScore)
|
|
}
|
|
}
|
|
|
|
// contains is a simple string contains check.
|
|
func contains(s, substr string) bool {
|
|
for i := 0; i <= len(s)-len(substr); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|