testConfig.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * 测试配置文件
  3. */
  4. export interface TestConfig {
  5. // API 配置
  6. api: {
  7. key: string
  8. secret: string
  9. baseUrl: string
  10. }
  11. // 测试配置
  12. test: {
  13. timeout: number
  14. retryAttempts: number
  15. retryDelay: number
  16. enableIntegrationTests: boolean
  17. enablePerformanceTests: boolean
  18. }
  19. // 交易配置
  20. trading: {
  21. testSymbols: string[]
  22. minQuantities: { [key: string]: number }
  23. priceRanges: { [key: string]: { min: number; max: number } }
  24. safePriceOffset: number // 安全价格偏移量(避免真实成交)
  25. }
  26. // 性能测试配置
  27. performance: {
  28. concurrentCallLimit: number
  29. memoryThreshold: number // MB
  30. responseTimeThreshold: number // ms
  31. largeDataSize: number
  32. }
  33. }
  34. /**
  35. * 开发环境配置
  36. */
  37. export const devConfig: TestConfig = {
  38. api: {
  39. key: process.env.API_KEY || '',
  40. secret: process.env.API_SECRET || '',
  41. baseUrl: 'https://fapi.binance.com',
  42. },
  43. test: {
  44. timeout: 10000,
  45. retryAttempts: 3,
  46. retryDelay: 1000,
  47. enableIntegrationTests: true,
  48. enablePerformanceTests: true,
  49. },
  50. trading: {
  51. testSymbols: ['BTCUSDT', 'ETHUSDT', 'ADAUSDT'],
  52. minQuantities: {
  53. BTCUSDT: 0.001,
  54. ETHUSDT: 0.01,
  55. ADAUSDT: 10,
  56. },
  57. priceRanges: {
  58. BTCUSDT: { min: 40000, max: 60000 },
  59. ETHUSDT: { min: 2000, max: 4000 },
  60. ADAUSDT: { min: 0.3, max: 0.7 },
  61. },
  62. safePriceOffset: 0.5, // 50% 的价格偏移,确保不会真实成交
  63. },
  64. performance: {
  65. concurrentCallLimit: 10,
  66. memoryThreshold: 50, // 50MB
  67. responseTimeThreshold: 5000, // 5秒
  68. largeDataSize: 1000,
  69. },
  70. }
  71. /**
  72. * 生产环境配置
  73. */
  74. export const prodConfig: TestConfig = {
  75. api: {
  76. key: process.env.API_KEY || '',
  77. secret: process.env.API_SECRET || '',
  78. baseUrl: 'https://fapi.binance.com',
  79. },
  80. test: {
  81. timeout: 30000,
  82. retryAttempts: 5,
  83. retryDelay: 2000,
  84. enableIntegrationTests: false, // 生产环境禁用集成测试
  85. enablePerformanceTests: false,
  86. },
  87. trading: {
  88. testSymbols: ['BTCUSDT'], // 生产环境只测试主要交易对
  89. minQuantities: {
  90. BTCUSDT: 0.001,
  91. },
  92. priceRanges: {
  93. BTCUSDT: { min: 40000, max: 60000 },
  94. },
  95. safePriceOffset: 0.8, // 80% 的价格偏移
  96. },
  97. performance: {
  98. concurrentCallLimit: 5,
  99. memoryThreshold: 100, // 100MB
  100. responseTimeThreshold: 10000, // 10秒
  101. largeDataSize: 500,
  102. },
  103. }
  104. /**
  105. * CI/CD 环境配置
  106. */
  107. export const ciConfig: TestConfig = {
  108. api: {
  109. key: process.env.API_KEY || '',
  110. secret: process.env.API_SECRET || '',
  111. baseUrl: 'https://fapi.binance.com',
  112. },
  113. test: {
  114. timeout: 15000,
  115. retryAttempts: 3,
  116. retryDelay: 1500,
  117. enableIntegrationTests: false, // CI 环境通常禁用集成测试
  118. enablePerformanceTests: true,
  119. },
  120. trading: {
  121. testSymbols: ['BTCUSDT', 'ETHUSDT'],
  122. minQuantities: {
  123. BTCUSDT: 0.001,
  124. ETHUSDT: 0.01,
  125. },
  126. priceRanges: {
  127. BTCUSDT: { min: 40000, max: 60000 },
  128. ETHUSDT: { min: 2000, max: 4000 },
  129. },
  130. safePriceOffset: 0.7,
  131. },
  132. performance: {
  133. concurrentCallLimit: 8,
  134. memoryThreshold: 75, // 75MB
  135. responseTimeThreshold: 8000, // 8秒
  136. largeDataSize: 750,
  137. },
  138. }
  139. /**
  140. * 获取当前环境的测试配置
  141. */
  142. export function getTestConfig(): TestConfig {
  143. const env = process.env.NODE_ENV || 'development'
  144. switch (env) {
  145. case 'production':
  146. return prodConfig
  147. case 'ci':
  148. case 'test':
  149. return ciConfig
  150. case 'development':
  151. default:
  152. return devConfig
  153. }
  154. }
  155. /**
  156. * 验证测试配置
  157. */
  158. export function validateTestConfig(config: TestConfig): boolean {
  159. // 验证 API 配置
  160. if (!config.api.key || !config.api.secret) {
  161. console.warn('⚠️ API 密钥未配置,某些测试可能会被跳过')
  162. }
  163. // 验证测试配置
  164. if (config.test.timeout < 1000) {
  165. throw new Error('测试超时时间不能少于 1000ms')
  166. }
  167. if (config.test.retryAttempts < 1) {
  168. throw new Error('重试次数不能少于 1')
  169. }
  170. // 验证交易配置
  171. if (config.trading.testSymbols.length === 0) {
  172. throw new Error('测试交易对不能为空')
  173. }
  174. // 验证性能配置
  175. if (config.performance.concurrentCallLimit < 1) {
  176. throw new Error('并发调用限制不能少于 1')
  177. }
  178. return true
  179. }
  180. /**
  181. * 获取安全的测试价格(避免真实成交)
  182. */
  183. export function getSafeTestPrice(symbol: string, direction: 'buy' | 'sell' = 'buy'): number {
  184. const config = getTestConfig()
  185. const priceRange = config.trading.priceRanges[symbol]
  186. if (!priceRange) {
  187. throw new Error(`未找到交易对 ${symbol} 的价格范围配置`)
  188. }
  189. const { min, max } = priceRange
  190. const midPrice = (min + max) / 2
  191. const offset = midPrice * config.trading.safePriceOffset
  192. return direction === 'buy' ? midPrice - offset : midPrice + offset
  193. }
  194. /**
  195. * 获取测试用的最小数量
  196. */
  197. export function getTestMinQuantity(symbol: string): number {
  198. const config = getTestConfig()
  199. const minQuantity = config.trading.minQuantities[symbol]
  200. if (!minQuantity) {
  201. throw new Error(`未找到交易对 ${symbol} 的最小数量配置`)
  202. }
  203. return minQuantity
  204. }
  205. /**
  206. * 检查是否应该运行集成测试
  207. */
  208. export function shouldRunIntegrationTests(): boolean {
  209. const config = getTestConfig()
  210. return config.test.enableIntegrationTests && !!(config.api.key && config.api.secret)
  211. }
  212. /**
  213. * 检查是否应该运行性能测试
  214. */
  215. export function shouldRunPerformanceTests(): boolean {
  216. const config = getTestConfig()
  217. return config.test.enablePerformanceTests
  218. }
  219. /**
  220. * 获取测试超时时间
  221. */
  222. export function getTestTimeout(): number {
  223. const config = getTestConfig()
  224. return config.test.timeout
  225. }
  226. /**
  227. * 获取重试配置
  228. */
  229. export function getRetryConfig(): { attempts: number; delay: number } {
  230. const config = getTestConfig()
  231. return {
  232. attempts: config.test.retryAttempts,
  233. delay: config.test.retryDelay,
  234. }
  235. }