| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /**
- * 测试配置文件
- */
- export interface TestConfig {
- // API 配置
- api: {
- key: string
- secret: string
- baseUrl: string
- }
- // 测试配置
- test: {
- timeout: number
- retryAttempts: number
- retryDelay: number
- enableIntegrationTests: boolean
- enablePerformanceTests: boolean
- }
- // 交易配置
- trading: {
- testSymbols: string[]
- minQuantities: { [key: string]: number }
- priceRanges: { [key: string]: { min: number; max: number } }
- safePriceOffset: number // 安全价格偏移量(避免真实成交)
- }
- // 性能测试配置
- performance: {
- concurrentCallLimit: number
- memoryThreshold: number // MB
- responseTimeThreshold: number // ms
- largeDataSize: number
- }
- }
- /**
- * 开发环境配置
- */
- export const devConfig: TestConfig = {
- api: {
- key: process.env.API_KEY || '',
- secret: process.env.API_SECRET || '',
- baseUrl: 'https://fapi.binance.com',
- },
- test: {
- timeout: 10000,
- retryAttempts: 3,
- retryDelay: 1000,
- enableIntegrationTests: true,
- enablePerformanceTests: true,
- },
- trading: {
- testSymbols: ['BTCUSDT', 'ETHUSDT', 'ADAUSDT'],
- minQuantities: {
- BTCUSDT: 0.001,
- ETHUSDT: 0.01,
- ADAUSDT: 10,
- },
- priceRanges: {
- BTCUSDT: { min: 40000, max: 60000 },
- ETHUSDT: { min: 2000, max: 4000 },
- ADAUSDT: { min: 0.3, max: 0.7 },
- },
- safePriceOffset: 0.5, // 50% 的价格偏移,确保不会真实成交
- },
- performance: {
- concurrentCallLimit: 10,
- memoryThreshold: 50, // 50MB
- responseTimeThreshold: 5000, // 5秒
- largeDataSize: 1000,
- },
- }
- /**
- * 生产环境配置
- */
- export const prodConfig: TestConfig = {
- api: {
- key: process.env.API_KEY || '',
- secret: process.env.API_SECRET || '',
- baseUrl: 'https://fapi.binance.com',
- },
- test: {
- timeout: 30000,
- retryAttempts: 5,
- retryDelay: 2000,
- enableIntegrationTests: false, // 生产环境禁用集成测试
- enablePerformanceTests: false,
- },
- trading: {
- testSymbols: ['BTCUSDT'], // 生产环境只测试主要交易对
- minQuantities: {
- BTCUSDT: 0.001,
- },
- priceRanges: {
- BTCUSDT: { min: 40000, max: 60000 },
- },
- safePriceOffset: 0.8, // 80% 的价格偏移
- },
- performance: {
- concurrentCallLimit: 5,
- memoryThreshold: 100, // 100MB
- responseTimeThreshold: 10000, // 10秒
- largeDataSize: 500,
- },
- }
- /**
- * CI/CD 环境配置
- */
- export const ciConfig: TestConfig = {
- api: {
- key: process.env.API_KEY || '',
- secret: process.env.API_SECRET || '',
- baseUrl: 'https://fapi.binance.com',
- },
- test: {
- timeout: 15000,
- retryAttempts: 3,
- retryDelay: 1500,
- enableIntegrationTests: false, // CI 环境通常禁用集成测试
- enablePerformanceTests: true,
- },
- trading: {
- testSymbols: ['BTCUSDT', 'ETHUSDT'],
- minQuantities: {
- BTCUSDT: 0.001,
- ETHUSDT: 0.01,
- },
- priceRanges: {
- BTCUSDT: { min: 40000, max: 60000 },
- ETHUSDT: { min: 2000, max: 4000 },
- },
- safePriceOffset: 0.7,
- },
- performance: {
- concurrentCallLimit: 8,
- memoryThreshold: 75, // 75MB
- responseTimeThreshold: 8000, // 8秒
- largeDataSize: 750,
- },
- }
- /**
- * 获取当前环境的测试配置
- */
- export function getTestConfig(): TestConfig {
- const env = process.env.NODE_ENV || 'development'
- switch (env) {
- case 'production':
- return prodConfig
- case 'ci':
- case 'test':
- return ciConfig
- case 'development':
- default:
- return devConfig
- }
- }
- /**
- * 验证测试配置
- */
- export function validateTestConfig(config: TestConfig): boolean {
- // 验证 API 配置
- if (!config.api.key || !config.api.secret) {
- console.warn('⚠️ API 密钥未配置,某些测试可能会被跳过')
- }
- // 验证测试配置
- if (config.test.timeout < 1000) {
- throw new Error('测试超时时间不能少于 1000ms')
- }
- if (config.test.retryAttempts < 1) {
- throw new Error('重试次数不能少于 1')
- }
- // 验证交易配置
- if (config.trading.testSymbols.length === 0) {
- throw new Error('测试交易对不能为空')
- }
- // 验证性能配置
- if (config.performance.concurrentCallLimit < 1) {
- throw new Error('并发调用限制不能少于 1')
- }
- return true
- }
- /**
- * 获取安全的测试价格(避免真实成交)
- */
- export function getSafeTestPrice(symbol: string, direction: 'buy' | 'sell' = 'buy'): number {
- const config = getTestConfig()
- const priceRange = config.trading.priceRanges[symbol]
- if (!priceRange) {
- throw new Error(`未找到交易对 ${symbol} 的价格范围配置`)
- }
- const { min, max } = priceRange
- const midPrice = (min + max) / 2
- const offset = midPrice * config.trading.safePriceOffset
- return direction === 'buy' ? midPrice - offset : midPrice + offset
- }
- /**
- * 获取测试用的最小数量
- */
- export function getTestMinQuantity(symbol: string): number {
- const config = getTestConfig()
- const minQuantity = config.trading.minQuantities[symbol]
- if (!minQuantity) {
- throw new Error(`未找到交易对 ${symbol} 的最小数量配置`)
- }
- return minQuantity
- }
- /**
- * 检查是否应该运行集成测试
- */
- export function shouldRunIntegrationTests(): boolean {
- const config = getTestConfig()
- return config.test.enableIntegrationTests && !!(config.api.key && config.api.secret)
- }
- /**
- * 检查是否应该运行性能测试
- */
- export function shouldRunPerformanceTests(): boolean {
- const config = getTestConfig()
- return config.test.enablePerformanceTests
- }
- /**
- * 获取测试超时时间
- */
- export function getTestTimeout(): number {
- const config = getTestConfig()
- return config.test.timeout
- }
- /**
- * 获取重试配置
- */
- export function getRetryConfig(): { attempts: number; delay: number } {
- const config = getTestConfig()
- return {
- attempts: config.test.retryAttempts,
- delay: config.test.retryDelay,
- }
- }
|