diff --git a/src/commands/SpotCommand.test.ts b/src/commands/SpotCommand.test.ts new file mode 100644 index 0000000..d4012ce --- /dev/null +++ b/src/commands/SpotCommand.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, test } from "bun:test"; + +import { SpotCommand } from "./SpotCommand.ts"; + +// Access the private static history() handler through the class without +// invoking commander. Only the limit validation path is exercised, so no +// network or keypair is needed. +function historyWithLimit(limit: string): Promise { + // @ts-expect-error test access to private static method + return SpotCommand.history({ limit }); +} + +describe("SpotCommand.history --limit validation", () => { + test("rejects limit above the documented maximum of 15", async () => { + expect(historyWithLimit("16")).rejects.toThrow( + "--limit must be between 1 and 15." + ); + expect(historyWithLimit("1000")).rejects.toThrow( + "--limit must be between 1 and 15." + ); + }); + + test("rejects non-positive and non-numeric limits", async () => { + expect(historyWithLimit("0")).rejects.toThrow( + "--limit must be a positive number." + ); + expect(historyWithLimit("-3")).rejects.toThrow( + "--limit must be a positive number." + ); + expect(historyWithLimit("abc")).rejects.toThrow( + "--limit must be a positive number." + ); + }); +}); diff --git a/src/commands/SpotCommand.ts b/src/commands/SpotCommand.ts index ab28856..1b268e2 100644 --- a/src/commands/SpotCommand.ts +++ b/src/commands/SpotCommand.ts @@ -20,6 +20,12 @@ import { Output } from "../lib/Output.ts"; import { Signer } from "../lib/Signer.ts"; import { Swap } from "../lib/Swap.ts"; +// Spot history pagination is bounded by the datapi API (which silently caps to 30), +// but the public docs promise a stricter user-facing limit of 15. Validate that +// before the network call so users get the same error message regardless of the +// datapi snapshot we hit. See docs/spot.md. +const SPOT_HISTORY_MAX_LIMIT = 15; + export class SpotCommand { public static register(program: Command): void { const spot = program @@ -613,6 +619,11 @@ export class SpotCommand { if (isNaN(limit) || limit <= 0) { throw new Error("--limit must be a positive number."); } + if (limit > SPOT_HISTORY_MAX_LIMIT) { + throw new Error( + `--limit must be between 1 and ${SPOT_HISTORY_MAX_LIMIT}.` + ); + } const address = opts.address ??