-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.test.ts
More file actions
75 lines (65 loc) · 2.23 KB
/
Copy pathclient.test.ts
File metadata and controls
75 lines (65 loc) · 2.23 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
import { afterEach, describe, expect, it, vi } from 'vitest'
import { PixelmuseClient } from './client.js'
import { ApiError } from './types.js'
describe('PixelmuseClient', () => {
afterEach(() => {
vi.restoreAllMocks()
})
it('deleteGeneration succeeds for 204 No Content responses', async () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(null, { status: 204 }),
)
const client = new PixelmuseClient('pm_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
await expect(client.deleteGeneration('gen_123')).resolves.toBeUndefined()
expect(fetchSpy).toHaveBeenCalledTimes(1)
})
it('getGeneration still parses successful JSON responses', async () => {
const generationPayload = {
id: 'gen_123',
status: 'succeeded',
model: 'nano-banana-2',
prompt: 'test prompt',
output: ['data:image/png;base64,AAAA'],
credits_charged: 1,
error: null,
created_at: '2026-03-28T00:00:00.000Z',
completed_at: '2026-03-28T00:00:01.000Z',
visibility: 'private',
}
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(JSON.stringify(generationPayload), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}),
)
const client = new PixelmuseClient('pm_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
const result = await client.getGeneration('gen_123')
expect(result.id).toBe('gen_123')
expect(result.status).toBe('succeeded')
})
it('surfaces structured API errors from JSON payloads', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
new Response(
JSON.stringify({
error: { message: 'Quota exceeded', code: 'QUOTA_EXCEEDED' },
}),
{
status: 429,
headers: {
'Content-Type': 'application/json',
'X-RateLimit-Remaining': '0',
'Retry-After': '12',
},
},
),
)
const client = new PixelmuseClient('pm_live_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
await expect(client.getAccount()).rejects.toMatchObject<ApiError>({
message: 'Quota exceeded',
status: 429,
code: 'QUOTA_EXCEEDED',
rateLimitRemaining: 0,
retryAfter: 12,
})
})
})