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

6.0 KB
Raw Blame
import React from 'react';
import { getDefaultStore } from 'jotai';
import { ChatContext } from '@librechat/chat/react';
import { ContentTypes } from 'librechat-data-provider';
import { renderHook, act } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { TMessage } from 'librechat-data-provider';
import type { ChatContract } from '../contract';
import useCompactConversation, { compactingConversationAtom } from '../useCompactConversation';

const mockAsk = jest.fn();
/** The message cache hands back one array until it is written, as React Query does. */
const mockStoredMessages: TMessage[] = [];
let mockContext: {
  index: number;
  isSubmitting: boolean;
  conversation: { conversationId: string; endpoint: string } | null;
};
let mockLatestMessage: TMessage | null;

jest.mock('~/hooks/Messages/useLatestMessage', () => ({
  useLatestMessage: () => mockLatestMessage,
}));

type ContentPart = NonNullable<TMessage['content']>[number];

const summaryPart = (overrides: Record<string, unknown> = {}): ContentPart =>
  ({
    type: ContentTypes.SUMMARY,
    content: [{ type: ContentTypes.TEXT, text: 'checkpoint' }],
    boundary: { messageId: 'step_summary', contentIndex: 0 },
    ...overrides,
  }) as ContentPart;

const leaf = (overrides: Partial<TMessage> = {}): TMessage =>
  ({
    messageId: 'a1',
    parentMessageId: 'u1',
    conversationId: 'convo-1',
    isCreatedByUser: false,
    text: 'answer',
    ...overrides,
  }) as TMessage;

/** The hook reads status and submits through the real `useChat` facade over this contract. */
const getContract = () =>
  ({
    ...mockContext,
    ask: mockAsk,
    getMessages: () => mockStoredMessages,
    messagesKey: mockContext.conversation?.conversationId ?? '',
    setMessages: jest.fn(),
    subscribeMessages: () => () => undefined,
    getMessagesRevision: () => 0,
    latestMessageId: undefined,
    regenerate: jest.fn(),
    stopGenerating: jest.fn(),
  }) as unknown as ChatContract;

const queryClient = new QueryClient();
const wrapper = ({ children }: { children: React.ReactNode }) => (
  <QueryClientProvider client={queryClient}>
    <ChatContext.Provider value={getContract()}>{children}</ChatContext.Provider>
  </QueryClientProvider>
);

describe('useCompactConversation', () => {
  beforeEach(() => {
    mockAsk.mockClear();
    mockContext = {
      index: 0,
      isSubmitting: false,
      conversation: { conversationId: 'convo-1', endpoint: 'openAI' },
    };
    mockLatestMessage = leaf();
    getDefaultStore().set(compactingConversationAtom, null);
  });

  it('drops a marker left by a compaction that finished while the view was away', () => {
    getDefaultStore().set(compactingConversationAtom, 'convo-1');
    const hook = renderHook(() => useCompactConversation(), { wrapper });
    expect(hook.result.current.isCompacting).toBe(false);

    /** The next ordinary turn in the same conversation is not a compaction. */
    mockContext.isSubmitting = true;
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(false);
  });

  it('keeps the marker when it mounts into a compaction still streaming', () => {
    getDefaultStore().set(compactingConversationAtom, 'convo-1');
    mockContext.isSubmitting = true;
    const hook = renderHook(() => useCompactConversation(), { wrapper });
    expect(hook.result.current.isCompacting).toBe(true);

    mockContext.isSubmitting = false;
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(false);
    expect(getDefaultStore().get(compactingConversationAtom)).toBeNull();
  });

  it('submits a compaction anchored on the leaf itself', () => {
    const { result } = renderHook(() => useCompactConversation(), { wrapper });

    expect(result.current.canCompact).toBe(true);
    act(() => result.current.compact());

    /** The server compacts up to `parentMessageId`, so it must be the leaf,
     *  not the leaf's own parent, or the latest reply is left out. */
    expect(mockAsk).toHaveBeenCalledWith(
      { text: '', conversationId: 'convo-1', messageId: 'a1', parentMessageId: 'a1' },
      { compact: true },
    );
  });

  it.each([
    [
      'a new conversation',
      () => (mockContext.conversation = { conversationId: 'new', endpoint: 'openAI' }),
    ],
    ['a submission in flight', () => (mockContext.isSubmitting = true)],
    ['no leaf', () => (mockLatestMessage = null)],
    [
      'a leaf that is already a finished compaction',
      () => (mockLatestMessage = leaf({ text: '', content: [summaryPart()] })),
    ],
  ])('cannot compact with %s', (_label, arrange) => {
    arrange();
    const { result } = renderHook(() => useCompactConversation(), { wrapper });

    expect(result.current.canCompact).toBe(false);
    act(() => result.current.compact());
    expect(mockAsk).not.toHaveBeenCalled();
  });

  it.each([
    ['still streaming', summaryPart({ summarizing: true })],
    ['failed', summaryPart({ failed: true })],
    ['stored without a boundary', summaryPart({ boundary: undefined })],
  ])('lets an interrupted compaction (summary %s) be retried', (_label, part) => {
    mockLatestMessage = leaf({ text: '', content: [part] });
    const { result } = renderHook(() => useCompactConversation(), { wrapper });

    expect(result.current.canCompact).toBe(true);
  });

  it('reports compacting only for the conversation it submitted, until the turn settles', () => {
    const hook = renderHook(() => useCompactConversation(), { wrapper });
    act(() => hook.result.current.compact());

    mockContext.isSubmitting = true;
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(true);

    mockContext.conversation = { conversationId: 'convo-2', endpoint: 'openAI' };
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(false);

    mockContext.conversation = { conversationId: 'convo-1', endpoint: 'openAI' };
    mockContext.isSubmitting = false;
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(false);
    mockContext.isSubmitting = true;
    hook.rerender();
    expect(hook.result.current.isCompacting).toBe(false);
  });
});