|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { |
3 | 3 | completedWorkoutsCount, |
| 4 | + countCompletedInCurrentWeek, |
4 | 5 | formatCompactKg, |
5 | 6 | getExercisePR, |
6 | 7 | personalRecordsExerciseCount, |
@@ -61,10 +62,79 @@ describe("personalRecordsExerciseCount", () => { |
61 | 62 | }); |
62 | 63 |
|
63 | 64 | describe("weeklyVolumeSeriesNow", () => { |
| 65 | + afterEach(() => { |
| 66 | + vi.useRealTimers(); |
| 67 | + }); |
| 68 | + |
64 | 69 | it("returns 7 volume slots", () => { |
65 | 70 | const { volumeKg } = weeklyVolumeSeriesNow([]); |
66 | 71 | expect(volumeKg).toHaveLength(7); |
67 | 72 | }); |
| 73 | + |
| 74 | + function done(startedAt: string, weightKg: number, reps: number) { |
| 75 | + return { |
| 76 | + startedAt, |
| 77 | + endedAt: startedAt, |
| 78 | + items: [{ type: "strength", sets: [{ weightKg, reps }] }], |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + // Domain invariant: week boundaries are Europe/Kyiv, not the host tz. |
| 83 | + // Mon 2026-06-08 00:00 Kyiv (EEST, UTC+3) = 2026-06-07T21:00:00Z. |
| 84 | + it("anchors the Mon..Sun week to Europe/Kyiv regardless of host tz", () => { |
| 85 | + vi.useFakeTimers(); |
| 86 | + vi.setSystemTime(new Date("2026-06-10T12:00:00Z")); // Wed of that week |
| 87 | + |
| 88 | + const { weekStartMs, volumeKg } = weeklyVolumeSeriesNow([ |
| 89 | + done("2026-06-07T20:30:00Z", 100, 1), // Sun 23:30 Kyiv → previous week |
| 90 | + done("2026-06-07T21:30:00Z", 60, 5), // Mon 00:30 Kyiv → idx 0 |
| 91 | + done("2026-06-10T10:00:00Z", 40, 10), // Wed 13:00 Kyiv → idx 2 |
| 92 | + ]); |
| 93 | + |
| 94 | + expect(weekStartMs).toBe(Date.parse("2026-06-07T21:00:00Z")); |
| 95 | + expect(volumeKg).toEqual([300, 0, 400, 0, 0, 0, 0]); |
| 96 | + }); |
| 97 | + |
| 98 | + // DST week: Kyiv springs forward Sun 2026-03-29 03:00 EET → 04:00 EEST, |
| 99 | + // so the week Mon 2026-03-23 .. Sun 2026-03-29 is 167 hours long. |
| 100 | + it("buckets days correctly across the spring-forward DST week", () => { |
| 101 | + vi.useFakeTimers(); |
| 102 | + vi.setSystemTime(new Date("2026-03-26T12:00:00Z")); // Thu of DST week |
| 103 | + |
| 104 | + const { weekStartMs, volumeKg } = weeklyVolumeSeriesNow([ |
| 105 | + done("2026-03-22T22:30:00Z", 60, 5), // Mon 00:30 Kyiv (EET) → idx 0 |
| 106 | + done("2026-03-29T20:30:00Z", 100, 1), // Sun 23:30 Kyiv (EEST) → idx 6 |
| 107 | + done("2026-03-29T21:30:00Z", 999, 1), // Mon 00:30 Kyiv next week → out |
| 108 | + ]); |
| 109 | + |
| 110 | + // Mon 2026-03-23 00:00 Kyiv (EET, UTC+2) = 2026-03-22T22:00:00Z. |
| 111 | + expect(weekStartMs).toBe(Date.parse("2026-03-22T22:00:00Z")); |
| 112 | + expect(volumeKg).toEqual([300, 0, 0, 0, 0, 0, 100]); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("countCompletedInCurrentWeek", () => { |
| 117 | + afterEach(() => { |
| 118 | + vi.useRealTimers(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("uses the Kyiv week boundary, not the host-local one", () => { |
| 122 | + vi.useFakeTimers(); |
| 123 | + vi.setSystemTime(new Date("2026-06-10T12:00:00Z")); |
| 124 | + |
| 125 | + const mk = (startedAt: string) => ({ |
| 126 | + startedAt, |
| 127 | + endedAt: startedAt, |
| 128 | + items: [], |
| 129 | + }); |
| 130 | + expect( |
| 131 | + countCompletedInCurrentWeek([ |
| 132 | + mk("2026-06-07T20:30:00Z"), // Sun 23:30 Kyiv → previous week |
| 133 | + mk("2026-06-07T21:30:00Z"), // Mon 00:30 Kyiv → this week |
| 134 | + mk("2026-06-12T10:00:00Z"), // Fri → this week |
| 135 | + ]), |
| 136 | + ).toBe(2); |
| 137 | + }); |
68 | 138 | }); |
69 | 139 |
|
70 | 140 | describe("formatCompactKg", () => { |
|
0 commit comments