Skip to content

Commit

Permalink
test: api timeout handling for plugin-binance
Browse files Browse the repository at this point in the history
  • Loading branch information
ai16z-demirix committed Jan 18, 2025
1 parent 3512475 commit abd7caf
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/plugin-binance/__tests__/trade.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { TradeService } from '../src/services/trade';
import { AuthenticationError, InvalidSymbolError, MinNotionalError } from '../src/types/internal/error';
import { AuthenticationError, InvalidSymbolError, MinNotionalError, ApiError } from '../src/types/internal/error';
import { ORDER_TYPES, TIME_IN_FORCE } from '../src/constants/api';

// Mock the Binance client
Expand Down Expand Up @@ -207,5 +207,30 @@ describe('TradeService', () => {
// price is missing
})).rejects.toThrow('Price is required for LIMIT orders');
});

it('should handle API timeout', async () => {
// Mock successful exchange info response first
mockExchangeInfo.mockResolvedValueOnce(mockExchangeInfoResponse);

// Mock order request to timeout
mockNewOrder.mockImplementationOnce(() =>
new Promise((_, reject) =>
setTimeout(() => reject(new Error('timeout')), 1000)
)
);

const service = new TradeService({
apiKey: 'test',
secretKey: 'test',
timeout: 500 // Set lower timeout to trigger faster
});

await expect(service.executeTrade({
symbol: 'BTCUSDT',
side: 'BUY',
type: ORDER_TYPES.MARKET,
quantity: 1
})).rejects.toBeInstanceOf(ApiError);
});
});
});

0 comments on commit abd7caf

Please # to comment.