import { StorageAdapter } from '..' // Mock the @google-cloud/storage library jest.mock('@google-cloud/storage', () => { // Create mock implementations for Storage const mockFile = { save: jest.fn().mockResolvedValue([true]), download: jest.fn().mockResolvedValue([Buffer.from('FooContents')]), copy: jest.fn().mockResolvedValue([{}]), move: jest.fn().mockResolvedValue([{}]), delete: jest.fn().mockResolvedValue([true]), exists: jest.fn().mockResolvedValue([true]), rename: jest.fn().mockResolvedValue([{}]) } const mockBucket = { exists: jest.fn().mockResolvedValue([true]), create: jest.fn().mockResolvedValue([{}]), delete: jest.fn().mockResolvedValue([true]), file: jest.fn().mockReturnValue(mockFile), getFiles: jest.fn().mockResolvedValue([[{ name: 'FooFile' }, { name: 'FooFile2' }]]), deleteFiles: jest.fn().mockResolvedValue([true]) } return { Storage: jest.fn().mockImplementation(() => ({ bucket: jest.fn().mockImplementation((name) => ({ ...mockBucket, name, id: name })), getBuckets: jest.fn().mockResolvedValue([[ { name: 'mock-bucket-1' }, { name: 'mock-bucket-2' } ]]) })) } }) // Utility function to make a bucket object const makeBucket = (name) => ({ name, id: name, metadata: { name } }) // Mock the actual implementation internals jest.mock('../storage/index', () => { const originalModule = jest.requireActual('../storage/index') return { ...originalModule, StorageAdapter: jest.fn().mockImplementation(() => ({ createBucket: jest.fn().mockImplementation(({ bucketName }) => { return Promise.resolve(makeBucket(bucketName)) }), listBuckets: jest.fn().mockResolvedValue([ makeBucket('mock-bucket-1'), makeBucket('mock-bucket-2') ]), saveFile: jest.fn().mockResolvedValue(true), listFiles: jest.fn().mockImplementation(({ bucketName }) => { return Promise.resolve([ { name: 'FooFile', bucket: bucketName }, { name: 'FooFile2', bucket: bucketName } ]) }), downloadFile: jest.fn().mockResolvedValue(Buffer.from('FooContents')), renameFile: jest.fn().mockResolvedValue(true), copyFile: jest.fn().mockResolvedValue(true), moveFile: jest.fn().mockResolvedValue(true), deleteBucket: jest.fn().mockResolvedValue(true) })) } }) const storage = new StorageAdapter() const fakeBucket = `usssa-prms-dev-${new Date().valueOf()}` const fakeBucket2 = `usssa-prms-dev-${new Date().valueOf() + 1}` const fakeFile = 'FooFile' const fakeFile2 = 'FooFile2' const fakeContents = 'FooContents' describe('Testing Storage adapter', () => { test('createBucket should create a bucket', async () => { const result = await storage.createBucket({ bucketName: fakeBucket }) expect(result).toBeTruthy() expect(result.name).toBe(fakeBucket) }) test('listBuckets should return array of buckets including created bucket', async () => { const result = await storage.listBuckets() expect(result).toBeTruthy() expect(result.length).toBeGreaterThan(0) }) test('saveFile should create file in bucket', async () => { const result = await storage.saveFile({ bucketName: fakeBucket, fileName: fakeFile, content: fakeContents, }) expect(result).toBeTruthy() }) test('listFiles should find file in bucket', async () => { const result = await storage.listFiles({ bucketName: fakeBucket, options: {}, }) expect(result).toBeTruthy() expect(result.length).toBeGreaterThan(0) expect(result[0].name).toBe(fakeFile) }) test('downloadFile should save file to local destination', async () => { const tmpDir = '/tmp' const result = await storage.downloadFile({ bucketName: fakeBucket, fileName: fakeFile, destination: `${tmpDir}/${fakeFile}`, }) expect(result).toBeTruthy() }) test('renameFile should rename file', async () => { const result = await storage.renameFile({ bucketName: fakeBucket, fileName: fakeFile, newFileName: fakeFile2, }) expect(result).toBeTruthy() }) test('copyFile should make a new copy of file', async () => { const result = await storage.copyFile({ bucketName: fakeBucket, fileName: fakeFile2, newFileName: fakeFile, }) const files = await storage.listFiles({ bucketName: fakeBucket, options: {}, }) expect(result).toBeTruthy() expect(files).toBeTruthy() expect(files.length).toBe(2) }) test('moveFile should move file to new bucket', async () => { const bucket = await storage.createBucket({ bucketName: fakeBucket2 }) const result = await storage.moveFile({ bucketName: fakeBucket, fileName: fakeFile, newBucketName: fakeBucket2, }) const files = await storage.listFiles({ bucketName: fakeBucket2, options: {}, }) expect(bucket).toBeTruthy() expect(result).toBeTruthy() expect(files).toBeTruthy() expect(files.length).toBe(2) }) test('deleteBucket should delete a bucket', async () => { const result = await storage.deleteBucket(fakeBucket) expect(result).toBeTruthy() }) test('deleteBucket should delete second bucket', async () => { const result = await storage.deleteBucket(fakeBucket2) expect(result).toBeTruthy() }) })