Skip to content

Automate browser based workflows with AI

Fail the run-timeline query when the API returns a non-array payload (SKY-13162) (#7827)

Aaron Perez0da06fe78f14
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.test.tsx b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.test.tsx
new file mode 100644
index 000000000..1870e78c0
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.test.tsx
@@ -0,0 +1,82 @@
+// @vitest-environment jsdom
+
+import type { ReactNode } from "react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { renderHook, waitFor } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+const { mockGet, mockGetClient } = vi.hoisted(() => ({
+ mockGet: vi.fn(),
+ mockGetClient: vi.fn(),
+}));
+
+vi.mock("@/api/AxiosClient", () => ({
+ getClient: mockGetClient,
+}));
+vi.mock("@/hooks/useCredentialGetter", () => ({
+ useCredentialGetter: () => null,
+}));
+vi.mock("@/hooks/useFirstParam", () => ({
+ useFirstParam: () => "wr_1",
+}));
+vi.mock("./useGlobalWorkflowsQuery", () => ({
+ useGlobalWorkflowsQuery: () => ({ data: [] }),
+}));
+vi.mock("./useWorkflowRunWithWorkflowQuery", () => ({
+ useWorkflowRunWithWorkflowQuery: () => ({
+ data: {
+ status: "completed",
+ workflow: { workflow_permanent_id: "wpid_1" },
+ },
+ dataUpdatedAt: 0,
+ }),
+}));
+
+import { useWorkflowRunTimelineQuery } from "./useWorkflowRunTimelineQuery";
+
+function wrapper({ children }: { children: ReactNode }) {
+ const queryClient = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ return (
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
+ );
+}
+
+afterEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("useWorkflowRunTimelineQuery", () => {
+ it("returns the timeline when the response is an array", async () => {
+ mockGet.mockResolvedValue({ data: [] });
+ mockGetClient.mockResolvedValue({ get: mockGet });
+
+ const { result } = renderHook(() => useWorkflowRunTimelineQuery(), {
+ wrapper,
+ });
+
+ await waitFor(() => expect(result.current.isSuccess).toBe(true));
+ expect(result.current.data).toEqual([]);
+ });
+
+ // A 2xx whose body will not JSON.parse arrives as a raw string, and an object
+ // body arrives as an object; either reaches callers typed as an array (SKY-13162).
+ it.each([
+ ["a truncated JSON array", '[{"type":"block"'],
+ ["an HTML error page", "<!doctype html><html></html>"],
+ ["an empty body", ""],
+ ["a JSON object", { detail: "nope" }],
+ ])("fails the query when the response is %s", async (_label, body) => {
+ mockGet.mockResolvedValue({ data: body });
+ mockGetClient.mockResolvedValue({ get: mockGet });
+
+ const { result } = renderHook(() => useWorkflowRunTimelineQuery(), {
+ wrapper,
+ });
+
+ await waitFor(() => expect(result.current.isError).toBe(true));
+ // Callers guard on `undefined`; a non-array must never reach them.
+ expect(result.current.data).toBeUndefined();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
index 468872bcb..e80df6476 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts
@@ -72,12 +72,16 @@ function useWorkflowRunTimelineQuery(options?: { workflowRunId?: string }) {
if (isGlobalWorkflow) {
params.set("template", "true");
}
- return client
- .get(
- `/workflows/${workflowPermanentId}/runs/${workflowRunId}/timeline`,
- { params, signal },
- )
- .then((response) => response.data);
+ const { data } = await client.get(
+ `/workflows/${workflowPermanentId}/runs/${workflowRunId}/timeline`,
+ { params, signal },
+ );
+ // axios hands back the raw string when a 2xx body fails JSON.parse, so an
+ // unvalidated response.data reaches callers typed as an array but isn't one.
+ if (!Array.isArray(data)) {
+ throw new Error("Workflow run timeline response was not an array");
+ }
+ return data;
},
// No independent refetchInterval - timeline follows workflow run query's timing
// via the useEffect above that invalidates on dataUpdatedAt changes