Skip to content

The platform for continuously improving AI agents.

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

import { appendChatDraft } from "@/components/datasets/workspace-chat-draft";
import { InterruptedReply } from "./interrupted-reply";
import { SelectionActions } from "./selection-actions";

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

it("keeps a full draft intact when an added quote would exceed the input limit", () => {
  expect(appendChatDraft("Existing question", "> a long passage", 20)).toBeNull();
  expect(appendChatDraft("", "> passage\n\n", 20)).toBe("> passage\n\n");
});

it("uses the selected passage for a follow-up and dismisses Escape before the chat closes", () => {
  vi.stubGlobal("matchMedia", () => ({ matches: false }));
  Range.prototype.getBoundingClientRect = () =>
    ({ bottom: 30, left: 20, top: 20, width: 80 }) as DOMRect;
  vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue({
    bottom: 600,
    left: 0,
    top: 0,
    width: 500,
  } as DOMRect);
  const action = vi.fn();
  const outerKey = vi.fn();
  render(
    <div onKeyDown={outerKey}>
      <SelectionActions onAction={action}>
        <p>Accuracy improved on development data.</p>
      </SelectionActions>
    </div>
  );
  const paragraph = screen.getByText("Accuracy improved on development data.");
  const range = document.createRange();
  range.selectNodeContents(paragraph);
  act(() => {
    window.getSelection()?.removeAllRanges();
    window.getSelection()?.addRange(range);
    document.dispatchEvent(new Event("selectionchange"));
  });
  fireEvent.click(screen.getByRole("button", { hidden: true, name: "Quote" }));
  expect(action).toHaveBeenCalledWith(
    expect.objectContaining({ message: "> Accuracy improved on development data.\n\n" })
  );
  act(() => {
    window.getSelection()?.addRange(range);
    document.dispatchEvent(new Event("selectionchange"));
  });
  fireEvent.keyDown(paragraph, { key: "Escape" });
  expect(outerKey).not.toHaveBeenCalled();
  expect(screen.queryByRole("toolbar", { hidden: true, name: "Selection actions" })).toBeNull();
});

it.each([
  [new Error("Run unavailable"), "Run unavailable"],
  [new Error(""), "Could not continue."],
  ["", "Could not continue."],
])("keeps an interrupted reply and reports a failed continuation without silently retrying", async (error, message) => {
  vi.stubGlobal("matchMedia", () => ({
    addEventListener() {},
    matches: false,
    removeEventListener() {},
  }));
  const resume = vi.fn().mockRejectedValue(error);
  render(<InterruptedReply onContinue={resume} reason="error" text="Retained findings" />);
  expect(resume).not.toHaveBeenCalled();
  fireEvent.click(screen.getByRole("button", { name: "Continue" }));
  expect((await screen.findByRole("alert")).textContent).toBe(message);
  expect(
    screen.getByText(
      (_, element) => element?.tagName === "P" && element.textContent === "Retained findings"
    )
  ).toBeTruthy();
  expect(resume).toHaveBeenCalledOnce();
});