|
1 | 1 | import { StorageClient } from '../src/index' |
2 | | -import { StorageUnknownError } from '../src/lib/common/errors' |
| 2 | +import { StorageApiError, StorageUnknownError } from '../src/lib/common/errors' |
3 | 3 |
|
4 | 4 | // Create a simple Response implementation for testing |
5 | 5 | class MockResponse { |
@@ -539,4 +539,72 @@ describe('Bucket API Error Handling', () => { |
539 | 539 | mockFn.mockRestore() |
540 | 540 | }) |
541 | 541 | }) |
| 542 | + |
| 543 | + describe('purgeBucketCache', () => { |
| 544 | + const PURGE_URL = 'http://localhost:8000/storage/v1' |
| 545 | + const BUCKET = 'avatars' |
| 546 | + |
| 547 | + afterEach(() => { |
| 548 | + jest.restoreAllMocks() |
| 549 | + }) |
| 550 | + |
| 551 | + it('issues DELETE to /cdn/{bucket} and returns the server message', async () => { |
| 552 | + const fetchMock = jest.fn().mockResolvedValue( |
| 553 | + new Response(JSON.stringify({ message: 'success' }), { |
| 554 | + status: 200, |
| 555 | + headers: { 'Content-Type': 'application/json' }, |
| 556 | + }) |
| 557 | + ) |
| 558 | + global.fetch = fetchMock |
| 559 | + |
| 560 | + const client = new StorageClient(PURGE_URL, { apikey: 'service-role-token' }) |
| 561 | + const { data, error } = await client.purgeBucketCache(BUCKET) |
| 562 | + |
| 563 | + expect(error).toBeNull() |
| 564 | + expect(data?.message).toBe('success') |
| 565 | + expect(fetchMock).toHaveBeenCalledWith( |
| 566 | + `${PURGE_URL}/cdn/${BUCKET}`, |
| 567 | + expect.objectContaining({ method: 'DELETE' }) |
| 568 | + ) |
| 569 | + }) |
| 570 | + |
| 571 | + it('surfaces server errors via StorageApiError', async () => { |
| 572 | + const fetchMock = jest |
| 573 | + .fn() |
| 574 | + .mockResolvedValue( |
| 575 | + new Response( |
| 576 | + JSON.stringify({ statusCode: '404', error: 'Not Found', message: 'Object not found' }), |
| 577 | + { status: 404, headers: { 'Content-Type': 'application/json' } } |
| 578 | + ) |
| 579 | + ) |
| 580 | + global.fetch = fetchMock |
| 581 | + |
| 582 | + const client = new StorageClient(PURGE_URL, { apikey: 'service-role-token' }) |
| 583 | + const { data, error } = await client.purgeBucketCache(BUCKET) |
| 584 | + |
| 585 | + expect(data).toBeNull() |
| 586 | + expect(error).toBeInstanceOf(StorageApiError) |
| 587 | + expect(error?.message).toBe('Object not found') |
| 588 | + }) |
| 589 | + |
| 590 | + it('forwards the AbortController signal to fetch', async () => { |
| 591 | + const fetchMock = jest.fn().mockResolvedValue( |
| 592 | + new Response(JSON.stringify({ message: 'success' }), { |
| 593 | + status: 200, |
| 594 | + headers: { 'Content-Type': 'application/json' }, |
| 595 | + }) |
| 596 | + ) |
| 597 | + global.fetch = fetchMock |
| 598 | + |
| 599 | + const client = new StorageClient(PURGE_URL, { apikey: 'service-role-token' }) |
| 600 | + const controller = new AbortController() |
| 601 | + const { error } = await client.purgeBucketCache(BUCKET, { signal: controller.signal }) |
| 602 | + |
| 603 | + expect(error).toBeNull() |
| 604 | + expect(fetchMock).toHaveBeenCalledWith( |
| 605 | + `${PURGE_URL}/cdn/${BUCKET}`, |
| 606 | + expect.objectContaining({ method: 'DELETE', signal: controller.signal }) |
| 607 | + ) |
| 608 | + }) |
| 609 | + }) |
542 | 610 | }) |
0 commit comments