test_hedging_sessions_get.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. /**
  3. * Contract test for GET /api/v1/hedging/sessions
  4. * Tests the API contract for listing hedging sessions
  5. */
  6. var __importDefault = (this && this.__importDefault) || function (mod) {
  7. return (mod && mod.__esModule) ? mod : { "default": mod };
  8. };
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const globals_1 = require("@jest/globals");
  11. const axios_1 = __importDefault(require("axios"));
  12. (0, globals_1.describe)('GET /api/v1/hedging/sessions', () => {
  13. let client;
  14. const baseURL = 'http://localhost:3000/api/v1/hedging';
  15. (0, globals_1.beforeAll)(() => {
  16. client = axios_1.default.create({
  17. baseURL,
  18. headers: {
  19. 'Content-Type': 'application/json',
  20. 'Authorization': 'Bearer test-api-key'
  21. },
  22. timeout: 5000
  23. });
  24. });
  25. (0, globals_1.afterAll)(() => {
  26. // Cleanup if needed
  27. });
  28. (0, globals_1.describe)('Basic Functionality', () => {
  29. (0, globals_1.it)('should return list of hedging sessions', async () => {
  30. try {
  31. const response = await client.get('/sessions');
  32. // Should return 200 OK
  33. (0, globals_1.expect)(response.status).toBe(200);
  34. // Response should match schema
  35. const responseData = response.data;
  36. (0, globals_1.expect)(responseData.success).toBe(true);
  37. (0, globals_1.expect)(responseData.sessions).toBeDefined();
  38. (0, globals_1.expect)(Array.isArray(responseData.sessions)).toBe(true);
  39. (0, globals_1.expect)(responseData.total).toBeDefined();
  40. (0, globals_1.expect)(typeof responseData.total).toBe('number');
  41. (0, globals_1.expect)(responseData.limit).toBeDefined();
  42. (0, globals_1.expect)(responseData.offset).toBeDefined();
  43. // Each session should have required fields
  44. responseData.sessions.forEach(session => {
  45. (0, globals_1.expect)(session.id).toBeDefined();
  46. (0, globals_1.expect)(session.name).toBeDefined();
  47. (0, globals_1.expect)(session.status).toBeDefined();
  48. (0, globals_1.expect)(['pending', 'active', 'paused', 'completed', 'failed']).toContain(session.status);
  49. (0, globals_1.expect)(session.accounts).toBeDefined();
  50. (0, globals_1.expect)(Array.isArray(session.accounts)).toBe(true);
  51. (0, globals_1.expect)(session.volumeTarget).toBeDefined();
  52. (0, globals_1.expect)(session.volumeGenerated).toBeDefined();
  53. (0, globals_1.expect)(session.startTime).toBeDefined();
  54. });
  55. }
  56. catch (error) {
  57. // This test should fail initially since the endpoint doesn't exist yet
  58. (0, globals_1.expect)(error.response?.status).toBe(404);
  59. }
  60. });
  61. (0, globals_1.it)('should return empty list when no sessions exist', async () => {
  62. try {
  63. const response = await client.get('/sessions');
  64. const responseData = response.data;
  65. (0, globals_1.expect)(responseData.success).toBe(true);
  66. (0, globals_1.expect)(responseData.sessions).toEqual([]);
  67. (0, globals_1.expect)(responseData.total).toBe(0);
  68. }
  69. catch (error) {
  70. // This test should fail initially since the endpoint doesn't exist yet
  71. (0, globals_1.expect)(error.response?.status).toBe(404);
  72. }
  73. });
  74. });
  75. (0, globals_1.describe)('Query Parameters', () => {
  76. (0, globals_1.it)('should filter sessions by status', async () => {
  77. try {
  78. const response = await client.get('/sessions?status=active');
  79. (0, globals_1.expect)(response.status).toBe(200);
  80. const responseData = response.data;
  81. (0, globals_1.expect)(responseData.success).toBe(true);
  82. // All returned sessions should have active status
  83. responseData.sessions.forEach(session => {
  84. (0, globals_1.expect)(session.status).toBe('active');
  85. });
  86. }
  87. catch (error) {
  88. // This test should fail initially since the endpoint doesn't exist yet
  89. (0, globals_1.expect)(error.response?.status).toBe(404);
  90. }
  91. });
  92. (0, globals_1.it)('should filter sessions by account ID', async () => {
  93. try {
  94. const response = await client.get('/sessions?accountId=account-1');
  95. (0, globals_1.expect)(response.status).toBe(200);
  96. const responseData = response.data;
  97. (0, globals_1.expect)(responseData.success).toBe(true);
  98. // All returned sessions should include the specified account
  99. responseData.sessions.forEach(session => {
  100. (0, globals_1.expect)(session.accounts).toContain('account-1');
  101. });
  102. }
  103. catch (error) {
  104. // This test should fail initially since the endpoint doesn't exist yet
  105. (0, globals_1.expect)(error.response?.status).toBe(404);
  106. }
  107. });
  108. (0, globals_1.it)('should support pagination with limit and offset', async () => {
  109. try {
  110. const response = await client.get('/sessions?limit=10&offset=20');
  111. (0, globals_1.expect)(response.status).toBe(200);
  112. const responseData = response.data;
  113. (0, globals_1.expect)(responseData.success).toBe(true);
  114. (0, globals_1.expect)(responseData.limit).toBe(10);
  115. (0, globals_1.expect)(responseData.offset).toBe(20);
  116. (0, globals_1.expect)(responseData.sessions.length).toBeLessThanOrEqual(10);
  117. }
  118. catch (error) {
  119. // This test should fail initially since the endpoint doesn't exist yet
  120. (0, globals_1.expect)(error.response?.status).toBe(404);
  121. }
  122. });
  123. (0, globals_1.it)('should use default pagination values', async () => {
  124. try {
  125. const response = await client.get('/sessions');
  126. (0, globals_1.expect)(response.status).toBe(200);
  127. const responseData = response.data;
  128. (0, globals_1.expect)(responseData.success).toBe(true);
  129. (0, globals_1.expect)(responseData.limit).toBe(50); // Default limit
  130. (0, globals_1.expect)(responseData.offset).toBe(0); // Default offset
  131. }
  132. catch (error) {
  133. // This test should fail initially since the endpoint doesn't exist yet
  134. (0, globals_1.expect)(error.response?.status).toBe(404);
  135. }
  136. });
  137. (0, globals_1.it)('should handle multiple query parameters', async () => {
  138. try {
  139. const response = await client.get('/sessions?status=active&accountId=account-1&limit=5&offset=0');
  140. (0, globals_1.expect)(response.status).toBe(200);
  141. const responseData = response.data;
  142. (0, globals_1.expect)(responseData.success).toBe(true);
  143. (0, globals_1.expect)(responseData.limit).toBe(5);
  144. (0, globals_1.expect)(responseData.offset).toBe(0);
  145. // All returned sessions should match both filters
  146. responseData.sessions.forEach(session => {
  147. (0, globals_1.expect)(session.status).toBe('active');
  148. (0, globals_1.expect)(session.accounts).toContain('account-1');
  149. });
  150. }
  151. catch (error) {
  152. // This test should fail initially since the endpoint doesn't exist yet
  153. (0, globals_1.expect)(error.response?.status).toBe(404);
  154. }
  155. });
  156. });
  157. (0, globals_1.describe)('Error Handling', () => {
  158. (0, globals_1.it)('should reject request without authorization header', async () => {
  159. const clientWithoutAuth = axios_1.default.create({
  160. baseURL,
  161. headers: {
  162. 'Content-Type': 'application/json'
  163. }
  164. });
  165. try {
  166. await clientWithoutAuth.get('/sessions');
  167. fail('Should have rejected unauthorized request');
  168. }
  169. catch (error) {
  170. (0, globals_1.expect)(error.response?.status).toBe(401);
  171. }
  172. });
  173. (0, globals_1.it)('should reject request with invalid authorization token', async () => {
  174. const clientWithInvalidAuth = axios_1.default.create({
  175. baseURL,
  176. headers: {
  177. 'Content-Type': 'application/json',
  178. 'Authorization': 'Bearer invalid-token'
  179. }
  180. });
  181. try {
  182. await clientWithInvalidAuth.get('/sessions');
  183. fail('Should have rejected request with invalid token');
  184. }
  185. catch (error) {
  186. (0, globals_1.expect)(error.response?.status).toBe(401);
  187. }
  188. });
  189. (0, globals_1.it)('should handle invalid query parameters gracefully', async () => {
  190. try {
  191. const response = await client.get('/sessions?status=invalid_status');
  192. (0, globals_1.expect)(response.status).toBe(400);
  193. (0, globals_1.expect)(response.data.success).toBe(false);
  194. (0, globals_1.expect)(response.data.error.code).toBe('INVALID_QUERY_PARAMETER');
  195. }
  196. catch (error) {
  197. // This test should fail initially since the endpoint doesn't exist yet
  198. (0, globals_1.expect)(error.response?.status).toBe(404);
  199. }
  200. });
  201. (0, globals_1.it)('should handle invalid pagination parameters', async () => {
  202. try {
  203. const response = await client.get('/sessions?limit=-1&offset=-5');
  204. (0, globals_1.expect)(response.status).toBe(400);
  205. (0, globals_1.expect)(response.data.success).toBe(false);
  206. (0, globals_1.expect)(response.data.error.code).toBe('INVALID_QUERY_PARAMETER');
  207. }
  208. catch (error) {
  209. // This test should fail initially since the endpoint doesn't exist yet
  210. (0, globals_1.expect)(error.response?.status).toBe(404);
  211. }
  212. });
  213. });
  214. (0, globals_1.describe)('Rate Limiting', () => {
  215. (0, globals_1.it)('should handle rate limiting gracefully', async () => {
  216. // Make multiple requests quickly to test rate limiting
  217. const requests = Array(10).fill(null).map(() => client.get('/sessions'));
  218. try {
  219. const responses = await Promise.all(requests);
  220. // All requests should succeed (rate limit not exceeded)
  221. responses.forEach(response => {
  222. (0, globals_1.expect)(response.status).toBe(200);
  223. });
  224. }
  225. catch (error) {
  226. // If rate limited, should return 429
  227. if (error.response?.status === 429) {
  228. (0, globals_1.expect)(error.response.status).toBe(429);
  229. (0, globals_1.expect)(error.response.data.error.code).toBe('RATE_LIMIT_EXCEEDED');
  230. }
  231. else {
  232. // Otherwise, endpoint doesn't exist yet
  233. (0, globals_1.expect)(error.response?.status).toBe(404);
  234. }
  235. }
  236. });
  237. });
  238. });
  239. //# sourceMappingURL=test_hedging_sessions_get.js.map