|
| 1 | +import { Command } from "commander" |
| 2 | +import { afterAll, expect, it, vi } from "vitest" |
| 3 | +import { OpenSeaAPIError } from "../src/client.js" |
| 4 | + |
| 5 | +vi.mock("../src/commands/index.js", () => ({ |
| 6 | + accountsCommand: () => new Command("accounts"), |
| 7 | + collectionsCommand: () => new Command("collections"), |
| 8 | + eventsCommand: () => new Command("events"), |
| 9 | + listingsCommand: () => new Command("listings"), |
| 10 | + nftsCommand: () => new Command("nfts"), |
| 11 | + offersCommand: () => new Command("offers"), |
| 12 | + searchCommand: () => new Command("search"), |
| 13 | + swapsCommand: () => new Command("swaps"), |
| 14 | + tokensCommand: () => new Command("tokens"), |
| 15 | +})) |
| 16 | + |
| 17 | +const exitSpy = vi |
| 18 | + .spyOn(process, "exit") |
| 19 | + .mockImplementation(() => undefined as never) |
| 20 | +const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 21 | + |
| 22 | +vi.spyOn(Command.prototype, "parseAsync").mockRejectedValue( |
| 23 | + new OpenSeaAPIError(429, "Rate limit exceeded", "/api/v2/test"), |
| 24 | +) |
| 25 | + |
| 26 | +afterAll(() => { |
| 27 | + vi.restoreAllMocks() |
| 28 | +}) |
| 29 | + |
| 30 | +it("exits with code 3 and 'Rate Limited' label on 429 error", async () => { |
| 31 | + await import("../src/cli.js") |
| 32 | + await vi.waitFor(() => { |
| 33 | + expect(exitSpy).toHaveBeenCalled() |
| 34 | + }) |
| 35 | + |
| 36 | + expect(exitSpy).toHaveBeenCalledWith(3) |
| 37 | + const output = stderrSpy.mock.calls[0][0] as string |
| 38 | + const parsed = JSON.parse(output) |
| 39 | + expect(parsed.error).toBe("Rate Limited") |
| 40 | + expect(parsed.status).toBe(429) |
| 41 | + expect(parsed.path).toBe("/api/v2/test") |
| 42 | + expect(parsed.message).toBe("Rate limit exceeded") |
| 43 | +}) |
0 commit comments