|
| 1 | +/** |
| 2 | + * Tests for the unified S3 client factory. |
| 3 | + */ |
| 4 | + |
| 5 | +import { createS3Client, S3ConfigError } from '../src/client'; |
| 6 | +import type { StorageConnectionConfig } from '../src/client'; |
| 7 | + |
| 8 | +describe('createS3Client', () => { |
| 9 | + const baseConfig: StorageConnectionConfig = { |
| 10 | + provider: 's3', |
| 11 | + region: 'us-east-1', |
| 12 | + accessKeyId: 'AKIATEST', |
| 13 | + secretAccessKey: 'secrettest', |
| 14 | + }; |
| 15 | + |
| 16 | + it('creates client for AWS S3', () => { |
| 17 | + const client = createS3Client(baseConfig); |
| 18 | + expect(client).toBeDefined(); |
| 19 | + expect(typeof client.send).toBe('function'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('creates client for MinIO with endpoint', () => { |
| 23 | + const client = createS3Client({ |
| 24 | + ...baseConfig, |
| 25 | + provider: 'minio', |
| 26 | + endpoint: 'http://minio:9000', |
| 27 | + }); |
| 28 | + expect(client).toBeDefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('creates client for R2 with endpoint', () => { |
| 32 | + const client = createS3Client({ |
| 33 | + ...baseConfig, |
| 34 | + provider: 'r2', |
| 35 | + endpoint: 'https://account.r2.cloudflarestorage.com', |
| 36 | + }); |
| 37 | + expect(client).toBeDefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('creates client for GCS with endpoint', () => { |
| 41 | + const client = createS3Client({ |
| 42 | + ...baseConfig, |
| 43 | + provider: 'gcs', |
| 44 | + endpoint: 'https://storage.googleapis.com', |
| 45 | + }); |
| 46 | + expect(client).toBeDefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('creates client for DO Spaces with endpoint', () => { |
| 50 | + const client = createS3Client({ |
| 51 | + ...baseConfig, |
| 52 | + provider: 'spaces', |
| 53 | + endpoint: 'https://nyc3.digitaloceanspaces.com', |
| 54 | + }); |
| 55 | + expect(client).toBeDefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('throws S3ConfigError on missing accessKeyId', () => { |
| 59 | + expect(() => |
| 60 | + createS3Client({ ...baseConfig, accessKeyId: '' }), |
| 61 | + ).toThrow(S3ConfigError); |
| 62 | + }); |
| 63 | + |
| 64 | + it('throws S3ConfigError on missing secretAccessKey', () => { |
| 65 | + expect(() => |
| 66 | + createS3Client({ ...baseConfig, secretAccessKey: '' }), |
| 67 | + ).toThrow(S3ConfigError); |
| 68 | + }); |
| 69 | + |
| 70 | + it('throws S3ConfigError on missing region', () => { |
| 71 | + expect(() => |
| 72 | + createS3Client({ ...baseConfig, region: '' }), |
| 73 | + ).toThrow(S3ConfigError); |
| 74 | + }); |
| 75 | + |
| 76 | + it('throws on non-AWS provider without endpoint', () => { |
| 77 | + expect(() => |
| 78 | + createS3Client({ ...baseConfig, provider: 'minio' }), |
| 79 | + ).toThrow(S3ConfigError); |
| 80 | + expect(() => |
| 81 | + createS3Client({ ...baseConfig, provider: 'minio' }), |
| 82 | + ).toThrow("endpoint is required for provider 'minio'"); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not throw on AWS S3 without endpoint', () => { |
| 86 | + expect(() => createS3Client(baseConfig)).not.toThrow(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('respects explicit forcePathStyle override', () => { |
| 90 | + const client = createS3Client({ |
| 91 | + ...baseConfig, |
| 92 | + forcePathStyle: true, |
| 93 | + }); |
| 94 | + expect(client).toBeDefined(); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('S3ConfigError', () => { |
| 99 | + it('has correct name and code', () => { |
| 100 | + const err = new S3ConfigError('INVALID_CONFIG', 'test message'); |
| 101 | + expect(err.name).toBe('S3ConfigError'); |
| 102 | + expect(err.code).toBe('INVALID_CONFIG'); |
| 103 | + expect(err.message).toBe('test message'); |
| 104 | + expect(err).toBeInstanceOf(Error); |
| 105 | + }); |
| 106 | +}); |
0 commit comments