import assert from 'assert'; const API_URL = 'http://localhost:7860/api/search'; // Test cases const tests = [ { name: 'Search movie by TMDB ID', payload: { query: 'tmdb:155', // The Dark Knight type: 'movie' }, expectedTitle: 'The Dark Knight' }, { name: 'Search movie by IMDB ID', payload: { query: 'tt0468569', // The Dark Knight type: 'movie' }, expectedTitle: 'The Dark Knight' }, { name: 'Search movie by text with year', payload: { query: 'The Dark Knight 2008', type: 'movie' }, expectedTitle: 'The Dark Knight' }, { name: 'Search TV show by TMDB ID', payload: { query: 'tmdb:1396:1:1', // Breaking Bad S01E01 type: 'series' }, expectedTitle: 'Breaking Bad' }, { name: 'Search TV show by IMDB ID', payload: { query: 'tt0903747:1:1', // Breaking Bad S01E01 type: 'series' }, expectedTitle: 'Breaking Bad' }, { name: 'Search TV show by text S01E01 format', payload: { query: 'Breaking Bad S01E01', type: 'series' }, expectedTitle: 'Breaking Bad' }, { name: 'Search TV show by text 1x01 format', payload: { query: 'Breaking Bad 1x01', type: 'series' }, expectedTitle: 'Breaking Bad' }, { name: 'Search recent movie by TMDB ID', payload: { query: 'tmdb:933131', // Oppenheimer type: 'movie' }, expectedTitle: 'Oppenheimer' }, { name: 'Search popular TV show by TMDB ID', payload: { query: 'tmdb:66732:1:1', // Stranger Things S01E01 type: 'series' }, expectedTitle: 'Stranger Things' } ]; // Helper function to validate result structure function validateResult(result, testCase) { assert(result.query, 'Result should have a query field'); assert(result.originalQuery, 'Result should have an originalQuery field'); assert(result.type, 'Result should have a type field'); assert(Array.isArray(result.results), 'Results should be an array'); if (result.tmdbData) { if (testCase.type === 'movie') { assert(result.tmdbData.title, 'TMDB data should have a title for movies'); assert(result.tmdbData.release_date, 'TMDB data should have a release date'); } else { assert(result.tmdbData.showTitle, 'TMDB data should have a show title for series'); assert(result.tmdbData.episodeTitle, 'TMDB data should have an episode title'); assert(result.tmdbData.seasonNumber, 'TMDB data should have a season number'); assert(result.tmdbData.episodeNumber, 'TMDB data should have an episode number'); } } } // Helper function to clean HTML tags from title function cleanTitle(title) { return title .replace(/<[^>]*>/g, '') .replace(/\s+/g, ' ') .trim(); } // Helper function to format byte sizes function formatSize(sizeStr) { if (!sizeStr) return 'Unknown'; const parts = sizeStr.split(' '); const size = parseFloat(parts[0]); const unit = parts[1]; return `${size.toFixed(2)} ${unit}`; } // Helper function to format quality info function formatQuality(quality, size, seeders) { return `[${quality}] [${formatSize(size)}] [šŸ‘„ ${seeders}]`; } // Run tests async function runTests() { console.log('Starting API tests...\n'); let passedTests = 0; let failedTests = 0; for (const test of tests) { try { console.log(`=== Running test: ${test.name} ===`); console.log('Request:', `${test.payload.type} | ${test.payload.query}\n`); const response = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(test.payload) }); assert(response.ok, `Response should be ok (got ${response.status})`); const result = await response.json(); validateResult(result, test.payload); // Print metadata if (result.tmdbData) { console.log('Metadata:'); console.log('========='); if (test.payload.type === 'movie') { console.log(`šŸŽ¬ ${result.tmdbData.title} (${new Date(result.tmdbData.release_date).getFullYear()})`); } else { console.log(`šŸ“ŗ ${result.tmdbData.showTitle}`); console.log(`Episode: ${result.tmdbData.episodeTitle}`); console.log(`S${String(result.tmdbData.seasonNumber).padStart(2, '0')}E${String(result.tmdbData.episodeNumber).padStart(2, '0')}`); } console.log(); } // Print results console.log(`Found ${result.results.length} results:`); console.log('=================='); const topResults = result.results.slice(0, 3); if (topResults.length > 0) { topResults.forEach((item, index) => { console.log(`${index + 1}. ${cleanTitle(item.title)}`); console.log(` ${formatQuality(item.quality, item.size, item.seeders)}`); }); } else { console.log('No results found'); } passedTests++; console.log('\nāœ… Test passed\n'); } catch (error) { console.error('āŒ Test failed:', error.message); if (error.response) { console.error('Response:', await error.response.text()); } failedTests++; } console.log('================================================\n'); } // Print summary console.log('Test Summary'); console.log('============'); console.log(`Total Tests: ${tests.length}`); console.log(`āœ… Passed: ${passedTests}`); console.log(`āŒ Failed: ${failedTests}`); } // Error handler for uncaught promises process.on('unhandledRejection', (error) => { console.error('Unhandled promise rejection:', error); process.exit(1); }); // Run the tests console.log('Make sure the API server is running before starting tests!\n'); runTests();