mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor(core): Move execution engine code out of n8n-workflow (no-changelog) (#12147)
This commit is contained in:
committed by
GitHub
parent
73f0c4cca9
commit
5a055ed526
@@ -1,70 +1,16 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import get from 'lodash/get';
|
||||
import path from 'path';
|
||||
|
||||
import type {
|
||||
IExecuteSingleFunctions,
|
||||
IHttpRequestOptions,
|
||||
IN8nHttpFullResponse,
|
||||
IN8nHttpResponse,
|
||||
INode,
|
||||
INodeTypes,
|
||||
IRunExecutionData,
|
||||
} from '@/Interfaces';
|
||||
import type { Workflow } from '@/Workflow';
|
||||
import type { INodeTypes } from '@/Interfaces';
|
||||
|
||||
import { NodeTypes as NodeTypesClass } from './NodeTypes';
|
||||
|
||||
export function getExecuteSingleFunctions(
|
||||
workflow: Workflow,
|
||||
runExecutionData: IRunExecutionData,
|
||||
runIndex: number,
|
||||
node: INode,
|
||||
itemIndex: number,
|
||||
): IExecuteSingleFunctions {
|
||||
return mock<IExecuteSingleFunctions>({
|
||||
getItemIndex: () => itemIndex,
|
||||
getNodeParameter: (parameterName: string) => {
|
||||
return workflow.expression.getParameterValue(
|
||||
get(node.parameters, parameterName),
|
||||
runExecutionData,
|
||||
runIndex,
|
||||
itemIndex,
|
||||
node.name,
|
||||
[],
|
||||
'internal',
|
||||
{},
|
||||
);
|
||||
},
|
||||
getWorkflow: () => ({
|
||||
id: workflow.id,
|
||||
name: workflow.name,
|
||||
active: workflow.active,
|
||||
}),
|
||||
helpers: mock<IExecuteSingleFunctions['helpers']>({
|
||||
async httpRequest(
|
||||
requestOptions: IHttpRequestOptions,
|
||||
): Promise<IN8nHttpFullResponse | IN8nHttpResponse> {
|
||||
return {
|
||||
body: {
|
||||
headers: {},
|
||||
statusCode: 200,
|
||||
requestOptions,
|
||||
},
|
||||
};
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
let nodeTypesInstance: NodeTypesClass | undefined;
|
||||
|
||||
export function NodeTypes(): INodeTypes {
|
||||
if (nodeTypesInstance === undefined) {
|
||||
nodeTypesInstance = new NodeTypesClass();
|
||||
}
|
||||
|
||||
return nodeTypesInstance;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,8 @@ import {
|
||||
import {
|
||||
getNodeParameters,
|
||||
getNodeHints,
|
||||
isSingleExecution,
|
||||
isSubNodeType,
|
||||
applyDeclarativeNodeOptionParameters,
|
||||
convertNodeToAiTool,
|
||||
} from '@/NodeHelpers';
|
||||
import type { Workflow } from '@/Workflow';
|
||||
|
||||
@@ -3542,34 +3540,6 @@ describe('NodeHelpers', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSingleExecution', () => {
|
||||
test('should determine based on node parameters if it would be executed once', () => {
|
||||
expect(isSingleExecution('n8n-nodes-base.code', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.code', { mode: 'runOnceForEachItem' })).toEqual(
|
||||
false,
|
||||
);
|
||||
expect(isSingleExecution('n8n-nodes-base.executeWorkflow', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.executeWorkflow', { mode: 'each' })).toEqual(false);
|
||||
expect(isSingleExecution('n8n-nodes-base.crateDb', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.crateDb', { operation: 'update' })).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.timescaleDb', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.timescaleDb', { operation: 'update' })).toEqual(
|
||||
true,
|
||||
);
|
||||
expect(isSingleExecution('n8n-nodes-base.microsoftSql', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.microsoftSql', { operation: 'update' })).toEqual(
|
||||
true,
|
||||
);
|
||||
expect(isSingleExecution('n8n-nodes-base.microsoftSql', { operation: 'delete' })).toEqual(
|
||||
true,
|
||||
);
|
||||
expect(isSingleExecution('n8n-nodes-base.questDb', {})).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.mongoDb', { operation: 'insert' })).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.mongoDb', { operation: 'update' })).toEqual(true);
|
||||
expect(isSingleExecution('n8n-nodes-base.redis', {})).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSubNodeType', () => {
|
||||
const tests: Array<[boolean, Pick<INodeTypeDescription, 'outputs'> | null]> = [
|
||||
[false, null],
|
||||
@@ -3637,177 +3607,4 @@ describe('NodeHelpers', () => {
|
||||
expect(nodeType.description.properties).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertNodeToAiTool', () => {
|
||||
let fullNodeWrapper: { description: INodeTypeDescription };
|
||||
|
||||
beforeEach(() => {
|
||||
fullNodeWrapper = {
|
||||
description: {
|
||||
displayName: 'Test Node',
|
||||
name: 'testNode',
|
||||
group: ['test'],
|
||||
description: 'A test node',
|
||||
version: 1,
|
||||
defaults: {},
|
||||
inputs: [NodeConnectionType.Main],
|
||||
outputs: [NodeConnectionType.Main],
|
||||
properties: [],
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it('should modify the name and displayName correctly', () => {
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.name).toBe('testNodeTool');
|
||||
expect(result.description.displayName).toBe('Test Node Tool');
|
||||
});
|
||||
|
||||
it('should update inputs and outputs', () => {
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.inputs).toEqual([]);
|
||||
expect(result.description.outputs).toEqual([NodeConnectionType.AiTool]);
|
||||
});
|
||||
|
||||
it('should remove the usableAsTool property', () => {
|
||||
fullNodeWrapper.description.usableAsTool = true;
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.usableAsTool).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should add toolDescription property if it doesn't exist", () => {
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
const toolDescriptionProp = result.description.properties.find(
|
||||
(prop) => prop.name === 'toolDescription',
|
||||
);
|
||||
expect(toolDescriptionProp).toBeDefined();
|
||||
expect(toolDescriptionProp?.type).toBe('string');
|
||||
expect(toolDescriptionProp?.default).toBe(fullNodeWrapper.description.description);
|
||||
});
|
||||
|
||||
it('should set codex categories correctly', () => {
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.codex).toEqual({
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Tools'],
|
||||
Tools: ['Other Tools'],
|
||||
},
|
||||
resources: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('should preserve existing properties', () => {
|
||||
const existingProp: INodeProperties = {
|
||||
displayName: 'Existing Prop',
|
||||
name: 'existingProp',
|
||||
type: 'string',
|
||||
default: 'test',
|
||||
};
|
||||
fullNodeWrapper.description.properties = [existingProp];
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.properties).toHaveLength(3); // Existing prop + toolDescription + notice
|
||||
expect(result.description.properties).toContainEqual(existingProp);
|
||||
});
|
||||
|
||||
it('should handle nodes with resource property', () => {
|
||||
const resourceProp: INodeProperties = {
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [{ name: 'User', value: 'user' }],
|
||||
default: 'user',
|
||||
};
|
||||
fullNodeWrapper.description.properties = [resourceProp];
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.properties[1].name).toBe('descriptionType');
|
||||
expect(result.description.properties[2].name).toBe('toolDescription');
|
||||
expect(result.description.properties[3]).toEqual(resourceProp);
|
||||
});
|
||||
|
||||
it('should handle nodes with operation property', () => {
|
||||
const operationProp: INodeProperties = {
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
options: [{ name: 'Create', value: 'create' }],
|
||||
default: 'create',
|
||||
};
|
||||
fullNodeWrapper.description.properties = [operationProp];
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.properties[1].name).toBe('descriptionType');
|
||||
expect(result.description.properties[2].name).toBe('toolDescription');
|
||||
expect(result.description.properties[3]).toEqual(operationProp);
|
||||
});
|
||||
|
||||
it('should handle nodes with both resource and operation properties', () => {
|
||||
const resourceProp: INodeProperties = {
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [{ name: 'User', value: 'user' }],
|
||||
default: 'user',
|
||||
};
|
||||
const operationProp: INodeProperties = {
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
options: [{ name: 'Create', value: 'create' }],
|
||||
default: 'create',
|
||||
};
|
||||
fullNodeWrapper.description.properties = [resourceProp, operationProp];
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.properties[1].name).toBe('descriptionType');
|
||||
expect(result.description.properties[2].name).toBe('toolDescription');
|
||||
expect(result.description.properties[3]).toEqual(resourceProp);
|
||||
expect(result.description.properties[4]).toEqual(operationProp);
|
||||
});
|
||||
|
||||
it('should handle nodes with empty properties', () => {
|
||||
fullNodeWrapper.description.properties = [];
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.properties).toHaveLength(2);
|
||||
expect(result.description.properties[1].name).toBe('toolDescription');
|
||||
});
|
||||
|
||||
it('should handle nodes with existing codex property', () => {
|
||||
fullNodeWrapper.description.codex = {
|
||||
categories: ['Existing'],
|
||||
subcategories: {
|
||||
Existing: ['Category'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [{ url: 'https://example.com' }],
|
||||
},
|
||||
};
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.codex).toEqual({
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Tools'],
|
||||
Tools: ['Other Tools'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [{ url: 'https://example.com' }],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle nodes with very long names', () => {
|
||||
fullNodeWrapper.description.name = 'veryLongNodeNameThatExceedsNormalLimits'.repeat(10);
|
||||
fullNodeWrapper.description.displayName =
|
||||
'Very Long Node Name That Exceeds Normal Limits'.repeat(10);
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.name.endsWith('Tool')).toBe(true);
|
||||
expect(result.description.displayName.endsWith('Tool')).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle nodes with special characters in name and displayName', () => {
|
||||
fullNodeWrapper.description.name = 'special@#$%Node';
|
||||
fullNodeWrapper.description.displayName = 'Special @#$% Node';
|
||||
const result = convertNodeToAiTool(fullNodeWrapper);
|
||||
expect(result.description.name).toBe('special@#$%NodeTool');
|
||||
expect(result.description.displayName).toBe('Special @#$% Node Tool');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -6,22 +6,14 @@ import type {
|
||||
IBinaryKeyData,
|
||||
IConnections,
|
||||
IDataObject,
|
||||
IExecuteData,
|
||||
INode,
|
||||
INodeExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeParameters,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
INodeTypes,
|
||||
IRunExecutionData,
|
||||
ITriggerFunctions,
|
||||
ITriggerResponse,
|
||||
IWorkflowExecuteAdditionalData,
|
||||
NodeParameterValueType,
|
||||
} from '@/Interfaces';
|
||||
import * as NodeHelpers from '@/NodeHelpers';
|
||||
import { Workflow, type WorkflowParameters } from '@/Workflow';
|
||||
import { Workflow } from '@/Workflow';
|
||||
|
||||
process.env.TEST_VARIABLE_1 = 'valueEnvVariable1';
|
||||
|
||||
@@ -33,126 +25,6 @@ interface StubNode {
|
||||
}
|
||||
|
||||
describe('Workflow', () => {
|
||||
describe('checkIfWorkflowCanBeActivated', () => {
|
||||
const disabledNode = mock<INode>({ type: 'triggerNode', disabled: true });
|
||||
const unknownNode = mock<INode>({ type: 'unknownNode' });
|
||||
const noTriggersNode = mock<INode>({ type: 'noTriggersNode' });
|
||||
const pollNode = mock<INode>({ type: 'pollNode' });
|
||||
const triggerNode = mock<INode>({ type: 'triggerNode' });
|
||||
const webhookNode = mock<INode>({ type: 'webhookNode' });
|
||||
|
||||
const nodeTypes = mock<INodeTypes>();
|
||||
nodeTypes.getByNameAndVersion.mockImplementation((type) => {
|
||||
// TODO: getByNameAndVersion signature needs to be updated to allow returning undefined
|
||||
if (type === 'unknownNode') return undefined as unknown as INodeType;
|
||||
const partial: Partial<INodeType> = {
|
||||
poll: undefined,
|
||||
trigger: undefined,
|
||||
webhook: undefined,
|
||||
description: mock<INodeTypeDescription>({
|
||||
properties: [],
|
||||
}),
|
||||
};
|
||||
if (type === 'pollNode') partial.poll = jest.fn();
|
||||
if (type === 'triggerNode') partial.trigger = jest.fn();
|
||||
if (type === 'webhookNode') partial.webhook = jest.fn();
|
||||
return mock(partial);
|
||||
});
|
||||
|
||||
test.each([
|
||||
['should skip disabled nodes', disabledNode, [], false],
|
||||
['should skip nodes marked as ignored', triggerNode, ['triggerNode'], false],
|
||||
['should skip unknown nodes', unknownNode, [], false],
|
||||
['should skip nodes with no trigger method', noTriggersNode, [], false],
|
||||
['should activate if poll method exists', pollNode, [], true],
|
||||
['should activate if trigger method exists', triggerNode, [], true],
|
||||
['should activate if webhook method exists', webhookNode, [], true],
|
||||
])('%s', async (_, node, ignoredNodes, expected) => {
|
||||
const params = mock<WorkflowParameters>({ nodeTypes });
|
||||
params.nodes = [node];
|
||||
const workflow = new Workflow(params);
|
||||
expect(workflow.checkIfWorkflowCanBeActivated(ignoredNodes)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkReadyForExecution', () => {
|
||||
const disabledNode = mock<INode>({ name: 'Disabled Node', disabled: true });
|
||||
const startNode = mock<INode>({ name: 'Start Node' });
|
||||
const unknownNode = mock<INode>({ name: 'Unknown Node', type: 'unknownNode' });
|
||||
|
||||
const nodeParamIssuesSpy = jest.spyOn(NodeHelpers, 'getNodeParametersIssues');
|
||||
|
||||
const nodeTypes = mock<INodeTypes>();
|
||||
nodeTypes.getByNameAndVersion.mockImplementation((type) => {
|
||||
// TODO: getByNameAndVersion signature needs to be updated to allow returning undefined
|
||||
if (type === 'unknownNode') return undefined as unknown as INodeType;
|
||||
return mock<INodeType>({
|
||||
description: {
|
||||
properties: [],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
it('should return null if there are no nodes', () => {
|
||||
const workflow = new Workflow({
|
||||
nodes: [],
|
||||
connections: {},
|
||||
active: false,
|
||||
nodeTypes,
|
||||
});
|
||||
|
||||
const issues = workflow.checkReadyForExecution();
|
||||
expect(issues).toBe(null);
|
||||
expect(nodeTypes.getByNameAndVersion).not.toHaveBeenCalled();
|
||||
expect(nodeParamIssuesSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return null if there are no enabled nodes', () => {
|
||||
const workflow = new Workflow({
|
||||
nodes: [disabledNode],
|
||||
connections: {},
|
||||
active: false,
|
||||
nodeTypes,
|
||||
});
|
||||
|
||||
const issues = workflow.checkReadyForExecution({ startNode: disabledNode.name });
|
||||
expect(issues).toBe(null);
|
||||
expect(nodeTypes.getByNameAndVersion).toHaveBeenCalledTimes(1);
|
||||
expect(nodeParamIssuesSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return typeUnknown for unknown nodes', () => {
|
||||
const workflow = new Workflow({
|
||||
nodes: [unknownNode],
|
||||
connections: {},
|
||||
active: false,
|
||||
nodeTypes,
|
||||
});
|
||||
|
||||
const issues = workflow.checkReadyForExecution({ startNode: unknownNode.name });
|
||||
expect(issues).toEqual({ [unknownNode.name]: { typeUnknown: true } });
|
||||
expect(nodeTypes.getByNameAndVersion).toHaveBeenCalledTimes(2);
|
||||
expect(nodeParamIssuesSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return issues for regular nodes', () => {
|
||||
const workflow = new Workflow({
|
||||
nodes: [startNode],
|
||||
connections: {},
|
||||
active: false,
|
||||
nodeTypes,
|
||||
});
|
||||
nodeParamIssuesSpy.mockReturnValue({ execution: false });
|
||||
|
||||
const issues = workflow.checkReadyForExecution({ startNode: startNode.name });
|
||||
expect(issues).toEqual({ [startNode.name]: { execution: false } });
|
||||
expect(nodeTypes.getByNameAndVersion).toHaveBeenCalledTimes(2);
|
||||
expect(nodeParamIssuesSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('renameNodeInParameterValue', () => {
|
||||
describe('for expressions', () => {
|
||||
const tests = [
|
||||
@@ -2023,69 +1895,6 @@ describe('Workflow', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('runNode', () => {
|
||||
const nodeTypes = mock<INodeTypes>();
|
||||
const triggerNode = mock<INode>();
|
||||
const triggerResponse = mock<ITriggerResponse>({
|
||||
closeFunction: jest.fn(),
|
||||
// This node should never trigger, or return
|
||||
manualTriggerFunction: async () => await new Promise(() => {}),
|
||||
});
|
||||
const triggerNodeType = mock<INodeType>({
|
||||
description: {
|
||||
properties: [],
|
||||
},
|
||||
execute: undefined,
|
||||
poll: undefined,
|
||||
webhook: undefined,
|
||||
async trigger(this: ITriggerFunctions) {
|
||||
return triggerResponse;
|
||||
},
|
||||
});
|
||||
|
||||
nodeTypes.getByNameAndVersion.mockReturnValue(triggerNodeType);
|
||||
|
||||
const workflow = new Workflow({
|
||||
nodeTypes,
|
||||
nodes: [triggerNode],
|
||||
connections: {},
|
||||
active: false,
|
||||
});
|
||||
|
||||
const executionData = mock<IExecuteData>();
|
||||
const runExecutionData = mock<IRunExecutionData>();
|
||||
const additionalData = mock<IWorkflowExecuteAdditionalData>();
|
||||
const nodeExecuteFunctions = mock<INodeExecuteFunctions>();
|
||||
const triggerFunctions = mock<ITriggerFunctions>();
|
||||
nodeExecuteFunctions.getExecuteTriggerFunctions.mockReturnValue(triggerFunctions);
|
||||
const abortController = new AbortController();
|
||||
|
||||
test('should call closeFunction when manual trigger is aborted', async () => {
|
||||
const runPromise = workflow.runNode(
|
||||
executionData,
|
||||
runExecutionData,
|
||||
0,
|
||||
additionalData,
|
||||
nodeExecuteFunctions,
|
||||
'manual',
|
||||
abortController.signal,
|
||||
);
|
||||
// Yield back to the event-loop to let async parts of `runNode` execute
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
let isSettled = false;
|
||||
void runPromise.then(() => {
|
||||
isSettled = true;
|
||||
});
|
||||
expect(isSettled).toBe(false);
|
||||
expect(abortController.signal.aborted).toBe(false);
|
||||
expect(triggerResponse.closeFunction).not.toHaveBeenCalled();
|
||||
|
||||
abortController.abort();
|
||||
expect(triggerResponse.closeFunction).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('__getConnectionsByDestination', () => {
|
||||
it('should return empty object when there are no connections', () => {
|
||||
const workflow = new Workflow({
|
||||
|
||||
Reference in New Issue
Block a user