|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import ContributorsPage from './page'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | + |
| 5 | +describe('ContributorsPage - Timezone Boundaries & Calendar Alignment', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.restoreAllMocks(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('verifies standard timestamp formatting in rate limit message', async () => { |
| 11 | + const timestamp = 1704067200; // 2024-01-01T00:00:00.000Z |
| 12 | + global.fetch = vi.fn(() => |
| 13 | + Promise.resolve({ |
| 14 | + ok: false, |
| 15 | + status: 403, |
| 16 | + headers: { |
| 17 | + get: (name: string) => { |
| 18 | + if (name === 'x-ratelimit-remaining') return '0'; |
| 19 | + if (name === 'x-ratelimit-reset') return String(timestamp); |
| 20 | + return null; |
| 21 | + }, |
| 22 | + }, |
| 23 | + json: async () => [], |
| 24 | + }) |
| 25 | + ) as unknown as typeof fetch; |
| 26 | + |
| 27 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 28 | + |
| 29 | + const element = await ContributorsPage(); |
| 30 | + // Element renders fallback UI because of the API rate limit error, fetch fails gracefully |
| 31 | + expect(element).toBeDefined(); |
| 32 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 33 | + 'Failed to fetch contributors:', |
| 34 | + expect.objectContaining({ |
| 35 | + message: expect.stringContaining('Please try again after 2024-01-01T00:00:00.000Z.'), |
| 36 | + }) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it('verifies timezone/calendar alignment during leap year boundaries', async () => { |
| 41 | + const timestamp = 1709164800; // 2024-02-29T00:00:00.000Z (Leap day) |
| 42 | + global.fetch = vi.fn(() => |
| 43 | + Promise.resolve({ |
| 44 | + ok: false, |
| 45 | + status: 403, |
| 46 | + headers: { |
| 47 | + get: (name: string) => { |
| 48 | + if (name === 'x-ratelimit-remaining') return '0'; |
| 49 | + if (name === 'x-ratelimit-reset') return String(timestamp); |
| 50 | + return null; |
| 51 | + }, |
| 52 | + }, |
| 53 | + json: async () => [], |
| 54 | + }) |
| 55 | + ) as unknown as typeof fetch; |
| 56 | + |
| 57 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 58 | + |
| 59 | + await ContributorsPage(); |
| 60 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 61 | + 'Failed to fetch contributors:', |
| 62 | + expect.objectContaining({ |
| 63 | + message: expect.stringContaining('Please try again after 2024-02-29T00:00:00.000Z.'), |
| 64 | + }) |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('verifies timezone/calendar alignment during year-end boundary rollovers', async () => { |
| 69 | + const timestamp = 1798761599; // 2026-12-31T23:59:59.000Z |
| 70 | + global.fetch = vi.fn(() => |
| 71 | + Promise.resolve({ |
| 72 | + ok: false, |
| 73 | + status: 403, |
| 74 | + headers: { |
| 75 | + get: (name: string) => { |
| 76 | + if (name === 'x-ratelimit-remaining') return '0'; |
| 77 | + if (name === 'x-ratelimit-reset') return String(timestamp); |
| 78 | + return null; |
| 79 | + }, |
| 80 | + }, |
| 81 | + json: async () => [], |
| 82 | + }) |
| 83 | + ) as unknown as typeof fetch; |
| 84 | + |
| 85 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 86 | + |
| 87 | + await ContributorsPage(); |
| 88 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 89 | + 'Failed to fetch contributors:', |
| 90 | + expect.objectContaining({ |
| 91 | + message: expect.stringContaining('Please try again after 2026-12-31T23:59:59.000Z.'), |
| 92 | + }) |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('handles invalid non-numeric or empty rate limit reset headers gracefully without throwing formatter exceptions', async () => { |
| 97 | + global.fetch = vi.fn(() => |
| 98 | + Promise.resolve({ |
| 99 | + ok: false, |
| 100 | + status: 429, |
| 101 | + headers: { |
| 102 | + get: (name: string) => { |
| 103 | + if (name === 'x-ratelimit-reset') return 'invalid-timestamp'; |
| 104 | + return null; |
| 105 | + }, |
| 106 | + }, |
| 107 | + json: async () => [], |
| 108 | + }) |
| 109 | + ) as unknown as typeof fetch; |
| 110 | + |
| 111 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 112 | + |
| 113 | + await ContributorsPage(); |
| 114 | + // It should omit the date reset message entirely when timestamp is invalid |
| 115 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 116 | + 'Failed to fetch contributors:', |
| 117 | + expect.objectContaining({ |
| 118 | + message: 'GitHub API rate limit exceeded. Please try again later.', |
| 119 | + }) |
| 120 | + ); |
| 121 | + }); |
| 122 | +}); |
0 commit comments