Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

test(frontend): add tool-call detection + session ID validation tests; fix EMPTY_NODES ref

Zamil Majdy2f3b29f5891b
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
index 1f5e23e4c0..dc7b9cba37 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
@@ -205,6 +205,32 @@ describe("useBuilderChatPanel – session lifecycle", () => {
expect(mockPostV2CreateSession).toHaveBeenCalledOnce();
expect(result.current.sessionId).toBe("sess-existing");
});
+
+ it("sets sessionError when session creation returns a path-traversal id (security validation)", async () => {
+ mockPostV2CreateSession.mockResolvedValue({
+ status: 200,
+ data: { id: "../../admin" },
+ });
+ const { result } = renderHook(() => useBuilderChatPanel());
+
+ await openAndFlush(() => result.current.handleToggle());
+
+ expect(result.current.sessionError).toBe(true);
+ expect(result.current.sessionId).toBeNull();
+ });
+
+ it("sets sessionError when session creation returns an id with spaces", async () => {
+ mockPostV2CreateSession.mockResolvedValue({
+ status: 200,
+ data: { id: "sess 1" },
+ });
+ const { result } = renderHook(() => useBuilderChatPanel());
+
+ await openAndFlush(() => result.current.handleToggle());
+
+ expect(result.current.sessionError).toBe(true);
+ expect(result.current.sessionId).toBeNull();
+ });
});
describe("useBuilderChatPanel – no auto-send on open", () => {
@@ -1278,3 +1304,108 @@ describe("useBuilderChatPanel – prototype pollution guard", () => {
);
});
});
+
+describe("useBuilderChatPanel – tool call detection", () => {
+ function makeDynamicToolPart(
+ toolName: string,
+ toolCallId: string,
+ state: string,
+ output: unknown = null,
+ ) {
+ return { type: "dynamic-tool", toolName, toolCallId, state, output };
+ }
+
+ it("calls onGraphEdited when edit_agent tool call completes", async () => {
+ mockChatStatus = "ready";
+ mockChatMessages = [
+ {
+ id: "m1",
+ role: "assistant",
+ parts: [
+ makeDynamicToolPart("edit_agent", "tc-1", "output-available", null),
+ ],
+ },
+ ];
+ const onGraphEdited = vi.fn();
+ renderHook(() => useBuilderChatPanel({ onGraphEdited }));
+
+ await act(async () => {
+ await new Promise<void>((r) => setTimeout(r, 0));
+ });
+
+ expect(onGraphEdited).toHaveBeenCalledOnce();
+ });
+
+ it("does NOT call onGraphEdited for a tool call that is not output-available", async () => {
+ mockChatStatus = "ready";
+ mockChatMessages = [
+ {
+ id: "m1",
+ role: "assistant",
+ parts: [
+ makeDynamicToolPart("edit_agent", "tc-pending", "pending", null),
+ ],
+ },
+ ];
+ const onGraphEdited = vi.fn();
+ renderHook(() => useBuilderChatPanel({ onGraphEdited }));
+
+ await act(async () => {
+ await new Promise<void>((r) => setTimeout(r, 0));
+ });
+
+ expect(onGraphEdited).not.toHaveBeenCalled();
+ });
+
+ it("does NOT call onGraphEdited when status is streaming", async () => {
+ mockChatStatus = "streaming";
+ mockChatMessages = [
+ {
+ id: "m1",
+ role: "assistant",
+ parts: [
+ makeDynamicToolPart(
+ "edit_agent",
+ "tc-stream",
+ "output-available",
+ null,
+ ),
+ ],
+ },
+ ];
+ const onGraphEdited = vi.fn();
+ renderHook(() => useBuilderChatPanel({ onGraphEdited }));
+
+ await act(async () => {
+ await new Promise<void>((r) => setTimeout(r, 0));
+ });
+
+ expect(onGraphEdited).not.toHaveBeenCalled();
+ });
+
+ it("does NOT process the same tool call twice (processedToolCallsRef deduplication)", async () => {
+ mockChatStatus = "ready";
+ const part = makeDynamicToolPart(
+ "edit_agent",
+ "tc-dedup",
+ "output-available",
+ null,
+ );
+ mockChatMessages = [{ id: "m1", role: "assistant", parts: [part] }];
+
+ const onGraphEdited = vi.fn();
+ const { rerender } = renderHook(() =>
+ useBuilderChatPanel({ onGraphEdited }),
+ );
+
+ await act(async () => {
+ await new Promise<void>((r) => setTimeout(r, 0));
+ });
+
+ expect(onGraphEdited).toHaveBeenCalledOnce();
+
+ act(() => rerender());
+
+ expect(onGraphEdited).toHaveBeenCalledOnce();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
index 0ba5542bce..d7685d5237 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
@@ -46,6 +46,9 @@ interface UndoSnapshot {
*/
const graphSessionCache = new Map<string, string>();
+/** Stable empty array so the useShallow selector returns the same reference when the panel is closed. */
+const EMPTY_NODES: never[] = [];
+
/** Clears the session cache. Exported only for use in tests. */
export function clearGraphSessionCacheForTesting() {
graphSessionCache.clear();
@@ -106,7 +109,9 @@ export function useBuilderChatPanel({
});
const { toast } = useToast();
- const nodes = useNodeStore(useShallow((s) => (isOpen ? s.nodes : [])));
+ const nodes = useNodeStore(
+ useShallow((s) => (isOpen ? s.nodes : EMPTY_NODES)),
+ );
const setNodes = useNodeStore((s) => s.setNodes);
const setEdges = useEdgeStore((s) => s.setEdges);