| 8 | const BASE_URL = 'http://localhost:3000'; |
| 9 | |
| 10 | async function testRecentProfilesAPI() { |
| 11 | console.log('🧪 Testing /api/profiles/recent Endpoint\n'); |
| 12 | console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'); |
| 13 | |
| 14 | try { |
| 15 | // Test 1: Fetch recent profiles (default limit: 50) |
| 16 | console.log('📋 Test 1: Fetch recent profiles (default params)...'); |
| 17 | const res1 = await fetch(`${BASE_URL}/api/profiles/recent`); |
| 18 | const data1 = await res1.json(); |
| 19 | |
| 20 | if (!res1.ok) { |
| 21 | throw new Error(`API error: ${data1.error || res1.statusText}`); |
| 22 | } |
| 23 | |
| 24 | console.log(`✅ PASS: Fetched ${data1.count} profiles`); |
| 25 | console.log(` Total in DB: ${res1.headers.get('X-Total-Count')}`); |
| 26 | |
| 27 | if (data1.count > 0) { |
| 28 | console.log(` First profile: ${data1.profiles[0].fullName}`); |
| 29 | console.log(` Updated at: ${data1.profiles[0].updatedAt}\n`); |
| 30 | } else { |
| 31 | console.log(' ⚠️ No profiles in database yet\n'); |
| 32 | console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); |
| 33 | console.log('ℹ️ Run a search first to populate the database\n'); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Test 2: Verify profiles are sorted by updatedAt (newest first) |
| 38 | console.log('📋 Test 2: Verify profiles are sorted by updatedAt desc...'); |
| 39 | if (data1.profiles.length > 1) { |
| 40 | const first = new Date(data1.profiles[0].updatedAt).getTime(); |
| 41 | const second = new Date(data1.profiles[1].updatedAt).getTime(); |
| 42 | |
| 43 | if (first >= second) { |
| 44 | console.log('✅ PASS: Profiles are sorted correctly (newest first)\n'); |
| 45 | } else { |
| 46 | throw new Error('Profiles are not sorted correctly!'); |
| 47 | } |
| 48 | } else { |
| 49 | console.log('⚠️ SKIP: Only one profile, cannot verify sorting\n'); |
| 50 | } |
| 51 | |
| 52 | // Test 3: Test limit parameter |
| 53 | console.log('📋 Test 3: Test limit parameter (?limit=5)...'); |
| 54 | const res3 = await fetch(`${BASE_URL}/api/profiles/recent?limit=5`); |
| 55 | const data3 = await res3.json(); |
| 56 | |
| 57 | if (data3.count <= 5) { |
| 58 | console.log(`✅ PASS: Returned ${data3.count} profiles (limit respected)\n`); |
| 59 | } else { |
| 60 | throw new Error(`Expected max 5 profiles, got ${data3.count}`); |
| 61 | } |
| 62 | |
| 63 | // Test 4: Test 'before' parameter (pagination) |
| 64 | if (data1.profiles.length > 0) { |
| 65 | console.log('📋 Test 4: Test pagination with ?before parameter...'); |
| 66 | const beforeTimestamp = data1.profiles[0].updatedAt; |
| 67 | const res4 = await fetch( |