|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
2 | | -import { OpenSeaClient } from "../src/client.js" |
| 2 | +import { OpenSeaAPIError, OpenSeaClient } from "../src/client.js" |
3 | 3 | import { OpenSeaCLI } from "../src/sdk.js" |
4 | 4 |
|
5 | | -vi.mock("../src/client.js", () => { |
| 5 | +vi.mock("../src/client.js", async importOriginal => { |
| 6 | + const actual = await importOriginal<typeof import("../src/client.js")>() |
6 | 7 | const MockOpenSeaClient = vi.fn() |
7 | 8 | MockOpenSeaClient.prototype.get = vi.fn() |
8 | 9 | MockOpenSeaClient.prototype.post = vi.fn() |
9 | 10 | MockOpenSeaClient.prototype.getDefaultChain = vi.fn(() => "ethereum") |
10 | 11 | MockOpenSeaClient.prototype.getApiKeyPrefix = vi.fn(() => "test...") |
11 | | - return { OpenSeaClient: MockOpenSeaClient, OpenSeaAPIError: vi.fn() } |
| 12 | + return { |
| 13 | + OpenSeaClient: MockOpenSeaClient, |
| 14 | + OpenSeaAPIError: actual.OpenSeaAPIError, |
| 15 | + } |
12 | 16 | }) |
13 | 17 |
|
14 | 18 | describe("OpenSeaCLI", () => { |
@@ -419,12 +423,34 @@ describe("OpenSeaCLI", () => { |
419 | 423 | }) |
420 | 424 | expect(result.status).toBe("ok") |
421 | 425 | expect(result.key_prefix).toBe("test...") |
422 | | - expect(result.message).toBe( |
423 | | - "API key is valid and connectivity is working", |
| 426 | + expect(result.message).toBe("Connectivity is working") |
| 427 | + }) |
| 428 | + |
| 429 | + it("check returns error on authentication failure", async () => { |
| 430 | + mockGet.mockRejectedValue( |
| 431 | + new OpenSeaAPIError(401, "Unauthorized", "/api/v2/collections"), |
424 | 432 | ) |
| 433 | + const result = await sdk.health.check() |
| 434 | + expect(result.status).toBe("error") |
| 435 | + expect(result.key_prefix).toBe("test...") |
| 436 | + expect(result.message).toContain("Authentication failed (401)") |
| 437 | + }) |
| 438 | + |
| 439 | + it("check returns error on API error", async () => { |
| 440 | + mockGet.mockRejectedValue( |
| 441 | + new OpenSeaAPIError( |
| 442 | + 500, |
| 443 | + "Internal Server Error", |
| 444 | + "/api/v2/collections", |
| 445 | + ), |
| 446 | + ) |
| 447 | + const result = await sdk.health.check() |
| 448 | + expect(result.status).toBe("error") |
| 449 | + expect(result.key_prefix).toBe("test...") |
| 450 | + expect(result.message).toContain("API error (500)") |
425 | 451 | }) |
426 | 452 |
|
427 | | - it("check returns error when API call fails", async () => { |
| 453 | + it("check returns error when network fails", async () => { |
428 | 454 | mockGet.mockRejectedValue(new Error("fetch failed")) |
429 | 455 | const result = await sdk.health.check() |
430 | 456 | expect(result.status).toBe("error") |
|
0 commit comments