mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-04-17 17:21:30 +00:00
feat: add fake trading module with crypto, stocks, forex and gold
Paper trading system with 5 commands (trade_topup, trade_buy, trade_sell, trade_convert, trade_stats). Supports VN stocks via TCBS, crypto via CoinGecko, forex via ER-API, and gold via PAX Gold proxy. Per-user portfolio stored in KV with 60s price caching. 54 new tests.
This commit is contained in:
79
tests/modules/trading/format.test.js
Normal file
79
tests/modules/trading/format.test.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
formatCrypto,
|
||||
formatCurrency,
|
||||
formatPnL,
|
||||
formatStock,
|
||||
formatUSD,
|
||||
formatVND,
|
||||
} from "../../../src/modules/trading/format.js";
|
||||
|
||||
describe("trading/format", () => {
|
||||
describe("formatVND", () => {
|
||||
it("formats with dot thousands separator", () => {
|
||||
expect(formatVND(15000000)).toBe("15.000.000 VND");
|
||||
});
|
||||
it("handles zero", () => {
|
||||
expect(formatVND(0)).toBe("0 VND");
|
||||
});
|
||||
it("handles negative", () => {
|
||||
expect(formatVND(-1500000)).toBe("-1.500.000 VND");
|
||||
});
|
||||
it("rounds to integer", () => {
|
||||
expect(formatVND(1234.56)).toBe("1.235 VND");
|
||||
});
|
||||
it("handles small numbers", () => {
|
||||
expect(formatVND(500)).toBe("500 VND");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatUSD", () => {
|
||||
it("formats with comma separator and 2 decimals", () => {
|
||||
expect(formatUSD(1234.5)).toBe("$1,234.50");
|
||||
});
|
||||
it("handles zero", () => {
|
||||
expect(formatUSD(0)).toBe("$0.00");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatCrypto", () => {
|
||||
it("strips trailing zeros", () => {
|
||||
expect(formatCrypto(0.001)).toBe("0.001");
|
||||
expect(formatCrypto(1.0)).toBe("1");
|
||||
});
|
||||
it("preserves up to 8 decimals", () => {
|
||||
expect(formatCrypto(0.12345678)).toBe("0.12345678");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatStock", () => {
|
||||
it("floors to integer", () => {
|
||||
expect(formatStock(1.7)).toBe("1");
|
||||
expect(formatStock(150)).toBe("150");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatCurrency", () => {
|
||||
it("dispatches to VND formatter", () => {
|
||||
expect(formatCurrency(5000, "VND")).toBe("5.000 VND");
|
||||
});
|
||||
it("dispatches to USD formatter", () => {
|
||||
expect(formatCurrency(100.5, "USD")).toBe("$100.50");
|
||||
});
|
||||
it("falls back for unknown currency", () => {
|
||||
expect(formatCurrency(42, "EUR")).toBe("42 EUR");
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatPnL", () => {
|
||||
it("shows positive P&L", () => {
|
||||
expect(formatPnL(12000000, 10000000)).toBe("+2.000.000 VND (+20.00%)");
|
||||
});
|
||||
it("shows negative P&L", () => {
|
||||
expect(formatPnL(8000000, 10000000)).toBe("-2.000.000 VND (-20.00%)");
|
||||
});
|
||||
it("handles zero investment", () => {
|
||||
expect(formatPnL(0, 0)).toBe("+0 VND (+0.00%)");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user