|
| 1 | +import type { S3Client } from '@aws-sdk/client-s3'; |
| 2 | + |
| 3 | +const mockLogger = { info: jest.fn(), error: jest.fn() }; |
| 4 | + |
| 5 | +jest.mock('@aws-sdk/client-s3', () => ({ |
| 6 | + S3Client: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +jest.mock('@librechat/data-schemas', () => ({ |
| 10 | + logger: mockLogger, |
| 11 | +})); |
| 12 | + |
| 13 | +describe('initializeS3', () => { |
| 14 | + const REQUIRED_ENV = { |
| 15 | + AWS_REGION: 'us-east-1', |
| 16 | + AWS_BUCKET_NAME: 'test-bucket', |
| 17 | + AWS_ACCESS_KEY_ID: 'test-key-id', |
| 18 | + AWS_SECRET_ACCESS_KEY: 'test-secret', |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.resetModules(); |
| 23 | + jest.clearAllMocks(); |
| 24 | + Object.assign(process.env, REQUIRED_ENV); |
| 25 | + delete process.env.AWS_ENDPOINT_URL; |
| 26 | + delete process.env.AWS_FORCE_PATH_STYLE; |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + for (const key of Object.keys(REQUIRED_ENV)) { |
| 31 | + delete process.env[key]; |
| 32 | + } |
| 33 | + delete process.env.AWS_ENDPOINT_URL; |
| 34 | + delete process.env.AWS_FORCE_PATH_STYLE; |
| 35 | + }); |
| 36 | + |
| 37 | + async function load() { |
| 38 | + const { S3Client: MockS3Client } = jest.requireMock('@aws-sdk/client-s3') as { |
| 39 | + S3Client: jest.MockedClass<typeof S3Client>; |
| 40 | + }; |
| 41 | + const { initializeS3 } = await import('../s3'); |
| 42 | + return { MockS3Client, initializeS3 }; |
| 43 | + } |
| 44 | + |
| 45 | + it('should initialize with region and credentials', async () => { |
| 46 | + const { MockS3Client, initializeS3 } = await load(); |
| 47 | + initializeS3(); |
| 48 | + expect(MockS3Client).toHaveBeenCalledWith( |
| 49 | + expect.objectContaining({ |
| 50 | + region: 'us-east-1', |
| 51 | + credentials: { accessKeyId: 'test-key-id', secretAccessKey: 'test-secret' }, |
| 52 | + }), |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should include endpoint when AWS_ENDPOINT_URL is set', async () => { |
| 57 | + process.env.AWS_ENDPOINT_URL = 'https://fsn1.your-objectstorage.com'; |
| 58 | + const { MockS3Client, initializeS3 } = await load(); |
| 59 | + initializeS3(); |
| 60 | + expect(MockS3Client).toHaveBeenCalledWith( |
| 61 | + expect.objectContaining({ endpoint: 'https://fsn1.your-objectstorage.com' }), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not include endpoint when AWS_ENDPOINT_URL is not set', async () => { |
| 66 | + const { MockS3Client, initializeS3 } = await load(); |
| 67 | + initializeS3(); |
| 68 | + const config = MockS3Client.mock.calls[0][0] as Record<string, unknown>; |
| 69 | + expect(config).not.toHaveProperty('endpoint'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should set forcePathStyle when AWS_FORCE_PATH_STYLE is true', async () => { |
| 73 | + process.env.AWS_FORCE_PATH_STYLE = 'true'; |
| 74 | + const { MockS3Client, initializeS3 } = await load(); |
| 75 | + initializeS3(); |
| 76 | + expect(MockS3Client).toHaveBeenCalledWith(expect.objectContaining({ forcePathStyle: true })); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not set forcePathStyle when AWS_FORCE_PATH_STYLE is false', async () => { |
| 80 | + process.env.AWS_FORCE_PATH_STYLE = 'false'; |
| 81 | + const { MockS3Client, initializeS3 } = await load(); |
| 82 | + initializeS3(); |
| 83 | + const config = MockS3Client.mock.calls[0][0] as Record<string, unknown>; |
| 84 | + expect(config).not.toHaveProperty('forcePathStyle'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not set forcePathStyle when AWS_FORCE_PATH_STYLE is not set', async () => { |
| 88 | + const { MockS3Client, initializeS3 } = await load(); |
| 89 | + initializeS3(); |
| 90 | + const config = MockS3Client.mock.calls[0][0] as Record<string, unknown>; |
| 91 | + expect(config).not.toHaveProperty('forcePathStyle'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return null and log error when AWS_REGION is not set', async () => { |
| 95 | + delete process.env.AWS_REGION; |
| 96 | + const { initializeS3 } = await load(); |
| 97 | + const result = initializeS3(); |
| 98 | + expect(result).toBeNull(); |
| 99 | + expect(mockLogger.error).toHaveBeenCalledWith( |
| 100 | + '[initializeS3] AWS_REGION is not set. Cannot initialize S3.', |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should return the same instance on subsequent calls', async () => { |
| 105 | + const { MockS3Client, initializeS3 } = await load(); |
| 106 | + const first = initializeS3(); |
| 107 | + const second = initializeS3(); |
| 108 | + expect(first).toBe(second); |
| 109 | + expect(MockS3Client).toHaveBeenCalledTimes(1); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should use default credentials chain when keys are not provided', async () => { |
| 113 | + delete process.env.AWS_ACCESS_KEY_ID; |
| 114 | + delete process.env.AWS_SECRET_ACCESS_KEY; |
| 115 | + const { MockS3Client, initializeS3 } = await load(); |
| 116 | + initializeS3(); |
| 117 | + const config = MockS3Client.mock.calls[0][0] as Record<string, unknown>; |
| 118 | + expect(config).not.toHaveProperty('credentials'); |
| 119 | + expect(mockLogger.info).toHaveBeenCalledWith( |
| 120 | + '[initializeS3] S3 initialized using default credentials (IRSA).', |
| 121 | + ); |
| 122 | + }); |
| 123 | +}); |
0 commit comments