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

10.4 KB
Raw Blame
import React from 'react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import '@testing-library/jest-dom';
import ConvoOptions from '../ConvoOptions';

const mockArchiveMutate = jest.fn();
const mockNavigate = jest.fn();
const mockNewConversation = jest.fn();
const mockRetainView = jest.fn();
const mockSetIsPopoverActive = jest.fn();
const mockAnnouncePolite = jest.fn();
const mockSetConversation = jest.fn();
const mockMutationHook = jest.fn();

jest.mock('@ariakit/react', () => ({
  MenuButton: jest
    .requireActual('react')
    .forwardRef(
      (
        { children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>,
        ref: React.ForwardedRef<HTMLButtonElement>,
      ) => (
        <button ref={ref} {...props}>
          {children}
        </button>
      ),
    ),
}));
jest.mock('~/utils', () => ({
  cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
  rowActionClasses: () => 'row-action',
}));
jest.mock('librechat-data-provider', () => ({
  QueryKeys: { messages: 'messages' },
  PermissionTypes: { SHARED_LINKS: 'shared_links' },
  Permissions: { CREATE: 'create' },
}));
jest.mock('@librechat/client', () => ({
  DropdownPopup: ({
    trigger,
    items,
  }: {
    trigger: React.ReactNode;
    items: Array<{ label: string; onClick: () => void }>;
  }) => (
    <>
      {trigger}
      {items
        .filter((item) => item.label === 'com_ui_archive' || item.label === 'com_ui_unarchive')
        .map((item) => (
          <button key={item.label} type="button" onClick={item.onClick}>
            {item.label}
          </button>
        ))}
    </>
  ),
  Spinner: () => <span role="status" />,
  buttonVariants: () => '',
  useToastContext: () => ({ showToast: jest.fn() }),
  useMediaQuery: () => false,
  useRemScale: () => 1,
}));

jest.mock('~/data-provider', () => ({
  useDuplicateConversationMutation: () => ({ mutate: jest.fn(), isLoading: false }),
  useAssignConversationToProjectMutation: () => ({ mutate: jest.fn(), isLoading: false }),
  useDeleteConversationMutation: () => {
    mockMutationHook('delete');
    return { mutate: jest.fn(), isLoading: false };
  },
  useGetStartupConfig: () => ({ data: { sharedLinksEnabled: false } }),
  useArchiveConvoMutation: () => {
    mockMutationHook('archive');
    return { mutate: mockArchiveMutate, isLoading: false };
  },
  usePinConversationMutation: () => {
    mockMutationHook('pin');
    return { mutate: jest.fn(), isLoading: false };
  },
  useMarkConversationUnreadMutation: () => {
    mockMutationHook('markUnread');
    return { mutate: jest.fn(), isLoading: false };
  },
}));

jest.mock('~/hooks', () => ({
  useHasAccess: () => true,
  useLocalize: () => (key: string) => key,
  useNavigateToConvo: () => ({ navigateToConvo: jest.fn() }),
  useNewConvo: () => ({ newConversation: mockNewConversation }),
}));

jest.mock('~/Providers', () => ({
  useChatContext: () => ({
    index: 0,
    conversation: { conversationId: 'conversation-1', isArchived: true },
    setConversation: mockSetConversation,
  }),
  useLiveAnnouncer: () => ({ announcePolite: mockAnnouncePolite }),
}));

let mockOpenRouteConvoId: string | undefined = 'conversation-1';

jest.mock('react-router-dom', () => {
  const actual = jest.requireActual('react-router-dom');
  return {
    ...actual,
    useNavigate: () => mockNavigate,
    useParams: () => ({ conversationId: mockOpenRouteConvoId }),
  };
});

jest.mock('../DeleteButton', () => () => null);
jest.mock('../ShareButton', () => () => null);

const renderOptions = (isArchived: boolean, isActiveConvo = false) => {
  const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  const tree = (active: boolean) => (
    <QueryClientProvider client={queryClient}>
      <MemoryRouter initialEntries={['/c/conversation-1']}>
        <Routes>
          <Route
            path="/c/:conversationId"
            element={
              <ConvoOptions
                conversationId="conversation-1"
                title="A conversation"
                isArchived={isArchived}
                retainView={mockRetainView}
                renameHandler={jest.fn()}
                isPopoverActive={true}
                setIsPopoverActive={mockSetIsPopoverActive}
                isActiveConvo={active}
              />
            }
          />
        </Routes>
      </MemoryRouter>
    </QueryClientProvider>
  );

  const { rerender } = render(tree(isActiveConvo));
  /** Opening another chat moves the route and drops this row's active state. */
  return { openAnotherChat: () => rerender(tree(false)) };
};

describe('ConvoOptions archive action', () => {
  beforeEach(() => {
    jest.useFakeTimers();
    mockArchiveMutate.mockReset();
    mockNavigate.mockReset();
    mockNewConversation.mockReset();
    mockRetainView.mockReset();
    mockSetIsPopoverActive.mockReset();
    mockAnnouncePolite.mockReset();
    mockSetConversation.mockReset();
    mockOpenRouteConvoId = 'conversation-1';
  });

  afterEach(() => {
    jest.useRealTimers();
  });

  /** The row's own state decides the direction: an unarchived pin stays listed beside the
   *  archive, and the previous page's rows outlive a status switch, so a list-level flag
   *  would offer to restore a conversation that was never archived. */
  it('restores the open archived conversation state without navigating away from it', () => {
    renderOptions(true);

    fireEvent.click(screen.getByRole('button', { name: 'com_ui_unarchive' }));

    expect(mockArchiveMutate).toHaveBeenCalledWith(
      { conversationId: 'conversation-1', isArchived: false },
      expect.objectContaining({ onSuccess: expect.any(Function), onError: expect.any(Function) }),
    );
    const [, callbacks] = mockArchiveMutate.mock.calls[0];
    act(() => callbacks.onSuccess());
    expect(mockNavigate).not.toHaveBeenCalled();
    expect(mockNewConversation).not.toHaveBeenCalled();
    /* The restored row leaves the archived list, unmounting this menu, so the message has
       to reach the app-level live region rather than one inside the row. */
    expect(mockAnnouncePolite).toHaveBeenCalledWith({
      message: 'com_ui_convo_unarchived',
      isStatus: true,
    });
    const [updater] = mockSetConversation.mock.calls[0];
    expect(updater({ conversationId: 'conversation-1', isArchived: true })).toEqual({
      conversationId: 'conversation-1',
      isArchived: false,
    });
    /* The request outlives the row: if another chat is open by the time it resolves, the
       restore must not replace it with the one the callback was created for. */
    const otherOpen = { conversationId: 'conversation-2', isArchived: false };
    expect(updater(otherOpen)).toBe(otherOpen);
  });

  it('archives an unarchived conversation and leaves the one it just hid', () => {
    renderOptions(false);

    fireEvent.click(screen.getByRole('button', { name: 'com_ui_archive' }));

    expect(mockArchiveMutate).toHaveBeenCalledWith(
      { conversationId: 'conversation-1', isArchived: true },
      expect.objectContaining({ onSuccess: expect.any(Function), onError: expect.any(Function) }),
    );
    const [, callbacks] = mockArchiveMutate.mock.calls[0];
    act(() => callbacks.onSuccess());
    expect(mockNewConversation).toHaveBeenCalledTimes(1);
    expect(mockNavigate).toHaveBeenCalledWith('/c/new', { replace: true });
  });

  /** Archiving the open chat opens a new one in its place, but the request outlives the
   *  click: if the user has opened another chat by then, that is the one on screen and it
   *  must be left alone. */
  it('leaves a newer selection alone when the archive resolves', () => {
    const { openAnotherChat } = renderOptions(false, true);

    fireEvent.click(screen.getByRole('button', { name: 'com_ui_archive' }));
    mockOpenRouteConvoId = 'conversation-2';
    act(() => openAnotherChat());

    const [, callbacks] = mockArchiveMutate.mock.calls[0];
    act(() => callbacks.onSuccess());
    expect(mockNewConversation).not.toHaveBeenCalled();
    expect(mockNavigate).not.toHaveBeenCalled();
  });
});

describe('ConvoOptions mount cost', () => {
  const ROWS = 30;
  const renderRows = (openIndex: number | null) => {
    const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
    const tree = (open: number | null) => (
      <QueryClientProvider client={queryClient}>
        <MemoryRouter initialEntries={['/c/conversation-0']}>
          <Routes>
            <Route
              path="/c/:conversationId"
              element={
                <>
                  {Array.from({ length: ROWS }, (_, index) => (
                    <ConvoOptions
                      key={index}
                      conversationId={`conversation-${index}`}
                      title={`Chat ${index}`}
                      retainView={mockRetainView}
                      renameHandler={jest.fn()}
                      isPopoverActive={index === open}
                      setIsPopoverActive={mockSetIsPopoverActive}
                      isActiveConvo={false}
                    />
                  ))}
                </>
              }
            />
          </Routes>
        </MemoryRouter>
      </QueryClientProvider>
    );
    const view = render(tree(openIndex));
    return { open: (index: number | null) => view.rerender(tree(index)) };
  };

  beforeEach(() => {
    mockMutationHook.mockReset();
  });

  /** A Pinned section mounts every row at once, and every row the pointer crossed keeps its
   *  trigger: those triggers must not each carry the menu's mutations and route subscriptions. */
  it('runs no menu mutations for triggers whose menu was never used', () => {
    renderRows(null);

    expect(screen.getAllByRole('button', { name: 'com_nav_convo_menu_options' })).toHaveLength(
      ROWS,
    );
    expect(mockMutationHook).not.toHaveBeenCalled();
  });

  it('mounts the menu actions for the one row whose menu opens, and keeps them after it closes', () => {
    const rows = renderRows(null);

    act(() => rows.open(4));
    const hooksWhileOpen = new Set(mockMutationHook.mock.calls.map(([name]) => name));
    expect(hooksWhileOpen).toEqual(new Set(['delete', 'archive', 'pin', 'markUnread']));

    mockMutationHook.mockReset();
    act(() => rows.open(null));
    /* Only the row that was opened renders again, and it keeps its actions mounted. */
    expect(mockMutationHook).toHaveBeenCalledTimes(4);
  });
});