|
| 1 | +import { expect, test, mock, beforeEach, describe } from "bun:test"; |
| 2 | + |
| 3 | +describe("GitHubButton Caching Logic", () => { |
| 4 | + const namespace = "test-owner"; |
| 5 | + const repo = "test-repo"; |
| 6 | + const cacheKey = `gh-count-${namespace}-${repo}`; |
| 7 | + const CACHE_DURATION = 2 * 60 * 60 * 1000; |
| 8 | + |
| 9 | + // Mocking localStorage |
| 10 | + const localStorageMock = (() => { |
| 11 | + let store: Record<string, string> = {}; |
| 12 | + return { |
| 13 | + getItem: (key: string) => store[key] || null, |
| 14 | + setItem: (key: string, value: string) => { |
| 15 | + store[key] = value.toString(); |
| 16 | + }, |
| 17 | + clear: () => { |
| 18 | + store = {}; |
| 19 | + }, |
| 20 | + }; |
| 21 | + })(); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + localStorageMock.clear(); |
| 25 | + mock.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + // Since we can't easily test the component without React dependencies, |
| 29 | + // we can test the logic that would be inside the component. |
| 30 | + // We'll simulate the fetchCount function's behavior. |
| 31 | + |
| 32 | + async function simulateFetchCount(type: string, signal: any) { |
| 33 | + let count: number | null = null; |
| 34 | + |
| 35 | + // Try local cache first |
| 36 | + try { |
| 37 | + const cached = localStorageMock.getItem(cacheKey); |
| 38 | + if (cached) { |
| 39 | + const { data, timestamp } = JSON.parse(cached) as { |
| 40 | + data: any; |
| 41 | + timestamp: number; |
| 42 | + }; |
| 43 | + if (Date.now() - timestamp < CACHE_DURATION) { |
| 44 | + const nextCount = data[`${type}_count`]; |
| 45 | + if (typeof nextCount === "number") { |
| 46 | + count = nextCount; |
| 47 | + return count; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } catch { |
| 52 | + // Ignore localStorage errors |
| 53 | + } |
| 54 | + |
| 55 | + const response = await fetch(`https://api.github.com/repos/${namespace}/${repo}`, { |
| 56 | + signal, |
| 57 | + headers: { |
| 58 | + Accept: "application/vnd.github+json", |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + if (!response.ok) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + const data = await response.json(); |
| 67 | + |
| 68 | + // Save to cache |
| 69 | + try { |
| 70 | + localStorageMock.setItem(cacheKey, JSON.stringify({ data, timestamp: Date.now() })); |
| 71 | + } catch { |
| 72 | + // Ignore localStorage errors |
| 73 | + } |
| 74 | + |
| 75 | + const nextCount = data[`${type}_count`]; |
| 76 | + if (typeof nextCount === "number") { |
| 77 | + count = nextCount; |
| 78 | + } |
| 79 | + return count; |
| 80 | + } |
| 81 | + |
| 82 | + test("fetches from API when no cache exists", async () => { |
| 83 | + const mockFetch = mock(() => |
| 84 | + Promise.resolve({ |
| 85 | + ok: true, |
| 86 | + json: () => Promise.resolve({ stargazers_count: 123 }), |
| 87 | + }) |
| 88 | + ); |
| 89 | + global.fetch = mockFetch as any; |
| 90 | + |
| 91 | + const count = await simulateFetchCount("stargazers", null); |
| 92 | + |
| 93 | + expect(count).toBe(123); |
| 94 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 95 | + const cached = JSON.parse(localStorageMock.getItem(cacheKey) || "{}"); |
| 96 | + expect(cached.data.stargazers_count).toBe(123); |
| 97 | + }); |
| 98 | + |
| 99 | + test("uses cache when valid", async () => { |
| 100 | + const mockData = { stargazers_count: 456 }; |
| 101 | + localStorageMock.setItem( |
| 102 | + cacheKey, |
| 103 | + JSON.stringify({ data: mockData, timestamp: Date.now() }) |
| 104 | + ); |
| 105 | + |
| 106 | + const mockFetch = mock(() => |
| 107 | + Promise.resolve({ |
| 108 | + ok: true, |
| 109 | + json: () => Promise.resolve({ stargazers_count: 789 }), |
| 110 | + }) |
| 111 | + ); |
| 112 | + global.fetch = mockFetch as any; |
| 113 | + |
| 114 | + const count = await simulateFetchCount("stargazers", null); |
| 115 | + |
| 116 | + expect(count).toBe(456); |
| 117 | + // Should not have called fetch because cache was valid |
| 118 | + expect(mockFetch).toHaveBeenCalledTimes(0); |
| 119 | + }); |
| 120 | + |
| 121 | + test("fetches from API when cache is expired", async () => { |
| 122 | + const expiredTimestamp = Date.now() - 3 * 60 * 60 * 1000; // 3 hours ago |
| 123 | + const mockData = { stargazers_count: 456 }; |
| 124 | + localStorageMock.setItem( |
| 125 | + cacheKey, |
| 126 | + JSON.stringify({ data: mockData, timestamp: expiredTimestamp }) |
| 127 | + ); |
| 128 | + |
| 129 | + const mockFetch = mock(() => |
| 130 | + Promise.resolve({ |
| 131 | + ok: true, |
| 132 | + json: () => Promise.resolve({ stargazers_count: 789 }), |
| 133 | + }) |
| 134 | + ); |
| 135 | + global.fetch = mockFetch as any; |
| 136 | + |
| 137 | + const count = await simulateFetchCount("stargazers", null); |
| 138 | + |
| 139 | + expect(count).toBe(789); |
| 140 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 141 | + const cached = JSON.parse(localStorageMock.getItem(cacheKey) || "{}"); |
| 142 | + expect(cached.data.stargazers_count).toBe(789); |
| 143 | + }); |
| 144 | +}); |
0 commit comments