mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-09-20 04:23:43 +00:00
Opt-in module for gold paper trading with VND currency. Commands: /gold_topup, /gold_buy, /gold_sell, /gold_stats. Pricing: spot XAU USD converted to VND per luong via GoldPrice.org + ExchangeRate-API. Features: FX caching, dust normalization (1e-9), HTTPS-only URL validation, per-user key locking, namespace isolation from trading module.
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package gold
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/testutil"
|
|
)
|
|
|
|
func TestHandlersRejectExtraArgs(t *testing.T) {
|
|
ctx := context.Background()
|
|
s := newTestState(2_000_000, nil)
|
|
rb := testutil.NewRecordingBot(t)
|
|
cases := []struct {
|
|
name string
|
|
text string
|
|
want string
|
|
}{
|
|
{name: "topup currency", text: "/gold_topup 100 USD", want: "Usage: /gold_topup <amount>"},
|
|
{name: "buy unit", text: "/gold_buy 1 oz", want: "Usage: /gold_buy <luong>"},
|
|
{name: "sell symbol", text: "/gold_sell 1 SJC", want: "Usage: /gold_sell <luong>"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
rb.Reset()
|
|
update := testutil.NewPrivateMessage(7, tc.text)
|
|
var err error
|
|
switch {
|
|
case strings.HasPrefix(tc.text, "/gold_topup"):
|
|
err = s.handleTopup(ctx, rb.Bot, update)
|
|
case strings.HasPrefix(tc.text, "/gold_buy"):
|
|
err = s.handleBuy(ctx, rb.Bot, update)
|
|
case strings.HasPrefix(tc.text, "/gold_sell"):
|
|
err = s.handleSell(ctx, rb.Bot, update)
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("handler: %v", err)
|
|
}
|
|
rb.AssertSentText(t, tc.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandlersRejectTooLargeValues(t *testing.T) {
|
|
ctx := context.Background()
|
|
s := newTestState(1e308, nil)
|
|
rb := testutil.NewRecordingBot(t)
|
|
if err := s.handleTopup(ctx, rb.Bot, testutil.NewPrivateMessage(7, "/gold_topup 1e308")); err != nil {
|
|
t.Fatalf("topup: %v", err)
|
|
}
|
|
rb.AssertSentText(t, "supported range")
|
|
rb.Reset()
|
|
if err := s.handleBuy(ctx, rb.Bot, testutil.NewPrivateMessage(7, "/gold_buy 2")); err != nil {
|
|
t.Fatalf("buy: %v", err)
|
|
}
|
|
rb.AssertSentText(t, "Trade value is too large")
|
|
}
|