123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- "use strict";
- /**
- * Contract test for POST /api/v1/hedging/sessions/{id}/start
- * Tests the API contract for starting hedging sessions
- */
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const globals_1 = require("@jest/globals");
- const axios_1 = __importDefault(require("axios"));
- (0, globals_1.describe)('POST /api/v1/hedging/sessions/{id}/start', () => {
- let client;
- const baseURL = 'http://localhost:3000/api/v1/hedging';
- (0, globals_1.beforeAll)(() => {
- client = axios_1.default.create({
- baseURL,
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer test-api-key'
- },
- timeout: 5000
- });
- });
- (0, globals_1.afterAll)(() => {
- // Cleanup if needed
- });
- (0, globals_1.describe)('Basic Functionality', () => {
- (0, globals_1.it)('should start a pending hedging session', async () => {
- const sessionId = 'test-session-1';
- try {
- const response = await client.post(`/sessions/${sessionId}/start`);
- // Should return 200 OK
- (0, globals_1.expect)(response.status).toBe(200);
- // Response should match schema
- (0, globals_1.expect)(response.data.success).toBe(true);
- (0, globals_1.expect)(response.data.session).toBeDefined();
- (0, globals_1.expect)(response.data.session.id).toBe(sessionId);
- (0, globals_1.expect)(response.data.session.status).toBe('active');
- (0, globals_1.expect)(response.data.session.startTime).toBeDefined();
- (0, globals_1.expect)(new Date(response.data.session.startTime)).toBeInstanceOf(Date);
- }
- catch (error) {
- // This test should fail initially since the endpoint doesn't exist yet
- (0, globals_1.expect)(error.response?.status).toBe(404);
- }
- });
- (0, globals_1.it)('should return appropriate success message', async () => {
- const sessionId = 'test-session-2';
- try {
- const response = await client.post(`/sessions/${sessionId}/start`);
- (0, globals_1.expect)(response.status).toBe(200);
- (0, globals_1.expect)(response.data.success).toBe(true);
- (0, globals_1.expect)(response.data.message).toBeDefined();
- (0, globals_1.expect)(typeof response.data.message).toBe('string');
- }
- catch (error) {
- // This test should fail initially since the endpoint doesn't exist yet
- (0, globals_1.expect)(error.response?.status).toBe(404);
- }
- });
- });
- (0, globals_1.describe)('Session Status Validation', () => {
- (0, globals_1.it)('should reject starting a session that is not in pending status', async () => {
- const activeSessionId = 'active-session-1';
- try {
- await client.post(`/sessions/${activeSessionId}/start`);
- fail('Should have rejected starting an active session');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('INVALID_SESSION_STATUS');
- (0, globals_1.expect)(error.response?.data.error.message).toContain('pending');
- }
- });
- (0, globals_1.it)('should reject starting a completed session', async () => {
- const completedSessionId = 'completed-session-1';
- try {
- await client.post(`/sessions/${completedSessionId}/start`);
- fail('Should have rejected starting a completed session');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('INVALID_SESSION_STATUS');
- }
- });
- (0, globals_1.it)('should reject starting a failed session', async () => {
- const failedSessionId = 'failed-session-1';
- try {
- await client.post(`/sessions/${failedSessionId}/start`);
- fail('Should have rejected starting a failed session');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('INVALID_SESSION_STATUS');
- }
- });
- });
- (0, globals_1.describe)('Account Validation', () => {
- (0, globals_1.it)('should reject starting session with inactive accounts', async () => {
- const sessionWithInactiveAccount = 'session-inactive-account';
- try {
- await client.post(`/sessions/${sessionWithInactiveAccount}/start`);
- fail('Should have rejected starting session with inactive account');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('ACCOUNT_NOT_ACTIVE');
- }
- });
- (0, globals_1.it)('should reject starting session with insufficient balance', async () => {
- const sessionInsufficientBalance = 'session-insufficient-balance';
- try {
- await client.post(`/sessions/${sessionInsufficientBalance}/start`);
- fail('Should have rejected starting session with insufficient balance');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('INSUFFICIENT_BALANCE');
- }
- });
- });
- (0, globals_1.describe)('Risk Validation', () => {
- (0, globals_1.it)('should reject starting session with active risk breaches', async () => {
- const sessionWithRiskBreach = 'session-risk-breach';
- try {
- await client.post(`/sessions/${sessionWithRiskBreach}/start`);
- fail('Should have rejected starting session with active risk breaches');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('RISK_LIMIT_EXCEEDED');
- }
- });
- (0, globals_1.it)('should reject starting session that would exceed risk limits', async () => {
- const sessionExceedsRisk = 'session-exceeds-risk';
- try {
- await client.post(`/sessions/${sessionExceedsRisk}/start`);
- fail('Should have rejected starting session that exceeds risk limits');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(400);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('RISK_LIMIT_EXCEEDED');
- }
- });
- });
- (0, globals_1.describe)('Session Not Found', () => {
- (0, globals_1.it)('should return 404 for non-existent session', async () => {
- const nonExistentSessionId = 'non-existent-session';
- try {
- await client.post(`/sessions/${nonExistentSessionId}/start`);
- fail('Should have returned 404 for non-existent session');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(404);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('SESSION_NOT_FOUND');
- }
- });
- (0, globals_1.it)('should return 404 for invalid session ID format', async () => {
- const invalidSessionId = 'invalid-session-id-format!@#';
- try {
- await client.post(`/sessions/${invalidSessionId}/start`);
- fail('Should have returned 404 for invalid session ID');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(404);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('SESSION_NOT_FOUND');
- }
- });
- });
- (0, globals_1.describe)('Authentication', () => {
- (0, globals_1.it)('should reject request without authorization header', async () => {
- const clientWithoutAuth = axios_1.default.create({
- baseURL,
- headers: {
- 'Content-Type': 'application/json'
- }
- });
- try {
- await clientWithoutAuth.post('/sessions/test-session/start');
- fail('Should have rejected unauthorized request');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(401);
- }
- });
- (0, globals_1.it)('should reject request with invalid authorization token', async () => {
- const clientWithInvalidAuth = axios_1.default.create({
- baseURL,
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer invalid-token'
- }
- });
- try {
- await clientWithInvalidAuth.post('/sessions/test-session/start');
- fail('Should have rejected request with invalid token');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(401);
- }
- });
- });
- (0, globals_1.describe)('Concurrent Operations', () => {
- (0, globals_1.it)('should handle concurrent start requests for same session', async () => {
- const sessionId = 'concurrent-session';
- try {
- // Make multiple concurrent start requests
- const requests = Array(3).fill(null).map(() => client.post(`/sessions/${sessionId}/start`));
- const responses = await Promise.all(requests);
- // Only one should succeed, others should fail with appropriate error
- const successfulResponses = responses.filter(r => r.status === 200);
- const failedResponses = responses.filter(r => r.status !== 200);
- (0, globals_1.expect)(successfulResponses.length).toBe(1);
- (0, globals_1.expect)(failedResponses.length).toBe(2);
- failedResponses.forEach(response => {
- (0, globals_1.expect)(response.data.success).toBe(false);
- (0, globals_1.expect)(response.data.error.code).toBe('INVALID_SESSION_STATUS');
- });
- }
- catch (error) {
- // This test should fail initially since the endpoint doesn't exist yet
- (0, globals_1.expect)(error.response?.status).toBe(404);
- }
- });
- });
- (0, globals_1.describe)('Market Data Validation', () => {
- (0, globals_1.it)('should reject starting session when market data is unavailable', async () => {
- const sessionNoMarketData = 'session-no-market-data';
- try {
- await client.post(`/sessions/${sessionNoMarketData}/start`);
- fail('Should have rejected starting session without market data');
- }
- catch (error) {
- (0, globals_1.expect)(error.response?.status).toBe(503);
- (0, globals_1.expect)(error.response?.data.success).toBe(false);
- (0, globals_1.expect)(error.response?.data.error.code).toBe('MARKET_DATA_UNAVAILABLE');
- }
- });
- });
- });
- //# sourceMappingURL=test_hedging_start.js.map
|