Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
125 changes: 125 additions & 0 deletions test-pagination.js
Original file line number Diff line number Diff line change
@@ -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();
68 changes: 68 additions & 0 deletions test-unit.js
Original file line number Diff line number Diff line change
@@ -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();