Skip to content

The platform for continuously improving AI agents.

4.3 KB
Raw Blame
// @vitest-environment jsdom
// covers: console-logic
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";

import { type CanvasHandle, ExperimentCanvas } from "./canvas";

afterEach(() => {
  cleanup();
  vi.unstubAllGlobals();
});

it("finishes a node exit after undo removes it from the experiment document", async () => {
  vi.stubGlobal(
    "ResizeObserver",
    class {
      observe() {}
      unobserve() {}
      disconnect() {}
    }
  );
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  let canvas: CanvasHandle | undefined;
  render(
    <QueryClientProvider client={client}>
      <ExperimentCanvas
        document={{ edges: [], nodes: [] }}
        locked={false}
        onChange={() => {}}
        onFocus={() => {}}
        onInvalidChange={() => {}}
        onReady={(handle) => {
          canvas = handle;
        }}
        projectId="project"
        receipts={[]}
        saveDraft={async () => 1}
        saved
      />
    </QueryClientProvider>
  );
  await act(async () =>
    canvas!.add({
      config: { model: "Qwen/Qwen3.5-0.8B" },
      id: "candidate",
      kind: "model",
      notes: "",
      operation: null,
      position: { x: 0, y: 0 },
      title: "Candidate",
    })
  );
  await screen.findByTestId("rf__node-candidate");
  await act(async () => canvas!.undo());
  expect(canvas!.document().nodes).toEqual([]);
  await waitFor(() => expect(screen.queryByTestId("rf__node-candidate")).toBeNull());
  expect(screen.getByRole("region", { name: "Experiment stages" })).toBeTruthy();
  client.clear();
});

it("keeps invalid source editable, recovers the exact draft and restores declared outputs on undo", async () => {
  const { fireEvent } = await import("@testing-library/react");
  vi.stubGlobal(
    "ResizeObserver",
    class {
      observe() {}
      unobserve() {}
      disconnect() {}
    }
  );
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  const document = {
    edges: [],
    nodes: [
      {
        config: { ports: { inputs: {}, outputs: { report: "artifact" } } },
        id: "analysis",
        kind: "component" as const,
        notes: "",
        operation: null,
        position: { x: 0, y: 0 },
        title: "Analysis",
      },
    ],
    outputs: { report: { node: "analysis", port: "report" } },
    parameters: { seed: { default: 37, type: "integer" } },
  };
  let canvas: CanvasHandle | undefined;
  const invalid = vi.fn();
  const changes: unknown[] = [];
  render(
    <QueryClientProvider client={client}>
      <ExperimentCanvas
        document={document}
        focus="analysis"
        locked={false}
        onChange={() => changes.push(canvas!.document())}
        onFocus={() => {}}
        onInvalidChange={invalid}
        onReady={(handle) => {
          canvas = handle;
        }}
        projectId="project"
        receipts={[]}
        saveDraft={async () => 1}
        saved
      />
    </QueryClientProvider>
  );
  const parameters = (await screen.findByLabelText("Analysis parameters")) as HTMLTextAreaElement;
  const disclosure = screen.getByRole("button", { name: "Advanced settings" });
  fireEvent.click(disclosure);
  const original = parameters.value;
  parameters.focus();
  fireEvent.change(parameters, { target: { value: '{"ports":' } });
  await waitFor(() => expect(invalid).toHaveBeenLastCalledWith(true));
  expect(screen.getByLabelText("Analysis parameters")).toBe(parameters);
  expect(window.document.activeElement).toBe(parameters);
  expect(disclosure.getAttribute("aria-expanded")).toBe("true");
  expect(parameters.value).toBe('{"ports":');
  expect(() => canvas!.document()).toThrow();
  fireEvent.change(screen.getByLabelText("Analysis parameters"), { target: { value: original } });
  expect(() => canvas!.document()).not.toThrow();
  await waitFor(() => expect(invalid).toHaveBeenLastCalledWith(false));
  expect(changes.at(-1)).toEqual(document);
  fireEvent.click(screen.getByRole("button", { name: "Remove node" }));
  expect(canvas!.document().outputs).toEqual({});
  expect(canvas!.document().nodes).toEqual([]);
  await act(async () => canvas!.undo());
  expect(canvas!.document()).toEqual(document);
  client.clear();
});