|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import Leaderboard, { Contributor } from './Leaderboard'; |
| 5 | + |
| 6 | +// Mock Next.js Image |
| 7 | +vi.mock('next/image', () => ({ |
| 8 | + default: (props: React.ImgHTMLAttributes<HTMLImageElement>) => <img alt="mock" {...props} />, |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock Framer Motion |
| 12 | +vi.mock('framer-motion', async () => { |
| 13 | + const actual = await vi.importActual('framer-motion'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + motion: { |
| 17 | + div: ({ |
| 18 | + children, |
| 19 | + className, |
| 20 | + onClick, |
| 21 | + style, |
| 22 | + }: { |
| 23 | + children?: React.ReactNode; |
| 24 | + className?: string; |
| 25 | + onClick?: React.MouseEventHandler<HTMLDivElement>; |
| 26 | + style?: React.CSSProperties; |
| 27 | + }) => ( |
| 28 | + <div className={className} onClick={onClick} style={style} data-testid="motion-div"> |
| 29 | + {children} |
| 30 | + </div> |
| 31 | + ), |
| 32 | + }, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +// IntersectionObserver mock |
| 37 | +beforeEach(() => { |
| 38 | + const mockIntersectionObserver = vi.fn(); |
| 39 | + mockIntersectionObserver.mockReturnValue({ |
| 40 | + observe: () => null, |
| 41 | + unobserve: () => null, |
| 42 | + disconnect: () => null, |
| 43 | + }); |
| 44 | + window.IntersectionObserver = |
| 45 | + mockIntersectionObserver as unknown as typeof window.IntersectionObserver; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('Leaderboard - Massive Scaling & High Bounds (Issue #2754 Equivalent)', () => { |
| 49 | + it('Massive Array Population (Render Bounds): handles processing arrays of 1000+ contributors cleanly without crashing', () => { |
| 50 | + const massiveContributors = Array.from({ length: 1005 }).map((_, i) => ({ |
| 51 | + id: i, |
| 52 | + login: `user${i}`, |
| 53 | + avatar_url: `https://avatars.githubusercontent.com/u/${i}?v=4`, |
| 54 | + contributions: 100 - i, // fake sort |
| 55 | + html_url: `https://github.com/user${i}`, |
| 56 | + })); |
| 57 | + |
| 58 | + const start = performance.now(); |
| 59 | + const { container } = render(<Leaderboard contributors={massiveContributors} />); |
| 60 | + const end = performance.now(); |
| 61 | + |
| 62 | + // Verify it extracts podium and leaves 1002 in the list |
| 63 | + const listEntries = container.querySelectorAll('.flex.items-center.justify-between.p-4'); |
| 64 | + expect(listEntries.length).toBe(1002); |
| 65 | + |
| 66 | + // Performance bounds check (bumped to 5000ms to account for slower CI runners) |
| 67 | + expect(end - start).toBeLessThan(5000); |
| 68 | + }); |
| 69 | + |
| 70 | + it('Extreme High Contribution Metric Bounds: safely renders millions of commits without integer layout overflow', () => { |
| 71 | + const highMetricData: Contributor[] = [ |
| 72 | + { id: 1, login: 'whale', avatar_url: '', html_url: '', contributions: 999999999 }, |
| 73 | + { id: 2, login: 'shark', avatar_url: '', html_url: '', contributions: 888888888 }, |
| 74 | + { id: 3, login: 'dolphin', avatar_url: '', html_url: '', contributions: 777777777 }, |
| 75 | + { id: 4, login: 'fish', avatar_url: '', html_url: '', contributions: 666666666 }, |
| 76 | + ]; |
| 77 | + |
| 78 | + const { getByText } = render(<Leaderboard contributors={highMetricData} />); |
| 79 | + |
| 80 | + // Podium text |
| 81 | + expect(getByText('999999999')).toBeTruthy(); |
| 82 | + expect(getByText('888888888')).toBeTruthy(); |
| 83 | + expect(getByText('777777777')).toBeTruthy(); |
| 84 | + |
| 85 | + // List text |
| 86 | + expect(getByText('666666666')).toBeTruthy(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Rank Formatting Overflow Prevention: ensures extremely high index numbers do not break grid bounds', () => { |
| 90 | + // Check rank #1000+ string bounds |
| 91 | + const highRankData = Array.from({ length: 1500 }).map((_, i) => ({ |
| 92 | + id: i, |
| 93 | + login: `user${i}`, |
| 94 | + avatar_url: '', |
| 95 | + contributions: 10, |
| 96 | + html_url: '', |
| 97 | + })); |
| 98 | + |
| 99 | + const { getByText } = render(<Leaderboard contributors={highRankData} />); |
| 100 | + |
| 101 | + // Verify #1000 renders cleanly inside the bounding flex box |
| 102 | + const highRankItem = getByText('#1000'); |
| 103 | + expect(highRankItem).toBeTruthy(); |
| 104 | + expect(highRankItem.className).toContain('font-mono'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('Podium Extraction Memory Allocation: strictly segments exactly the top 3 nodes regardless of massive data input sizes', () => { |
| 108 | + const massiveData = Array.from({ length: 500 }).map((_, i) => ({ |
| 109 | + id: i, |
| 110 | + login: `user${i}`, |
| 111 | + avatar_url: '', |
| 112 | + contributions: 10, |
| 113 | + html_url: '', |
| 114 | + })); |
| 115 | + |
| 116 | + const { container } = render(<Leaderboard contributors={massiveData} />); |
| 117 | + |
| 118 | + // Check that we only rendered 3 PodiumItems (Rank 1, 2, 3) |
| 119 | + const crowns = container.querySelectorAll('.absolute.-top-8.z-30'); // Crown icons exist in podiums only |
| 120 | + expect(crowns.length).toBe(3); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Structural Text Wrapping Safety: extreme long usernames truncate without clipping or blowing out DOM boundaries', () => { |
| 124 | + const extremeNameData: Contributor[] = [ |
| 125 | + { id: 1, login: 'a'.repeat(500), avatar_url: '', html_url: '', contributions: 100 }, |
| 126 | + { id: 2, login: 'b'.repeat(500), avatar_url: '', html_url: '', contributions: 90 }, |
| 127 | + { id: 3, login: 'c'.repeat(500), avatar_url: '', html_url: '', contributions: 80 }, |
| 128 | + ]; |
| 129 | + |
| 130 | + const { getByText } = render(<Leaderboard contributors={extremeNameData} />); |
| 131 | + |
| 132 | + const longNameUser = getByText('a'.repeat(500)); |
| 133 | + // Verify it uses truncate to stop boundary breakages |
| 134 | + expect(longNameUser.className).toContain('truncate'); |
| 135 | + }); |
| 136 | +}); |
0 commit comments