Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

9.6 KB
Raw Blame
import React from 'react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen, waitFor, within, fireEvent, act } from '@testing-library/react';
import type { TConversationPullRequest } from 'librechat-data-provider';
import PullRequestChip from '../Chip';

const mockGet = jest.fn();

jest.mock('~/data-provider/Endpoints', () => ({
  useGetStartupConfig: () => ({ data: { pullRequestsEnabled: true } }),
}));

jest.mock('librechat-data-provider', () => {
  const actual = jest.requireActual('librechat-data-provider');
  return {
    ...actual,
    dataService: {
      ...actual.dataService,
      getConversationPullRequest: (...args: unknown[]) => mockGet(...args),
    },
  };
});

jest.mock('~/hooks', () => ({
  useLocalize: () => (key: string, values?: Record<string, string | number>) =>
    values == null ? key : `${key}:${Object.values(values).join(',')}`,
}));

/**
 * Ariakit opens a hovercard only for a pointer that is really moving, and only accepts a bare
 * synthetic hover when NODE_ENV is "test". CI runs the suite with NODE_ENV=development, so the
 * tests move the pointer the way a browser reports it.
 */
let pointerX = 100;
const movePointerOver = (element: HTMLElement) => {
  pointerX += 7;
  fireEvent.mouseMove(element, {
    screenX: pointerX,
    screenY: pointerX,
    movementX: 7,
    movementY: 7,
  });
};
const movePointerAway = (element: HTMLElement) => {
  fireEvent.mouseLeave(element);
  fireEvent.mouseMove(document.body, { screenX: 900, screenY: 900, movementX: 7, movementY: 7 });
};

const pr: TConversationPullRequest = {
  number: 1234,
  title: 'Simplify Single Tool Execution Path',
  url: 'https://github.com/LibreChat-AI/LibreChat/pull/1234',
  additions: 1234,
  deletions: 56,
  state: 'open',
  isDraft: false,
  mergeable: 'clean',
  checks: 'passing',
};

const renderChip = () => {
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  return render(
    <QueryClientProvider client={client}>
      <PullRequestChip conversationId="convo-1" />
    </QueryClientProvider>,
  );
};

describe('PullRequestChip', () => {
  beforeEach(() => {
    mockGet.mockReset();
  });

  it('renders nothing while loading, so the header does not shift', () => {
    mockGet.mockReturnValue(new Promise(() => undefined));
    const { container } = renderChip();
    expect(container).toBeEmptyDOMElement();
  });

  it('renders nothing for a conversation without a pull request', async () => {
    mockGet.mockResolvedValue({ pullRequest: null });
    const { container } = renderChip();
    await waitFor(() => expect(mockGet).toHaveBeenCalled());
    expect(container).toBeEmptyDOMElement();
  });

  it('renders nothing when the first lookup fails', async () => {
    mockGet.mockRejectedValue(new Error('503'));
    const { container } = renderChip();
    await waitFor(() => expect(mockGet).toHaveBeenCalled());
    expect(container).toBeEmptyDOMElement();
  });

  it('shows the title, a CI dot and a label that states everything the colors say', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const button = await screen.findByTestId('header-pull-request-button');
    expect(button).toHaveTextContent(pr.title);
    expect(screen.getByTestId('pull-request-ci-dot')).toHaveClass('bg-status-success');
    const label = button.getAttribute('aria-label') ?? '';
    expect(label).toContain(pr.title);
    expect(label).toContain('com_ui_pr_state:com_ui_pr_state_open');
    expect(label).toContain('com_ui_pr_checks:com_ui_pr_checks_passing');
  });

  it.each([
    ['failing', 'bg-status-error'],
    ['running', 'bg-status-warning'],
  ] as const)('colors the dot for %s checks', async (checks, dotClass) => {
    mockGet.mockResolvedValue({ pullRequest: { ...pr, checks } });
    renderChip();
    expect(await screen.findByTestId('pull-request-ci-dot')).toHaveClass(dotClass);
  });

  it('never shows the agent picture, even when the agent has one', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const dot = await screen.findByTestId('pull-request-ci-dot');
    expect(screen.getAllByTestId('pull-request-ci-dot')).toHaveLength(1);
    expect(screen.getByTestId('header-pull-request-button').querySelector('img')).toBeNull();
    expect(dot.parentElement?.querySelector('svg')).not.toBeNull();
  });

  it('keeps the dot on the pull request icon', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const dot = await screen.findByTestId('pull-request-ci-dot');
    expect(screen.getAllByTestId('pull-request-ci-dot')).toHaveLength(1);
    expect(dot.parentElement?.querySelector('svg')).not.toBeNull();
    expect(dot.parentElement?.querySelector('img')).toBeNull();
  });

  it('shows no dot when there are no checks', async () => {
    mockGet.mockResolvedValue({ pullRequest: { ...pr, checks: 'none' } });
    renderChip();
    await screen.findByTestId('header-pull-request-button');
    expect(screen.queryByTestId('pull-request-ci-dot')).not.toBeInTheDocument();
  });

  it('keeps the card closed until the chip is hovered or focused', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const button = await screen.findByTestId('header-pull-request-button');
    expect(screen.queryByTestId('pull-request-card')).not.toBeInTheDocument();
    expect(button).toHaveAttribute('aria-expanded', 'false');
  });

  it('opens the card on hover and says so on the chip', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const button = await screen.findByTestId('header-pull-request-button');
    movePointerOver(button);
    const card = await screen.findByTestId('pull-request-card');
    expect(card.closest('[role="dialog"]')).toHaveAccessibleName('com_ui_pull_request');
    expect(button).toHaveAttribute('aria-expanded', 'true');
  });

  it('closes the card when the pointer leaves', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    const button = await screen.findByTestId('header-pull-request-button');
    movePointerOver(button);
    await screen.findByTestId('pull-request-card');
    movePointerAway(button);
    await waitFor(() => expect(screen.queryByTestId('pull-request-card')).not.toBeInTheDocument());
    expect(button).toHaveAttribute('aria-expanded', 'false');
  });

  it('opens the card for keyboard focus', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    await screen.findByTestId('header-pull-request-button');
    await userEvent.tab();
    expect(screen.getByTestId('header-pull-request-button')).toHaveFocus();
    expect(await screen.findByTestId('pull-request-card')).toBeInTheDocument();
  });

  it('shows the GitHub tooltip inside the open card, so it is not painted behind it', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    movePointerOver(await screen.findByTestId('header-pull-request-button'));
    const card = await screen.findByTestId('pull-request-card');
    const layer = card.closest('[role="dialog"]') as HTMLElement;
    await userEvent.tab();
    act(() => screen.getByTestId('pull-request-github-link').focus());
    expect(await within(layer).findByRole('tooltip')).toHaveTextContent('com_ui_pr_open_in_github');
  });

  it('opens a card with the number, line counts, badges and a safe GitHub link', async () => {
    mockGet.mockResolvedValue({ pullRequest: pr });
    renderChip();
    await userEvent.click(await screen.findByTestId('header-pull-request-button'));
    const card = await screen.findByTestId('pull-request-card');
    expect(within(card).getByText('com_ui_pr_label:1234')).toBeInTheDocument();
    expect(within(card).getByText('+1234')).toBeInTheDocument();
    expect(within(card).getByText('-56')).toBeInTheDocument();
    expect(within(card).getByText(pr.title)).toBeInTheDocument();
    expect(within(card).getByText('com_ui_pr_state:com_ui_pr_state_open')).toBeInTheDocument();
    expect(within(card).getByText('com_ui_pr_checks:com_ui_pr_checks_passing')).toBeInTheDocument();
    const link = within(card).getByTestId('pull-request-github-link');
    expect(link).toHaveAttribute('href', pr.url);
    expect(link).toHaveAttribute('target', '_blank');
    expect(link).toHaveAttribute('rel', 'noopener noreferrer');
    expect(link).toHaveAccessibleName('com_ui_pr_open_in_github');
  });

  it('keeps the last known pull request when a later refresh fails', async () => {
    mockGet.mockResolvedValueOnce({ pullRequest: pr });
    const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
    render(
      <QueryClientProvider client={client}>
        <PullRequestChip conversationId="convo-1" />
      </QueryClientProvider>,
    );
    await userEvent.click(await screen.findByTestId('header-pull-request-button'));
    await screen.findByTestId('pull-request-card');
    expect(screen.queryByText('com_ui_pr_refresh_failed')).not.toBeInTheDocument();

    mockGet.mockRejectedValue(new Error('503'));
    await client.refetchQueries();

    expect(await screen.findByText('com_ui_pr_refresh_failed')).toBeInTheDocument();
    expect(screen.getByTestId('pull-request-card')).toHaveTextContent(pr.title);
  });

  it('labels a merged pull request and drops its CI dot', async () => {
    mockGet.mockResolvedValue({ pullRequest: { ...pr, state: 'merged' } });
    renderChip();
    const button = await screen.findByTestId('header-pull-request-button');
    expect(button.getAttribute('aria-label')).toContain('com_ui_pr_state_merged');
    expect(screen.queryByTestId('pull-request-ci-dot')).not.toBeInTheDocument();
  });
});