2023-06-21 00:38:28 -07:00
|
|
|
import type { IRun, WorkflowTestData } from 'n8n-workflow';
|
2024-05-13 05:46:02 -07:00
|
|
|
import {
|
|
|
|
ApplicationError,
|
|
|
|
createDeferredPromise,
|
|
|
|
NodeExecutionOutput,
|
|
|
|
Workflow,
|
|
|
|
} from 'n8n-workflow';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { WorkflowExecute } from '@/WorkflowExecute';
|
2019-07-26 01:21:31 -07:00
|
|
|
|
2023-06-21 00:38:28 -07:00
|
|
|
import * as Helpers from './helpers';
|
2023-07-05 09:47:34 -07:00
|
|
|
import { legacyWorkflowExecuteTests, v1WorkflowExecuteTests } from './helpers/constants';
|
2019-07-26 01:21:31 -07:00
|
|
|
|
|
|
|
describe('WorkflowExecute', () => {
|
2023-07-05 09:47:34 -07:00
|
|
|
describe('v0 execution order', () => {
|
|
|
|
const tests: WorkflowTestData[] = legacyWorkflowExecuteTests;
|
2019-07-26 01:21:31 -07:00
|
|
|
|
|
|
|
const executionMode = 'manual';
|
|
|
|
const nodeTypes = Helpers.NodeTypes();
|
|
|
|
|
|
|
|
for (const testData of tests) {
|
|
|
|
test(testData.description, async () => {
|
2020-02-16 19:06:51 -08:00
|
|
|
const workflowInstance = new Workflow({
|
|
|
|
id: 'test',
|
|
|
|
nodes: testData.input.workflowData.nodes,
|
|
|
|
connections: testData.input.workflowData.connections,
|
|
|
|
active: false,
|
|
|
|
nodeTypes,
|
2023-07-05 09:47:34 -07:00
|
|
|
settings: {
|
|
|
|
executionOrder: 'v0',
|
|
|
|
},
|
2020-02-16 19:06:51 -08:00
|
|
|
});
|
2019-07-26 01:21:31 -07:00
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const waitPromise = createDeferredPromise<IRun>();
|
2019-07-26 01:21:31 -07:00
|
|
|
const nodeExecutionOrder: string[] = [];
|
|
|
|
const additionalData = Helpers.WorkflowExecuteAdditionalData(
|
|
|
|
waitPromise,
|
|
|
|
nodeExecutionOrder,
|
|
|
|
);
|
|
|
|
|
|
|
|
const workflowExecute = new WorkflowExecute(additionalData, executionMode);
|
|
|
|
|
2021-08-20 09:08:40 -07:00
|
|
|
const executionData = await workflowExecute.run(workflowInstance);
|
2019-07-26 01:21:31 -07:00
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const result = await waitPromise.promise;
|
2019-07-26 01:21:31 -07:00
|
|
|
|
2019-08-09 03:19:28 -07:00
|
|
|
// Check if the data from WorkflowExecute is identical to data received
|
|
|
|
// by the webhooks
|
|
|
|
expect(executionData).toEqual(result);
|
|
|
|
|
2019-07-26 01:21:31 -07:00
|
|
|
// Check if the output data of the nodes is correct
|
|
|
|
for (const nodeName of Object.keys(testData.output.nodeData)) {
|
|
|
|
if (result.data.resultData.runData[nodeName] === undefined) {
|
2023-11-30 00:06:19 -08:00
|
|
|
throw new ApplicationError('Data for node is missing', { extra: { nodeName } });
|
2019-07-26 01:21:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const resultData = result.data.resultData.runData[nodeName].map((nodeData) => {
|
|
|
|
if (nodeData.data === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return nodeData.data.main[0]!.map((entry) => entry.json);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(resultData).toEqual(testData.output.nodeData[nodeName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the nodes did execute in the correct order
|
|
|
|
expect(nodeExecutionOrder).toEqual(testData.output.nodeExecutionOrder);
|
|
|
|
|
|
|
|
// Check if other data has correct value
|
|
|
|
expect(result.finished).toEqual(true);
|
|
|
|
expect(result.data.executionData!.contextData).toEqual({});
|
|
|
|
expect(result.data.executionData!.nodeExecutionStack).toEqual([]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-06-21 00:38:28 -07:00
|
|
|
|
2023-07-05 09:47:34 -07:00
|
|
|
describe('v1 execution order', () => {
|
|
|
|
const tests: WorkflowTestData[] = v1WorkflowExecuteTests;
|
|
|
|
|
|
|
|
const executionMode = 'manual';
|
|
|
|
const nodeTypes = Helpers.NodeTypes();
|
|
|
|
|
|
|
|
for (const testData of tests) {
|
|
|
|
test(testData.description, async () => {
|
|
|
|
const workflowInstance = new Workflow({
|
|
|
|
id: 'test',
|
|
|
|
nodes: testData.input.workflowData.nodes,
|
|
|
|
connections: testData.input.workflowData.connections,
|
|
|
|
active: false,
|
|
|
|
nodeTypes,
|
|
|
|
settings: {
|
|
|
|
executionOrder: 'v1',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const waitPromise = createDeferredPromise<IRun>();
|
2023-07-05 09:47:34 -07:00
|
|
|
const nodeExecutionOrder: string[] = [];
|
|
|
|
const additionalData = Helpers.WorkflowExecuteAdditionalData(
|
|
|
|
waitPromise,
|
|
|
|
nodeExecutionOrder,
|
|
|
|
);
|
|
|
|
|
|
|
|
const workflowExecute = new WorkflowExecute(additionalData, executionMode);
|
|
|
|
|
|
|
|
const executionData = await workflowExecute.run(workflowInstance);
|
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const result = await waitPromise.promise;
|
2023-07-05 09:47:34 -07:00
|
|
|
|
|
|
|
// Check if the data from WorkflowExecute is identical to data received
|
|
|
|
// by the webhooks
|
|
|
|
expect(executionData).toEqual(result);
|
|
|
|
|
|
|
|
// Check if the output data of the nodes is correct
|
|
|
|
for (const nodeName of Object.keys(testData.output.nodeData)) {
|
|
|
|
if (result.data.resultData.runData[nodeName] === undefined) {
|
2023-11-30 00:06:19 -08:00
|
|
|
throw new ApplicationError('Data for node is missing', { extra: { nodeName } });
|
2023-07-05 09:47:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const resultData = result.data.resultData.runData[nodeName].map((nodeData) => {
|
|
|
|
if (nodeData.data === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-07-29 08:08:20 -07:00
|
|
|
const toMap = testData.output.testAllOutputs
|
|
|
|
? nodeData.data.main
|
|
|
|
: [nodeData.data.main[0]!];
|
|
|
|
return toMap.map((data) => data!.map((entry) => entry.json));
|
2023-07-05 09:47:34 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// expect(resultData).toEqual(testData.output.nodeData[nodeName]);
|
|
|
|
expect(resultData).toEqual(testData.output.nodeData[nodeName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the nodes did execute in the correct order
|
|
|
|
expect(nodeExecutionOrder).toEqual(testData.output.nodeExecutionOrder);
|
|
|
|
|
|
|
|
// Check if other data has correct value
|
|
|
|
expect(result.finished).toEqual(true);
|
|
|
|
expect(result.data.executionData!.contextData).toEqual({});
|
|
|
|
expect(result.data.executionData!.nodeExecutionStack).toEqual([]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-21 00:38:28 -07:00
|
|
|
//run tests on json files from specified directory, default 'workflows'
|
|
|
|
//workflows must have pinned data that would be used to test output after execution
|
|
|
|
describe('run test workflows', () => {
|
|
|
|
const tests: WorkflowTestData[] = Helpers.workflowToTests(__dirname);
|
|
|
|
|
|
|
|
const executionMode = 'manual';
|
|
|
|
const nodeTypes = Helpers.NodeTypes(Helpers.getNodeTypes(tests));
|
|
|
|
|
|
|
|
for (const testData of tests) {
|
|
|
|
test(testData.description, async () => {
|
|
|
|
const workflowInstance = new Workflow({
|
|
|
|
id: 'test',
|
|
|
|
nodes: testData.input.workflowData.nodes,
|
|
|
|
connections: testData.input.workflowData.connections,
|
|
|
|
active: false,
|
|
|
|
nodeTypes,
|
2023-07-05 09:47:34 -07:00
|
|
|
settings: testData.input.workflowData.settings,
|
2023-06-21 00:38:28 -07:00
|
|
|
});
|
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const waitPromise = createDeferredPromise<IRun>();
|
2023-06-21 00:38:28 -07:00
|
|
|
const nodeExecutionOrder: string[] = [];
|
|
|
|
const additionalData = Helpers.WorkflowExecuteAdditionalData(
|
|
|
|
waitPromise,
|
|
|
|
nodeExecutionOrder,
|
|
|
|
);
|
|
|
|
|
|
|
|
const workflowExecute = new WorkflowExecute(additionalData, executionMode);
|
|
|
|
|
|
|
|
const executionData = await workflowExecute.run(workflowInstance);
|
|
|
|
|
2024-09-13 06:53:03 -07:00
|
|
|
const result = await waitPromise.promise;
|
2023-06-21 00:38:28 -07:00
|
|
|
|
|
|
|
// Check if the data from WorkflowExecute is identical to data received
|
|
|
|
// by the webhooks
|
|
|
|
expect(executionData).toEqual(result);
|
|
|
|
|
|
|
|
// Check if the output data of the nodes is correct
|
|
|
|
for (const nodeName of Object.keys(testData.output.nodeData)) {
|
|
|
|
if (result.data.resultData.runData[nodeName] === undefined) {
|
2023-11-30 00:06:19 -08:00
|
|
|
throw new ApplicationError('Data for node is missing', { extra: { nodeName } });
|
2023-06-21 00:38:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const resultData = result.data.resultData.runData[nodeName].map((nodeData) => {
|
|
|
|
if (nodeData.data === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return nodeData.data.main[0];
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(resultData).toEqual(testData.output.nodeData[nodeName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if other data has correct value
|
|
|
|
expect(result.finished).toEqual(true);
|
|
|
|
// expect(result.data.executionData!.contextData).toEqual({}); //Fails when test workflow Includes splitInbatches
|
|
|
|
expect(result.data.executionData!.nodeExecutionStack).toEqual([]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-05-13 05:46:02 -07:00
|
|
|
|
|
|
|
describe('WorkflowExecute, NodeExecutionOutput type test', () => {
|
|
|
|
//TODO Add more tests here when execution hints are added to some node types
|
|
|
|
const nodeExecutionOutput = new NodeExecutionOutput(
|
|
|
|
[[{ json: { data: 123 } }]],
|
|
|
|
[{ message: 'TEXT HINT' }],
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(nodeExecutionOutput).toBeInstanceOf(NodeExecutionOutput);
|
|
|
|
expect(nodeExecutionOutput[0][0].json.data).toEqual(123);
|
|
|
|
expect(nodeExecutionOutput.getHints()[0].message).toEqual('TEXT HINT');
|
|
|
|
});
|
2019-07-26 01:21:31 -07:00
|
|
|
});
|