eletrotupi / tcc/ commit / 6974d55

frontend: carry rounded minutes into the hour in the sleep mapper

The sleep card rounded the leftover minutes without carrying them, so an
average of 7.999 rendered as "7h 60min". Minutes are now derived from
the total, which folds the overflow into the hour, and a test pins it
Pedro Lucas Porcellis porcellis@eletrotupi.com · 9 days ago 6974d5508f215de8c94b04469cf20d238069477f Browse files
Parents dcee93d
Changed files
2
Additions
+12
Deletions
-2
frontend/lib/history/mappers.ts
1 1
import type { HistoryCard } from "@/lib/history/types";
2 2
import type { MoodEntry, SleepRecord, Trigger, CareAction } from "@/lib/api";
3 3
import { getMood } from "@/constants/moods";
4 4

5 5

35 35
    variant: "red",
36 36
  };
37 37
}
38 38

39 39
export function mapSleepToHistoryCard(record: SleepRecord): HistoryCard {
40 -
  const hours = Math.floor(record.average);
41 -
  const minutes = Math.round((record.average - hours) * 60);
40 +
  const totalMinutes = Math.round(record.average * 60);
41 +
  const hours = Math.floor(totalMinutes / 60);
42 +
  const minutes = totalMinutes % 60;
42 43
  const duration = `${hours}h ${minutes.toString().padStart(2, "0")}min`;
43 44
  const timestamp = typeof record.createdAt === 'string'
44 45
    ? record.createdAt
45 46
    : record.createdAt.toISOString();
46 47

159 160
        summary: "",
160 161
        raw: record,
161 162
      };
162 163
  }
163 164
}
frontend/tests/history/mappers.unit.test.ts
1 1
import {
2 2
  mapCareActionToHistoryCard,
3 3
  mapMoodToHistoryCard,
4 4
  mapSleepToHistoryCard,
5 5
  mapTriggerToHistoryCard,
203 203
    );
204 204
    expect(mapSleepToHistoryCard(sleep({ average: 6.0167 })).summary).toBe(
205 205
      "6h 01min",
206 206
    );
207 207
  });
208 +

209 +
  it("carries rounded minutes into the hour", () => {
210 +
    expect(mapSleepToHistoryCard(sleep({ average: 7.999 })).summary).toBe(
211 +
      "8h 00min",
212 +
    );
213 +
    expect(mapSleepToHistoryCard(sleep({ average: 5.999 })).summary).toBe(
214 +
      "6h 00min",
215 +
    );
216 +
  });
208 217
});
209 218

210 219
describe("mapTriggerToHistoryCard", () => {
211 220
  it("maps the record into a trigger card", () => {
212 221
    const record = trigger({
432 441
      summary: "",
433 442
      raw: record,
434 443
    });
435 444
  });
436 445
});