Become a Pro / website · becomeapro.ai / scripts / tests / test-news-api.js
| 1 | // This is a test script for the news API |
| 2 | // Run with: node scripts/tests/test-news-api.js |
| 3 | |
| 4 | import fetch from 'node-fetch'; |
| 5 | |
| 6 | const BASE_URL = 'http://localhost:3000/api/news'; |
| 7 | const API_KEY = process.env.NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY || ''; // Only needed for write operations |
| 8 | |
| 9 | // No longer exit if API key is not set, since we're only doing read operations |
| 10 | if (!API_KEY) { |
| 11 | console.warn('WARNING: NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY environment variable is not set. Only read operations will work.'); |
| 12 | } |
| 13 | |
| 14 | async function testGetAllNews() { |
| 15 | console.log('\n🧪 Testing GET all news...'); |
| 16 | |
| 17 | try { |
| 18 | const response = await fetch(BASE_URL); |
| 19 | |
| 20 | if (!response.ok) { |
| 21 | throw new Error(`HTTP error! status: ${response.status}`); |
| 22 | } |
| 23 | |
| 24 | const data = await response.json(); |
| 25 | |
| 26 | console.log(`✅ SUCCESS: Retrieved ${data.length} news articles`); |
| 27 | console.log('Sample news article:', data[0] ? { |
| 28 | id: data[0].id, |
| 29 | title: data[0].title, |
| 30 | url: data[0].url |
| 31 | } : 'No news articles found'); |
| 32 | |
| 33 | return data; |
| 34 | } catch (error) { |
| 35 | console.error('❌ ERROR:', error.message); |
| 36 | return null; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | async function testCreateNews() { |
| 41 | console.log('\n🧪 DISABLED: Test for creating news is disabled to prevent test data in production'); |
| 42 | return null; // Disabled to prevent test data in production |
| 43 | } |
| 44 | |
| 45 | async function testUpdateNews(id) { |
| 46 | console.log('\n🧪 DISABLED: Test for updating news is disabled to prevent test data in production'); |
| 47 | return null; // Disabled to prevent test data in production |
| 48 | } |
| 49 | |
| 50 | async function testDeleteNews(id) { |
| 51 | console.log('\n🧪 DISABLED: Test for deleting news is disabled to prevent test data in production'); |
| 52 | return false; // Disabled to prevent test data in production |
| 53 | } |
| 54 | |
| 55 | async function runTests() { |
| 56 | console.log('📝 Starting News API tests (READ ONLY)...'); |
| 57 | |
| 58 | // Test GET all news |
| 59 | await testGetAllNews(); |
| 60 | |
| 61 | // Skip tests that modify data |
| 62 | console.log('\n⚠️ Tests for creating, updating, and deleting news are disabled to prevent test data in production'); |
| 63 | |
| 64 | console.log('\n🎯 All tests completed!'); |
| 65 | } |
| 66 | |
| 67 | // Run the tests |
| 68 | runTests(); |