diff --git a/src/store/utils/__tests__/connectedInputs.test.ts b/src/store/utils/__tests__/connectedInputs.test.ts index 85425187..85c30c98 100644 --- a/src/store/utils/__tests__/connectedInputs.test.ts +++ b/src/store/utils/__tests__/connectedInputs.test.ts @@ -140,6 +140,60 @@ describe("getConnectedInputsPure", () => { expect(result.text).toBe("three"); }); + it("should wrap out-of-bounds arrayItemIndex via modulo", () => { + const nodes = [ + makeNode("arr", "array", { outputText: "[\"one\",\"two\"]", outputItems: ["one", "two"] }), + makeNode("gen", "nanoBanana"), + ]; + const edges = [{ + id: "arr-gen-stale", + source: "arr", + target: "gen", + sourceHandle: "text", + targetHandle: "text", + data: { arrayItemIndex: 3 }, // index 3 on 2-item array wraps to index 1 + }] as WorkflowEdge[]; + + const result = getConnectedInputsPure("gen", nodes, edges); + expect(result.text).toBe("two"); + }); + + it("should return null for out-of-bounds arrayItemIndex on empty array", () => { + const nodes = [ + makeNode("arr", "array", { outputText: "[]", outputItems: [] }), + makeNode("gen", "nanoBanana"), + ]; + const edges = [{ + id: "arr-gen-empty", + source: "arr", + target: "gen", + sourceHandle: "text", + targetHandle: "text", + data: { arrayItemIndex: 0 }, + }] as WorkflowEdge[]; + + const result = getConnectedInputsPure("gen", nodes, edges); + expect(result.text).toBeNull(); + }); + + it("should wrap large arrayItemIndex correctly", () => { + const nodes = [ + makeNode("arr", "array", { outputText: "[\"a\",\"b\",\"c\"]", outputItems: ["a", "b", "c"] }), + makeNode("gen", "nanoBanana"), + ]; + const edges = [{ + id: "arr-gen-large", + source: "arr", + target: "gen", + sourceHandle: "text", + targetHandle: "text", + data: { arrayItemIndex: 7 }, // 7 % 3 = 1 + }] as WorkflowEdge[]; + + const result = getConnectedInputsPure("gen", nodes, edges); + expect(result.text).toBe("b"); + }); + it("should extract text from promptConstructor outputText", () => { const nodes = [ makeNode("pc", "promptConstructor", { outputText: "constructed", template: "tmpl" }), diff --git a/src/store/utils/connectedInputs.ts b/src/store/utils/connectedInputs.ts index bd7c0917..959a3051 100644 --- a/src/store/utils/connectedInputs.ts +++ b/src/store/utils/connectedInputs.ts @@ -91,7 +91,10 @@ function getSourceOutput( const arrayData = sourceNode.data as ArrayNodeData; const dataIndex = edgeData?.arrayItemIndex; if (typeof dataIndex === "number" && Number.isInteger(dataIndex) && dataIndex >= 0) { - return { type: "text", value: arrayData.outputItems[dataIndex] ?? null }; + const items = arrayData.outputItems; + if (items.length === 0) return { type: "text", value: null }; + const clampedIndex = dataIndex % items.length; + return { type: "text", value: items[clampedIndex] ?? null }; } if (sourceHandle?.startsWith("text-")) { const index = Number(sourceHandle.replace("text-", ""));