1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import { describe, it, expect } from "bun:test";
import {
buildHeatmap,
formatDateKey,
levelFor,
startOfUtcDay,
daysBetween,
__internal,
} from "../lib/contribution-heatmap";
describe("heatmap — levelFor", () => {
it("0 count → level 0 regardless of max", () => {
expect(levelFor(0, 0)).toBe(0);
expect(levelFor(0, 10)).toBe(0);
});
it("max=0 keeps everything at 0", () => {
expect(levelFor(5, 0)).toBe(0);
});
it("buckets by quartile of max", () => {
expect(levelFor(1, 10)).toBe(1);
expect(levelFor(3, 10)).toBe(2);
expect(levelFor(6, 10)).toBe(3);
expect(levelFor(9, 10)).toBe(4);
});
it("edge — exactly 25% → level 1", () => {
expect(levelFor(25, 100)).toBe(1);
expect(levelFor(26, 100)).toBe(2);
});
});
describe("heatmap — formatDateKey + startOfUtcDay", () => {
it("formats YYYY-MM-DD in UTC", () => {
const d = new Date("2026-01-02T23:59:59Z");
expect(formatDateKey(d)).toBe("2026-01-02");
});
it("strips time to 00:00:00 UTC", () => {
const d = startOfUtcDay(new Date("2026-03-15T18:30:00Z"));
expect(d.getUTCHours()).toBe(0);
expect(d.getUTCMinutes()).toBe(0);
expect(d.getUTCSeconds()).toBe(0);
expect(formatDateKey(d)).toBe("2026-03-15");
});
it("__internal re-exports match", () => {
expect(__internal.formatDateKey).toBe(formatDateKey);
expect(__internal.startOfUtcDay).toBe(startOfUtcDay);
});
});
describe("heatmap — daysBetween", () => {
it("returns 0 for same UTC day regardless of time", () => {
const a = new Date("2026-01-02T00:00:00Z");
const b = new Date("2026-01-02T23:59:59Z");
expect(daysBetween(a, b)).toBe(0);
});
it("returns positive delta for later end", () => {
expect(
daysBetween(new Date("2026-01-01Z"), new Date("2026-01-05Z"))
).toBe(4);
});
});
describe("heatmap — buildHeatmap", () => {
const TODAY = new Date("2026-12-15T12:00:00Z");
it("empty activity → zero totals, no levels above 0", () => {
const h = buildHeatmap([], 365, TODAY);
expect(h.totalContributions).toBe(0);
expect(h.maxDayCount).toBe(0);
expect(h.longestStreak).toBe(0);
expect(h.currentStreak).toBe(0);
for (const w of h.weeks) {
for (const d of w.days) {
if (d) expect(d.level).toBe(0);
}
}
});
it("sums contributions on the same day", () => {
const h = buildHeatmap(
[
{ createdAt: "2026-12-10T10:00:00Z" },
{ createdAt: "2026-12-10T11:00:00Z" },
{ createdAt: "2026-12-10T23:59:59Z" },
],
30,
TODAY
);
expect(h.totalContributions).toBe(3);
expect(h.maxDayCount).toBe(3);
});
it("drops activity outside the window", () => {
const h = buildHeatmap(
[
{ createdAt: "2025-01-01T00:00:00Z" },
{ createdAt: "2026-12-14T00:00:00Z" },
],
30,
TODAY
);
expect(h.totalContributions).toBe(1);
});
it("computes longest + current streaks", () => {
const h = buildHeatmap(
[
{ createdAt: "2026-12-13T00:00:00Z" },
{ createdAt: "2026-12-14T00:00:00Z" },
{ createdAt: "2026-12-15T00:00:00Z" },
],
30,
TODAY
);
expect(h.longestStreak).toBe(3);
expect(h.currentStreak).toBe(3);
});
it("currentStreak is 0 when today has no activity", () => {
const h = buildHeatmap(
[
{ createdAt: "2026-12-13T00:00:00Z" },
{ createdAt: "2026-12-14T00:00:00Z" },
],
30,
TODAY
);
expect(h.longestStreak).toBe(2);
expect(h.currentStreak).toBe(0);
});
it("weeks are Sunday-aligned with 7 entries each", () => {
const h = buildHeatmap([], 30, TODAY);
for (const w of h.weeks) {
expect(w.days.length).toBe(7);
}
for (const w of h.weeks) {
w.days.forEach((d, idx) => {
if (d) expect(d.dow).toBe(idx);
});
}
});
it("start/end dates reflect the window", () => {
const h = buildHeatmap([], 10, TODAY);
expect(h.endDate).toBe("2026-12-15");
expect(h.startDate).toBe("2026-12-06");
});
it("tolerates invalid createdAt entries", () => {
const h = buildHeatmap(
[
{ createdAt: "not a date" },
{ createdAt: "2026-12-14T00:00:00Z" },
],
30,
TODAY
);
expect(h.totalContributions).toBe(1);
});
it("grid covers entire window continuously", () => {
const h = buildHeatmap([], 14, TODAY);
const nonNull: string[] = [];
for (const w of h.weeks) {
for (const d of w.days) {
if (d) nonNull.push(d.date);
}
}
expect(nonNull.length).toBe(14);
for (let i = 1; i < nonNull.length; i++) {
expect(nonNull[i] > nonNull[i - 1]).toBe(true);
}
});
});
|