cd3db8d453
Motivation: 部分 meme 币等标的价格极小(如 0.00000059),原有格式化统一保留两位小数会将其显示为 0.00,导致推送消息中的价格信息失真、误导用户;同时 viper 加载配置时会将嵌套 map 的 key 统一转为小写,导致按大写策略编码配置的策略覆盖项无法命中,仓位倍数、杠杆等覆盖参数失效。 Changes: * 新增价格展示格式化逻辑:绝对值不小于 1 的数值保留两位小数,小于 1 的数值采用最短精确表示,避免极小价格被截断为 0.00 * 策略覆盖查找改为大小写不敏感匹配,兼容 viper 将配置 key 小写化的行为,确保策略编码以任意大小写配置均可生效 * 补充极小价格展示、配置加载解析及策略覆盖匹配的单元测试
151 lines
4.1 KiB
Go
151 lines
4.1 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeSubscriptionDefaults(t *testing.T) {
|
|
cfg := &Config{Subscriptions: []SubscriptionConfig{{
|
|
URL: "amqps://example.invalid/vhost",
|
|
Queue: "trade.signal.notify.queue",
|
|
Source: "trade-signal",
|
|
}}}
|
|
if err := cfg.NormalizeSubscriptions(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := cfg.Subscriptions[0]
|
|
if s.Name != "trade.signal.notify.queue" {
|
|
t.Fatalf("name=%q", s.Name)
|
|
}
|
|
if s.MaxRetry != 3 {
|
|
t.Fatalf("max_retry=%d", s.MaxRetry)
|
|
}
|
|
if s.ExchangeType != "fanout" {
|
|
t.Fatalf("exchange_type=%q", s.ExchangeType)
|
|
}
|
|
if s.Formatter != "trade_signal" {
|
|
t.Fatalf("formatter=%q", s.Formatter)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeSkipsEmptyURL(t *testing.T) {
|
|
cfg := &Config{Subscriptions: []SubscriptionConfig{{
|
|
Queue: "q", Source: "s",
|
|
}}}
|
|
if err := cfg.NormalizeSubscriptions(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := len(cfg.ActiveSubscriptions()); n != 0 {
|
|
t.Fatalf("active=%d", n)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeUnknownFormatter(t *testing.T) {
|
|
cfg := &Config{Subscriptions: []SubscriptionConfig{{
|
|
URL: "amqps://example.invalid/vhost", Queue: "q", Source: "s", Formatter: "other",
|
|
}}}
|
|
if err := cfg.NormalizeSubscriptions(); err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestLoadExpandsRabbitMQURL(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
yaml := []byte(`
|
|
server:
|
|
port: 8080
|
|
admin_key: test
|
|
subscriptions:
|
|
- name: trade-signal
|
|
url: "${RABBITMQ_URL}"
|
|
queue: q
|
|
source: trade-signal
|
|
`)
|
|
if err := os.WriteFile(path, yaml, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("RABBITMQ_URL", "amqps://user:pass@example.invalid:5671/vhost")
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cfg.Subscriptions) != 1 {
|
|
t.Fatalf("subs=%d", len(cfg.Subscriptions))
|
|
}
|
|
if cfg.Subscriptions[0].URL != "amqps://user:pass@example.invalid:5671/vhost" {
|
|
t.Fatalf("url=%q", cfg.Subscriptions[0].URL)
|
|
}
|
|
if n := len(cfg.ActiveSubscriptions()); n != 1 {
|
|
t.Fatalf("active=%d", n)
|
|
}
|
|
}
|
|
|
|
func TestActiveSubscriptionsSkipsPlaceholderURL(t *testing.T) {
|
|
cfg := &Config{Subscriptions: []SubscriptionConfig{{
|
|
URL: "${RABBITMQ_URL}", Queue: "q", Source: "s", Name: "trade-signal",
|
|
}}}
|
|
if n := len(cfg.ActiveSubscriptions()); n != 0 {
|
|
t.Fatalf("placeholder should not be active, n=%d", n)
|
|
}
|
|
}
|
|
|
|
func TestLoadParsesStrategyOverrides(t *testing.T) {
|
|
cfg, err := Load(filepath.Join("..", "..", "config", "config.yaml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var sub *SubscriptionConfig
|
|
for i := range cfg.Subscriptions {
|
|
if cfg.Subscriptions[i].Name == "trade-signal" {
|
|
sub = &cfg.Subscriptions[i]
|
|
break
|
|
}
|
|
}
|
|
if sub == nil {
|
|
t.Fatal("trade-signal subscription not found")
|
|
}
|
|
if len(sub.StrategyOverrides) == 0 {
|
|
t.Fatalf("strategy_overrides not parsed: %+v", *sub)
|
|
}
|
|
// Viper lower-cases nested map keys during load, so the parsed key is "blong".
|
|
o, ok := sub.StrategyOverrides["blong"]
|
|
if !ok {
|
|
t.Fatalf("blong override missing, got keys=%v", sub.StrategyOverrides)
|
|
}
|
|
got := o.QuantityMultiplierFor("OPEN")
|
|
if got != 100 {
|
|
t.Fatalf("open multiplier=%v want 100", got)
|
|
}
|
|
if o.Leverage == nil || *o.Leverage != 100 {
|
|
t.Fatalf("leverage=%v want 100", o.Leverage)
|
|
}
|
|
}
|
|
|
|
func TestLoadStrategyOverridesKeyInsensitive(t *testing.T) {
|
|
// Confirms viper lower-cases keys; tradesignal.overrideFor matches case-insensitively.
|
|
sub := SubscriptionConfig{StrategyOverrides: map[string]StrategyOverride{
|
|
"blong": {Leverage: intPtr(100)},
|
|
}}
|
|
if _, ok := sub.StrategyOverrides["BLONG"]; ok {
|
|
t.Fatalf("expected case-sensitive map; overrides=%v", sub.StrategyOverrides)
|
|
}
|
|
}
|
|
|
|
func intPtr(v int) *int { return &v }
|
|
|
|
func TestQuantityMultiplierFor(t *testing.T) {
|
|
o := StrategyOverride{QuantityMultipliers: QuantityMultipliers{Open: 100, Add: 0}}
|
|
if o.QuantityMultiplierFor("OPEN") != 100 {
|
|
t.Fatalf("open=%v", o.QuantityMultiplierFor("OPEN"))
|
|
}
|
|
if o.QuantityMultiplierFor("ADD") != 1 {
|
|
t.Fatalf("add<=0 should be 1, got %v", o.QuantityMultiplierFor("ADD"))
|
|
}
|
|
if o.QuantityMultiplierFor("UNKNOWN") != 1 {
|
|
t.Fatalf("unknown=%v", o.QuantityMultiplierFor("UNKNOWN"))
|
|
}
|
|
}
|