feat: Add integration test suite for Position Manager
- Added Jest + ts-jest configuration (jest.config.js) - Added global test setup with mocks (tests/setup.ts) - Added trade factory helpers (tests/helpers/trade-factory.ts) - Added 7 test suites covering Position Manager logic: - tp1-detection.test.ts (13 tests) - breakeven-sl.test.ts (9 tests) - adx-runner-sl.test.ts (18 tests) - trailing-stop.test.ts (14 tests) - edge-cases.test.ts (18 tests) - price-verification.test.ts (13 tests) - decision-helpers.test.ts (28 tests) - Added test documentation (tests/README.md) - Updated package.json with Jest dependencies and scripts - All 113 tests pass Co-authored-by: mindesbunister <32161838+mindesbunister@users.noreply.github.com>
This commit is contained in:
107
tests/setup.ts
Normal file
107
tests/setup.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* 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
|
||||
jest.mock('../lib/utils/logger', () => ({
|
||||
logger: {
|
||||
log: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
info: jest.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
// 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
|
||||
jest.mock('../lib/drift/orders', () => ({
|
||||
closePosition: jest.fn(() => Promise.resolve({ success: true, realizedPnL: 0 })),
|
||||
cancelAllOrders: jest.fn(() => Promise.resolve({ success: true, cancelledCount: 0 })),
|
||||
placeExitOrders: jest.fn(() => Promise.resolve({ success: true })),
|
||||
}))
|
||||
|
||||
// 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 {}
|
||||
Reference in New Issue
Block a user