Skip to content

The platform for continuously improving AI agents.

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

import apiClient from "@/client";
import type { NativeEvaluation } from "@/openapi";
import { CancelEvaluation } from "./cancel-evaluation";

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

const plan = (state: string) => ({ deletedAt: null, id: "plan-1", state }) as NativeEvaluation;

function mount(state: string) {
  const client = new QueryClient();
  render(
    <QueryClientProvider client={client}>
      <CancelEvaluation plan={plan(state)} />
    </QueryClientProvider>
  );
  return client;
}

it("cancels only after confirming and keeps the server's state", async () => {
  const cancel = vi
    .spyOn(apiClient.nativeEvaluations, "nativeEvaluationsCancelCreate")
    .mockResolvedValue(plan("cancelling"));
  const client = mount("running");

  fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
  expect(cancel).not.toHaveBeenCalled();
  await act(async () => {
    fireEvent.click(screen.getByRole("button", { name: "Cancel evaluation" }));
  });

  expect(cancel).toHaveBeenCalledExactlyOnceWith({ id: "plan-1" });
  expect(client.getQueryData<NativeEvaluation>(["decision-comparison", "plan-1"])?.state).toBe(
    "cancelling"
  );
});

it("offers no cancel once the evaluation has ended or is ending", () => {
  for (const state of ["completed", "cancelling", "cancelled", "draft"]) {
    mount(state);
    expect(screen.queryByRole("button", { name: "Cancel" })).toBeNull();
    cleanup();
  }
});