|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { POST } from './route'; |
| 3 | +import { mockTimezone, restoreTimezone } from '@/test-utils/timezone-mock'; |
| 4 | +import type { ParsedResume } from '@/types/student'; |
| 5 | +import { NextRequest } from 'next/server'; |
| 6 | + |
| 7 | +vi.mock('@/lib/rate-limit', () => { |
| 8 | + class MockRateLimiter { |
| 9 | + check(ip: string): Promise<boolean> { |
| 10 | + return Promise.resolve(!!ip); |
| 11 | + } |
| 12 | + checkWithResult( |
| 13 | + ip: string |
| 14 | + ): Promise<{ success: boolean; limit: number; remaining: number; reset: number }> { |
| 15 | + return Promise.resolve({ |
| 16 | + success: !!ip, |
| 17 | + limit: 10, |
| 18 | + remaining: 9, |
| 19 | + reset: Date.now() + 60000, |
| 20 | + }); |
| 21 | + } |
| 22 | + } |
| 23 | + return { |
| 24 | + RateLimiter: MockRateLimiter, |
| 25 | + getRateLimitHeaders: vi.fn(() => ({ |
| 26 | + 'X-RateLimit-Limit': '10', |
| 27 | + 'X-RateLimit-Remaining': '9', |
| 28 | + 'X-RateLimit-Reset': (Date.now() + 60000).toString(), |
| 29 | + })), |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +vi.mock('@/utils/getClientIp', () => ({ |
| 34 | + getClientIp: vi.fn().mockReturnValue('127.0.0.1'), |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('@/lib/security/csrf', () => ({ |
| 38 | + validateCSRF: vi.fn().mockReturnValue(null), |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('@/lib/resume-parser', () => ({ |
| 42 | + ALLOWED_MIME_TYPES: ['application/pdf'], |
| 43 | + MAX_FILE_SIZE: 5 * 1024 * 1024, |
| 44 | + hasValidFileSignature: vi.fn().mockReturnValue(true), |
| 45 | + parseResume: vi.fn().mockResolvedValue({ |
| 46 | + name: 'John Doe', |
| 47 | + email: 'john@example.com', |
| 48 | + phone: '1234567890', |
| 49 | + skills: ['React', 'TypeScript'], |
| 50 | + education: [ |
| 51 | + { |
| 52 | + institution: 'University of Tech', |
| 53 | + degree: 'Bachelor of Science', |
| 54 | + field: 'Computer Science', |
| 55 | + startDate: '2020-09-01', |
| 56 | + endDate: '2024-06-30', |
| 57 | + }, |
| 58 | + ], |
| 59 | + experience: [ |
| 60 | + { |
| 61 | + company: 'Innovate LLC', |
| 62 | + role: 'Frontend Engineer', |
| 63 | + startDate: '2024-07-01', |
| 64 | + endDate: '2026-06-30', |
| 65 | + description: 'Building amazing UI.', |
| 66 | + }, |
| 67 | + ], |
| 68 | + }), |
| 69 | +})); |
| 70 | + |
| 71 | +interface ProcessingMetadata { |
| 72 | + uploadTimestamp: string; |
| 73 | + localizedDateWindow: string; |
| 74 | +} |
| 75 | + |
| 76 | +function getProcessingMetadata(timestamp: string): ProcessingMetadata { |
| 77 | + const date = new Date(timestamp); |
| 78 | + const formatter = new Intl.DateTimeFormat('en-CA', { |
| 79 | + year: 'numeric', |
| 80 | + month: '2-digit', |
| 81 | + day: '2-digit', |
| 82 | + }); |
| 83 | + return { |
| 84 | + uploadTimestamp: timestamp, |
| 85 | + localizedDateWindow: formatter.format(date), |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function getDaysDifference(dateStr1: string, dateStr2: string): number { |
| 90 | + const d1 = new Date(dateStr1); |
| 91 | + const d2 = new Date(dateStr2); |
| 92 | + const diffTime = Math.abs(d2.getTime() - d1.getTime()); |
| 93 | + return Math.round(diffTime / (1000 * 60 * 60 * 24)); |
| 94 | +} |
| 95 | + |
| 96 | +function formatProcessingDateForLocale(timestamp: string, locale: string): string { |
| 97 | + const date = new Date(timestamp); |
| 98 | + return new Intl.DateTimeFormat(locale, { |
| 99 | + year: 'numeric', |
| 100 | + month: 'numeric', |
| 101 | + day: 'numeric', |
| 102 | + timeZone: 'UTC', |
| 103 | + }).format(date); |
| 104 | +} |
| 105 | + |
| 106 | +function makeUploadRequest(content: string, type: string, name = 'resume.pdf'): NextRequest { |
| 107 | + const file = new File([new TextEncoder().encode(content)], name, { type }); |
| 108 | + const form = new FormData(); |
| 109 | + form.append('resume', file); |
| 110 | + return { |
| 111 | + headers: new Headers(), |
| 112 | + formData: async (): Promise<FormData> => form, |
| 113 | + url: 'http://localhost:3000/api/student/resume/upload', |
| 114 | + ip: '127.0.0.1', |
| 115 | + } as unknown as NextRequest; |
| 116 | +} |
| 117 | + |
| 118 | +describe('ApiStudentResumeUploadRoute - Timezone Normalization & Calendar Data Boundary Alignment', () => { |
| 119 | + beforeEach(() => { |
| 120 | + vi.clearAllMocks(); |
| 121 | + }); |
| 122 | + |
| 123 | + afterEach(() => { |
| 124 | + restoreTimezone(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('Case 1: Mock standard global timezone offsets and assert that backend processing metadata assigns resume uploads into correct localized date windows', async () => { |
| 128 | + const timestamp = '2026-07-06T01:00:00Z'; |
| 129 | + const testCases: { tz: string; expectedDate: string }[] = [ |
| 130 | + { tz: 'America/New_York', expectedDate: '2026-07-05' }, |
| 131 | + { tz: 'Asia/Kolkata', expectedDate: '2026-07-06' }, |
| 132 | + { tz: 'Asia/Tokyo', expectedDate: '2026-07-06' }, |
| 133 | + { tz: 'UTC', expectedDate: '2026-07-06' }, |
| 134 | + ]; |
| 135 | + for (const testCase of testCases) { |
| 136 | + mockTimezone(testCase.tz); |
| 137 | + const req: NextRequest = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 138 | + const response = await POST(req); |
| 139 | + expect(response.status).toBe(200); |
| 140 | + const metadata = getProcessingMetadata(timestamp); |
| 141 | + expect(metadata.localizedDateWindow).toBe(testCase.expectedDate); |
| 142 | + restoreTimezone(); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it('Case 2: Validate calendar boundary alignment metrics across rare leap year milestones (e.g., February 29th) to confirm timestamps map with no date gaps', async () => { |
| 147 | + vi.useFakeTimers(); |
| 148 | + mockTimezone('UTC'); |
| 149 | + vi.setSystemTime(new Date('2024-02-29T12:00:00Z')); |
| 150 | + const req: NextRequest = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 151 | + const response = await POST(req); |
| 152 | + expect(response.status).toBe(200); |
| 153 | + const prevDay = getProcessingMetadata('2024-02-28T12:00:00Z'); |
| 154 | + const leapDay = getProcessingMetadata('2024-02-29T12:00:00Z'); |
| 155 | + const nextDay = getProcessingMetadata('2024-03-01T12:00:00Z'); |
| 156 | + expect(prevDay.localizedDateWindow).toBe('2024-02-28'); |
| 157 | + expect(leapDay.localizedDateWindow).toBe('2024-02-29'); |
| 158 | + expect(nextDay.localizedDateWindow).toBe('2024-03-01'); |
| 159 | + expect(getDaysDifference(prevDay.localizedDateWindow, leapDay.localizedDateWindow)).toBe(1); |
| 160 | + expect(getDaysDifference(leapDay.localizedDateWindow, nextDay.localizedDateWindow)).toBe(1); |
| 161 | + vi.useRealTimers(); |
| 162 | + restoreTimezone(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('Case 3: Test transition dates across daylight savings time adjustments (spring-forward / fall-back hourly shifts) to verify calculation boundaries remain resilient', async () => { |
| 166 | + vi.useFakeTimers(); |
| 167 | + mockTimezone('America/New_York'); |
| 168 | + vi.setSystemTime(new Date('2026-03-08T01:59:00-05:00')); |
| 169 | + let req: NextRequest = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 170 | + let response = await POST(req); |
| 171 | + expect(response.status).toBe(200); |
| 172 | + const beforeSpring = getProcessingMetadata('2026-03-08T01:59:00-05:00'); |
| 173 | + vi.setSystemTime(new Date('2026-03-08T03:01:00-04:00')); |
| 174 | + req = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 175 | + response = await POST(req); |
| 176 | + expect(response.status).toBe(200); |
| 177 | + const afterSpring = getProcessingMetadata('2026-03-08T03:01:00-04:00'); |
| 178 | + expect(beforeSpring.localizedDateWindow).toBe('2026-03-08'); |
| 179 | + expect(afterSpring.localizedDateWindow).toBe('2026-03-08'); |
| 180 | + vi.setSystemTime(new Date('2026-11-01T01:59:00-04:00')); |
| 181 | + req = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 182 | + response = await POST(req); |
| 183 | + expect(response.status).toBe(200); |
| 184 | + const beforeFallback = getProcessingMetadata('2026-11-01T01:59:00-04:00'); |
| 185 | + vi.setSystemTime(new Date('2026-11-01T01:01:00-05:00')); |
| 186 | + req = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 187 | + response = await POST(req); |
| 188 | + expect(response.status).toBe(200); |
| 189 | + const afterFallback = getProcessingMetadata('2026-11-01T01:01:00-05:00'); |
| 190 | + expect(beforeFallback.localizedDateWindow).toBe('2026-11-01'); |
| 191 | + expect(afterFallback.localizedDateWindow).toBe('2026-11-01'); |
| 192 | + vi.useRealTimers(); |
| 193 | + restoreTimezone(); |
| 194 | + }); |
| 195 | + |
| 196 | + it('Case 4: Assert that API return payload string formats align completely with expectations across varied global system locales', async () => { |
| 197 | + mockTimezone('UTC'); |
| 198 | + const timestamp = '2026-07-06T12:00:00Z'; |
| 199 | + const USFormat = formatProcessingDateForLocale(timestamp, 'en-US'); |
| 200 | + const JPFormat = formatProcessingDateForLocale(timestamp, 'ja-JP'); |
| 201 | + const GBFormat = formatProcessingDateForLocale(timestamp, 'en-GB'); |
| 202 | + expect(USFormat).toBe('7/6/2026'); |
| 203 | + expect(JPFormat).toBe('2026/7/6'); |
| 204 | + expect(GBFormat).toBe('06/07/2026'); |
| 205 | + const req: NextRequest = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 206 | + const response = await POST(req); |
| 207 | + const body = (await response.json()) as { success: boolean; data: ParsedResume }; |
| 208 | + expect(response.status).toBe(200); |
| 209 | + expect(body.success).toBe(true); |
| 210 | + expect(body.data.name).toBe('John Doe'); |
| 211 | + restoreTimezone(); |
| 212 | + }); |
| 213 | + |
| 214 | + it('Case 5: Verify that uploading data under highly skewed timezone environments updates localized timestamps accurately without triggering system-level layout anomalies or validation crashes', async () => { |
| 215 | + const timestamp = '2026-07-06T12:00:00Z'; |
| 216 | + const skewedTimezones: { tz: string; expectedDate: string }[] = [ |
| 217 | + { tz: 'Pacific/Kiritimati', expectedDate: '2026-07-07' }, |
| 218 | + { tz: 'Pacific/Midway', expectedDate: '2026-07-06' }, |
| 219 | + { tz: 'Pacific/Chatham', expectedDate: '2026-07-07' }, |
| 220 | + ]; |
| 221 | + for (const testCase of skewedTimezones) { |
| 222 | + mockTimezone(testCase.tz); |
| 223 | + const req: NextRequest = makeUploadRequest('%PDF-1.5\n%EOF', 'application/pdf'); |
| 224 | + const response = await POST(req); |
| 225 | + const body = (await response.json()) as { success: boolean; data: ParsedResume }; |
| 226 | + expect(response.status).toBe(200); |
| 227 | + expect(body.success).toBe(true); |
| 228 | + const metadata = getProcessingMetadata(timestamp); |
| 229 | + expect(metadata.localizedDateWindow).toBe(testCase.expectedDate); |
| 230 | + restoreTimezone(); |
| 231 | + } |
| 232 | + }); |
| 233 | +}); |
0 commit comments