-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_test.go
More file actions
220 lines (195 loc) · 5.86 KB
/
interactive_test.go
File metadata and controls
220 lines (195 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"strings"
"testing"
exchanges "github.com/QuantProcessing/exchanges"
"github.com/shopspring/decimal"
)
func TestPrintLiveOrderBook(t *testing.T) {
ob := &exchanges.OrderBook{
Symbol: "BTC",
Asks: []exchanges.Level{
{Price: decimal.RequireFromString("95002"), Quantity: decimal.RequireFromString("0.5")},
{Price: decimal.RequireFromString("95001"), Quantity: decimal.RequireFromString("1.0")},
},
Bids: []exchanges.Level{
{Price: decimal.RequireFromString("95000"), Quantity: decimal.RequireFromString("2.0")},
{Price: decimal.RequireFromString("94999"), Quantity: decimal.RequireFromString("0.8")},
},
}
output := captureOutput(func() {
printLiveOrderBook(ob, 5)
})
// Verify key elements are present
if !strings.Contains(output, "BTC") {
t.Error("output should contain symbol")
}
if !strings.Contains(output, "ASKS") {
t.Error("output should contain ASKS header")
}
if !strings.Contains(output, "BIDS") {
t.Error("output should contain BIDS header")
}
if !strings.Contains(output, "95001") {
t.Error("output should contain ask price")
}
if !strings.Contains(output, "95000") {
t.Error("output should contain bid price")
}
// Should show spread
if !strings.Contains(output, "spread") {
t.Error("output should show spread")
}
// Should contain bars
if !strings.Contains(output, "█") {
t.Error("output should contain bar characters")
}
}
func TestPrintLiveOrderBookEmpty(t *testing.T) {
ob := &exchanges.OrderBook{Symbol: "BTC"}
// Should not panic with empty book
output := captureOutput(func() {
printLiveOrderBook(ob, 5)
})
if !strings.Contains(output, "BTC") {
t.Error("empty orderbook should still show symbol")
}
}
func TestPrintLiveOrderBookDepthLimited(t *testing.T) {
ob := &exchanges.OrderBook{
Symbol: "ETH",
Asks: []exchanges.Level{
{Price: decimal.RequireFromString("3001"), Quantity: decimal.RequireFromString("10")},
{Price: decimal.RequireFromString("3002"), Quantity: decimal.RequireFromString("20")},
{Price: decimal.RequireFromString("3003"), Quantity: decimal.RequireFromString("30")},
},
Bids: []exchanges.Level{
{Price: decimal.RequireFromString("3000"), Quantity: decimal.RequireFromString("15")},
{Price: decimal.RequireFromString("2999"), Quantity: decimal.RequireFromString("25")},
{Price: decimal.RequireFromString("2998"), Quantity: decimal.RequireFromString("5")},
},
}
output := captureOutput(func() {
printLiveOrderBook(ob, 2) // Only depth=2
})
// Should show first 2 levels only
if !strings.Contains(output, "3001") {
t.Error("should show first ask level")
}
if !strings.Contains(output, "3000") {
t.Error("should show first bid level")
}
// 3003 is the 3rd level, should NOT appear with depth=2
if strings.Contains(output, "3003") {
t.Error("should NOT show 3rd ask level with depth=2")
}
}
// ============================================================================
// Interactive state tests
// ============================================================================
func TestInteractiveStatePrompt(t *testing.T) {
state := &interactiveState{
exchName: "BINANCE",
market: "perp",
useWS: false,
}
prompt := state.prompt()
if !strings.Contains(prompt, "BINANCE") {
t.Error("prompt should contain exchange name")
}
if !strings.Contains(prompt, "perp") {
t.Error("prompt should contain market type")
}
if !strings.Contains(prompt, "rest") {
t.Error("prompt should show rest mode")
}
}
func TestInteractiveStatePromptWS(t *testing.T) {
state := &interactiveState{
exchName: "OKX",
market: "spot",
useWS: true,
}
prompt := state.prompt()
if !strings.Contains(prompt, "OKX") {
t.Error("prompt should contain exchange name")
}
if !strings.Contains(prompt, "spot") {
t.Error("prompt should contain market type")
}
if !strings.Contains(prompt, "ws") {
t.Error("prompt should show ws mode")
}
}
func TestResolveExchange(t *testing.T) {
// With explicit exchange
result := resolveExchange("BINANCE")
if result != "BINANCE" {
t.Errorf("resolveExchange('BINANCE') = %s, want BINANCE", result)
}
// Lowercase should be uppercased
result = resolveExchange("binance")
if result != "BINANCE" {
t.Errorf("resolveExchange('binance') = %s, want BINANCE", result)
}
}
func TestPrintInteractiveHelp(t *testing.T) {
output := captureOutput(func() {
printInteractiveHelp()
})
// Check major sections
if !strings.Contains(output, "Market Data") {
t.Error("help should contain Market Data section")
}
if !strings.Contains(output, "Trading") {
t.Error("help should contain Trading section")
}
if !strings.Contains(output, "Account") {
t.Error("help should contain Account section")
}
if !strings.Contains(output, "Streaming") {
t.Error("help should contain Streaming section")
}
if !strings.Contains(output, "Session") {
t.Error("help should contain Session section")
}
// Check some commands are listed
if !strings.Contains(output, "ticker") {
t.Error("help should list ticker command")
}
if !strings.Contains(output, "watch-ticker") {
t.Error("help should list watch-ticker command")
}
if !strings.Contains(output, "spot-balances") {
t.Error("help should list spot-balances command")
}
if !strings.Contains(output, "json") {
t.Error("help should list json toggle command")
}
}
func TestPrintStatus(t *testing.T) {
state := &interactiveState{
exchName: "GRVT",
market: "perp",
useWS: true,
jsonOut: false,
}
// printStatus reads cfg.Exchanges.ConfiguredExchanges() which would panic with nil cfg
// So we test the non-panicking parts only
output := captureOutput(func() {
// We can't call printStatus directly as it reads s.cfg
// but we can test the mode logic
mode := "REST"
if state.useWS {
mode = "WebSocket"
}
_ = mode
})
_ = output
// Test the mode detection logic
if state.useWS {
// Should resolve to "WebSocket"
} else {
t.Error("useWS should be true")
}
}