|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | +import { OpenSeaAPIError } from "../../src/client.js" |
| 3 | +import { healthCommand } from "../../src/commands/health.js" |
| 4 | +import { type CommandTestContext, createCommandTestContext } from "../mocks.js" |
| 5 | + |
| 6 | +describe("healthCommand", () => { |
| 7 | + let ctx: CommandTestContext |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + ctx = createCommandTestContext() |
| 11 | + }) |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + vi.restoreAllMocks() |
| 15 | + }) |
| 16 | + |
| 17 | + it("creates command with correct name", () => { |
| 18 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 19 | + expect(cmd.name()).toBe("health") |
| 20 | + }) |
| 21 | + |
| 22 | + it("outputs ok status when both connectivity and auth succeed", async () => { |
| 23 | + ctx.mockClient.get.mockResolvedValue({}) |
| 24 | + |
| 25 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 26 | + await cmd.parseAsync([], { from: "user" }) |
| 27 | + |
| 28 | + expect(ctx.mockClient.get).toHaveBeenCalledWith("/api/v2/collections", { |
| 29 | + limit: 1, |
| 30 | + }) |
| 31 | + expect(ctx.mockClient.get).toHaveBeenCalledWith( |
| 32 | + "/api/v2/listings/collection/boredapeyachtclub/all", |
| 33 | + { |
| 34 | + limit: 1, |
| 35 | + }, |
| 36 | + ) |
| 37 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 38 | + expect(output.status).toBe("ok") |
| 39 | + expect(output.key_prefix).toBe("test...") |
| 40 | + expect(output.authenticated).toBe(true) |
| 41 | + expect(output.message).toBe("Connectivity and authentication are working") |
| 42 | + }) |
| 43 | + |
| 44 | + it("outputs error status when connectivity fails", async () => { |
| 45 | + ctx.mockClient.get.mockRejectedValue( |
| 46 | + new OpenSeaAPIError(500, "Internal Server Error", "/api/v2/collections"), |
| 47 | + ) |
| 48 | + |
| 49 | + const mockExit = vi |
| 50 | + .spyOn(process, "exit") |
| 51 | + .mockImplementation(() => undefined as never) |
| 52 | + |
| 53 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 54 | + await cmd.parseAsync([], { from: "user" }) |
| 55 | + |
| 56 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 57 | + expect(output.status).toBe("error") |
| 58 | + expect(output.authenticated).toBe(false) |
| 59 | + expect(output.message).toContain("API error (500)") |
| 60 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 61 | + }) |
| 62 | + |
| 63 | + it("outputs error status when auth fails (401)", async () => { |
| 64 | + ctx.mockClient.get |
| 65 | + .mockResolvedValueOnce({}) // connectivity ok |
| 66 | + .mockRejectedValueOnce( |
| 67 | + new OpenSeaAPIError( |
| 68 | + 401, |
| 69 | + "Unauthorized", |
| 70 | + "/api/v2/listings/collection/boredapeyachtclub/all", |
| 71 | + ), |
| 72 | + ) |
| 73 | + |
| 74 | + const mockExit = vi |
| 75 | + .spyOn(process, "exit") |
| 76 | + .mockImplementation(() => undefined as never) |
| 77 | + |
| 78 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 79 | + await cmd.parseAsync([], { from: "user" }) |
| 80 | + |
| 81 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 82 | + expect(output.status).toBe("error") |
| 83 | + expect(output.authenticated).toBe(false) |
| 84 | + expect(output.message).toContain("Authentication failed (401)") |
| 85 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 86 | + }) |
| 87 | + |
| 88 | + it("outputs error status on network errors", async () => { |
| 89 | + ctx.mockClient.get.mockRejectedValue(new TypeError("fetch failed")) |
| 90 | + |
| 91 | + const mockExit = vi |
| 92 | + .spyOn(process, "exit") |
| 93 | + .mockImplementation(() => undefined as never) |
| 94 | + |
| 95 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 96 | + await cmd.parseAsync([], { from: "user" }) |
| 97 | + |
| 98 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 99 | + expect(output.status).toBe("error") |
| 100 | + expect(output.message).toContain("Network error: fetch failed") |
| 101 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 102 | + }) |
| 103 | + |
| 104 | + it("reports ok with unverified auth when listings endpoint has non-auth error", async () => { |
| 105 | + ctx.mockClient.get |
| 106 | + .mockResolvedValueOnce({}) // connectivity ok |
| 107 | + .mockRejectedValueOnce( |
| 108 | + new OpenSeaAPIError( |
| 109 | + 500, |
| 110 | + "Server Error", |
| 111 | + "/api/v2/listings/collection/boredapeyachtclub/all", |
| 112 | + ), |
| 113 | + ) |
| 114 | + |
| 115 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 116 | + await cmd.parseAsync([], { from: "user" }) |
| 117 | + |
| 118 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 119 | + expect(output.status).toBe("ok") |
| 120 | + expect(output.authenticated).toBe(false) |
| 121 | + expect(output.message).toContain("could not be verified") |
| 122 | + }) |
| 123 | + |
| 124 | + it("exits with code 3 on rate limit (429)", async () => { |
| 125 | + ctx.mockClient.get.mockRejectedValue( |
| 126 | + new OpenSeaAPIError(429, "Too Many Requests", "/api/v2/collections"), |
| 127 | + ) |
| 128 | + |
| 129 | + const mockExit = vi |
| 130 | + .spyOn(process, "exit") |
| 131 | + .mockImplementation(() => undefined as never) |
| 132 | + |
| 133 | + const cmd = healthCommand(ctx.getClient, ctx.getFormat) |
| 134 | + await cmd.parseAsync([], { from: "user" }) |
| 135 | + |
| 136 | + const output = JSON.parse(ctx.consoleSpy.mock.calls[0][0] as string) |
| 137 | + expect(output.status).toBe("error") |
| 138 | + expect(output.rate_limited).toBe(true) |
| 139 | + expect(output.message).toContain("Rate limited") |
| 140 | + expect(mockExit).toHaveBeenCalledWith(3) |
| 141 | + }) |
| 142 | +}) |
0 commit comments