Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

3.6 KB
Raw Blame
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { QuickActions } from "../../src/dashboard/quick-actions.js";

describe("QuickActions", () => {
  it("renders all three default action buttons + last-synced footer", () => {
    render(
      <QuickActions
        onAction={vi.fn()}
        lastSyncAt="2026-01-01T00:00:00Z"
        lastResyncAt={null}
      />,
    );
    expect(screen.getByRole("button", { name: /Sync/i })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: /Full Re-index/i })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: /Dead Code Scan/i })).toBeInTheDocument();
    expect(screen.getByText(/Last re-indexed/)).toBeInTheDocument();
    expect(screen.getByText(/never/i)).toBeInTheDocument();
  });

  it("opens confirm dialog for needsConfirm actions and forwards key on confirm", async () => {
    const onAction = vi.fn().mockResolvedValue(undefined);
    render(<QuickActions onAction={onAction} pageCount={50} modelName="gpt-5.4-mini" />);

    fireEvent.click(screen.getByRole("button", { name: /^Sync$/ }));
    expect(await screen.findByText("Sync Repository")).toBeInTheDocument();
    // The confirm button inside the dialog re-uses the same action label.
    const confirmButtons = screen.getAllByRole("button", { name: /^Sync$/ });
    fireEvent.click(confirmButtons[confirmButtons.length - 1]!);
    await waitFor(() => expect(onAction).toHaveBeenCalledWith("sync"));
  });

  it("prices the estimate with the rates it is given, and shows none without them", async () => {
    const { unmount } = render(
      <QuickActions
        onAction={vi.fn()}
        pageCount={100}
        modelName="some-model"
        costPer1k={{ input: 0.001, output: 0.005 }}
      />,
    );
    fireEvent.click(screen.getByRole("button", { name: /Full Re-index/i }));
    // 100 pages * (3.5K in * $0.001 + 2.2K out * $0.005) = $1.45.
    expect(await screen.findByText(/1\.45/)).toBeInTheDocument();
    unmount();

    render(<QuickActions onAction={vi.fn()} pageCount={100} modelName="some-model" />);
    fireEvent.click(screen.getByRole("button", { name: /Full Re-index/i }));
    expect(await screen.findByText("Full Re-index", { selector: "h2" })).toBeInTheDocument();
    expect(screen.queryByText(/Estimated Cost/i)).not.toBeInTheDocument();
  });

  it("renders activeJobSlot in place of the buttons when provided", () => {
    render(
      <QuickActions
        onAction={vi.fn()}
        activeJobSlot={<div data-testid="job-progress">progress</div>}
      />,
    );
    expect(screen.getByTestId("job-progress")).toBeInTheDocument();
    expect(screen.queryByRole("button", { name: /^Sync$/ })).not.toBeInTheDocument();
  });

  it("bounds the job panel's width in the header variant", () => {
    // The header's actions slot does not shrink, so an unbounded panel takes
    // its content's width and crushes the repo name beside it (#4690).
    render(
      <QuickActions
        onAction={vi.fn()}
        variant="header"
        activeJobSlot={<div data-testid="job-progress">progress</div>}
      />,
    );
    const container = screen.getByTestId("job-progress").parentElement!;
    expect(container).toHaveClass("min-w-0", "sm:w-80");
  });

  it("fires non-confirming actions immediately (dead-code path)", () => {
    const onAction = vi.fn().mockResolvedValue(undefined);
    render(<QuickActions onAction={onAction} />);
    fireEvent.click(screen.getByRole("button", { name: /Dead Code Scan/i }));
    expect(onAction).toHaveBeenCalledWith("dead-code");
  });
});