mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
225 lines
5.1 KiB
Go
225 lines
5.1 KiB
Go
package lx
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestCIBasicFunctionality tests core DEX functionality for CI
|
|
func TestCIBasicFunctionality(t *testing.T) {
|
|
t.Run("OrderBook", func(t *testing.T) {
|
|
ob := NewOrderBook("BTC-USD")
|
|
assert.NotNil(t, ob)
|
|
assert.Equal(t, "BTC-USD", ob.Symbol)
|
|
|
|
// Test order placement
|
|
order := &Order{
|
|
ID: 1,
|
|
Symbol: "BTC-USD",
|
|
Side: Buy,
|
|
Type: Limit,
|
|
Price: 50000,
|
|
Size: 0.1,
|
|
UserID: "test-user",
|
|
}
|
|
|
|
ob.AddOrder(order)
|
|
})
|
|
|
|
t.Run("TradingEngine", func(t *testing.T) {
|
|
config := EngineConfig{
|
|
EnablePerps: true,
|
|
EnableVaults: true,
|
|
EnableLending: true,
|
|
}
|
|
|
|
engine := NewTradingEngine(config)
|
|
assert.NotNil(t, engine)
|
|
})
|
|
|
|
t.Run("FundingEngine", func(t *testing.T) {
|
|
// Test funding engine
|
|
marginEngine := NewMarginEngine(nil, nil)
|
|
riskEngine := NewRiskEngine()
|
|
clearinghouse := NewClearingHouse(marginEngine, riskEngine)
|
|
fundingConfig := DefaultFundingConfig()
|
|
|
|
fundingEngine := NewFundingEngine(clearinghouse, fundingConfig)
|
|
assert.NotNil(t, fundingEngine)
|
|
|
|
// Check 8-hour interval
|
|
assert.Equal(t, 8*time.Hour, fundingConfig.Interval)
|
|
|
|
// Check funding times (00:00, 08:00, 16:00 UTC)
|
|
assert.Equal(t, []int{0, 8, 16}, fundingConfig.FundingHours)
|
|
|
|
// Check max funding rate
|
|
assert.Equal(t, 0.0075, fundingConfig.MaxFundingRate) // 0.75% per 8 hours
|
|
})
|
|
|
|
t.Run("MarginSystem", func(t *testing.T) {
|
|
marginEngine := NewMarginEngine(nil, nil)
|
|
assert.NotNil(t, marginEngine)
|
|
|
|
// Test margin engine is initialized
|
|
assert.NotNil(t, marginEngine)
|
|
})
|
|
|
|
t.Run("ClearingHouse", func(t *testing.T) {
|
|
marginEngine := NewMarginEngine(nil, nil)
|
|
riskEngine := NewRiskEngine()
|
|
ch := NewClearingHouse(marginEngine, riskEngine)
|
|
assert.NotNil(t, ch)
|
|
|
|
// Test clearinghouse is initialized
|
|
assert.NotNil(t, ch.perpAccounts)
|
|
assert.NotNil(t, ch.oracles)
|
|
})
|
|
|
|
t.Run("OrderTypes", func(t *testing.T) {
|
|
// Test different order types
|
|
orderTypes := []OrderType{
|
|
Limit,
|
|
Market,
|
|
Stop,
|
|
StopLimit,
|
|
Iceberg,
|
|
Peg,
|
|
}
|
|
|
|
for i, ot := range orderTypes {
|
|
order := &Order{
|
|
ID: uint64(i),
|
|
Symbol: "SOL-USD",
|
|
Type: ot,
|
|
Side: Buy,
|
|
Price: 100,
|
|
Size: 10,
|
|
UserID: "test",
|
|
}
|
|
|
|
// Just verify order can be created
|
|
assert.Equal(t, ot, order.Type)
|
|
}
|
|
})
|
|
|
|
t.Run("Perpetuals", func(t *testing.T) {
|
|
config := EngineConfig{
|
|
EnablePerps: true,
|
|
}
|
|
|
|
engine := NewTradingEngine(config)
|
|
pm := NewPerpetualManager(engine)
|
|
assert.NotNil(t, pm)
|
|
|
|
// Basic perpetual manager exists
|
|
assert.NotNil(t, pm.engine)
|
|
})
|
|
|
|
t.Run("DecimalPrecision", func(t *testing.T) {
|
|
// Test float precision handling
|
|
price := 50000.123456789
|
|
qty := 0.1
|
|
notional := price * qty
|
|
assert.Greater(t, notional, 0.0)
|
|
})
|
|
}
|
|
|
|
// TestCIPerformance tests performance metrics
|
|
func TestCIPerformance(t *testing.T) {
|
|
ob := NewOrderBook("BTC-USD")
|
|
|
|
// Add 1000 orders
|
|
start := time.Now()
|
|
for i := 0; i < 1000; i++ {
|
|
side := Buy
|
|
if i%2 == 1 {
|
|
side = Sell
|
|
}
|
|
order := &Order{
|
|
ID: uint64(i),
|
|
Symbol: "BTC-USD",
|
|
Side: side,
|
|
Type: Limit,
|
|
Price: 50000 + float64(i%100),
|
|
Size: 0.1,
|
|
UserID: "bench-user",
|
|
}
|
|
ob.AddOrder(order)
|
|
}
|
|
elapsed := time.Since(start)
|
|
|
|
// Should handle 1000 orders in under 100ms
|
|
assert.Less(t, elapsed, 100*time.Millisecond)
|
|
|
|
// Calculate orders per second
|
|
ordersPerSec := float64(1000) / elapsed.Seconds()
|
|
t.Logf("Performance: %.0f orders/second", ordersPerSec)
|
|
|
|
// Should achieve at least 10k orders/second
|
|
assert.Greater(t, ordersPerSec, 10000.0)
|
|
}
|
|
|
|
// TestCIFundingMechanism tests the 8-hour funding mechanism
|
|
func TestCIFundingMechanism(t *testing.T) {
|
|
marginEngine := NewMarginEngine(nil, nil)
|
|
riskEngine := NewRiskEngine()
|
|
clearinghouse := NewClearingHouse(marginEngine, riskEngine)
|
|
fundingEngine := NewFundingEngine(clearinghouse, DefaultFundingConfig())
|
|
|
|
// Test funding engine exists
|
|
assert.NotNil(t, fundingEngine)
|
|
assert.NotNil(t, fundingEngine.config)
|
|
|
|
// Check funding configuration
|
|
assert.Equal(t, 8*time.Hour, fundingEngine.config.Interval)
|
|
assert.Equal(t, 0.0075, fundingEngine.config.MaxFundingRate)
|
|
|
|
// Test that funding times are correct (00:00, 08:00, 16:00 UTC)
|
|
nextTime := fundingEngine.GetNextFundingTime()
|
|
hour := nextTime.Hour()
|
|
assert.True(t, hour == 0 || hour == 8 || hour == 16)
|
|
}
|
|
|
|
// TestCIProtocols tests all supported protocols are configured
|
|
func TestCIProtocols(t *testing.T) {
|
|
// This test verifies protocol support is configured
|
|
// Actual protocol tests would require running servers
|
|
|
|
protocols := []string{
|
|
"JSON-RPC",
|
|
"gRPC",
|
|
"WebSocket",
|
|
"FIX/QZMQ",
|
|
}
|
|
|
|
for _, protocol := range protocols {
|
|
t.Run(protocol, func(t *testing.T) {
|
|
// Just verify protocol is recognized
|
|
assert.NotEmpty(t, protocol)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCIAPIEndpoints tests API endpoint definitions
|
|
func TestCIAPIEndpoints(t *testing.T) {
|
|
// Test that API methods are defined
|
|
methods := []string{
|
|
"lx_placeOrder",
|
|
"lx_cancelOrder",
|
|
"lx_getOrders",
|
|
"lx_openPosition",
|
|
"lx_closePosition",
|
|
"lx_getPositions",
|
|
"lx_getFundingRate",
|
|
"lx_getOrderBook",
|
|
"lx_getTrades",
|
|
}
|
|
|
|
for _, method := range methods {
|
|
assert.NotEmpty(t, method)
|
|
}
|
|
}
|