From 3a8a8b1195b3c8560b58d724bc98fb666a94549a Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Wed, 4 Sep 2024 14:54:47 +0100 Subject: [PATCH 1/3] fix issue with multiple loops breaking automation context --- .../tests/scenarios/scenarios.spec.ts | 26 +++++++++++++++++++ packages/server/src/threads/automation.ts | 8 ++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/server/src/automations/tests/scenarios/scenarios.spec.ts b/packages/server/src/automations/tests/scenarios/scenarios.spec.ts index a0dab7f177..a4fd473ac9 100644 --- a/packages/server/src/automations/tests/scenarios/scenarios.spec.ts +++ b/packages/server/src/automations/tests/scenarios/scenarios.spec.ts @@ -362,6 +362,32 @@ describe("Automation Scenarios", () => { } ) }) + + it("should run an automation where a loop is used twice to ensure context correctness further down the tree", async () => { + const builder = createAutomationBuilder({ + name: "Test Trigger with Loop and Create Row", + }) + + const results = await builder + .appAction({ fields: {} }) + .loop({ + option: LoopStepType.ARRAY, + binding: [1, 2, 3], + }) + .serverLog({ text: "Message {{loop.currentItem}}" }) + .serverLog({ text: "{{steps.1.iterations}}" }) + .loop({ + option: LoopStepType.ARRAY, + binding: [1, 2, 3], + }) + .serverLog({ text: "{{loop.currentItem}}" }) + .serverLog({ text: "{{steps.3.iterations}}" }) + .run() + + // We want to ensure that bindings are corr + expect(results.steps[1].outputs.message).toContain("- 3") + expect(results.steps[3].outputs.message).toContain("- 3") + }) }) describe("Row Automations", () => { diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index eff8407104..5e95ab9d89 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -449,7 +449,11 @@ class Orchestrator { outputs: tempOutput, inputs: steps[stepToLoopIndex].inputs, }) - this.context.steps[currentIndex + 1] = tempOutput + this.context.steps[this.context.steps.length] = tempOutput + this.context.steps = this.context.steps.filter( + item => !item.hasOwnProperty("currentItem") + ) + this.loopStepOutputs = [] } @@ -569,8 +573,8 @@ class Orchestrator { this.loopStepOutputs.push(outputs) } else { this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs) + this.context.steps[this.context.steps.length] = outputs } - this.context.steps[this.context.steps.length] = outputs } } From 5e4b2fa500ba012011a7d68a885aba2e015e5871 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Wed, 4 Sep 2024 15:13:11 +0100 Subject: [PATCH 2/3] use .call --- packages/server/src/threads/automation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 5e95ab9d89..668599bdcc 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -451,7 +451,7 @@ class Orchestrator { }) this.context.steps[this.context.steps.length] = tempOutput this.context.steps = this.context.steps.filter( - item => !item.hasOwnProperty("currentItem") + item => !item.hasOwnProperty.call("currentItem") ) this.loopStepOutputs = [] From 9782ddb9eeacf813a6ba9b58ff189315c1c377b7 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Wed, 4 Sep 2024 15:29:07 +0100 Subject: [PATCH 3/3] missing param --- packages/server/src/threads/automation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index 668599bdcc..478dade56f 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -451,7 +451,7 @@ class Orchestrator { }) this.context.steps[this.context.steps.length] = tempOutput this.context.steps = this.context.steps.filter( - item => !item.hasOwnProperty.call("currentItem") + item => !item.hasOwnProperty.call(item, "currentItem") ) this.loopStepOutputs = []