mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 19:32:15 +00:00
fix: Run evaluations loop manually always from first row (#15794)
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { mock } from 'vitest-mock-extended';
|
||||
import { continueEvaluationLoop, type SimplifiedExecution } from './executionFinished';
|
||||
import type { ITaskData } from 'n8n-workflow';
|
||||
import { EVALUATION_TRIGGER_NODE_TYPE } from 'n8n-workflow';
|
||||
import type { INodeUi } from '@/Interface';
|
||||
import type { Router } from 'vue-router';
|
||||
|
||||
const runWorkflow = vi.fn();
|
||||
|
||||
vi.mock('@/composables/useRunWorkflow', () => ({
|
||||
useRunWorkflow: vi.fn(() => ({
|
||||
runWorkflow,
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('continueEvaluationLoop()', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should call runWorkflow() if workflow has eval trigger that executed successfully with rows left', () => {
|
||||
const evalTriggerNodeName = 'eval-trigger';
|
||||
const evalTriggerNodeData = mock<ITaskData>({
|
||||
data: {
|
||||
main: [
|
||||
[
|
||||
{
|
||||
json: {
|
||||
row_number: 1,
|
||||
_rowsLeft: 1,
|
||||
header1: 'value1',
|
||||
},
|
||||
},
|
||||
],
|
||||
],
|
||||
},
|
||||
});
|
||||
const execution = mock<SimplifiedExecution>({
|
||||
status: 'success',
|
||||
workflowData: {
|
||||
nodes: [
|
||||
mock<INodeUi>({
|
||||
type: EVALUATION_TRIGGER_NODE_TYPE,
|
||||
name: evalTriggerNodeName,
|
||||
}),
|
||||
],
|
||||
},
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {
|
||||
[evalTriggerNodeName]: [evalTriggerNodeData],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
continueEvaluationLoop(execution, mock<Router>());
|
||||
|
||||
expect(runWorkflow).toHaveBeenCalledWith({
|
||||
triggerNode: evalTriggerNodeName,
|
||||
nodeData: evalTriggerNodeData,
|
||||
rerunTriggerNode: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('should not call runWorkflow() if workflow execution status is not success', () => {
|
||||
const execution = mock<SimplifiedExecution>({
|
||||
status: 'error',
|
||||
workflowData: {
|
||||
nodes: [
|
||||
mock<INodeUi>({
|
||||
type: EVALUATION_TRIGGER_NODE_TYPE,
|
||||
name: 'eval-trigger',
|
||||
}),
|
||||
],
|
||||
},
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
continueEvaluationLoop(execution, mock<Router>());
|
||||
|
||||
expect(runWorkflow).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call runWorkflow() if eval trigger node does not exist in workflow', () => {
|
||||
const execution = mock<SimplifiedExecution>({
|
||||
status: 'success',
|
||||
workflowData: {
|
||||
nodes: [],
|
||||
},
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
continueEvaluationLoop(execution, mock<Router>());
|
||||
|
||||
expect(runWorkflow).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call runWorkflow() if eval trigger node exists but has no run data', () => {
|
||||
const evalTriggerNodeName = 'eval-trigger';
|
||||
const execution = mock<SimplifiedExecution>({
|
||||
status: 'success',
|
||||
workflowData: {
|
||||
nodes: [
|
||||
mock<INodeUi>({
|
||||
type: EVALUATION_TRIGGER_NODE_TYPE,
|
||||
name: evalTriggerNodeName,
|
||||
}),
|
||||
],
|
||||
},
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
continueEvaluationLoop(execution, mock<Router>());
|
||||
|
||||
expect(runWorkflow).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call runWorkflow() if eval trigger node run data has no rows left', () => {
|
||||
const evalTriggerNodeName = 'eval-trigger';
|
||||
const evalTriggerNodeData = mock<ITaskData>({
|
||||
data: {
|
||||
main: [
|
||||
[
|
||||
{
|
||||
json: {
|
||||
row_number: 1,
|
||||
_rowsLeft: 0,
|
||||
header1: 'value1',
|
||||
},
|
||||
},
|
||||
],
|
||||
],
|
||||
},
|
||||
});
|
||||
const execution = mock<SimplifiedExecution>({
|
||||
status: 'success',
|
||||
workflowData: {
|
||||
nodes: [
|
||||
mock<INodeUi>({
|
||||
type: EVALUATION_TRIGGER_NODE_TYPE,
|
||||
name: evalTriggerNodeName,
|
||||
}),
|
||||
],
|
||||
},
|
||||
data: {
|
||||
resultData: {
|
||||
runData: {
|
||||
[evalTriggerNodeName]: [evalTriggerNodeData],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
continueEvaluationLoop(execution, mock<Router>());
|
||||
|
||||
expect(runWorkflow).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -25,6 +25,7 @@ import { getTriggerNodeServiceName } from '@/utils/nodeTypesUtils';
|
||||
import { useExternalHooks } from '@/composables/useExternalHooks';
|
||||
import { useNodeHelpers } from '@/composables/useNodeHelpers';
|
||||
import { useNodeTypesStore } from '@/stores/nodeTypes.store';
|
||||
import { useRunWorkflow } from '@/composables/useRunWorkflow';
|
||||
|
||||
export type SimplifiedExecution = Pick<
|
||||
IExecutionResponse,
|
||||
@@ -94,34 +95,6 @@ export async function executionFinished(
|
||||
}
|
||||
}
|
||||
|
||||
// Implicit looping: This will re-trigger the evaluation trigger if it exists on a successful execution of the workflow.
|
||||
if (execution.status === 'success' && execution.data?.startData?.destinationNode === undefined) {
|
||||
// check if we have an evaluation trigger in our workflow and whether it has any run data
|
||||
const evalTrigger = execution.workflowData.nodes.find(
|
||||
(node) => node.type === EVALUATION_TRIGGER_NODE_TYPE,
|
||||
);
|
||||
const triggerRunData = evalTrigger
|
||||
? execution?.data?.resultData?.runData[evalTrigger.name]
|
||||
: undefined;
|
||||
|
||||
if (evalTrigger && triggerRunData !== undefined) {
|
||||
const mainData = triggerRunData[0]?.data?.main[0];
|
||||
const rowsLeft = mainData ? (mainData[0]?.json?._rowsLeft as number) : 0;
|
||||
|
||||
if (rowsLeft && rowsLeft > 0) {
|
||||
// Find the button that belongs to the evaluation trigger, and click it.
|
||||
const testId = `execute-workflow-button-${evalTrigger.name}`;
|
||||
|
||||
setTimeout(() => {
|
||||
const button = Array.from(document.querySelectorAll('[data-test-id]')).filter((x) =>
|
||||
(x as HTMLElement)?.dataset?.testId?.startsWith(testId),
|
||||
)[0];
|
||||
(button as HTMLElement)?.click();
|
||||
}, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const runExecutionData = getRunExecutionData(execution);
|
||||
uiStore.setProcessingExecutionResults(false);
|
||||
|
||||
@@ -134,6 +107,47 @@ export async function executionFinished(
|
||||
}
|
||||
|
||||
setRunExecutionData(execution, runExecutionData);
|
||||
|
||||
continueEvaluationLoop(execution, options.router);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implicit looping: This will re-trigger the evaluation trigger if it exists on a successful execution of the workflow.
|
||||
* @param execution
|
||||
* @param router
|
||||
*/
|
||||
export function continueEvaluationLoop(
|
||||
execution: SimplifiedExecution,
|
||||
router: ReturnType<typeof useRouter>,
|
||||
) {
|
||||
if (execution.status !== 'success' || execution.data?.startData?.destinationNode !== undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
// check if we have an evaluation trigger in our workflow and whether it has any run data
|
||||
const evaluationTrigger = execution.workflowData.nodes.find(
|
||||
(node) => node.type === EVALUATION_TRIGGER_NODE_TYPE,
|
||||
);
|
||||
const triggerRunData = evaluationTrigger
|
||||
? execution?.data?.resultData?.runData[evaluationTrigger.name]
|
||||
: undefined;
|
||||
|
||||
if (!evaluationTrigger || triggerRunData === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mainData = triggerRunData[0]?.data?.main[0];
|
||||
const rowsLeft = mainData ? (mainData[0]?.json?._rowsLeft as number) : 0;
|
||||
|
||||
if (rowsLeft && rowsLeft > 0) {
|
||||
const { runWorkflow } = useRunWorkflow({ router });
|
||||
void runWorkflow({
|
||||
triggerNode: evaluationTrigger.name,
|
||||
// pass output of previous node run to trigger next run
|
||||
nodeData: triggerRunData[0],
|
||||
rerunTriggerNode: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -511,6 +511,77 @@ describe('useRunWorkflow({ router })', () => {
|
||||
name: triggerNode,
|
||||
data: nodeData,
|
||||
},
|
||||
startNodes: [],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should retrigger workflow from child node if triggerNode and nodeData are passed in', async () => {
|
||||
// ARRANGE
|
||||
const composable = useRunWorkflow({ router });
|
||||
const triggerNode = 'Chat Trigger';
|
||||
const nodeData = mock<ITaskData>();
|
||||
vi.mocked(workflowHelpers).getCurrentWorkflow.mockReturnValue(
|
||||
mock<Workflow>({
|
||||
getChildNodes: vi.fn().mockReturnValue([{ name: 'Child node', type: 'nodes.child' }]),
|
||||
}),
|
||||
);
|
||||
vi.mocked(workflowHelpers).getWorkflowDataToSave.mockResolvedValue(
|
||||
mock<IWorkflowData>({ nodes: [] }),
|
||||
);
|
||||
|
||||
const { runWorkflow } = composable;
|
||||
|
||||
// ACT
|
||||
await runWorkflow({ triggerNode, nodeData });
|
||||
|
||||
// ASSERT
|
||||
expect(workflowsStore.runWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
triggerToStartFrom: {
|
||||
name: triggerNode,
|
||||
data: nodeData,
|
||||
},
|
||||
startNodes: [
|
||||
{
|
||||
name: {
|
||||
name: 'Child node',
|
||||
type: 'nodes.child',
|
||||
},
|
||||
sourceData: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should retrigger workflow from trigger node if rerunTriggerNode is set', async () => {
|
||||
// ARRANGE
|
||||
const composable = useRunWorkflow({ router });
|
||||
const triggerNode = 'Chat Trigger';
|
||||
const nodeData = mock<ITaskData>();
|
||||
vi.mocked(workflowHelpers).getCurrentWorkflow.mockReturnValue(
|
||||
mock<Workflow>({
|
||||
getChildNodes: vi.fn().mockReturnValue([{ name: 'Child node', type: 'nodes.child' }]),
|
||||
}),
|
||||
);
|
||||
vi.mocked(workflowHelpers).getWorkflowDataToSave.mockResolvedValue(
|
||||
mock<IWorkflowData>({ nodes: [] }),
|
||||
);
|
||||
|
||||
const { runWorkflow } = composable;
|
||||
|
||||
// ACT
|
||||
await runWorkflow({ triggerNode, nodeData, rerunTriggerNode: true });
|
||||
|
||||
// ASSERT
|
||||
expect(workflowsStore.runWorkflow).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
triggerToStartFrom: {
|
||||
name: triggerNode,
|
||||
data: nodeData,
|
||||
},
|
||||
startNodes: [],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -118,6 +118,7 @@ export function useRunWorkflow(useRunWorkflowOpts: { router: ReturnType<typeof u
|
||||
async function runWorkflow(options: {
|
||||
destinationNode?: string;
|
||||
triggerNode?: string;
|
||||
rerunTriggerNode?: boolean;
|
||||
nodeData?: ITaskData;
|
||||
source?: string;
|
||||
}): Promise<IExecutionPushResponse | undefined> {
|
||||
@@ -171,7 +172,8 @@ export function useRunWorkflow(useRunWorkflowOpts: { router: ReturnType<typeof u
|
||||
) {
|
||||
executedNode = options.destinationNode;
|
||||
startNodeNames.push(options.destinationNode);
|
||||
} else if (options.triggerNode && options.nodeData) {
|
||||
} else if (options.triggerNode && options.nodeData && !options.rerunTriggerNode) {
|
||||
// starts execution from downstream nodes of trigger node
|
||||
startNodeNames.push(
|
||||
...workflow.getChildNodes(options.triggerNode, NodeConnectionTypes.Main, 1),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user