fix: loop context lost when max iterations are reached. Add/test cases for the same

This commit is contained in:
Naveen Kumar 2024-11-28 23:37:20 +05:30 committed by Naveen Kumar
parent ef5ecb565a
commit 6f8f866b7e
4 changed files with 45 additions and 15 deletions

View File

@ -152,6 +152,44 @@ describe("Loop automations", () => {
)
})
it("ensure the loop stops if the max iterations are reached", async () => {
const builder = createAutomationBuilder({
name: "Test Loop max iterations",
})
const results = await builder
.appAction({ fields: {} })
.loop({
option: LoopStepType.ARRAY,
binding: ["test", "test2", "test3"],
iterations: 2,
})
.serverLog({ text: "{{loop.currentItem}}" })
.serverLog({ text: "{{steps.1.iterations}}" })
.run()
expect(results.steps[1].outputs.iterations).toBe(2)
})
it("should run an automation with loop and max iterations to ensure context correctness further down the tree", async () => {
const builder = createAutomationBuilder({
name: "Test context down tree with Loop and max iterations",
})
const results = await builder
.appAction({ fields: {} })
.loop({
option: LoopStepType.ARRAY,
binding: ["test", "test2", "test3"],
iterations: 2,
})
.serverLog({ text: "{{loop.currentItem}}" })
.serverLog({ text: "{{steps.1.iterations}}" })
.run()
expect(results.steps[2].outputs.message).toContain("- 2")
})
it("should run an automation where a loop is successfully run twice", async () => {
const builder = createAutomationBuilder({
name: "Test Trigger with Loop and Create Row",

View File

@ -137,7 +137,6 @@ export enum InvalidColumns {
export enum AutomationErrors {
INCORRECT_TYPE = "INCORRECT_TYPE",
MAX_ITERATIONS = "MAX_ITERATIONS_REACHED",
FAILURE_CONDITION = "FAILURE_CONDITION_MET",
}

View File

@ -392,6 +392,7 @@ class Orchestrator {
let iterationCount = 0
let shouldCleanup = true
let reachedMaxIterations = false
for (let loopStepIndex = 0; loopStepIndex < iterations; loopStepIndex++) {
try {
@ -419,19 +420,8 @@ class Orchestrator {
loopStepIndex === env.AUTOMATION_MAX_ITERATIONS ||
(loopStep.inputs.iterations && loopStepIndex === maxIterations)
) {
this.updateContextAndOutput(
pathStepIdx + 1,
steps[stepToLoopIndex],
{
items: this.loopStepOutputs,
iterations: loopStepIndex,
},
{
status: AutomationErrors.MAX_ITERATIONS,
success: true,
}
)
shouldCleanup = false
reachedMaxIterations = true
shouldCleanup = true
break
}
@ -480,7 +470,9 @@ class Orchestrator {
success: true,
}
: {
success: true,
success: reachedMaxIterations
? AutomationStepStatus.MAX_ITERATIONS
: true,
items: this.loopStepOutputs,
iterations: iterationCount,
}

View File

@ -174,6 +174,7 @@ export enum AutomationFeature {
export enum AutomationStepStatus {
NO_ITERATIONS = "no_iterations",
MAX_ITERATIONS = "max_iterations_reached",
}
export enum AutomationStatus {