-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.test.ts
More file actions
247 lines (203 loc) · 7.13 KB
/
client.test.ts
File metadata and controls
247 lines (203 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { OpenSeaAPIError, OpenSeaClient } from "../src/client.js"
import { mockFetchResponse, mockFetchTextResponse } from "./mocks.js"
describe("OpenSeaClient", () => {
let client: OpenSeaClient
beforeEach(() => {
client = new OpenSeaClient({ apiKey: "test-key" })
})
afterEach(() => {
vi.restoreAllMocks()
})
describe("constructor", () => {
it("uses default base URL and chain", () => {
expect(client.getDefaultChain()).toBe("ethereum")
})
it("accepts custom base URL and chain", () => {
const custom = new OpenSeaClient({
apiKey: "key",
baseUrl: "https://custom.api",
chain: "base",
})
expect(custom.getDefaultChain()).toBe("base")
})
})
describe("get", () => {
it("makes GET request with correct headers", async () => {
const mockResponse = { name: "test" }
mockFetchResponse(mockResponse)
const result = await client.get("/api/v2/test")
expect(fetch).toHaveBeenCalledWith(
"https://api.opensea.io/api/v2/test",
expect.objectContaining({
method: "GET",
headers: {
Accept: "application/json",
"x-api-key": "test-key",
},
}),
)
expect(result).toEqual(mockResponse)
})
it("appends query params, skipping null and undefined", async () => {
mockFetchResponse({})
await client.get("/api/v2/test", {
chain: "ethereum",
limit: 10,
next: null,
missing: undefined,
})
const calledUrl = vi.mocked(fetch).mock.calls[0][0] as string
const url = new URL(calledUrl)
expect(url.searchParams.get("chain")).toBe("ethereum")
expect(url.searchParams.get("limit")).toBe("10")
expect(url.searchParams.has("next")).toBe(false)
expect(url.searchParams.has("missing")).toBe(false)
})
it("throws OpenSeaAPIError on non-ok response", async () => {
mockFetchTextResponse("Not Found", 404)
try {
await client.get("/api/v2/bad")
expect.fail("Should have thrown")
} catch (err) {
expect(err).toBeInstanceOf(OpenSeaAPIError)
const apiErr = err as OpenSeaAPIError
expect(apiErr.statusCode).toBe(404)
expect(apiErr.responseBody).toBe("Not Found")
expect(apiErr.path).toBe("/api/v2/bad")
}
})
})
describe("post", () => {
it("makes POST request with correct headers", async () => {
const mockResponse = { status: "ok" }
mockFetchResponse(mockResponse)
const result = await client.post("/api/v2/refresh")
expect(fetch).toHaveBeenCalledWith(
"https://api.opensea.io/api/v2/refresh",
expect.objectContaining({
method: "POST",
headers: {
Accept: "application/json",
"x-api-key": "test-key",
},
}),
)
expect(result).toEqual(mockResponse)
})
it("sends JSON body when provided", async () => {
mockFetchResponse({ id: 1 })
await client.post("/api/v2/create", { name: "test" })
expect(fetch).toHaveBeenCalledWith(
"https://api.opensea.io/api/v2/create",
expect.objectContaining({
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"x-api-key": "test-key",
},
body: JSON.stringify({ name: "test" }),
}),
)
})
it("appends query params when provided", async () => {
mockFetchResponse({})
await client.post("/api/v2/action", undefined, {
chain: "ethereum",
})
const calledUrl = vi.mocked(fetch).mock.calls[0][0] as string
const url = new URL(calledUrl)
expect(url.searchParams.get("chain")).toBe("ethereum")
})
it("throws OpenSeaAPIError on non-ok response", async () => {
mockFetchTextResponse("Server Error", 500)
await expect(client.post("/api/v2/fail")).rejects.toThrow(OpenSeaAPIError)
})
})
describe("timeout", () => {
it("passes AbortSignal.timeout to fetch calls", async () => {
const timedClient = new OpenSeaClient({
apiKey: "test-key",
timeout: 5000,
})
mockFetchResponse({ ok: true })
await timedClient.get("/api/v2/test")
const fetchOptions = vi.mocked(fetch).mock.calls[0][1] as RequestInit
expect(fetchOptions.signal).toBeInstanceOf(AbortSignal)
})
it("uses default 30s timeout", async () => {
mockFetchResponse({ ok: true })
await client.get("/api/v2/test")
const fetchOptions = vi.mocked(fetch).mock.calls[0][1] as RequestInit
expect(fetchOptions.signal).toBeInstanceOf(AbortSignal)
})
})
describe("verbose", () => {
it("logs request and response to stderr when enabled", async () => {
const verboseClient = new OpenSeaClient({
apiKey: "test-key",
verbose: true,
})
const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {})
mockFetchResponse({ name: "test" })
await verboseClient.get("/api/v2/collections/test")
expect(stderrSpy).toHaveBeenCalledWith(
expect.stringContaining(
"[verbose] GET https://api.opensea.io/api/v2/collections/test",
),
)
expect(stderrSpy).toHaveBeenCalledWith(
expect.stringContaining("[verbose] 200"),
)
})
it("does not log when verbose is disabled", async () => {
const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {})
mockFetchResponse({ name: "test" })
await client.get("/api/v2/test")
expect(stderrSpy).not.toHaveBeenCalled()
})
it("logs for post requests", async () => {
const verboseClient = new OpenSeaClient({
apiKey: "test-key",
verbose: true,
})
const stderrSpy = vi.spyOn(console, "error").mockImplementation(() => {})
mockFetchResponse({ status: "ok" })
await verboseClient.post("/api/v2/refresh")
expect(stderrSpy).toHaveBeenCalledWith(
expect.stringContaining("[verbose] POST"),
)
})
})
describe("getDefaultChain", () => {
it("returns the default chain", () => {
expect(client.getDefaultChain()).toBe("ethereum")
})
})
describe("getApiKeyPrefix", () => {
it("returns first 4 characters followed by ellipsis", () => {
expect(client.getApiKeyPrefix()).toBe("test...")
})
it("masks short API keys", () => {
const shortKeyClient = new OpenSeaClient({ apiKey: "ab" })
expect(shortKeyClient.getApiKeyPrefix()).toBe("***")
})
})
})
describe("OpenSeaAPIError", () => {
it("has correct properties", () => {
const error = new OpenSeaAPIError(404, "Not Found", "/api/v2/test")
expect(error.statusCode).toBe(404)
expect(error.responseBody).toBe("Not Found")
expect(error.path).toBe("/api/v2/test")
expect(error.name).toBe("OpenSeaAPIError")
expect(error.message).toBe(
"OpenSea API error 404 on /api/v2/test: Not Found",
)
})
it("is an instance of Error", () => {
const error = new OpenSeaAPIError(500, "fail", "/test")
expect(error).toBeInstanceOf(Error)
})
})