stock-bot/test-ib.ts
2025-06-12 08:03:09 -04:00

194 lines
5.9 KiB
TypeScript

/**
* Test Interactive Brokers functionality with network monitoring
*/
import { Browser } from '@stock-bot/browser';
import { getRandomProxyURL } from '@stock-bot/proxy';
async function testIBSymbolSummary() {
console.log('🚀 Testing Interactive Brokers Symbol Summary with Network Monitoring...');
try {
// Initialize browser
await Browser.initialize({
headless: true,
timeout: 30000,
blockResources: true,
enableNetworkLogging: true,
});
console.log('✅ Browser initialized');
// Get a random proxy
// Create page with proxy
const { page, contextId } = await Browser.createPageWithProxy(
'https://www.interactivebrokers.com/webrest/search/product-types/summary',
'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80'
);
console.log('📄 Page created with proxy');
// Set up network monitoring
let summaryResponse: any = null;
let requestCount = 0;
let responseCount = 0;
page.onNetworkEvent(event => {
console.log(`📡 Network Event: ${event.type} - ${event.method} ${event.url}`);
if (event.type === 'request') {
requestCount++;
console.log(` 📤 Request #${requestCount}: ${event.method} ${event.url}`);
// Log request data for POST requests
if (event.requestData) {
console.log(` 📝 Request Data: ${event.requestData.substring(0, 200)}...`);
}
}
if (event.type === 'response') {
responseCount++;
console.log(` 📥 Response #${responseCount}: ${event.status} ${event.url}`);
// Capture the summary response
if (event.url.includes('summary')) {
console.log(` 🎯 Found summary response!`);
summaryResponse = event.responseData;
if (event.responseData) {
try {
const data = JSON.parse(event.responseData);
console.log(` 📊 Summary Data: ${JSON.stringify(data, null, 2)}`);
} catch (e) {
console.log(` 📊 Raw Response: ${event.responseData.substring(0, 500)}...`);
}
}
}
}
if (event.type === 'failed') {
console.log(` ❌ Failed Request: ${event.url}`);
}
});
console.log('🔍 Network monitoring set up, waiting for page to load...');
// Wait for page to load and capture network activity
await page.waitForLoadState('domcontentloaded');
console.log('✅ Page loaded');
// Wait a bit more for any additional network requests
await new Promise(resolve => setTimeout(resolve, 3000));
console.log(`📊 Network Summary:`);
console.log(` 📤 Total Requests: ${requestCount}`);
console.log(` 📥 Total Responses: ${responseCount}`);
if (summaryResponse) {
console.log('✅ Successfully captured summary response');
try {
const parsed = JSON.parse(summaryResponse);
console.log(`🔢 Total symbols found: ${parsed?.data?.totalCount || 'Unknown'}`);
return parsed?.data?.totalCount || 0;
} catch (e) {
console.log('⚠️ Could not parse response as JSON');
return 1; // Indicate success but unknown count
}
} else {
console.log('❌ No summary response captured');
return 0;
}
} catch (error) {
console.error('❌ Test failed:', error);
// Log more details about the error
if (error instanceof Error) {
console.error('Error details:', {
message: error.message,
stack: error.stack,
name: error.name
});
}
return -1;
} finally {
try {
await Browser.close();
console.log('🔒 Browser closed');
} catch (closeError) {
console.error('Error closing browser:', closeError);
}
}
}
async function testWithDifferentProxy() {
console.log('\n🔄 Testing with different proxy configuration...');
try {
await Browser.initialize({
headless: true,
timeout: 15000,
blockResources: false, // Don't block resources for this test
});
// Test without proxy first
console.log('🌐 Testing without proxy...');
const { page: pageNoProxy, contextId: contextNoProxy } = await Browser.createPageWithProxy(
'https://httpbin.org/ip'
);
pageNoProxy.onNetworkEvent(event => {
if (event.type === 'response' && event.url.includes('httpbin.org/ip')) {
console.log('📍 No proxy IP response:', event.responseData);
}
});
await pageNoProxy.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 1000));
await Browser.closeContext(contextNoProxy);
// Test with proxy
console.log('🌐 Testing with proxy...');
const { page: pageWithProxy, contextId: contextWithProxy } = await Browser.createPageWithProxy(
'https://httpbin.org/ip',
'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80'
);
pageWithProxy.onNetworkEvent(event => {
if (event.type === 'response' && event.url.includes('httpbin.org/ip')) {
console.log('🔄 Proxy IP response:', event.responseData);
}
});
await pageWithProxy.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 1000));
await Browser.closeContext(contextWithProxy);
} catch (error) {
console.error('❌ Proxy test failed:', error);
} finally {
await Browser.close();
}
}
// Run the tests
async function runTests() {
console.log('🧪 Starting IB Network Monitoring Tests\n');
// Test 1: Main IB functionality
const result = await testIBSymbolSummary();
console.log(`\n🏁 Test Result: ${result}`);
// Test 2: Proxy verification
await testWithDifferentProxy();
console.log('\n✅ All tests completed!');
}
// Run if this file is executed directly
if (import.meta.main) {
runTests().catch(console.error);
}
export { testIBSymbolSummary, testWithDifferentProxy };