|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { |
| 3 | + DeleteObjectCommand, |
| 4 | + GetObjectCommand, |
| 5 | + HeadBucketCommand, |
| 6 | + S3Client, |
| 7 | +} from '@aws-sdk/client-s3'; |
| 8 | +import { createObjectStore, createObjectStoreClient } from '../lib/objectStore.js'; |
| 9 | + |
| 10 | +const config = { |
| 11 | + OBJECT_STORE_ENDPOINT: 'http://localhost:9000', |
| 12 | + OBJECT_STORE_BUCKET: 'clicked', |
| 13 | + OBJECT_STORE_ACCESS_KEY: 'clicked', |
| 14 | + OBJECT_STORE_SECRET_KEY: 'clickedsecret', |
| 15 | + OBJECT_STORE_REGION: 'us-east-1', |
| 16 | + OBJECT_STORE_FORCE_PATH_STYLE: true, |
| 17 | +}; |
| 18 | + |
| 19 | +describe('createObjectStoreClient', () => { |
| 20 | + it('configures the S3 client for path-style MinIO endpoints', () => { |
| 21 | + const client = createObjectStoreClient(config); |
| 22 | + expect(client).toBeInstanceOf(S3Client); |
| 23 | + expect(client.config.endpoint).toBeDefined(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('supports virtual-hosted AWS/R2 style endpoints when path style is disabled', () => { |
| 27 | + const client = createObjectStoreClient({ |
| 28 | + ...config, |
| 29 | + OBJECT_STORE_ENDPOINT: 'https://s3.amazonaws.com', |
| 30 | + OBJECT_STORE_FORCE_PATH_STYLE: false, |
| 31 | + }); |
| 32 | + expect(client).toBeInstanceOf(S3Client); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('ObjectStore', () => { |
| 37 | + const send = vi.fn(); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + send.mockReset(); |
| 41 | + vi.spyOn(S3Client.prototype, 'send').mockImplementation(send); |
| 42 | + }); |
| 43 | + |
| 44 | + it('checks bucket reachability with HeadBucket', async () => { |
| 45 | + send.mockResolvedValue({}); |
| 46 | + const store = createObjectStore(config); |
| 47 | + |
| 48 | + await store.ensureBucketReachable(); |
| 49 | + |
| 50 | + expect(send).toHaveBeenCalledWith(expect.any(HeadBucketCommand)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('uploads, reads, and deletes objects in the configured bucket', async () => { |
| 54 | + send.mockResolvedValue({}); |
| 55 | + const store = createObjectStore(config); |
| 56 | + |
| 57 | + await store.putObject('avatars/user.png', Buffer.from('png'), 'image/png'); |
| 58 | + await store.getObject('avatars/user.png'); |
| 59 | + await store.deleteObject('avatars/user.png'); |
| 60 | + |
| 61 | + expect(send).toHaveBeenNthCalledWith( |
| 62 | + 1, |
| 63 | + expect.objectContaining({ |
| 64 | + input: expect.objectContaining({ |
| 65 | + Bucket: 'clicked', |
| 66 | + Key: 'avatars/user.png', |
| 67 | + ContentType: 'image/png', |
| 68 | + }), |
| 69 | + }), |
| 70 | + ); |
| 71 | + expect(send).toHaveBeenNthCalledWith(2, expect.any(GetObjectCommand)); |
| 72 | + expect(send).toHaveBeenNthCalledWith(3, expect.any(DeleteObjectCommand)); |
| 73 | + }); |
| 74 | +}); |
0 commit comments