diff --git a/index.js b/index.js index e228a918..32b0a79c 100644 --- a/index.js +++ b/index.js @@ -17,27 +17,3 @@ app.get('/', (req, res) => { }); }); -// Portfolio aggregation endpoint -app.get('/api/user/:address/portfolio', (req, res) => { - const { address } = req.params; - - // Mock data for demonstration - replace with actual vault data - const mockVaults = [ - { type: 'advisor', locked: 80, claimable: 15 }, - { type: 'investor', locked: 20, claimable: 5 } - ]; - - // Calculate totals - const total_locked = mockVaults.reduce((sum, vault) => sum + vault.locked, 0); - const total_claimable = mockVaults.reduce((sum, vault) => sum + vault.claimable, 0); - - // Return the portfolio summary - res.json({ - total_locked, - total_claimable, - vaults: mockVaults, - address - }); -}); - -app.listen(port, () => console.log('Vesting API running')); diff --git a/package.json b/package.json index 21d03f1a..61e28cc5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "API for Vesting Vault", "main": "index.js", "scripts": { - "start": "node index.js" + "start": "node index.js", + "test": "node test-unit.js", + "test:integration": "node test-pagination.js" }, "dependencies": { "cors": "^2.8.6", diff --git a/test-pagination.js b/test-pagination.js new file mode 100644 index 00000000..7e26cc8f --- /dev/null +++ b/test-pagination.js @@ -0,0 +1,125 @@ +// Test script for pagination endpoint +const http = require('http'); + +// Test default pagination (page 1, limit 20) +const testDefaultPagination = () => { + return new Promise((resolve, reject) => { + const options = { + hostname: 'localhost', + port: 3000, + path: '/api/vaults', + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }; + + const req = http.request(options, (res) => { + let data = ''; + res.on('data', (chunk) => data += chunk); + res.on('end', () => { + try { + const response = JSON.parse(data); + console.log('โœ… Default Pagination Test:'); + console.log('Page:', response.pagination.current_page); + console.log('Limit:', response.pagination.per_page); + console.log('Total Vaults:', response.pagination.total_vaults); + console.log('Vaults returned:', response.vaults.length); + console.log('Has next page:', response.pagination.has_next_page); + + // Verify acceptance criteria + if (response.pagination.current_page === 1 && + response.pagination.per_page === 20 && + response.vaults.length <= 20) { + console.log('๐ŸŽ‰ SUCCESS: Default pagination works!'); + resolve(true); + } else { + console.log('โŒ FAILED: Default pagination test failed'); + resolve(false); + } + } catch (error) { + console.log('โŒ ERROR parsing response:', error.message); + resolve(false); + } + }); + }); + + req.on('error', (error) => { + console.log('โŒ ERROR making request:', error.message); + resolve(false); + }); + + req.end(); + }); +}; + +// Test page 2 with limit 10 +const testPage2Limit10 = () => { + return new Promise((resolve, reject) => { + const options = { + hostname: 'localhost', + port: 3000, + path: '/api/vaults?page=2&limit=10', + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }; + + const req = http.request(options, (res) => { + let data = ''; + res.on('data', (chunk) => data += chunk); + res.on('end', () => { + try { + const response = JSON.parse(data); + console.log('\nโœ… Page 2 Limit 10 Test:'); + console.log('Page:', response.pagination.current_page); + console.log('Limit:', response.pagination.per_page); + console.log('Vaults returned:', response.vaults.length); + console.log('Has prev page:', response.pagination.has_prev_page); + + // Verify acceptance criteria + if (response.pagination.current_page === 2 && + response.pagination.per_page === 10 && + response.vaults.length <= 10) { + console.log('๐ŸŽ‰ SUCCESS: Custom pagination works!'); + resolve(true); + } else { + console.log('โŒ FAILED: Custom pagination test failed'); + resolve(false); + } + } catch (error) { + console.log('โŒ ERROR parsing response:', error.message); + resolve(false); + } + }); + }); + + req.on('error', (error) => { + console.log('โŒ ERROR making request:', error.message); + resolve(false); + }); + + req.end(); + }); +}; + +// Run tests and exit with proper code +const runTests = async () => { + console.log('๐Ÿงช Testing Pagination for Issue #18\n'); + + try { + const test1 = await testDefaultPagination(); + const test2 = await testPage2Limit10(); + + if (test1 && test2) { + console.log('\n๐ŸŽ‰ ALL TESTS PASSED!'); + process.exit(0); + } else { + console.log('\nโŒ SOME TESTS FAILED!'); + process.exit(1); + } + } catch (error) { + console.log('\nโŒ TEST ERROR:', error.message); + process.exit(1); + } +}; + +// Run tests +runTests(); diff --git a/test-unit.js b/test-unit.js new file mode 100644 index 00000000..5c25682c --- /dev/null +++ b/test-unit.js @@ -0,0 +1,68 @@ +// Simple unit test for pagination logic +const { mockAllVaults } = require('./index.js'); + +// Mock the vaults data for testing +const mockVaults = Array.from({ length: 1500 }, (_, index) => ({ + id: index + 1, + type: index % 2 === 0 ? 'advisor' : 'investor', + locked: Math.floor(Math.random() * 1000) + 100, + claimable: Math.floor(Math.random() * 100) + 10, + created_at: new Date(Date.now() - Math.random() * 365 * 24 * 60 * 60 * 1000).toISOString() +})); + +// Test pagination logic +const testPaginationLogic = () => { + console.log('๐Ÿงช Testing Pagination Logic'); + + // Test default pagination (page 1, limit 20) + const page = 1; + const limit = 20; + const startIndex = (page - 1) * limit; + const endIndex = startIndex + limit; + const paginatedVaults = mockVaults.slice(startIndex, endIndex); + + // Calculate pagination metadata + const totalVaults = mockVaults.length; + const totalPages = Math.ceil(totalVaults / limit); + const hasNextPage = page < totalPages; + const hasPrevPage = page > 1; + + // Verify results + const test1 = paginatedVaults.length === 20; + const test2 = hasNextPage === true; + const test3 = hasPrevPage === false; + const test4 = totalVaults === 1500; + + console.log('โœ… Default pagination test:', test1 && test2 && test3 && test4 ? 'PASSED' : 'FAILED'); + + // Test page 2 with limit 10 + const page2 = 2; + const limit2 = 10; + const startIndex2 = (page2 - 1) * limit2; + const endIndex2 = startIndex2 + limit2; + const paginatedVaults2 = mockVaults.slice(startIndex2, endIndex2); + + const totalPages2 = Math.ceil(totalVaults / limit2); + const hasNextPage2 = page2 < totalPages2; + const hasPrevPage2 = page2 > 1; + + const test5 = paginatedVaults2.length === 10; + const test6 = hasNextPage2 === true; + const test7 = hasPrevPage2 === true; + + console.log('โœ… Page 2 limit 10 test:', test5 && test6 && test7 ? 'PASSED' : 'FAILED'); + + // Overall result + const allTestsPassed = test1 && test2 && test3 && test4 && test5 && test6 && test7; + + if (allTestsPassed) { + console.log('\n๐ŸŽ‰ ALL UNIT TESTS PASSED!'); + process.exit(0); + } else { + console.log('\nโŒ SOME UNIT TESTS FAILED!'); + process.exit(1); + } +}; + +// Run unit tests +testPaginationLogic();