Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/lib/__tests__/github/fetchActivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,90 @@ import { describe, it, expect } from "vitest";
import { mockFetch, jsonResponse, MOCK_EVENTS } from "./setup";

describe("fetchActivity", () => {
it("Sakamoto algorithm calculates weekday correctly for leap year month >= 3", async () => {
// 2024-03-01 is Friday (5) and it is a leap year.
// 2025-05-18 is Sunday (0) and it is not a leap year.
const edgeEvents = [
{ type: "PushEvent", created_at: "2024-03-01T10:30:00Z" },
{ type: "PushEvent", created_at: "2025-05-18T10:30:00Z" }
];
mockFetch
.mockResolvedValueOnce(jsonResponse(edgeEvents))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse([]));

const { fetchActivity } = await import("../../github");
const result = await fetchActivity("testuser", "fake-token");

// 2024-03-01 (Friday = 5)
expect(result.heatmap[5][10]).toBeGreaterThanOrEqual(1);
// 2025-05-18 (Sunday = 0)
expect(result.heatmap[0][10]).toBeGreaterThanOrEqual(1);
});

it("Sakamoto algorithm calculates weekday correctly for leap year month >= 3", async () => {
// 2024-03-01 is Friday (5) and it is a leap year.
// 2025-05-18 is Sunday (0) and it is not a leap year.
const edgeEvents = [
{ type: "PushEvent", created_at: "2024-03-01T10:30:00Z" },
{ type: "PushEvent", created_at: "2025-05-18T10:30:00Z" }
];
mockFetch
.mockResolvedValueOnce(jsonResponse(edgeEvents))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse([]));

const { fetchActivity } = await import("../../github");
const result = await fetchActivity("testuser", "fake-token");

// 2024-03-01 (Friday = 5)
expect(result.heatmap[5][10]).toBeGreaterThanOrEqual(1);
// 2025-05-18 (Sunday = 0)
expect(result.heatmap[0][10]).toBeGreaterThanOrEqual(1);
});

it("Sakamoto algorithm calculates weekday correctly for month < 3", async () => {
// 2024-02-29 is Thursday (4)
// 2023-01-01 is Sunday (0)
const edgeEvents = [
{ type: "PushEvent", created_at: "2024-02-29T10:30:00Z" },
{ type: "PushEvent", created_at: "2023-01-01T10:30:00Z" }
];
mockFetch
.mockResolvedValueOnce(jsonResponse(edgeEvents))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse([]));

const { fetchActivity } = await import("../../github");
const result = await fetchActivity("testuser", "fake-token");

// 2024-02-29 (Thursday = 4)
expect(result.heatmap[4][10]).toBeGreaterThanOrEqual(1);
// 2023-01-01 (Sunday = 0)
expect(result.heatmap[0][10]).toBeGreaterThanOrEqual(1);
});

it("Sakamoto algorithm calculates weekday correctly for month >= 3 and leap years", async () => {
// 2024-03-01 is Friday (5)
// 2023-05-18 is Thursday (4)
const edgeEvents = [
{ type: "PushEvent", created_at: "2024-03-01T10:30:00Z" },
{ type: "PushEvent", created_at: "2023-05-18T10:30:00Z" }
];
mockFetch
.mockResolvedValueOnce(jsonResponse(edgeEvents))
.mockResolvedValueOnce(jsonResponse([]))
.mockResolvedValueOnce(jsonResponse([]));

const { fetchActivity } = await import("../../github");
const result = await fetchActivity("testuser", "fake-token");

// 2024-03-01 (Friday = 5)
expect(result.heatmap[5][10]).toBe(1);
// 2023-05-18 (Thursday = 4)
expect(result.heatmap[4][10]).toBe(1);
});

it("ヒートマップが 7×24 で初期化される", async () => {
mockFetch
.mockResolvedValueOnce(jsonResponse(MOCK_EVENTS))
Expand Down
15 changes: 14 additions & 1 deletion src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,20 @@ export const fetchActivity = cache(async function fetchActivity(

let day = dayCache.get(datePart);
if (day === undefined) {
day = new Date(datePart).getUTCDay(); // 0=Sun, 6=Sat
// Sakamoto's algorithm for fast day of week calculation
// Avoids allocating a new Date object inside the loop
const charCodeZero = 48; // '0'.charCodeAt(0)
const y = (datePart.charCodeAt(0) - charCodeZero) * 1000 +
(datePart.charCodeAt(1) - charCodeZero) * 100 +
(datePart.charCodeAt(2) - charCodeZero) * 10 +
(datePart.charCodeAt(3) - charCodeZero);
const m = (datePart.charCodeAt(5) - charCodeZero) * 10 +
(datePart.charCodeAt(6) - charCodeZero);
const d = (datePart.charCodeAt(8) - charCodeZero) * 10 +
(datePart.charCodeAt(9) - charCodeZero);
const t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
const yAdjusted = m < 3 ? y - 1 : y;
day = (yAdjusted + Math.floor(yAdjusted / 4) - Math.floor(yAdjusted / 100) + Math.floor(yAdjusted / 400) + t[m - 1] + d) % 7;
dayCache.set(datePart, day);
}

Expand Down
Loading