stock-bot/test-network-monitoring.ts
2025-06-13 13:38:02 -04:00

137 lines
4 KiB
TypeScript

/**
* Simple test to verify network monitoring is working
*/
import { Browser } from '@stock-bot/browser';
async function testNetworkMonitoring() {
console.log('🧪 Testing Network Monitoring with httpbin.org...');
try {
await Browser.initialize({
headless: true,
timeout: 15000,
blockResources: false, // Don't block resources so we can see requests
});
console.log('✅ Browser initialized');
// Test with a simple API that returns JSON
const { page, contextId } = await Browser.createPageWithProxy(
'https://httpbin.org/json'
);
let capturedRequests = 0;
let capturedResponses = 0;
let jsonResponse = null;
page.onNetworkEvent(event => {
console.log(`📡 ${event.type.toUpperCase()}: ${event.method} ${event.url}`);
if (event.type === 'request') {
capturedRequests++;
}
if (event.type === 'response') {
capturedResponses++;
console.log(` Status: ${event.status}`);
if (event.url.includes('httpbin.org/json') && event.responseData) {
jsonResponse = event.responseData;
console.log(` 📊 JSON Response: ${event.responseData}`);
}
}
});
await page.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 2000));
console.log(`\n📊 Summary:`);
console.log(` 📤 Requests captured: ${capturedRequests}`);
console.log(` 📥 Responses captured: ${capturedResponses}`);
console.log(` 📝 JSON data captured: ${jsonResponse ? 'Yes' : 'No'}`);
await Browser.closeContext(contextId);
return true;
} catch (error) {
console.error('❌ Test failed:', error);
return false;
} finally {
await Browser.close();
}
}
async function testWithProxy() {
console.log('\n🌐 Testing with proxy to see IP change...');
try {
await Browser.initialize({
headless: true,
timeout: 10000,
blockResources: false,
});
// Test IP without proxy
console.log('📍 Getting IP without proxy...');
const { page: page1, contextId: ctx1 } = await Browser.createPageWithProxy(
'https://httpbin.org/ip'
);
let ipWithoutProxy = null;
page1.onNetworkEvent(event => {
if (event.type === 'response' && event.url.includes('/ip') && event.responseData) {
ipWithoutProxy = JSON.parse(event.responseData).origin;
console.log(` 🔹 Your IP: ${ipWithoutProxy}`);
}
});
await page1.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 1000));
await Browser.closeContext(ctx1);
// Test IP with proxy
console.log('🔄 Getting IP with proxy...');
const { page: page2, contextId: ctx2 } = await Browser.createPageWithProxy(
'https://httpbin.org/ip',
'http://doimvbnb-US-rotate:w5fpiwrb9895@p.webshare.io:80'
);
let ipWithProxy = null;
page2.onNetworkEvent(event => {
if (event.type === 'response' && event.url.includes('/ip') && event.responseData) {
ipWithProxy = JSON.parse(event.responseData).origin;
console.log(` 🔸 Proxy IP: ${ipWithProxy}`);
}
});
await page2.waitForLoadState('domcontentloaded');
await new Promise(resolve => setTimeout(resolve, 1000));
await Browser.closeContext(ctx2);
if (ipWithoutProxy && ipWithProxy && ipWithoutProxy !== ipWithProxy) {
console.log('✅ Proxy is working - IPs are different!');
} else {
console.log('⚠️ Proxy may not be working - IPs are the same or not captured');
}
} catch (error) {
console.error('❌ Proxy test failed:', error);
} finally {
await Browser.close();
}
}
async function runTests() {
console.log('🚀 Network Monitoring Verification Tests\n');
const basicResult = await testNetworkMonitoring();
await testWithProxy();
console.log(`\n🏁 Network monitoring: ${basicResult ? '✅ WORKING' : '❌ FAILED'}`);
}
if (import.meta.main) {
runTests().catch(console.error);
}
export { testNetworkMonitoring, testWithProxy };