|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import cronTimes from '@/apps/scheduler/common/cron-times' |
| 4 | +import updateFlowStatus from '@/graphql/mutations/update-flow-status' |
| 5 | +import { |
| 6 | + REMOVE_AFTER_7_DAYS_OR_50_JOBS, |
| 7 | + REMOVE_AFTER_30_DAYS, |
| 8 | +} from '@/helpers/default-job-configuration' |
| 9 | +import flowQueue from '@/queues/flow' |
| 10 | + |
| 11 | +// In these tests we simulate the chaining of queries on currentUser.$relatedQuery('flows') |
| 12 | +// and the additional methods on the flow: $query, getTriggerStep etc. |
| 13 | +describe('updateFlowStatus', () => { |
| 14 | + let fakeFlow: any |
| 15 | + let fakeQuery: any |
| 16 | + let context: any |
| 17 | + let fakeTriggerStep: any |
| 18 | + let patchSpy: ReturnType<typeof vi.fn> |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + vi.resetAllMocks() |
| 22 | + |
| 23 | + // Create a fake flow object with default values. |
| 24 | + fakeFlow = { |
| 25 | + id: 'flow-1', |
| 26 | + active: false, |
| 27 | + steps: [{ position: 1 }, { position: 2 }], // contiguous by default |
| 28 | + // we will override $query to simulate patch operations |
| 29 | + $query: vi.fn(), |
| 30 | + getTriggerStep: vi.fn(), |
| 31 | + } |
| 32 | + patchSpy = vi.fn().mockResolvedValue(undefined) |
| 33 | + fakeFlow.$query.mockReturnValue({ patch: patchSpy }) |
| 34 | + |
| 35 | + // Fake the chained query methods on context.currentUser.$relatedQuery('flows') |
| 36 | + fakeQuery = { |
| 37 | + findOne: vi.fn().mockReturnThis(), |
| 38 | + withGraphJoined: vi.fn().mockReturnThis(), |
| 39 | + orderBy: vi.fn().mockReturnThis(), |
| 40 | + throwIfNotFound: vi.fn().mockResolvedValue(fakeFlow), |
| 41 | + } |
| 42 | + |
| 43 | + context = { |
| 44 | + currentUser: { |
| 45 | + $relatedQuery: vi.fn().mockReturnValue(fakeQuery), |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + // Set up a default fake trigger step returning a trigger command. |
| 50 | + fakeTriggerStep = { |
| 51 | + parameters: {}, |
| 52 | + getTriggerCommand: vi.fn().mockResolvedValue({ |
| 53 | + getInterval: vi.fn().mockReturnValue(cronTimes.everyHour), |
| 54 | + }), |
| 55 | + } |
| 56 | + fakeFlow.getTriggerStep.mockResolvedValue(fakeTriggerStep) |
| 57 | + |
| 58 | + // Mock flowQueue methods. |
| 59 | + flowQueue.add = vi.fn().mockResolvedValue(undefined) |
| 60 | + flowQueue.getRepeatableJobs = vi |
| 61 | + .fn() |
| 62 | + .mockResolvedValue([{ id: fakeFlow.id, key: 'repeat-key' }]) |
| 63 | + flowQueue.removeRepeatableByKey = vi.fn().mockResolvedValue(undefined) |
| 64 | + }) |
| 65 | + |
| 66 | + it('returns the flow without changes if the active status did not change', async () => { |
| 67 | + // Set the flow status to true and provide the same value in input. |
| 68 | + fakeFlow.active = true |
| 69 | + |
| 70 | + const params = { input: { id: fakeFlow.id, active: true } } |
| 71 | + const result = await updateFlowStatus({}, params, context) |
| 72 | + |
| 73 | + expect(result).toEqual(fakeFlow) |
| 74 | + // The patch/update should not be triggered |
| 75 | + expect(fakeFlow.$query).not.toHaveBeenCalled() |
| 76 | + expect(fakeFlow.getTriggerStep).not.toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it('throws an error when activating a flow with non-contiguous step positions', async () => { |
| 80 | + // Prepare a flow with steps that are not contiguous. |
| 81 | + fakeFlow.active = false |
| 82 | + fakeFlow.steps = [{ position: 1 }, { position: 3 }] |
| 83 | + |
| 84 | + const params = { input: { id: fakeFlow.id, active: true } } |
| 85 | + |
| 86 | + await expect(updateFlowStatus({}, params, context)).rejects.toThrow( |
| 87 | + 'Step positions are out of order. Please contact [email protected] for help.', |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + it('activates the flow and enqueues a job for non-webhook triggers', async () => { |
| 92 | + // Starting state where the flow is inactive and input sets it to active. |
| 93 | + fakeFlow.active = false |
| 94 | + fakeFlow.steps = [{ position: 1 }, { position: 2 }] |
| 95 | + |
| 96 | + const params = { input: { id: fakeFlow.id, active: true } } |
| 97 | + const result = await updateFlowStatus({}, params, context) |
| 98 | + |
| 99 | + // Validate that we patched the flow with active true and publishedAt set to an ISO string. |
| 100 | + expect(patchSpy).toHaveBeenCalledWith({ |
| 101 | + active: true, |
| 102 | + publishedAt: expect.any(String), |
| 103 | + }) |
| 104 | + |
| 105 | + // jobName is constructed as "flow-<flow.id>" |
| 106 | + expect(flowQueue.add).toHaveBeenCalledWith( |
| 107 | + `flow-${fakeFlow.id}`, |
| 108 | + { flowId: fakeFlow.id }, |
| 109 | + { |
| 110 | + repeat: { pattern: '0 * * * *' }, |
| 111 | + jobId: fakeFlow.id, |
| 112 | + removeOnComplete: REMOVE_AFTER_7_DAYS_OR_50_JOBS, |
| 113 | + removeOnFail: REMOVE_AFTER_30_DAYS, |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + expect(result).toEqual(fakeFlow) |
| 118 | + }) |
| 119 | + |
| 120 | + it('deactivates the flow and removes the repeatable job for non-webhook triggers', async () => { |
| 121 | + // For deactivation, ensure the current flow is active and we are setting it to inactive. |
| 122 | + fakeFlow.active = true |
| 123 | + |
| 124 | + // Simulate that a repeatable job exists for this flow. |
| 125 | + flowQueue.getRepeatableJobs = vi |
| 126 | + .fn() |
| 127 | + .mockResolvedValue([{ id: fakeFlow.id, key: 'repeat-key' }]) |
| 128 | + |
| 129 | + const params = { input: { id: fakeFlow.id, active: false } } |
| 130 | + const result = await updateFlowStatus({}, params, context) |
| 131 | + |
| 132 | + expect(patchSpy).toHaveBeenCalledWith({ |
| 133 | + active: false, |
| 134 | + publishedAt: null, |
| 135 | + }) |
| 136 | + |
| 137 | + expect(flowQueue.removeRepeatableByKey).toHaveBeenCalledWith('repeat-key') |
| 138 | + expect(result).toEqual(fakeFlow) |
| 139 | + }) |
| 140 | + |
| 141 | + it('does not perform any queue actions when the trigger type is webhook on activation', async () => { |
| 142 | + // Flow activation but the trigger is of type webhook. |
| 143 | + fakeFlow.active = false |
| 144 | + fakeFlow.steps = [{ position: 1 }, { position: 2 }] |
| 145 | + |
| 146 | + fakeTriggerStep.getTriggerCommand.mockResolvedValue({ |
| 147 | + type: 'webhook', |
| 148 | + }) |
| 149 | + |
| 150 | + const params = { input: { id: fakeFlow.id, active: true } } |
| 151 | + const result = await updateFlowStatus({}, params, context) |
| 152 | + |
| 153 | + // The patch should still occur. |
| 154 | + expect(patchSpy).toHaveBeenCalledWith({ |
| 155 | + active: true, |
| 156 | + publishedAt: expect.any(String), |
| 157 | + }) |
| 158 | + |
| 159 | + // But no job should be added when trigger type is webhook. |
| 160 | + expect(flowQueue.add).not.toHaveBeenCalled() |
| 161 | + expect(result).toEqual(fakeFlow) |
| 162 | + }) |
| 163 | + |
| 164 | + it('does not perform any queue actions when the trigger type is webhook on deactivation', async () => { |
| 165 | + // Flow deactivation but the trigger is of type webhook. |
| 166 | + fakeFlow.active = true |
| 167 | + |
| 168 | + fakeTriggerStep.getTriggerCommand.mockResolvedValue({ |
| 169 | + type: 'webhook', |
| 170 | + }) |
| 171 | + |
| 172 | + const params = { input: { id: fakeFlow.id, active: false } } |
| 173 | + const result = await updateFlowStatus({}, params, context) |
| 174 | + |
| 175 | + expect(patchSpy).toHaveBeenCalledWith({ |
| 176 | + active: false, |
| 177 | + publishedAt: null, |
| 178 | + }) |
| 179 | + |
| 180 | + // For webhook triggers no removal of a repeatable job should be attempted. |
| 181 | + expect(flowQueue.getRepeatableJobs).not.toHaveBeenCalled() |
| 182 | + expect(flowQueue.removeRepeatableByKey).not.toHaveBeenCalled() |
| 183 | + expect(result).toEqual(fakeFlow) |
| 184 | + }) |
| 185 | +}) |
0 commit comments