| 1 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | import { fetchRepoStats, lastPageFromLink, relativeAge, relativeTime } from "./github"; |
| 3 | |
| 4 | // We test the pure helper functions directly. |
| 5 | // The async fetch functions require mocking the global fetch. |
| 6 | |
| 7 | describe("production-build fallback", () => { |
| 8 | it("keeps static generation offline and uses the dated snapshot and never a zero", async () => { |
| 9 | const previousPhase = process.env.NEXT_PHASE; |
| 10 | process.env.NEXT_PHASE = "phase-production-build"; |
| 11 | const fetchMock = vi.fn(); |
| 12 | vi.stubGlobal("fetch", fetchMock); |
| 13 | |
| 14 | try { |
| 15 | const stats = await fetchRepoStats(); |
| 16 | expect(stats.source).toBe("snapshot"); |
| 17 | expect(stats.stars).toBeGreaterThan(0); |
| 18 | expect(stats.contributors).toBeNull(); |
| 19 | expect(fetchMock).not.toHaveBeenCalled(); |
| 20 | } finally { |
| 21 | if (previousPhase === undefined) delete process.env.NEXT_PHASE; |
| 22 | else process.env.NEXT_PHASE = previousPhase; |
| 23 | vi.unstubAllGlobals(); |
| 24 | } |
| 25 | }); |
| 26 | }); |
| 27 | |
| 28 | // ── relativeTime ────────────────────────────────────────────────────── |
| 29 | |
| 30 | describe("relativeTime", () => { |
| 31 | beforeEach(() => { |
| 32 | vi.useFakeTimers(); |
| 33 | vi.setSystemTime(new Date("2026-06-01T12:00:00Z")); |
| 34 | }); |
| 35 | |
| 36 | afterEach(() => { |
| 37 | vi.useRealTimers(); |
| 38 | }); |
| 39 | |
| 40 | it("returns 'just now' for less than 30 seconds ago", () => { |
| 41 | expect(relativeTime("2026-06-01T11:59:45Z")).toBe("just now"); |
| 42 | }); |
| 43 | |
| 44 | it("returns 'just now' for dates in the future", () => { |
| 45 | expect(relativeTime("2026-06-02T12:00:00Z")).toBe("just now"); |
| 46 | }); |
| 47 | |
| 48 | it("returns minutes for < 1 hour", () => { |
| 49 | expect(relativeTime("2026-06-01T11:55:00Z")).toBe("5m"); |
| 50 | expect(relativeTime("2026-06-01T11:30:00Z")).toBe("30m"); |
| 51 | }); |
| 52 | |
| 53 | it("returns hours for < 1 day", () => { |
| 54 | expect(relativeTime("2026-06-01T09:00:00Z")).toBe("3h"); |
| 55 | expect(relativeTime("2026-05-31T18:00:00Z")).toBe("18h"); |
| 56 | }); |
| 57 | |
| 58 | it("returns days for < 30 days", () => { |
| 59 | expect(relativeTime("2026-05-25T12:00:00Z")).toBe("7d"); |
| 60 | expect(relativeTime("2026-05-03T12:00:00Z")).toBe("29d"); |
| 61 | }); |
| 62 | |
| 63 | it("returns months for < 12 months", () => { |
| 64 | expect(relativeTime("2026-03-01T12:00:00Z")).toBe("3mo"); |
| 65 | expect(relativeTime("2025-08-01T12:00:00Z")).toBe("10mo"); |
| 66 | }); |
| 67 | |
| 68 | it("returns years for >= 12 months", () => { |
| 69 | expect(relativeTime("2024-06-01T12:00:00Z")).toBe("2y"); |
| 70 | expect(relativeTime("2025-01-01T00:00:00Z")).toBe("1y"); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | // ── relativeAge ─────────────────────────────────────────────────────── |
| 75 | |
| 76 | describe("relativeAge", () => { |
| 77 | beforeEach(() => { |
| 78 | vi.useFakeTimers(); |
| 79 | vi.setSystemTime(new Date("2026-06-01T12:00:00Z")); |
| 80 | }); |
| 81 | |
| 82 | afterEach(() => { |
| 83 | vi.useRealTimers(); |
| 84 | }); |
| 85 | |
| 86 | it("reports past ages as the negative counts Intl.RelativeTimeFormat wants", () => { |
| 87 | expect(relativeAge("2026-06-01T11:55:00Z")).toEqual({ value: -5, unit: "minute" }); |
| 88 | expect(relativeAge("2026-06-01T09:00:00Z")).toEqual({ value: -3, unit: "hour" }); |
| 89 | expect(relativeAge("2026-05-25T12:00:00Z")).toEqual({ value: -7, unit: "day" }); |
| 90 | expect(relativeAge("2026-03-01T12:00:00Z")).toEqual({ value: -3, unit: "month" }); |
| 91 | expect(relativeAge("2024-06-01T12:00:00Z")).toEqual({ value: -2, unit: "year" }); |
| 92 | }); |
| 93 | |
| 94 | it("collapses sub-minute, future, and unparseable dates to the locale's 'now'", () => { |
| 95 | expect(relativeAge("2026-06-01T11:59:45Z")).toEqual({ value: 0, unit: "second" }); |
| 96 | expect(relativeAge("2026-06-02T12:00:00Z")).toEqual({ value: 0, unit: "second" }); |
| 97 | expect(relativeAge("not-a-date")).toEqual({ value: 0, unit: "second" }); |
| 98 | }); |
| 99 | |
| 100 | it("agrees with the compact English form it backs", () => { |
| 101 | for (const iso of [ |
| 102 | "2026-06-01T11:59:45Z", |
| 103 | "2026-06-01T11:30:00Z", |
| 104 | "2026-05-31T18:00:00Z", |
| 105 | "2026-05-03T12:00:00Z", |
| 106 | "2025-08-01T12:00:00Z", |
| 107 | "2025-01-01T00:00:00Z", |
| 108 | ]) { |
| 109 | const age = relativeAge(iso); |
| 110 | const compact = relativeTime(iso); |
| 111 | if (age.unit === "second") expect(compact).toBe("just now"); |
| 112 | else expect(compact.startsWith(String(Math.abs(age.value)))).toBe(true); |
| 113 | } |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | // ── lastPageFromLink (via re-export test) ────────────────────────────── |
| 118 | |
| 119 | describe("lastPageFromLink", () => { |
| 120 | it("returns undefined for null input", () => { |
| 121 | expect(lastPageFromLink(null)).toBeUndefined(); |
| 122 | }); |
| 123 | |
| 124 | it("returns undefined for empty string", () => { |
| 125 | expect(lastPageFromLink("")).toBeUndefined(); |
| 126 | }); |
| 127 | |
| 128 | it("extracts page from Link header with last rel", () => { |
| 129 | const link = |
| 130 | '<https://api.github.com/repos/codewhale-hq/CodeWhale/issues?page=5>; rel="last"'; |
| 131 | expect(lastPageFromLink(link)).toBe(5); |
| 132 | }); |
| 133 | |
| 134 | it("extracts page from multi-part Link header", () => { |
| 135 | const link = [ |
| 136 | '<https://api.github.com/repos/codewhale-hq/CodeWhale/issues?page=1>; rel="prev"', |
| 137 | '<https://api.github.com/repos/codewhale-hq/CodeWhale/issues?page=3>; rel="last"', |
| 138 | ].join(", "); |
| 139 | expect(lastPageFromLink(link)).toBe(3); |
| 140 | }); |
| 141 | |
| 142 | it("returns undefined when no last rel present", () => { |
| 143 | const link = |
| 144 | '<https://api.github.com/repos/codewhale-hq/CodeWhale/issues?page=1>; rel="prev"'; |
| 145 | expect(lastPageFromLink(link)).toBeUndefined(); |
| 146 | }); |
| 147 | |
| 148 | it("returns undefined for invalid URL format", () => { |
| 149 | const link = "not-a-valid-link-header; rel=last"; |
| 150 | expect(lastPageFromLink(link)).toBeUndefined(); |
| 151 | }); |
| 152 | }); |
| 153 |