Skip to content

Automate browser based workflows with AI

test(SKY-11812): unit-cover the copilot auto-open-Editor one-shot (#7109)

Celal Zamanoğluade173ea11f5
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 75fc916c4..9b937eb42 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -172,6 +172,10 @@ import { constructCacheKeyValue, getInitialParameters } from "./utils";
import { WorkflowCopilotChat } from "../copilot/WorkflowCopilotChat";
import { useStudioRunId } from "../studio/useStudioRunId";
import { copilotRunId } from "./copilotRunId";
+import {
+ initialEditorAutoOpenState,
+ shouldAutoOpenEditor,
+} from "./editorAutoOpen";
import { useStudioShellContext } from "../studio/StudioShellContext";
import { StudioShellPanelPortal } from "../studio/StudioShellPanelPortal";
import { useRecordingLauncherStore } from "@/store/useRecordingLauncherStore";
@@ -356,8 +360,8 @@ function Workspace({
// Armed iff the workflow has no blocks at mount — the studio shell remounts
// Workspace per workflow, so `workflow` is always populated here. The first
// copilot build that lands blocks auto-opens the Editor pane, exactly once.
- const editorAutoOpenArmedRef = useRef(
- workflow.workflow_definition.blocks.length === 0,
+ const editorAutoOpenStateRef = useRef(
+ initialEditorAutoOpenState(workflow.workflow_definition.blocks.length),
);
const location = useLocation();
const navigate = useNavigate();
@@ -2964,13 +2968,16 @@ function Workspace({
onWorkflowUpdate={(workflowData, options) => {
try {
applyWorkflowUpdate(workflowData, options);
- if (
- embedded &&
- options?.applied &&
- editorAutoOpenArmedRef.current &&
- workflowData.workflow_definition.blocks.length > 0
- ) {
- editorAutoOpenArmedRef.current = false;
+ const { fire, nextState } = shouldAutoOpenEditor(
+ editorAutoOpenStateRef.current,
+ {
+ embedded,
+ applied: options?.applied,
+ blockCount: workflowData.workflow_definition.blocks.length,
+ },
+ );
+ editorAutoOpenStateRef.current = nextState;
+ if (fire) {
openStudioPane("editor");
}
} catch (error) {
diff --git a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts
new file mode 100644
index 000000000..ceaf56516
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.test.ts
@@ -0,0 +1,120 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ initialEditorAutoOpenState,
+ shouldAutoOpenEditor,
+} from "./editorAutoOpen";
+
+describe("initialEditorAutoOpenState", () => {
+ it("arms when the agent starts empty", () => {
+ expect(initialEditorAutoOpenState(0)).toEqual({ armed: true });
+ });
+
+ it("does not arm when the agent already has blocks", () => {
+ expect(initialEditorAutoOpenState(3)).toEqual({ armed: false });
+ });
+});
+
+describe("shouldAutoOpenEditor", () => {
+ const armed = { armed: true };
+ const disarmed = { armed: false };
+
+ it("fires on the first applied build that lands blocks in studio", () => {
+ const result = shouldAutoOpenEditor(armed, {
+ embedded: true,
+ applied: true,
+ blockCount: 2,
+ });
+ expect(result.fire).toBe(true);
+ expect(result.nextState).toEqual({ armed: false });
+ });
+
+ it("does not fire for a draft (applied undefined)", () => {
+ const result = shouldAutoOpenEditor(armed, {
+ embedded: true,
+ applied: undefined,
+ blockCount: 2,
+ });
+ expect(result.fire).toBe(false);
+ expect(result.nextState).toEqual(armed);
+ });
+
+ it("does not fire for a snap-back (applied false)", () => {
+ const result = shouldAutoOpenEditor(armed, {
+ embedded: true,
+ applied: false,
+ blockCount: 2,
+ });
+ expect(result.fire).toBe(false);
+ expect(result.nextState).toEqual(armed);
+ });
+
+ it("does not fire outside the studio shell (embedded false)", () => {
+ const result = shouldAutoOpenEditor(armed, {
+ embedded: false,
+ applied: true,
+ blockCount: 2,
+ });
+ expect(result.fire).toBe(false);
+ expect(result.nextState).toEqual(armed);
+ });
+
+ it("does not fire when the applied update lands zero blocks", () => {
+ const result = shouldAutoOpenEditor(armed, {
+ embedded: true,
+ applied: true,
+ blockCount: 0,
+ });
+ expect(result.fire).toBe(false);
+ expect(result.nextState).toEqual(armed);
+ });
+
+ it("never fires when the agent already had blocks (started disarmed)", () => {
+ const result = shouldAutoOpenEditor(disarmed, {
+ embedded: true,
+ applied: true,
+ blockCount: 2,
+ });
+ expect(result.fire).toBe(false);
+ expect(result.nextState).toEqual(disarmed);
+ });
+
+ it("fires exactly once across a sequence of applied builds", () => {
+ let state = initialEditorAutoOpenState(0);
+
+ const first = shouldAutoOpenEditor(state, {
+ embedded: true,
+ applied: true,
+ blockCount: 1,
+ });
+ expect(first.fire).toBe(true);
+ state = first.nextState;
+
+ const second = shouldAutoOpenEditor(state, {
+ embedded: true,
+ applied: true,
+ blockCount: 4,
+ });
+ expect(second.fire).toBe(false);
+ expect(second.nextState).toEqual({ armed: false });
+ });
+
+ it("stays disarmed after firing regardless of the user closing the pane", () => {
+ // This state has no notion of pane-open/closed — re-firing the disarmed
+ // state it produces is the only way "after close" could matter, and it
+ // still doesn't fire.
+ const afterFire = shouldAutoOpenEditor(armed, {
+ embedded: true,
+ applied: true,
+ blockCount: 1,
+ }).nextState;
+
+ const afterUserClosedPane = shouldAutoOpenEditor(afterFire, {
+ embedded: true,
+ applied: true,
+ blockCount: 5,
+ });
+
+ expect(afterUserClosedPane.fire).toBe(false);
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts
new file mode 100644
index 000000000..ee2ab3c1e
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/editor/editorAutoOpen.ts
@@ -0,0 +1,30 @@
+// One-shot arm/fire decision for auto-opening the Editor pane when Copilot's
+// first applied build lands blocks on a previously-empty agent (SKY-11763).
+// Drafts/snap-backs (`applied` false/undefined) never fire it, and once it
+// fires it stays disarmed for the rest of the state's lifetime — closing the
+// pane afterward has no bearing since this state has no notion of pane-open.
+export type EditorAutoOpenState = {
+ armed: boolean;
+};
+
+export function initialEditorAutoOpenState(
+ blockCount: number,
+): EditorAutoOpenState {
+ return { armed: blockCount === 0 };
+}
+
+export function shouldAutoOpenEditor(
+ state: EditorAutoOpenState,
+ update: {
+ embedded: boolean;
+ applied: boolean | undefined;
+ blockCount: number;
+ },
+): { fire: boolean; nextState: EditorAutoOpenState } {
+ const fire =
+ state.armed &&
+ update.embedded &&
+ Boolean(update.applied) &&
+ update.blockCount > 0;
+ return { fire, nextState: fire ? { armed: false } : state };
+}