package football import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "toolbox/pkg/football/logic" "github.com/gin-gonic/gin" ) func TestFootballToolRoutes(t *testing.T) { // Set Gin to test mode gin.SetMode(gin.TestMode) // Create test router and register routes r := gin.New() api := r.Group("/api/football") tool := &footballTool{} tool.RegisterRoutes(api) // 1. Test /api/football/defaults reqDefaults, _ := http.NewRequest(http.MethodGet, "/api/football/defaults", nil) respDefaults := httptest.NewRecorder() r.ServeHTTP(respDefaults, reqDefaults) if respDefaults.Code != http.StatusOK { t.Fatalf("expected status 200 OK for defaults, got %d", respDefaults.Code) } var defaults struct { Groups map[string][]string `json:"groups"` CompletedMatches []logic.Match `json:"completed_matches"` ScoreOutcomes []logic.ScoreOutcome `json:"score_outcomes"` } if err := json.Unmarshal(respDefaults.Body.Bytes(), &defaults); err != nil { t.Fatalf("failed to parse defaults JSON response: %v", err) } if len(defaults.Groups) != 12 { t.Errorf("expected 12 groups, got %d", len(defaults.Groups)) } if len(defaults.CompletedMatches) == 0 { t.Errorf("expected completed matches in default configuration") } // 2. Test /api/football/simulate simReq := SimulateRequest{ Groups: defaults.Groups, CompletedMatches: defaults.CompletedMatches, ScoreOutcomes: defaults.ScoreOutcomes, Simulations: 100, // run a small number for fast test } reqBody, _ := json.Marshal(simReq) reqSim, _ := http.NewRequest(http.MethodPost, "/api/football/simulate", bytes.NewBuffer(reqBody)) reqSim.Header.Set("Content-Type", "application/json") respSim := httptest.NewRecorder() r.ServeHTTP(respSim, reqSim) if respSim.Code != http.StatusOK { t.Fatalf("expected status 200 OK for simulate, got %d. Body: %s", respSim.Code, respSim.Body.String()) } var results []logic.TeamProbability if err := json.Unmarshal(respSim.Body.Bytes(), &results); err != nil { t.Fatalf("failed to parse simulate JSON response: %v", err) } if len(results) != 48 { t.Errorf("expected 48 team probabilities, got %d", len(results)) } // Sum of TotalQualProb across all 48 teams should be exactly 32.0 var sum float64 for _, r := range results { sum += r.TotalQualProb } // Because of floating point representation it should be extremely close to 32.0 if sum < 31.9 || sum > 32.1 { t.Errorf("expected sum of probabilities to be 32.0, got %f", sum) } }