celo-poll/scripts/test-db.ts
2025-12-08 16:31:30 +03:00

112 lines
3.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Database Test Script
*
* Tests the PollCelo database connection and basic operations
*
* Usage:
* npx tsx scripts/test-db.ts
*/
import { getDb } from '@/lib/db';
import { createPoll } from '@/lib/db/polls';
import { Poll } from '@/types';
async function testDatabase() {
console.log('๐Ÿงช Testing PollCelo Database Connection...\n');
try {
// Test database connection
console.log('๐Ÿ“ก Testing database connection...');
const db = getDb(process.env.DATABASE_URL!);
console.log('โœ… Database connection successful!\n');
// Test basic poll operations
console.log('๐Ÿ“Š Testing poll operations...');
const testPoll: Omit<Poll, 'id' | 'totalPot' | 'winnersCount' | 'protocolFee'> = {
creatorFid: 1,
creatorAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
question: 'Test Poll - Will this database work?',
description: 'Testing database operations',
emoji: '๐Ÿงช',
startTime: Date.now(),
endTime: Date.now() + (24 * 60 * 60 * 1000), // 24 hours
status: 'active',
verificationType: 'manual',
verificationConfig: {},
creationFee: 5,
options: [
{ id: 'opt1', text: 'Yes', emoji: 'โœ…', votes: 0, totalAmount: 0 },
{ id: 'opt2', text: 'No', emoji: 'โŒ', votes: 0, totalAmount: 0 },
]
};
// Create test poll
console.log('๐Ÿ“ Creating test poll...');
const createdPoll = await createPoll(testPoll);
console.log(`โœ… Test poll created with ID: ${createdPoll.id}\n`);
// Test poll retrieval
console.log('๐Ÿ” Testing poll retrieval...');
const { getPollById } = await import('@/lib/db/polls');
const retrievedPoll = await getPollById(createdPoll.id);
if (retrievedPoll) {
console.log(`โœ… Poll retrieved successfully: ${retrievedPoll.question}`);
console.log(`๐Ÿ“Š Options: ${retrievedPoll.options.length} options\n`);
} else {
throw new Error('Failed to retrieve created poll');
}
// Test oracle systems integration
console.log('๐Ÿ”ฎ Testing oracle systems integration...');
// Test Chainlink (if configured)
try {
const { getChainlinkPrice } = await import('@/lib/oracle/chainlink');
const btcPrice = await getChainlinkPrice('0x898E82a89c21469df34A8A9a36D1873f74Be3A1D');
console.log(`โœ… Chainlink BTC Price: $${btcPrice?.price || 'N/A'}`);
} catch (error) {
console.log('โš ๏ธ Chainlink test failed (this is OK for testing)');
}
// Test ESPN
try {
const { getESPNScoreboard } = await import('@/lib/oracle/espn');
const nflGames = await getESPNScoreboard('football/nfl');
console.log(`โœ… ESPN NFL Games: ${nflGames.length} games found`);
} catch (error) {
console.log('โš ๏ธ ESPN test failed (this is OK for testing)');
}
// Test DexScreener
try {
const { getDexScreenerTopPairs } = await import('@/lib/oracle/dexscreener');
const topPairs = await getDexScreenerTopPairs('ethereum');
console.log(`โœ… DexScreener Ethereum Pairs: ${topPairs.length} pairs found`);
} catch (error) {
console.log('โš ๏ธ DexScreener test failed (this is OK for testing)');
}
console.log('\n๐ŸŽ‰ All database tests passed!');
console.log('\n๐Ÿ“‹ Next steps:');
console.log('1. Deploy to Vercel with environment variables');
console.log('2. Test with real FarCastor frames');
console.log('3. Create your first production poll!');
} catch (error) {
console.error('โŒ Database test failed:', error);
console.error('\n๐Ÿ”ง Troubleshooting:');
console.error('- Check DATABASE_URL in .env.local');
console.error('- Verify Neon database is running');
console.error('- Check network connectivity');
console.error('- Ensure all dependencies are installed\n');
process.exit(1);
}
}
// Run tests
testDatabase().catch(console.error);