2022-11-09 06:25:00 -08:00
|
|
|
import { ActiveExecutions } from '@/ActiveExecutions';
|
2022-08-30 01:49:30 -07:00
|
|
|
import PCancelable from 'p-cancelable';
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { Container } from 'typedi';
|
2023-05-23 17:01:45 -07:00
|
|
|
import type { IExecuteResponsePromiseData, IRun } from 'n8n-workflow';
|
2023-05-02 01:37:19 -07:00
|
|
|
import { createDeferredPromise } from 'n8n-workflow';
|
|
|
|
import type { IWorkflowExecutionDataProcess } from '@/Interfaces';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { ExecutionRepository } from '@db/repositories';
|
2022-08-30 01:49:30 -07:00
|
|
|
|
|
|
|
const FAKE_EXECUTION_ID = '15';
|
|
|
|
const FAKE_SECOND_EXECUTION_ID = '20';
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const updateExistingExecution = jest.fn();
|
|
|
|
const createNewExecution = jest.fn(async () => {
|
|
|
|
return { id: FAKE_EXECUTION_ID };
|
|
|
|
});
|
|
|
|
|
|
|
|
Container.set(ExecutionRepository, {
|
|
|
|
updateExistingExecution,
|
|
|
|
createNewExecution,
|
2022-08-30 01:49:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('ActiveExecutions', () => {
|
2022-11-09 06:25:00 -08:00
|
|
|
let activeExecutions: ActiveExecutions;
|
2022-08-30 01:49:30 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-11-09 06:25:00 -08:00
|
|
|
activeExecutions = new ActiveExecutions();
|
2022-08-30 01:49:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should initialize activeExecutions with empty list', () => {
|
|
|
|
expect(activeExecutions.getActiveExecutions().length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should add execution to active execution list', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
const executionId = await activeExecutions.add(newExecution);
|
2022-09-12 06:52:06 -07:00
|
|
|
|
2022-08-30 01:49:30 -07:00
|
|
|
expect(executionId).toBe(FAKE_EXECUTION_ID);
|
|
|
|
expect(activeExecutions.getActiveExecutions().length).toBe(1);
|
2023-06-20 10:13:18 -07:00
|
|
|
expect(createNewExecution).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateExistingExecution).toHaveBeenCalledTimes(0);
|
2022-08-30 01:49:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should update execution if add is called with execution ID', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
2022-11-09 06:25:00 -08:00
|
|
|
const executionId = await activeExecutions.add(
|
|
|
|
newExecution,
|
|
|
|
undefined,
|
|
|
|
FAKE_SECOND_EXECUTION_ID,
|
|
|
|
);
|
2022-09-12 06:52:06 -07:00
|
|
|
|
2022-08-30 01:49:30 -07:00
|
|
|
expect(executionId).toBe(FAKE_SECOND_EXECUTION_ID);
|
|
|
|
expect(activeExecutions.getActiveExecutions().length).toBe(1);
|
2023-06-20 10:13:18 -07:00
|
|
|
expect(createNewExecution).toHaveBeenCalledTimes(0);
|
|
|
|
expect(updateExistingExecution).toHaveBeenCalledTimes(1);
|
2022-08-30 01:49:30 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should fail attaching execution to invalid executionId', async () => {
|
|
|
|
const deferredPromise = mockCancelablePromise();
|
2022-09-12 06:52:06 -07:00
|
|
|
|
2022-08-30 01:49:30 -07:00
|
|
|
expect(() => {
|
|
|
|
activeExecutions.attachWorkflowExecution(FAKE_EXECUTION_ID, deferredPromise);
|
|
|
|
}).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should successfully attach execution to valid executionId', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
await activeExecutions.add(newExecution, undefined, FAKE_EXECUTION_ID);
|
|
|
|
const deferredPromise = mockCancelablePromise();
|
2022-09-12 06:52:06 -07:00
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
expect(() =>
|
|
|
|
activeExecutions.attachWorkflowExecution(FAKE_EXECUTION_ID, deferredPromise),
|
|
|
|
).not.toThrow();
|
2022-09-12 06:52:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should attach and resolve response promise to existing execution', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
await activeExecutions.add(newExecution, undefined, FAKE_EXECUTION_ID);
|
|
|
|
const deferredPromise = await mockDeferredPromise();
|
|
|
|
activeExecutions.attachResponsePromise(FAKE_EXECUTION_ID, deferredPromise);
|
2022-11-09 06:25:00 -08:00
|
|
|
const fakeResponse = { data: { resultData: { runData: {} } } };
|
2022-09-12 06:52:06 -07:00
|
|
|
activeExecutions.resolveResponsePromise(FAKE_EXECUTION_ID, fakeResponse);
|
|
|
|
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(deferredPromise.promise()).resolves.toEqual(fakeResponse);
|
2022-09-12 06:52:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should remove an existing execution', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
const executionId = await activeExecutions.add(newExecution);
|
|
|
|
activeExecutions.remove(executionId);
|
|
|
|
|
|
|
|
expect(activeExecutions.getActiveExecutions().length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should resolve post execute promise on removal', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
const executionId = await activeExecutions.add(newExecution);
|
|
|
|
const postExecutePromise = activeExecutions.getPostExecutePromise(executionId);
|
|
|
|
// Force the above to be executed since we cannot await it
|
|
|
|
await new Promise((res) => {
|
|
|
|
setTimeout(res, 100);
|
|
|
|
});
|
|
|
|
const fakeOutput = mockFullRunData();
|
|
|
|
activeExecutions.remove(executionId, fakeOutput);
|
|
|
|
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(postExecutePromise).resolves.toEqual(fakeOutput);
|
2022-09-12 06:52:06 -07:00
|
|
|
});
|
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
test('Should throw error when trying to create a promise with invalid execution', async () => {
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(activeExecutions.getPostExecutePromise(FAKE_EXECUTION_ID)).rejects.toThrow();
|
2022-09-12 06:52:06 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Should call function to cancel execution when asked to stop', async () => {
|
|
|
|
const newExecution = mockExecutionData();
|
|
|
|
const executionId = await activeExecutions.add(newExecution);
|
|
|
|
const cancelExecution = jest.fn();
|
|
|
|
const cancellablePromise = mockCancelablePromise();
|
|
|
|
cancellablePromise.cancel = cancelExecution;
|
|
|
|
activeExecutions.attachWorkflowExecution(executionId, cancellablePromise);
|
2023-05-23 17:01:45 -07:00
|
|
|
void activeExecutions.stopExecution(executionId);
|
2022-09-12 06:52:06 -07:00
|
|
|
|
|
|
|
expect(cancelExecution).toHaveBeenCalledTimes(1);
|
2022-08-30 01:49:30 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function mockExecutionData(): IWorkflowExecutionDataProcess {
|
|
|
|
return {
|
|
|
|
executionMode: 'manual',
|
|
|
|
workflowData: {
|
|
|
|
name: 'Test workflow 1',
|
|
|
|
active: false,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
nodes: [],
|
2022-11-09 06:25:00 -08:00
|
|
|
connections: {},
|
2022-08-30 01:49:30 -07:00
|
|
|
},
|
|
|
|
userId: uuid(),
|
2022-11-09 06:25:00 -08:00
|
|
|
};
|
2022-08-30 01:49:30 -07:00
|
|
|
}
|
|
|
|
|
2022-09-12 06:52:06 -07:00
|
|
|
function mockFullRunData(): IRun {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
resultData: {
|
2022-11-09 06:25:00 -08:00
|
|
|
runData: {},
|
|
|
|
},
|
2022-09-12 06:52:06 -07:00
|
|
|
},
|
|
|
|
mode: 'manual',
|
|
|
|
startedAt: new Date(),
|
2023-02-17 01:54:07 -08:00
|
|
|
status: 'new',
|
2022-09-12 06:52:06 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-23 17:01:45 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
|
|
const mockCancelablePromise = () => new PCancelable<IRun>((resolve) => resolve());
|
|
|
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
|
|
const mockDeferredPromise = () => createDeferredPromise<IExecuteResponsePromiseData>();
|