128 lines
3.4 KiB
TypeScript
128 lines
3.4 KiB
TypeScript
/**
|
|
* Jest Global Test Setup
|
|
*
|
|
* Configures the test environment for Position Manager integration tests.
|
|
* Mocks external dependencies to isolate unit testing.
|
|
*/
|
|
|
|
// Extend Jest matchers if needed
|
|
expect.extend({
|
|
toBeWithinRange(received: number, floor: number, ceiling: number) {
|
|
const pass = received >= floor && received <= ceiling
|
|
if (pass) {
|
|
return {
|
|
message: () => `expected ${received} not to be within range ${floor} - ${ceiling}`,
|
|
pass: true,
|
|
}
|
|
} else {
|
|
return {
|
|
message: () => `expected ${received} to be within range ${floor} - ${ceiling}`,
|
|
pass: false,
|
|
}
|
|
}
|
|
},
|
|
})
|
|
|
|
// Declare custom matchers for TypeScript
|
|
declare global {
|
|
namespace jest {
|
|
interface Matchers<R> {
|
|
toBeWithinRange(floor: number, ceiling: number): R
|
|
}
|
|
}
|
|
}
|
|
|
|
// Mock logger to reduce noise during tests
|
|
// Logger is mocked to:
|
|
// 1. Prevent console output during test runs
|
|
// 2. Isolate tests from external I/O operations
|
|
// 3. Enable verification of logging calls if needed
|
|
jest.mock('../lib/utils/logger', () => ({
|
|
logger: {
|
|
log: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
info: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
// Mock Drift SDK enums to avoid native bindings during tests
|
|
jest.mock('@drift-labs/sdk', () => ({
|
|
MarketType: { PERP: 'perp' },
|
|
PositionDirection: { LONG: 'long', SHORT: 'short' },
|
|
OrderType: {
|
|
LIMIT: 'limit',
|
|
TRIGGER_LIMIT: 'trigger_limit',
|
|
TRIGGER_MARKET: 'trigger_market',
|
|
MARKET: 'market',
|
|
},
|
|
OrderTriggerCondition: { BELOW: 'below', ABOVE: 'above' },
|
|
}))
|
|
|
|
// Mock Drift service to avoid network calls
|
|
jest.mock('../lib/drift/client', () => ({
|
|
getDriftService: jest.fn(() => ({
|
|
getPosition: jest.fn(),
|
|
getOraclePrice: jest.fn(),
|
|
isInitialized: true,
|
|
})),
|
|
initializeDriftService: jest.fn(),
|
|
}))
|
|
|
|
// Mock Pyth price monitor
|
|
jest.mock('../lib/pyth/price-monitor', () => ({
|
|
getPythPriceMonitor: jest.fn(() => ({
|
|
start: jest.fn(),
|
|
stop: jest.fn(),
|
|
getCachedPrice: jest.fn(),
|
|
getLatestPrice: jest.fn(),
|
|
})),
|
|
}))
|
|
|
|
// Mock database operations
|
|
jest.mock('../lib/database/trades', () => ({
|
|
getOpenTrades: jest.fn(() => Promise.resolve([])),
|
|
updateTradeExit: jest.fn(() => Promise.resolve()),
|
|
updateTradeState: jest.fn(() => Promise.resolve()),
|
|
createTrade: jest.fn(() => Promise.resolve()),
|
|
}))
|
|
|
|
// Mock Telegram notifications
|
|
jest.mock('../lib/notifications/telegram', () => ({
|
|
sendPositionClosedNotification: jest.fn(() => Promise.resolve()),
|
|
sendPositionOpenedNotification: jest.fn(() => Promise.resolve()),
|
|
}))
|
|
|
|
// Mock Drift orders (keep real placeExitOrders for validation tests)
|
|
jest.mock('../lib/drift/orders', () => {
|
|
const actual = jest.requireActual('../lib/drift/orders')
|
|
return {
|
|
...actual,
|
|
closePosition: jest.fn(() => Promise.resolve({ success: true, realizedPnL: 0 })),
|
|
cancelAllOrders: jest.fn(() => Promise.resolve({ success: true, cancelledCount: 0 })),
|
|
}
|
|
})
|
|
|
|
// Mock market data cache
|
|
jest.mock('../lib/trading/market-data-cache', () => ({
|
|
getMarketDataCache: jest.fn(() => ({
|
|
get: jest.fn(() => null),
|
|
set: jest.fn(),
|
|
})),
|
|
}))
|
|
|
|
// Mock stop hunt tracker
|
|
jest.mock('../lib/trading/stop-hunt-tracker', () => ({
|
|
getStopHuntTracker: jest.fn(() => ({
|
|
recordStopHunt: jest.fn(() => Promise.resolve()),
|
|
updateRevengeOutcome: jest.fn(() => Promise.resolve()),
|
|
})),
|
|
}))
|
|
|
|
// Reset mocks before each test
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
export {}
|