|
| 1 | +export const mockedRepo = { |
| 2 | + findOneBy: jest.fn(), |
| 3 | + createQueryBuilder: jest.fn(), |
| 4 | + save: jest.fn(), |
| 5 | + update: jest.fn(), |
| 6 | +}; |
| 7 | + |
| 8 | +jest.mock('../../../src/app/core/db/repo-factory', () => { |
| 9 | + return { |
| 10 | + repoFactory: () => jest.fn().mockReturnValue(mockedRepo), |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +const fileServiceMock = { |
| 15 | + save: jest.fn(), |
| 16 | +}; |
| 17 | +jest.mock('../../../src/app/file/file.service', () => { |
| 18 | + return { |
| 19 | + fileService: fileServiceMock, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +const flowVersionServiceMock = { |
| 24 | + getOneOrThrow: jest.fn(), |
| 25 | +}; |
| 26 | +jest.mock('../../../src/app/flows/flow-version/flow-version.service', () => { |
| 27 | + return { |
| 28 | + flowVersionService: flowVersionServiceMock, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +const logSerializerMock = { |
| 33 | + serialize: jest.fn(), |
| 34 | +}; |
| 35 | +jest.mock('../../../src/app/flows/flow-run/log-serializer', () => { |
| 36 | + return { |
| 37 | + logSerializer: logSerializerMock, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +import { |
| 42 | + FileCompression, |
| 43 | + FileType, |
| 44 | + FlowRunStatus, |
| 45 | + FlowRunTriggerSource, |
| 46 | + RunEnvironment, |
| 47 | + StepOutputStatus, |
| 48 | +} from '@openops/shared'; |
| 49 | +import { flowRunService } from '../../../src/app/flows/flow-run/flow-run-service'; |
| 50 | + |
| 51 | +describe('flowRunService.recordTriggerFailure', () => { |
| 52 | + const now = new Date('2024-01-02T03:04:05.000Z'); |
| 53 | + |
| 54 | + beforeAll(() => { |
| 55 | + jest.useFakeTimers(); |
| 56 | + }); |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + jest.setSystemTime(now); |
| 60 | + jest.clearAllMocks(); |
| 61 | + logSerializerMock.serialize.mockResolvedValue( |
| 62 | + Buffer.from('compressed-logs'), |
| 63 | + ); |
| 64 | + fileServiceMock.save.mockResolvedValue({ id: 'file_123' }); |
| 65 | + flowVersionServiceMock.getOneOrThrow.mockResolvedValue({ |
| 66 | + id: 'fv_1', |
| 67 | + flowId: 'flow_1', |
| 68 | + displayName: 'My Flow V1', |
| 69 | + trigger: { |
| 70 | + name: 'triggerStep', |
| 71 | + type: 'POLLING', |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + afterAll(() => { |
| 77 | + jest.useRealTimers(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should create a failed flow run with logs when triggerInput is provided', async () => { |
| 81 | + await flowRunService.recordTriggerFailure({ |
| 82 | + projectId: 'proj_1', |
| 83 | + flowVersionId: 'fv_1', |
| 84 | + errorMessage: 'Trigger failed to execute', |
| 85 | + reason: 'TRIGGER_ERROR', |
| 86 | + triggerInput: { a: 1 }, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(logSerializerMock.serialize).toHaveBeenCalledTimes(1); |
| 90 | + const serializeArg = (logSerializerMock.serialize as jest.Mock).mock |
| 91 | + .calls[0][0]; |
| 92 | + expect(serializeArg).toEqual({ |
| 93 | + executionState: { |
| 94 | + steps: { |
| 95 | + triggerStep: { |
| 96 | + type: 'POLLING', |
| 97 | + status: StepOutputStatus.FAILED, |
| 98 | + input: { a: 1 }, |
| 99 | + errorMessage: 'Trigger failed to execute', |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + expect(fileServiceMock.save).toHaveBeenCalledTimes(1); |
| 106 | + const saveArg = (fileServiceMock.save as jest.Mock).mock.calls[0][0]; |
| 107 | + expect(saveArg).toEqual( |
| 108 | + expect.objectContaining({ |
| 109 | + fileId: expect.any(String), |
| 110 | + data: Buffer.from('compressed-logs'), |
| 111 | + type: FileType.FLOW_RUN_LOG, |
| 112 | + compression: FileCompression.GZIP, |
| 113 | + projectId: 'proj_1', |
| 114 | + }), |
| 115 | + ); |
| 116 | + |
| 117 | + expect(mockedRepo.save).toHaveBeenCalledTimes(1); |
| 118 | + const saved = mockedRepo.save.mock.calls[0][0]; |
| 119 | + expect(saved).toMatchObject({ |
| 120 | + projectId: 'proj_1', |
| 121 | + flowId: 'flow_1', |
| 122 | + flowVersionId: 'fv_1', |
| 123 | + environment: RunEnvironment.PRODUCTION, |
| 124 | + flowDisplayName: 'My Flow V1', |
| 125 | + startTime: now.toISOString(), |
| 126 | + finishTime: now.toISOString(), |
| 127 | + status: FlowRunStatus.FAILED, |
| 128 | + triggerSource: FlowRunTriggerSource.TRIGGERED, |
| 129 | + terminationReason: 'TRIGGER_ERROR', |
| 130 | + tasks: 0, |
| 131 | + duration: 0, |
| 132 | + tags: [], |
| 133 | + }); |
| 134 | + expect(saved.logsFileId).toBe(saveArg.fileId); |
| 135 | + |
| 136 | + expect(typeof saved.id).toBe('string'); |
| 137 | + expect(saved.id.length).toBeGreaterThan(0); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should create a failed flow run without triggerInput when not provided', async () => { |
| 141 | + await flowRunService.recordTriggerFailure({ |
| 142 | + projectId: 'proj_2', |
| 143 | + flowVersionId: 'fv_1', |
| 144 | + errorMessage: 'Boom', |
| 145 | + reason: 'TRIGGER_ERROR_NO_INPUT', |
| 146 | + }); |
| 147 | + |
| 148 | + const serializeArg = (logSerializerMock.serialize as jest.Mock).mock |
| 149 | + .calls[0][0]; |
| 150 | + expect(serializeArg.executionState.steps.triggerStep).toEqual({ |
| 151 | + type: 'POLLING', |
| 152 | + status: StepOutputStatus.FAILED, |
| 153 | + input: undefined, |
| 154 | + errorMessage: 'Boom', |
| 155 | + }); |
| 156 | + |
| 157 | + const saved = mockedRepo.save.mock.calls[0][0]; |
| 158 | + expect(saved.projectId).toBe('proj_2'); |
| 159 | + expect(saved.terminationReason).toBe('TRIGGER_ERROR_NO_INPUT'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments