Skip to content

The platform for continuously improving AI agents.

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

import { TooltipProvider } from "@/components/ui/tooltip";
import { CellFromJSON } from "@/openapi";
import { WorkshopCluster } from "./workshop-cluster";

const network = vi.hoisted(() => ({ rows: vi.fn() }));
vi.mock("@/client", () => ({
  default: {
    datasets: { datasetsColumnsList: () => Promise.resolve([]) },
  },
  fetchWithAuth: network.rows,
}));
vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));
afterEach(() => {
  cleanup();
  vi.clearAllMocks();
  vi.unstubAllGlobals();
});

beforeEach(() => {
  vi.stubGlobal(
    "ResizeObserver",
    class {
      observe() {}
      disconnect() {}
    }
  );
});

it("expands the same Workshop cells, script and paged data inline without a popup", async () => {
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  const preparation = {
    edges: [{ kind: "split" as const, source: "source", target: "train", value: "train" }],
    nodes: ["source", "train"].map((id, index) => ({
      cell: CellFromJSON({
        columns: [{ name: "value", type: "string" }],
        fingerprint: "retained",
        id,
        position: index,
        rows: 20,
        script: index ? "print('retained transformation')" : "",
        state: "ok",
        title: id,
        transformation: {},
        version: "1.0",
      }),
      dataset: id,
      datasetName: "Training cases",
      intent: "train",
      role: id === "train" ? "train" : "",
      trainFormat: "",
    })),
    selected: "source",
  };
  const view = (collapsed: boolean) => (
    <QueryClientProvider client={client}>
      <TooltipProvider>
        <WorkshopCluster cellWidth={304} collapsed={collapsed} preparation={preparation} />
      </TooltipProvider>
    </QueryClientProvider>
  );
  network.rows.mockImplementation((url: string) => {
    const offset = Number(new URL(url, "http://localhost").searchParams.get("offset"));
    return Promise.resolve(
      new Response(
        JSON.stringify({
          columns: [{ name: "value", type: "string" }],
          limit: 10,
          marks: {},
          offset,
          rows: [{ _index: offset, value: `case-${offset}` }],
          total: 20,
        })
      )
    );
  });
  const rendered = render(view(true));
  expect(screen.getByRole("article", { name: "train" })).toBeTruthy();
  expect(screen.queryByRole("article", { name: "source" })).toBeNull();
  rendered.rerender(view(false));
  const cell = within(screen.getByRole("article", { name: "train" }));
  expect(screen.getByRole("article", { name: "source" })).toBeTruthy();
  expect(network.rows).not.toHaveBeenCalled();
  fireEvent.click(cell.getByRole("button", { name: /Script/ }));
  expect(await cell.findByLabelText("Script of train")).toBeTruthy();
  fireEvent.click(cell.getByRole("button", { name: /^Data.*20 rows/ }));
  expect(await cell.findByText("case-0")).toBeTruthy();
  expect(network.rows).toHaveBeenCalledWith(
    expect.stringContaining("/api/datasets/train/rows/?limit=10&offset=0&cell=train")
  );
  expect(screen.queryByRole("dialog")).toBeNull();
  fireEvent.click(cell.getByRole("button", { name: "Go to next page" }));
  expect(await cell.findByText("case-10")).toBeTruthy();
  expect(network.rows).toHaveBeenCalledWith(
    expect.stringContaining("/api/datasets/train/rows/?limit=10&offset=10&cell=train")
  );
  rendered.rerender(view(true));
  expect(screen.queryByRole("table")).toBeNull();
  rendered.rerender(view(false));
  expect(await cell.findByLabelText("Script of train")).toBeTruthy();
  expect(await cell.findByText("case-0")).toBeTruthy();
  fireEvent.click(cell.getByRole("button", { name: "Export 1.0 train" }));
  expect(await screen.findByRole("dialog", { name: "Export 1.0 train" })).toBeTruthy();
  client.clear();
});