test_hedging_session_creation.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. "use strict";
  2. /**
  3. * Integration test for hedging session creation
  4. * Tests the complete flow of creating and managing hedging sessions
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. const globals_1 = require("@jest/globals");
  8. const HedgingManager_1 = require("../../src/core/HedgingManager");
  9. const HedgingConfigManager_1 = require("../../src/core/HedgingConfigManager");
  10. (0, globals_1.describe)('Hedging Session Creation Integration', () => {
  11. let hedgingManager;
  12. let configManager;
  13. (0, globals_1.beforeAll)(async () => {
  14. // Initialize configuration manager
  15. configManager = new HedgingConfigManager_1.HedgingConfigManager({
  16. accounts: './config/accounts.json',
  17. hedging: './config/hedging-config.json',
  18. marketData: './config/market-data-config.json'
  19. });
  20. // Initialize hedging manager
  21. hedgingManager = new HedgingManager_1.HedgingManager({
  22. accounts: './config/accounts.json',
  23. hedging: './config/hedging-config.json',
  24. marketData: './config/market-data-config.json'
  25. });
  26. await hedgingManager.initialize();
  27. });
  28. (0, globals_1.afterAll)(async () => {
  29. if (hedgingManager) {
  30. await hedgingManager.shutdown();
  31. }
  32. });
  33. (0, globals_1.beforeEach)(() => {
  34. // Clean up any existing sessions before each test
  35. hedgingManager.cleanup();
  36. });
  37. (0, globals_1.describe)('Session Creation Flow', () => {
  38. (0, globals_1.it)('should create a new hedging session with valid parameters', async () => {
  39. const sessionRequest = {
  40. name: 'Integration Test Session',
  41. accountIds: ['account-1', 'account-2'],
  42. volumeTarget: 10000,
  43. strategy: {
  44. symbol: 'ETH/USD',
  45. volumeDistribution: 'equal',
  46. priceRange: {
  47. min: 0.001,
  48. max: 0.01
  49. },
  50. timing: {
  51. minInterval: 30,
  52. maxInterval: 120,
  53. orderSize: {
  54. min: 100,
  55. max: 500
  56. }
  57. },
  58. riskLimits: {
  59. maxPositionSize: 0.1,
  60. stopLossThreshold: 0.05,
  61. maxSlippage: 0.02
  62. },
  63. orderTypes: {
  64. primary: 'limit',
  65. fallback: 'market'
  66. }
  67. }
  68. };
  69. try {
  70. const session = await hedgingManager.createSession(sessionRequest);
  71. (0, globals_1.expect)(session).toBeDefined();
  72. (0, globals_1.expect)(session.id).toBeDefined();
  73. (0, globals_1.expect)(session.name).toBe(sessionRequest.name);
  74. (0, globals_1.expect)(session.status).toBe('pending');
  75. (0, globals_1.expect)(session.accounts).toEqual(sessionRequest.accountIds);
  76. (0, globals_1.expect)(session.strategy).toEqual(sessionRequest.strategy);
  77. (0, globals_1.expect)(session.volumeTarget).toBe(sessionRequest.volumeTarget);
  78. (0, globals_1.expect)(session.volumeGenerated).toBe(0);
  79. (0, globals_1.expect)(session.riskBreaches).toEqual([]);
  80. (0, globals_1.expect)(session.orders).toEqual([]);
  81. (0, globals_1.expect)(session.createdAt).toBeInstanceOf(Date);
  82. (0, globals_1.expect)(session.updatedAt).toBeInstanceOf(Date);
  83. }
  84. catch (error) {
  85. // This test should fail initially since HedgingManager doesn't exist yet
  86. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  87. }
  88. });
  89. (0, globals_1.it)('should validate account availability before creating session', async () => {
  90. const sessionRequest = {
  91. name: 'Test Session with Invalid Account',
  92. accountIds: ['account-1', 'non-existent-account'],
  93. volumeTarget: 10000,
  94. strategy: {
  95. symbol: 'ETH/USD',
  96. volumeDistribution: 'equal',
  97. priceRange: { min: 0.001, max: 0.01 },
  98. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  99. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  100. orderTypes: { primary: 'limit', fallback: 'market' }
  101. }
  102. };
  103. try {
  104. await hedgingManager.createSession(sessionRequest);
  105. fail('Should have rejected session with invalid account');
  106. }
  107. catch (error) {
  108. (0, globals_1.expect)(error.message).toContain('account');
  109. }
  110. });
  111. (0, globals_1.it)('should validate account balance before creating session', async () => {
  112. const sessionRequest = {
  113. name: 'Test Session with Insufficient Balance',
  114. accountIds: ['account-1', 'account-2'],
  115. volumeTarget: 1000000, // Very high volume target
  116. strategy: {
  117. symbol: 'ETH/USD',
  118. volumeDistribution: 'equal',
  119. priceRange: { min: 0.001, max: 0.01 },
  120. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  121. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  122. orderTypes: { primary: 'limit', fallback: 'market' }
  123. }
  124. };
  125. try {
  126. await hedgingManager.createSession(sessionRequest);
  127. fail('Should have rejected session with insufficient balance');
  128. }
  129. catch (error) {
  130. (0, globals_1.expect)(error.message).toContain('balance');
  131. }
  132. });
  133. (0, globals_1.it)('should validate strategy parameters before creating session', async () => {
  134. const sessionRequest = {
  135. name: 'Test Session with Invalid Strategy',
  136. accountIds: ['account-1', 'account-2'],
  137. volumeTarget: 10000,
  138. strategy: {
  139. symbol: 'ETH/USD',
  140. volumeDistribution: 'equal',
  141. priceRange: {
  142. min: 0.01,
  143. max: 0.001 // Invalid: min > max
  144. },
  145. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  146. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  147. orderTypes: { primary: 'limit', fallback: 'market' }
  148. }
  149. };
  150. try {
  151. await hedgingManager.createSession(sessionRequest);
  152. fail('Should have rejected session with invalid strategy');
  153. }
  154. catch (error) {
  155. (0, globals_1.expect)(error.message).toContain('strategy');
  156. }
  157. });
  158. });
  159. (0, globals_1.describe)('Session Management', () => {
  160. let testSessionId;
  161. (0, globals_1.beforeEach)(async () => {
  162. const sessionRequest = {
  163. name: 'Test Management Session',
  164. accountIds: ['account-1', 'account-2'],
  165. volumeTarget: 10000,
  166. strategy: {
  167. symbol: 'ETH/USD',
  168. volumeDistribution: 'equal',
  169. priceRange: { min: 0.001, max: 0.01 },
  170. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  171. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  172. orderTypes: { primary: 'limit', fallback: 'market' }
  173. }
  174. };
  175. try {
  176. const session = await hedgingManager.createSession(sessionRequest);
  177. testSessionId = session.id;
  178. }
  179. catch (error) {
  180. // Session creation will fail initially
  181. testSessionId = 'mock-session-id';
  182. }
  183. });
  184. (0, globals_1.it)('should retrieve session by ID', async () => {
  185. try {
  186. const session = await hedgingManager.getSession(testSessionId);
  187. (0, globals_1.expect)(session).toBeDefined();
  188. (0, globals_1.expect)(session.id).toBe(testSessionId);
  189. (0, globals_1.expect)(session.name).toBe('Test Management Session');
  190. }
  191. catch (error) {
  192. // This test should fail initially since HedgingManager doesn't exist yet
  193. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  194. }
  195. });
  196. (0, globals_1.it)('should list all sessions', async () => {
  197. try {
  198. const sessions = await hedgingManager.listSessions();
  199. (0, globals_1.expect)(Array.isArray(sessions)).toBe(true);
  200. (0, globals_1.expect)(sessions.length).toBeGreaterThanOrEqual(1);
  201. const testSession = sessions.find(s => s.id === testSessionId);
  202. (0, globals_1.expect)(testSession).toBeDefined();
  203. (0, globals_1.expect)(testSession.name).toBe('Test Management Session');
  204. }
  205. catch (error) {
  206. // This test should fail initially since HedgingManager doesn't exist yet
  207. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  208. }
  209. });
  210. (0, globals_1.it)('should filter sessions by status', async () => {
  211. try {
  212. const pendingSessions = await hedgingManager.listSessions({ status: 'pending' });
  213. (0, globals_1.expect)(Array.isArray(pendingSessions)).toBe(true);
  214. pendingSessions.forEach(session => {
  215. (0, globals_1.expect)(session.status).toBe('pending');
  216. });
  217. }
  218. catch (error) {
  219. // This test should fail initially since HedgingManager doesn't exist yet
  220. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  221. }
  222. });
  223. (0, globals_1.it)('should filter sessions by account ID', async () => {
  224. try {
  225. const accountSessions = await hedgingManager.listSessions({ accountId: 'account-1' });
  226. (0, globals_1.expect)(Array.isArray(accountSessions)).toBe(true);
  227. accountSessions.forEach(session => {
  228. (0, globals_1.expect)(session.accounts).toContain('account-1');
  229. });
  230. }
  231. catch (error) {
  232. // This test should fail initially since HedgingManager doesn't exist yet
  233. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  234. }
  235. });
  236. });
  237. (0, globals_1.describe)('Session State Transitions', () => {
  238. let testSessionId;
  239. (0, globals_1.beforeEach)(async () => {
  240. const sessionRequest = {
  241. name: 'Test State Transition Session',
  242. accountIds: ['account-1', 'account-2'],
  243. volumeTarget: 10000,
  244. strategy: {
  245. symbol: 'ETH/USD',
  246. volumeDistribution: 'equal',
  247. priceRange: { min: 0.001, max: 0.01 },
  248. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  249. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  250. orderTypes: { primary: 'limit', fallback: 'market' }
  251. }
  252. };
  253. try {
  254. const session = await hedgingManager.createSession(sessionRequest);
  255. testSessionId = session.id;
  256. }
  257. catch (error) {
  258. testSessionId = 'mock-session-id';
  259. }
  260. });
  261. (0, globals_1.it)('should start a pending session', async () => {
  262. try {
  263. const session = await hedgingManager.startSession(testSessionId);
  264. (0, globals_1.expect)(session.status).toBe('active');
  265. (0, globals_1.expect)(session.startTime).toBeDefined();
  266. (0, globals_1.expect)(session.startTime).toBeInstanceOf(Date);
  267. }
  268. catch (error) {
  269. // This test should fail initially since HedgingManager doesn't exist yet
  270. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  271. }
  272. });
  273. (0, globals_1.it)('should pause an active session', async () => {
  274. try {
  275. // First start the session
  276. await hedgingManager.startSession(testSessionId);
  277. // Then pause it
  278. const session = await hedgingManager.pauseSession(testSessionId);
  279. (0, globals_1.expect)(session.status).toBe('paused');
  280. }
  281. catch (error) {
  282. // This test should fail initially since HedgingManager doesn't exist yet
  283. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  284. }
  285. });
  286. (0, globals_1.it)('should resume a paused session', async () => {
  287. try {
  288. // Start and pause the session
  289. await hedgingManager.startSession(testSessionId);
  290. await hedgingManager.pauseSession(testSessionId);
  291. // Then resume it
  292. const session = await hedgingManager.resumeSession(testSessionId);
  293. (0, globals_1.expect)(session.status).toBe('active');
  294. }
  295. catch (error) {
  296. // This test should fail initially since HedgingManager doesn't exist yet
  297. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  298. }
  299. });
  300. (0, globals_1.it)('should stop an active session', async () => {
  301. try {
  302. // Start the session
  303. await hedgingManager.startSession(testSessionId);
  304. // Then stop it
  305. const session = await hedgingManager.stopSession(testSessionId);
  306. (0, globals_1.expect)(session.status).toBe('completed');
  307. (0, globals_1.expect)(session.endTime).toBeDefined();
  308. (0, globals_1.expect)(session.endTime).toBeInstanceOf(Date);
  309. }
  310. catch (error) {
  311. // This test should fail initially since HedgingManager doesn't exist yet
  312. (0, globals_1.expect)(error.message).toContain('HedgingManager');
  313. }
  314. });
  315. });
  316. (0, globals_1.describe)('Error Handling', () => {
  317. (0, globals_1.it)('should handle non-existent session operations gracefully', async () => {
  318. const nonExistentSessionId = 'non-existent-session-id';
  319. try {
  320. await hedgingManager.getSession(nonExistentSessionId);
  321. fail('Should have thrown error for non-existent session');
  322. }
  323. catch (error) {
  324. (0, globals_1.expect)(error.message).toContain('not found');
  325. }
  326. });
  327. (0, globals_1.it)('should handle invalid session state transitions', async () => {
  328. const sessionRequest = {
  329. name: 'Test Invalid Transition Session',
  330. accountIds: ['account-1', 'account-2'],
  331. volumeTarget: 10000,
  332. strategy: {
  333. symbol: 'ETH/USD',
  334. volumeDistribution: 'equal',
  335. priceRange: { min: 0.001, max: 0.01 },
  336. timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
  337. riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
  338. orderTypes: { primary: 'limit', fallback: 'market' }
  339. }
  340. };
  341. try {
  342. const session = await hedgingManager.createSession(sessionRequest);
  343. // Try to pause a pending session (should fail)
  344. await hedgingManager.pauseSession(session.id);
  345. fail('Should have rejected pausing a pending session');
  346. }
  347. catch (error) {
  348. (0, globals_1.expect)(error.message).toContain('status');
  349. }
  350. });
  351. });
  352. (0, globals_1.describe)('Configuration Integration', () => {
  353. (0, globals_1.it)('should use default strategy from configuration', async () => {
  354. try {
  355. const defaultStrategy = await configManager.getDefaultStrategy();
  356. (0, globals_1.expect)(defaultStrategy).toBeDefined();
  357. (0, globals_1.expect)(defaultStrategy.symbol).toBeDefined();
  358. (0, globals_1.expect)(defaultStrategy.volumeDistribution).toBeDefined();
  359. (0, globals_1.expect)(defaultStrategy.priceRange).toBeDefined();
  360. (0, globals_1.expect)(defaultStrategy.timing).toBeDefined();
  361. (0, globals_1.expect)(defaultStrategy.riskLimits).toBeDefined();
  362. (0, globals_1.expect)(defaultStrategy.orderTypes).toBeDefined();
  363. }
  364. catch (error) {
  365. // This test should fail initially since HedgingConfigManager doesn't exist yet
  366. (0, globals_1.expect)(error.message).toContain('HedgingConfigManager');
  367. }
  368. });
  369. (0, globals_1.it)('should validate configuration parameters', async () => {
  370. try {
  371. const config = await configManager.getConfig();
  372. (0, globals_1.expect)(config).toBeDefined();
  373. (0, globals_1.expect)(config.defaultStrategy).toBeDefined();
  374. (0, globals_1.expect)(config.monitoring).toBeDefined();
  375. (0, globals_1.expect)(config.sessionDefaults).toBeDefined();
  376. (0, globals_1.expect)(config.orderDefaults).toBeDefined();
  377. (0, globals_1.expect)(config.websocket).toBeDefined();
  378. }
  379. catch (error) {
  380. // This test should fail initially since HedgingConfigManager doesn't exist yet
  381. (0, globals_1.expect)(error.message).toContain('HedgingConfigManager');
  382. }
  383. });
  384. });
  385. });
  386. //# sourceMappingURL=test_hedging_session_creation.js.map