auction-scrapper/commands/test_connection.ts
Vakula Uladimir 12f005e335 init
2025-10-17 11:27:52 +03:00

148 lines
4.9 KiB
TypeScript

import { BaseCommand } from '@adonisjs/core/ace'
import type { CommandOptions } from '@adonisjs/core/types/ace'
import logger from '@adonisjs/core/services/logger'
/**
* Test network connectivity to icetrade.by
*
* Usage:
* node ace test:connection
*/
export default class TestConnection extends BaseCommand {
static commandName = 'test:connection'
static description = 'Test network connectivity to icetrade.by'
static options: CommandOptions = {
startApp: true,
}
async run() {
const testUrl = 'https://icetrade.by'
this.logger.info('Testing network connectivity...')
this.logger.info(`Target URL: ${testUrl}`)
try {
// Test 1: Basic DNS resolution
this.logger.info('Test 1: DNS resolution')
try {
const dns = await import('node:dns/promises')
const addresses = await dns.resolve4('icetrade.by')
this.logger.success(`DNS resolved: ${addresses.join(', ')}`)
} catch (error) {
this.logger.error(
`DNS resolution failed: ${error instanceof Error ? error.message : String(error)}`
)
throw error
}
// Test 2: Basic HTTP connection
this.logger.info('Test 2: HTTP connection')
const startTime = Date.now()
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 30000)
try {
const response = await fetch(testUrl, {
method: 'HEAD',
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
},
signal: controller.signal,
})
clearTimeout(timeoutId)
const duration = Date.now() - startTime
this.logger.success(`HTTP response: ${response.status} ${response.statusText}`)
this.logger.info(`Response time: ${duration}ms`)
this.logger.info(`Content-Type: ${response.headers.get('content-type')}`)
this.logger.info(`Server: ${response.headers.get('server')}`)
} catch (error) {
clearTimeout(timeoutId)
const err = error instanceof Error ? error : new Error(String(error))
this.logger.error(`HTTP connection failed: ${err.message}`)
// Log detailed error info
logger.error('Connection test failed', {
name: err.name,
message: err.message,
stack: err.stack,
cause: err.cause,
})
throw error
}
// Test 3: Full page fetch
this.logger.info('Test 3: Full page fetch')
try {
const fullResponse = await fetch(testUrl, {
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
Accept: 'text/html',
},
})
const text = await fullResponse.text()
this.logger.success(`Received ${text.length} bytes`)
this.logger.info(`First 200 chars: ${text.substring(0, 200).replace(/\s+/g, ' ')}`)
} catch (error) {
this.logger.error(
`Full fetch failed: ${error instanceof Error ? error.message : String(error)}`
)
throw error
}
// Test 4: Actual auction page
this.logger.info('Test 4: Auction list page')
const auctionUrl = 'https://icetrade.by/trades/index?onPage=100&p=1'
try {
const auctionResponse = await fetch(auctionUrl, {
headers: {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
Accept: 'text/html',
'Accept-Language': 'ru-RU,ru;q=0.9',
},
})
if (!auctionResponse.ok) {
throw new Error(
`HTTP ${auctionResponse.status}: ${auctionResponse.statusText}`
)
}
const html = await auctionResponse.text()
this.logger.success(`Auction page received: ${html.length} bytes`)
// Check for auction table
if (html.includes('table.auctions') || html.includes('class="auctions')) {
this.logger.success('Auction table found in HTML')
} else {
this.logger.warning('Auction table not found in HTML (page structure may have changed)')
}
} catch (error) {
this.logger.error(
`Auction page fetch failed: ${error instanceof Error ? error.message : String(error)}`
)
throw error
}
this.logger.success('All connectivity tests passed!')
} catch (error) {
this.logger.error('Connectivity test failed')
this.exitCode = 1
// Print environment info
this.logger.info('Environment info:')
this.logger.info(` NODE_ENV: ${process.env.NODE_ENV}`)
this.logger.info(` Platform: ${process.platform}`)
this.logger.info(` Node version: ${process.version}`)
}
}
}