|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +import { Logger, Probot } from 'probot'; |
| 11 | +import { OpensearchClient } from '../../src/utility/opensearch/opensearch-client'; |
| 12 | +import githubActivityEventsMonitor from '../../src/call/github-activity-events-monitor'; |
| 13 | + |
| 14 | +jest.mock('../../src/utility/opensearch/opensearch-client'); |
| 15 | + |
| 16 | +describe('githubActivityEventsMonitor', () => { |
| 17 | + let app: Probot; |
| 18 | + let context: any; |
| 19 | + let resource: any; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' }); |
| 23 | + app.log = { |
| 24 | + info: jest.fn(), |
| 25 | + error: jest.fn(), |
| 26 | + } as unknown as Logger; |
| 27 | + context = { |
| 28 | + name: 'eventType', |
| 29 | + id: 'id', |
| 30 | + payload: { |
| 31 | + repository: { |
| 32 | + name: 'repo', |
| 33 | + owner: { login: 'org' }, |
| 34 | + }, |
| 35 | + action: 'action', |
| 36 | + sender: { |
| 37 | + login: 'sender', |
| 38 | + }, |
| 39 | + }, |
| 40 | + }; |
| 41 | + resource = { |
| 42 | + organizations: new Map([ |
| 43 | + [ |
| 44 | + 'org', |
| 45 | + { |
| 46 | + repositories: new Map([['repo', 'repo object']]), |
| 47 | + }, |
| 48 | + ], |
| 49 | + ]), |
| 50 | + }; |
| 51 | + }); |
| 52 | + |
| 53 | + it('should index events', async () => { |
| 54 | + const mockClient = { |
| 55 | + index: jest.fn().mockResolvedValue({}), |
| 56 | + }; |
| 57 | + (OpensearchClient as jest.Mock).mockImplementation(() => { |
| 58 | + return { getClient: jest.fn().mockResolvedValue(mockClient) }; |
| 59 | + }); |
| 60 | + jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z'); |
| 61 | + await githubActivityEventsMonitor(app, context, resource); |
| 62 | + expect(mockClient.index).toHaveBeenCalledWith({ |
| 63 | + index: expect.stringMatching(/^github-activity-events-\d{2}-\d{4}$/), |
| 64 | + body: expect.objectContaining({ |
| 65 | + id: 'id', |
| 66 | + organization: 'org', |
| 67 | + repository: 'repo', |
| 68 | + type: 'eventType', |
| 69 | + action: 'action', |
| 70 | + sender: 'sender', |
| 71 | + created_at: '2024-10-04T21:00:06.875Z', |
| 72 | + }), |
| 73 | + }); |
| 74 | + expect(app.log.info).toHaveBeenCalledWith('Event indexed successfully.'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should log an error if indexing fails', async () => { |
| 78 | + const mockClient = { |
| 79 | + index: jest.fn().mockRejectedValue(new Error('Indexing failed')), |
| 80 | + }; |
| 81 | + (OpensearchClient as jest.Mock).mockImplementation(() => { |
| 82 | + return { getClient: jest.fn().mockResolvedValue(mockClient) }; |
| 83 | + }); |
| 84 | + await githubActivityEventsMonitor(app, context, resource); |
| 85 | + expect(app.log.error).toHaveBeenCalledWith('Error indexing event: Error: Indexing failed'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments