123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- "use strict";
- /**
- * Integration test for risk breach handling
- * Tests the complete flow of detecting, managing, and resolving risk breaches
- */
- Object.defineProperty(exports, "__esModule", { value: true });
- const globals_1 = require("@jest/globals");
- const HedgingManager_1 = require("../../src/core/HedgingManager");
- const RiskManager_1 = require("../../src/core/RiskManager");
- (0, globals_1.describe)('Risk Breach Handling Integration', () => {
- let hedgingManager;
- let riskManager;
- let testSessionId;
- (0, globals_1.beforeAll)(async () => {
- // Initialize hedging manager
- hedgingManager = new HedgingManager_1.HedgingManager({
- accounts: './config/accounts.json',
- hedging: './config/hedging-config.json',
- marketData: './config/market-data-config.json'
- });
- // Initialize risk manager
- riskManager = new RiskManager_1.RiskManager({
- riskCheckInterval: 1000,
- alertThresholds: {
- positionSize: 0.08,
- portfolioLoss: 0.03,
- slippage: 0.015
- },
- maxConcurrentBreaches: 10
- });
- await hedgingManager.initialize();
- await riskManager.initialize();
- });
- (0, globals_1.afterAll)(async () => {
- if (hedgingManager) {
- await hedgingManager.shutdown();
- }
- if (riskManager) {
- await riskManager.shutdown();
- }
- });
- (0, globals_1.beforeEach)(async () => {
- // Create a test session for each test
- const sessionRequest = {
- name: 'Risk Breach Test Session',
- accountIds: ['account-1', 'account-2'],
- volumeTarget: 10000,
- strategy: {
- symbol: 'ETH/USD',
- volumeDistribution: 'equal',
- priceRange: { min: 0.001, max: 0.01 },
- timing: { minInterval: 30, maxInterval: 120, orderSize: { min: 100, max: 500 } },
- riskLimits: { maxPositionSize: 0.1, stopLossThreshold: 0.05, maxSlippage: 0.02 },
- orderTypes: { primary: 'limit', fallback: 'market' }
- }
- };
- try {
- const session = await hedgingManager.createSession(sessionRequest);
- testSessionId = session.id;
- }
- catch (error) {
- testSessionId = 'mock-session-id';
- }
- });
- (0, globals_1.describe)('Risk Breach Detection', () => {
- (0, globals_1.it)('should detect position size breaches', async () => {
- try {
- // Mock position size breach scenario
- const breach = await riskManager.checkPositionSizeRisk(testSessionId, 'account-1', 0.12);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.breachType).toBe('position_size_exceeded');
- (0, globals_1.expect)(breach.severity).toBe('critical');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.accountId).toBe('account-1');
- (0, globals_1.expect)(breach.details.currentValue).toBe(0.12);
- (0, globals_1.expect)(breach.details.limitValue).toBe(0.1);
- (0, globals_1.expect)(breach.details.percentage).toBe(120);
- (0, globals_1.expect)(breach.resolved).toBe(false);
- (0, globals_1.expect)(breach.timestamp).toBeInstanceOf(Date);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should detect portfolio loss breaches', async () => {
- try {
- // Mock portfolio loss breach scenario
- const breach = await riskManager.checkPortfolioLossRisk(testSessionId, -0.06);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.breachType).toBe('portfolio_loss_exceeded');
- (0, globals_1.expect)(breach.severity).toBe('critical');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.accountId).toBeUndefined(); // Portfolio-level breach
- (0, globals_1.expect)(breach.details.currentValue).toBe(-0.06);
- (0, globals_1.expect)(breach.details.limitValue).toBe(-0.05);
- (0, globals_1.expect)(breach.details.percentage).toBe(120);
- (0, globals_1.expect)(breach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should detect slippage breaches', async () => {
- try {
- // Mock slippage breach scenario
- const breach = await riskManager.checkSlippageRisk(testSessionId, 'account-1', 0.025);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.breachType).toBe('slippage_exceeded');
- (0, globals_1.expect)(breach.severity).toBe('warning');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.accountId).toBe('account-1');
- (0, globals_1.expect)(breach.details.currentValue).toBe(0.025);
- (0, globals_1.expect)(breach.details.limitValue).toBe(0.02);
- (0, globals_1.expect)(breach.details.percentage).toBe(125);
- (0, globals_1.expect)(breach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should detect margin insufficient breaches', async () => {
- try {
- // Mock margin insufficient breach scenario
- const breach = await riskManager.checkMarginRisk(testSessionId, 'account-1', 0.05);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.breachType).toBe('margin_insufficient');
- (0, globals_1.expect)(breach.severity).toBe('critical');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.accountId).toBe('account-1');
- (0, globals_1.expect)(breach.details.currentValue).toBe(0.05);
- (0, globals_1.expect)(breach.details.limitValue).toBe(0.1);
- (0, globals_1.expect)(breach.details.percentage).toBe(50);
- (0, globals_1.expect)(breach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should detect account liquidation risk', async () => {
- try {
- // Mock liquidation risk breach scenario
- const breach = await riskManager.checkLiquidationRisk(testSessionId, 'account-1', 0.02);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.breachType).toBe('account_liquidation_risk');
- (0, globals_1.expect)(breach.severity).toBe('critical');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.accountId).toBe('account-1');
- (0, globals_1.expect)(breach.details.currentValue).toBe(0.02);
- (0, globals_1.expect)(breach.details.limitValue).toBe(0.05);
- (0, globals_1.expect)(breach.details.percentage).toBe(40);
- (0, globals_1.expect)(breach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- });
- (0, globals_1.describe)('Risk Breach Management', () => {
- let testBreachId;
- (0, globals_1.beforeEach)(async () => {
- try {
- // Create a test breach
- const breach = await riskManager.checkPositionSizeRisk(testSessionId, 'account-1', 0.12);
- testBreachId = breach.id;
- }
- catch (error) {
- testBreachId = 'mock-breach-id';
- }
- });
- (0, globals_1.it)('should store and retrieve risk breaches', async () => {
- try {
- const breach = await riskManager.getBreach(testBreachId);
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.id).toBe(testBreachId);
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(breach.breachType).toBe('position_size_exceeded');
- (0, globals_1.expect)(breach.severity).toBe('critical');
- (0, globals_1.expect)(breach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should list active breaches for a session', async () => {
- try {
- const breaches = await riskManager.getActiveBreaches(testSessionId);
- (0, globals_1.expect)(Array.isArray(breaches)).toBe(true);
- (0, globals_1.expect)(breaches.length).toBeGreaterThan(0);
- const testBreach = breaches.find(b => b.id === testBreachId);
- (0, globals_1.expect)(testBreach).toBeDefined();
- (0, globals_1.expect)(testBreach.resolved).toBe(false);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should filter breaches by type', async () => {
- try {
- const positionBreaches = await riskManager.getBreachesByType(testSessionId, 'position_size_exceeded');
- (0, globals_1.expect)(Array.isArray(positionBreaches)).toBe(true);
- positionBreaches.forEach(breach => {
- (0, globals_1.expect)(breach.breachType).toBe('position_size_exceeded');
- });
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should filter breaches by severity', async () => {
- try {
- const criticalBreaches = await riskManager.getBreachesBySeverity(testSessionId, 'critical');
- (0, globals_1.expect)(Array.isArray(criticalBreaches)).toBe(true);
- criticalBreaches.forEach(breach => {
- (0, globals_1.expect)(breach.severity).toBe('critical');
- });
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- });
- (0, globals_1.describe)('Risk Breach Resolution', () => {
- let testBreachId;
- (0, globals_1.beforeEach)(async () => {
- try {
- // Create a test breach
- const breach = await riskManager.checkPositionSizeRisk(testSessionId, 'account-1', 0.12);
- testBreachId = breach.id;
- }
- catch (error) {
- testBreachId = 'mock-breach-id';
- }
- });
- (0, globals_1.it)('should resolve a risk breach', async () => {
- try {
- const resolvedBreach = await riskManager.resolveBreach(testBreachId, 'Position reduced below limit');
- (0, globals_1.expect)(resolvedBreach).toBeDefined();
- (0, globals_1.expect)(resolvedBreach.id).toBe(testBreachId);
- (0, globals_1.expect)(resolvedBreach.resolved).toBe(true);
- (0, globals_1.expect)(resolvedBreach.resolvedAt).toBeDefined();
- (0, globals_1.expect)(resolvedBreach.resolvedAt).toBeInstanceOf(Date);
- (0, globals_1.expect)(resolvedBreach.action).toBe('Position reduced below limit');
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should acknowledge a risk breach without resolving', async () => {
- try {
- const acknowledgedBreach = await riskManager.acknowledgeBreach(testBreachId, 'ignore', 'Monitoring situation');
- (0, globals_1.expect)(acknowledgedBreach).toBeDefined();
- (0, globals_1.expect)(acknowledgedBreach.id).toBe(testBreachId);
- (0, globals_1.expect)(acknowledgedBreach.resolved).toBe(false);
- (0, globals_1.expect)(acknowledgedBreach.action).toBe('ignore');
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should prevent operations on resolved breaches', async () => {
- try {
- // First resolve the breach
- await riskManager.resolveBreach(testBreachId, 'Position reduced');
- // Try to resolve again
- await riskManager.resolveBreach(testBreachId, 'Another action');
- fail('Should have rejected resolving an already resolved breach');
- }
- catch (error) {
- (0, globals_1.expect)(error.message).toContain('already resolved');
- }
- });
- });
- (0, globals_1.describe)('Risk Monitoring', () => {
- (0, globals_1.it)('should continuously monitor risk metrics', async () => {
- const riskEvents = [];
- try {
- riskManager.on('breachDetected', (breach) => {
- riskEvents.push('breachDetected');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- });
- riskManager.on('breachResolved', (breach) => {
- riskEvents.push('breachResolved');
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- });
- // Start monitoring
- await riskManager.startMonitoring(testSessionId);
- // Wait for monitoring to detect risks
- await new Promise(resolve => setTimeout(resolve, 2000));
- // Stop monitoring
- await riskManager.stopMonitoring(testSessionId);
- (0, globals_1.expect)(riskEvents.length).toBeGreaterThan(0);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should calculate overall risk level', async () => {
- try {
- const riskLevel = await riskManager.calculateOverallRisk(testSessionId);
- (0, globals_1.expect)(riskLevel).toBeDefined();
- (0, globals_1.expect)(['low', 'medium', 'high']).toContain(riskLevel);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should generate risk status report', async () => {
- try {
- const riskStatus = await riskManager.getRiskStatus(testSessionId);
- (0, globals_1.expect)(riskStatus).toBeDefined();
- (0, globals_1.expect)(riskStatus.sessionId).toBe(testSessionId);
- (0, globals_1.expect)(riskStatus.overallRisk).toBeDefined();
- (0, globals_1.expect)(riskStatus.accountRisks).toBeDefined();
- (0, globals_1.expect)(Array.isArray(riskStatus.accountRisks)).toBe(true);
- (0, globals_1.expect)(riskStatus.portfolioRisk).toBeDefined();
- (0, globals_1.expect)(riskStatus.activeBreaches).toBeDefined();
- (0, globals_1.expect)(Array.isArray(riskStatus.activeBreaches)).toBe(true);
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- });
- (0, globals_1.describe)('Risk Breach Notifications', () => {
- (0, globals_1.it)('should emit breach detection events', async () => {
- const events = [];
- try {
- riskManager.on('breachDetected', (breach) => {
- events.push('breachDetected');
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.id).toBeDefined();
- (0, globals_1.expect)(breach.sessionId).toBe(testSessionId);
- });
- // Trigger a breach
- await riskManager.checkPositionSizeRisk(testSessionId, 'account-1', 0.12);
- (0, globals_1.expect)(events).toContain('breachDetected');
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- (0, globals_1.it)('should emit breach resolution events', async () => {
- const events = [];
- let testBreachId;
- try {
- riskManager.on('breachResolved', (breach) => {
- events.push('breachResolved');
- (0, globals_1.expect)(breach).toBeDefined();
- (0, globals_1.expect)(breach.id).toBe(testBreachId);
- });
- // Create and resolve a breach
- const breach = await riskManager.checkPositionSizeRisk(testSessionId, 'account-1', 0.12);
- testBreachId = breach.id;
- await riskManager.resolveBreach(testBreachId, 'Position reduced');
- (0, globals_1.expect)(events).toContain('breachResolved');
- }
- catch (error) {
- // This test should fail initially since RiskManager doesn't exist yet
- (0, globals_1.expect)(error.message).toContain('RiskManager');
- }
- });
- });
- (0, globals_1.describe)('Error Handling', () => {
- (0, globals_1.it)('should handle non-existent breach operations gracefully', async () => {
- const nonExistentBreachId = 'non-existent-breach-id';
- try {
- await riskManager.getBreach(nonExistentBreachId);
- fail('Should have thrown error for non-existent breach');
- }
- catch (error) {
- (0, globals_1.expect)(error.message).toContain('not found');
- }
- });
- (0, globals_1.it)('should handle invalid breach resolution attempts', async () => {
- const nonExistentBreachId = 'non-existent-breach-id';
- try {
- await riskManager.resolveBreach(nonExistentBreachId, 'Test resolution');
- fail('Should have thrown error for non-existent breach');
- }
- catch (error) {
- (0, globals_1.expect)(error.message).toContain('not found');
- }
- });
- (0, globals_1.it)('should handle monitoring errors gracefully', async () => {
- try {
- await riskManager.startMonitoring('non-existent-session');
- fail('Should have thrown error for non-existent session');
- }
- catch (error) {
- (0, globals_1.expect)(error.message).toContain('session');
- }
- });
- });
- });
- //# sourceMappingURL=test_risk_breach_handling.js.map
|