Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

Stop two food-history tests from rotting with the calendar (#176)

Carteraa0caa19eb1c
diff --git a/backend/controllers/food_test.go b/backend/controllers/food_test.go
index ffe0f48..f397e66 100644
--- a/backend/controllers/food_test.go
+++ b/backend/controllers/food_test.go
@@ -1437,11 +1437,16 @@ func TestGetFoodHistory_agreesWithDailyTotals(t *testing.T) {
uid := createTestUser(t)
setUserTimezone(t, uid, "America/New_York")
- // A fixed instant with a hard-coded expected day, rather than deriving the day
- // with the same zone lookup the endpoints use — that would let a wrong lookup
- // agree with itself. 01:00 UTC on Aug 8 is 21:00 on Aug 7 in New York.
- insertFoodLogAt(t, uid, "Late dinner", 700, time.Date(2026, 8, 8, 1, 0, 0, 0, time.UTC))
- const day = "2026-08-07"
+ // The expected day is still worked out without the zone lookup the endpoints use —
+ // that would let a wrong lookup agree with itself — but no longer from a fixed date.
+ // It was pinned to Aug 2026 and asked for days=400, which is above the handler's 365
+ // cap and so silently became 30: the test passed until Aug 7 aged out of that window,
+ // then failed on its own. 01:00 UTC is always the previous calendar day in New York
+ // (21:00 in EDT, 20:00 in EST), so plain date arithmetic gives the answer.
+ now := time.Now().UTC()
+ at := time.Date(now.Year(), now.Month(), now.Day(), 1, 0, 0, 0, time.UTC).AddDate(0, 0, -10)
+ insertFoodLogAt(t, uid, "Late dinner", 700, at)
+ day := at.AddDate(0, 0, -1).Format("2006-01-02")
c, w := newContext(uid, http.MethodGet, "/api/v1/food/stats?date="+day, nil)
th.GetDailyStats(c)
@@ -1450,7 +1455,7 @@ func TestGetFoodHistory_agreesWithDailyTotals(t *testing.T) {
t.Fatalf("daily totals put the 21:00-local entry somewhere else: got %v for %s", dailyCals, day)
}
- c, w = newContext(uid, http.MethodGet, "/api/v1/food/history?days=400", nil)
+ c, w = newContext(uid, http.MethodGet, "/api/v1/food/history?days=60", nil)
th.GetFoodHistory(c)
for _, p := range decodeResponse(t, w)["data"].([]any) {
if pt := p.(map[string]any); pt["date"].(string) == day {
diff --git a/backend/controllers/weight_test.go b/backend/controllers/weight_test.go
index 2d1439b..89809dc 100644
--- a/backend/controllers/weight_test.go
+++ b/backend/controllers/weight_test.go
@@ -731,21 +731,34 @@ func TestFoodHistory_neverReturnsANonDate(t *testing.T) {
VALUES (?, 'unreadable', 'lunch', 100, 0, 0, 0, 1, 'not-a-timestamp', '')`, uid); err != nil {
t.Fatalf("seed unreadable food: %v", err)
}
+ // Relative, not pinned. This was seeded at 2026-08-05 and asked for days=3650, which
+ // is above the handler's 365 cap and silently became 30 — so once Aug 5 aged out, the
+ // history came back empty and the loop below checked nothing while the test passed.
+ good := time.Now().UTC().AddDate(0, 0, -5)
+ goodDay := good.Format("2006-01-02")
if _, err := db.DB.Exec(
`INSERT INTO food_logs (user_id, name, meal, calories, protein, carbs, fat, servings, logged_at, logged_on)
- VALUES (?, 'good', 'lunch', 200, 0, 0, 0, 1, '2026-08-05T12:00:00Z', '2026-08-05')`, uid); err != nil {
+ VALUES (?, 'good', 'lunch', 200, 0, 0, 0, 1, ?, ?)`, uid, good.Format(time.RFC3339), goodDay); err != nil {
t.Fatalf("seed good food: %v", err)
}
- c, w := newContext(uid, http.MethodGet, "/api/v1/food/history?days=3650", nil)
+ c, w := newContext(uid, http.MethodGet, "/api/v1/food/history?days=60", nil)
th.GetFoodHistory(c)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
+ sawGood := false
for _, p := range decodeResponse(t, w)["data"].([]any) {
day := p.(map[string]any)["date"].(string)
if _, err := time.Parse("2006-01-02", day); err != nil {
t.Errorf("history returned %q as a date — clients parse this and the page throws", day)
}
+ if day == goodDay {
+ sawGood = true
+ }
+ }
+ // Without this an empty response passes, which is exactly how this test went quiet.
+ if !sawGood {
+ t.Errorf("history has no bucket for the readable entry on %s — the check above ran on nothing", goodDay)
}
}