TestHandleDashboardStats_WindowQueryParam: the same endpoint powers both the dashboard at-a-glance strip (default 7-day window) and the Settings 30-day usage card via ?window=30. The handler must faithfully forward the query value to the store + echo the effective window back as sample_window_days.
(t *testing.T)
| 417 | // faithfully forward the query value to the store + echo the |
| 418 | // effective window back as sample_window_days. |
| 419 | func TestHandleDashboardStats_WindowQueryParam(t *testing.T) { |
| 420 | ua, store, token := setupUserAuth(t) |
| 421 | ctx := context.Background() |
| 422 | user, _ := store.GetUserSession(ctx, token) |
| 423 | |
| 424 | // Default: no query → 7-day window. |
| 425 | req := authedRequest("GET", "/api/dashboard/stats", token) |
| 426 | w := httptest.NewRecorder() |
| 427 | ua.HandleDashboardStats(w, req) |
| 428 | if w.Code != http.StatusOK { |
| 429 | t.Fatalf("default status = %d, want 200; body=%s", w.Code, w.Body.String()) |
| 430 | } |
| 431 | var defaultBody struct { |
| 432 | SampleWindowDays int `json:"sample_window_days"` |
| 433 | } |
| 434 | json.NewDecoder(w.Body).Decode(&defaultBody) |
| 435 | if defaultBody.SampleWindowDays != 7 { |
| 436 | t.Errorf("default sample_window_days = %d, want 7", defaultBody.SampleWindowDays) |
| 437 | } |
| 438 | |
| 439 | // ?window=30 → 30-day window. |
| 440 | req = authedRequest("GET", "/api/dashboard/stats?window=30", token) |
| 441 | w = httptest.NewRecorder() |
| 442 | ua.HandleDashboardStats(w, req) |
| 443 | if w.Code != http.StatusOK { |
| 444 | t.Fatalf("window=30 status = %d, want 200", w.Code) |
| 445 | } |
| 446 | var thirtyBody struct { |
| 447 | SampleWindowDays int `json:"sample_window_days"` |
| 448 | InboundWindow int `json:"inbound_window"` |
| 449 | OutboundWindow int `json:"outbound_window"` |
| 450 | } |
| 451 | json.NewDecoder(w.Body).Decode(&thirtyBody) |
| 452 | if thirtyBody.SampleWindowDays != 30 { |
| 453 | t.Errorf("window=30 sample_window_days = %d, want 30", thirtyBody.SampleWindowDays) |
| 454 | } |
| 455 | |
| 456 | // Bad value: handler falls back to default rather than erroring, |
| 457 | // per the comment on HandleDashboardStats. |
| 458 | req = authedRequest("GET", "/api/dashboard/stats?window=not-a-number", token) |
| 459 | w = httptest.NewRecorder() |
| 460 | ua.HandleDashboardStats(w, req) |
| 461 | if w.Code != http.StatusOK { |
| 462 | t.Errorf("garbage window status = %d, want 200 (handler ignores junk)", w.Code) |
| 463 | } |
| 464 | |
| 465 | // Out-of-range: handler clamps at the store layer (90). |
| 466 | req = authedRequest("GET", "/api/dashboard/stats?window=9999", token) |
| 467 | w = httptest.NewRecorder() |
| 468 | ua.HandleDashboardStats(w, req) |
| 469 | var clampedBody struct { |
| 470 | SampleWindowDays int `json:"sample_window_days"` |
| 471 | } |
| 472 | json.NewDecoder(w.Body).Decode(&clampedBody) |
| 473 | if clampedBody.SampleWindowDays != 90 { |
| 474 | t.Errorf("window=9999 clamped to %d, want 90", clampedBody.SampleWindowDays) |
| 475 | } |
| 476 |
nothing calls this directly
no test coverage detected