| 1 | import {
|
| 2 | filterByCategory,
|
| 3 | groupByDate,
|
| 4 | mergeAndSort,
|
| 5 | } from "@/lib/utils/history";
|
| 6 | import type { HistoryCard, HistoryCategory } from "@/lib/history/types";
|
| 7 |
|
| 8 | function card(
|
| 9 | id: string,
|
| 10 | category: HistoryCategory,
|
| 11 | at: Date,
|
| 12 | ): HistoryCard {
|
| 13 | return {
|
| 14 | id,
|
| 15 | category,
|
| 16 | timestamp: at.toISOString(),
|
| 17 | title: id,
|
| 18 | summary: "",
|
| 19 | } as HistoryCard;
|
| 20 | }
|
| 21 |
|
| 22 | describe("mergeAndSort", () => {
|
| 23 | it("flattens the buckets into one descending list", () => {
|
| 24 | const oldest = card("oldest", "mood", new Date(2025, 0, 1, 10, 0));
|
| 25 | const newest = card("newest", "sleep", new Date(2025, 0, 3, 10, 0));
|
| 26 | const middle = card("middle", "trigger", new Date(2025, 0, 2, 10, 0));
|
| 27 |
|
| 28 | const merged = mergeAndSort([[oldest, middle], [newest]]);
|
| 29 |
|
| 30 | expect(merged.map((c) => c.id)).toEqual(["newest", "middle", "oldest"]);
|
| 31 | });
|
| 32 |
|
| 33 | it("returns an empty list for no buckets", () => {
|
| 34 | expect(mergeAndSort([])).toEqual([]);
|
| 35 | });
|
| 36 | });
|
| 37 |
|
| 38 | describe("groupByDate", () => {
|
| 39 | it("groups into fixed, ordered date buckets", () => {
|
| 40 | const morning = card("m1", "mood", new Date(2025, 0, 15, 9, 0));
|
| 41 | const evening = card("s1", "sleep", new Date(2025, 0, 15, 20, 0));
|
| 42 | const previous = card("t1", "trigger", new Date(2025, 0, 14, 12, 0));
|
| 43 |
|
| 44 | const grouped = groupByDate([morning, evening, previous]);
|
| 45 |
|
| 46 | expect(Object.keys(grouped)).toEqual([
|
| 47 | "quarta-feira, 15 de janeiro",
|
| 48 | "terça-feira, 14 de janeiro",
|
| 49 | ]);
|
| 50 | expect(grouped["quarta-feira, 15 de janeiro"].map((c) => c.id)).toEqual([
|
| 51 | "m1",
|
| 52 | "s1",
|
| 53 | ]);
|
| 54 | expect(grouped["terça-feira, 14 de janeiro"].map((c) => c.id)).toEqual([
|
| 55 | "t1",
|
| 56 | ]);
|
| 57 | });
|
| 58 |
|
| 59 | it("returns no buckets for an empty list", () => {
|
| 60 | expect(groupByDate([])).toEqual({});
|
| 61 | });
|
| 62 | });
|
| 63 |
|
| 64 | describe("filterByCategory", () => {
|
| 65 | const cards = [
|
| 66 | card("m", "mood", new Date(2025, 0, 1, 10, 0)),
|
| 67 | card("s", "sleep", new Date(2025, 0, 2, 10, 0)),
|
| 68 | card("t", "trigger", new Date(2025, 0, 3, 10, 0)),
|
| 69 | card("c", "care_action", new Date(2025, 0, 4, 10, 0)),
|
| 70 | ];
|
| 71 |
|
| 72 | it.each([
|
| 73 | ["mood", ["m"]],
|
| 74 | ["sleep", ["s"]],
|
| 75 | ["trigger", ["t"]],
|
| 76 | ["care_action", ["c"]],
|
| 77 | ] as const)("keeps only the %s cards", (category, ids) => {
|
| 78 | expect(filterByCategory(cards, category).map((c) => c.id)).toEqual(ids);
|
| 79 | });
|
| 80 |
|
| 81 | it("keeps everything for 'all'", () => {
|
| 82 | expect(filterByCategory(cards, "all").map((c) => c.id)).toEqual([
|
| 83 | "m",
|
| 84 | "s",
|
| 85 | "t",
|
| 86 | "c",
|
| 87 | ]);
|
| 88 | });
|
| 89 | });
|