Skip to content

Commit

Permalink
fix: Increment runIndex in WorkflowToolV2 tool executions to avoid re…
Browse files Browse the repository at this point in the history
…using out of date inputs (#13008)
  • Loading branch information
CharlieKolb authored and elsmr committed Feb 4, 2025
1 parent 25edde9 commit a4e45ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import type {

import { WorkflowToolService } from './utils/WorkflowToolService';

type ISupplyDataFunctionsWithRunIndex = ISupplyDataFunctions & { runIndex: number };

// Mock ISupplyDataFunctions interface
function createMockContext(overrides?: Partial<ISupplyDataFunctions>): ISupplyDataFunctions {
function createMockContext(
overrides?: Partial<ISupplyDataFunctions>,
): ISupplyDataFunctionsWithRunIndex {
return {
runIndex: 0,
getNodeParameter: jest.fn(),
getWorkflowDataProxy: jest.fn(),
getNode: jest.fn(),
Expand All @@ -35,11 +40,11 @@ function createMockContext(overrides?: Partial<ISupplyDataFunctions>): ISupplyDa
warn: jest.fn(),
},
...overrides,
} as ISupplyDataFunctions;
} as ISupplyDataFunctionsWithRunIndex;
}

describe('WorkflowTool::WorkflowToolService', () => {
let context: ISupplyDataFunctions;
let context: ISupplyDataFunctionsWithRunIndex;
let service: WorkflowToolService;

beforeEach(() => {
Expand Down Expand Up @@ -93,6 +98,7 @@ describe('WorkflowTool::WorkflowToolService', () => {

expect(result).toBe(JSON.stringify(TEST_RESPONSE, null, 2));
expect(context.addOutputData).toHaveBeenCalled();
expect(context.runIndex).toBe(1);
});

it('should handle errors during tool execution', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export class WorkflowToolService {
const errorResponse = `There was an error: "${executionError.message}"`;
void this.context.addOutputData(NodeConnectionType.AiTool, index, executionError);
return errorResponse;
} finally {
// @ts-expect-error this accesses a private member on the actual implementation to fix https://linear.app/n8n/issue/ADO-3186/bug-workflowtool-v2-always-uses-first-row-of-input-data
this.context.runIndex++;
}
};

Expand Down

0 comments on commit a4e45ac

Please sign in to comment.