|
| 1 | +import OSS from '../../../src/resources/oss'; |
| 2 | +import { ICredentials } from '@serverless-devs/component-interface'; |
| 3 | +import logger from '../../../src/logger'; |
| 4 | +import * as utils from '../../../src/utils'; |
| 5 | + |
| 6 | +// Mocks |
| 7 | +jest.mock('@serverless-cd/srm-aliyun-oss', () => { |
| 8 | + return { |
| 9 | + __esModule: true, |
| 10 | + default: jest.fn(), |
| 11 | + }; |
| 12 | +}); |
| 13 | +jest.mock('../../../src/logger'); |
| 14 | +jest.mock('../../../src/utils'); |
| 15 | + |
| 16 | +const OssMock = jest.requireMock('@serverless-cd/srm-aliyun-oss').default; |
| 17 | +const isAppCenterMock = utils.isAppCenter as jest.Mock; |
| 18 | + |
| 19 | +describe('OSS', () => { |
| 20 | + let mockCredentials: ICredentials; |
| 21 | + let oss: OSS; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + mockCredentials = { |
| 25 | + AccountID: 'test-account-id', |
| 26 | + AccessKeyID: 'test-access-key-id', |
| 27 | + AccessKeySecret: 'test-access-key-secret', |
| 28 | + SecurityToken: 'test-security-token', |
| 29 | + } as ICredentials; |
| 30 | + |
| 31 | + // Setup mocks |
| 32 | + isAppCenterMock.mockReturnValue(false); |
| 33 | + |
| 34 | + // Mock logger methods |
| 35 | + logger.debug = jest.fn(); |
| 36 | + logger.info = jest.fn(); |
| 37 | + logger.spin = jest.fn(); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('constructor', () => { |
| 45 | + it('should initialize correctly with credentials and endpoint', () => { |
| 46 | + const region = 'cn-hangzhou'; |
| 47 | + const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com'; |
| 48 | + |
| 49 | + oss = new OSS(region, mockCredentials, ossEndpoint); |
| 50 | + |
| 51 | + expect(oss.client).toBeDefined(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('deploy', () => { |
| 56 | + it('should deploy OSS resource and return ossBucket', async () => { |
| 57 | + const region = 'cn-hangzhou'; |
| 58 | + const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com'; |
| 59 | + const expectedOssBucket = 'test-oss-bucket'; |
| 60 | + |
| 61 | + // Mock Oss client |
| 62 | + const mockClient = { |
| 63 | + initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }), |
| 64 | + }; |
| 65 | + OssMock.mockImplementation(() => mockClient); |
| 66 | + |
| 67 | + oss = new OSS(region, mockCredentials, ossEndpoint); |
| 68 | + |
| 69 | + const result = await oss.deploy(); |
| 70 | + |
| 71 | + expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object)); |
| 72 | + expect(result).toEqual({ ossBucket: expectedOssBucket }); |
| 73 | + expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('init oss:')); |
| 74 | + expect(logger.spin).toHaveBeenCalledWith( |
| 75 | + 'creating', |
| 76 | + 'oss', |
| 77 | + `region: ${region}; ossBucket: ${expectedOssBucket}`, |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should log info message when isAppCenter returns true', async () => { |
| 82 | + const region = 'cn-hangzhou'; |
| 83 | + const ossEndpoint = 'https://oss-cn-hangzhou.aliyuncs.com'; |
| 84 | + const expectedOssBucket = 'test-oss-bucket'; |
| 85 | + |
| 86 | + // Mock isAppCenter to return true |
| 87 | + isAppCenterMock.mockReturnValue(true); |
| 88 | + |
| 89 | + // Mock Oss client |
| 90 | + const mockClient = { |
| 91 | + initOss: jest.fn().mockResolvedValue({ ossBucket: expectedOssBucket }), |
| 92 | + }; |
| 93 | + OssMock.mockImplementation(() => mockClient); |
| 94 | + |
| 95 | + oss = new OSS(region, mockCredentials, ossEndpoint); |
| 96 | + |
| 97 | + await oss.deploy(); |
| 98 | + |
| 99 | + expect(mockClient.initOss).toHaveBeenCalledWith(expect.any(Object)); |
| 100 | + expect(logger.info).toHaveBeenCalledWith(`created oss region: ${region};`); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments