mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-09 08:18:07 +00:00
- Introduced `normalizeEnvValue` function to improve handling of environment variable values, including trimming, unquoting, and stripping inline comments. - Updated `resolveSymbolFromEnv` and parsing functions to utilize the new normalization logic. - Added `validateAccountSnapshotForSymbol` function to validate account snapshots, ensuring numeric fields are correctly formatted and flagging any issues. - Implemented tests for environment variable parsing and account snapshot validation to ensure robustness and correctness.
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { StandxGateway } from "../src/exchanges/standx/gateway";
|
|
|
|
const ORIGINAL_FETCH = globalThis.fetch;
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = ORIGINAL_FETCH;
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("StandxGateway REST health", () => {
|
|
it("emits unhealthy after 3 consecutive REST failures, then healthy after a success", async () => {
|
|
const gateway = new StandxGateway({
|
|
token: "test-token",
|
|
symbol: "BTC-USD",
|
|
baseUrl: "https://example.com",
|
|
wsUrl: "wss://example.com/ws",
|
|
logger: () => {},
|
|
});
|
|
|
|
const events: Array<{ state: string; consecutiveErrors: number }> = [];
|
|
gateway.onRestHealthEvent((state, info) => {
|
|
events.push({ state, consecutiveErrors: info.consecutiveErrors });
|
|
});
|
|
|
|
globalThis.fetch = vi.fn(async () => {
|
|
return {
|
|
ok: false,
|
|
status: 500,
|
|
text: async () => "server error",
|
|
} as any;
|
|
}) as any;
|
|
|
|
await expect(gateway.queryOpenOrders("BTC-USD")).rejects.toThrow();
|
|
await expect(gateway.queryOpenOrders("BTC-USD")).rejects.toThrow();
|
|
await expect(gateway.queryOpenOrders("BTC-USD")).rejects.toThrow();
|
|
|
|
expect(events).toEqual([{ state: "unhealthy", consecutiveErrors: 3 }]);
|
|
|
|
globalThis.fetch = vi.fn(async () => {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
text: async () => "[]",
|
|
} as any;
|
|
}) as any;
|
|
|
|
await expect(gateway.queryOpenOrders("BTC-USD")).resolves.toEqual([]);
|
|
expect(events).toEqual([
|
|
{ state: "unhealthy", consecutiveErrors: 3 },
|
|
{ state: "healthy", consecutiveErrors: 0 },
|
|
]);
|
|
});
|
|
});
|
|
|