Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/commands/SpotCommand.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> {
// @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."
);
});
});
11 changes: 11 additions & 0 deletions src/commands/SpotCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ??
Expand Down