mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 06:51:59 +00:00
🧵 refactor: Render the Package Message List by Default in ChatScreen (#17753)
* refactor: Move the Messages View Provider Into @librechat/chat * refactor: Render the Package Message List by Default in ChatScreen * fix: Drop the Rail's IntersectionObserver Guard for a Package Spec Stub Nothing renders the rail without a browser: its observer starts in an effect, which never runs in a server render. The guard existed only because jsdom lacks the API, so jsdom now gets inert observers and matchMedia from the package's jest setup, and the spec's own stubs, which no longer matched the DOM lib's IntersectionObserver type, are gone. * test: Scroll a Sidebar Row Into View Before Dragging It A row under the fold has no pixel the mouse can press, so the drag selected text instead. Whether a run hit it depended on which earlier specs shared its shard and left chats in the Running and Finished sections. * test: Give the Client Specs an Inert IntersectionObserver With the rail's guard gone, a client spec that renders the message list reaches the observer jsdom lacks. The shared setup provides an inert one, as the package's jest setup does, and the child-thread spec's partial client mock gains the rem scale hook the rail reads. * chore: Retest Against Current Dev * test: Give the Test Messages Host the Row Hook Members The messages host gained the assistant, added-conversation, streamed-attachment and feedback hooks when the row hooks moved into the package, so the shared test host supplies them too.
This commit is contained in:
parent
1b857e46cb
commit
62cc2ffc19
21 changed files with 786 additions and 350 deletions
|
|
@ -1,208 +1,19 @@
|
|||
import React, { createContext, useContext, useMemo } from 'react';
|
||||
import { useChatContext } from './ChatContext';
|
||||
|
||||
interface MessagesViewContextValue {
|
||||
/** Core conversation data */
|
||||
conversation: ReturnType<typeof useChatContext>['conversation'];
|
||||
conversationId: string | null | undefined;
|
||||
|
||||
readOnly: boolean;
|
||||
|
||||
/** Submission and control states */
|
||||
isSubmitting: ReturnType<typeof useChatContext>['isSubmitting'];
|
||||
abortScroll: ReturnType<typeof useChatContext>['abortScroll'];
|
||||
setAbortScroll: ReturnType<typeof useChatContext>['setAbortScroll'];
|
||||
|
||||
/** Message operations */
|
||||
ask: ReturnType<typeof useChatContext>['ask'];
|
||||
regenerate: ReturnType<typeof useChatContext>['regenerate'];
|
||||
handleContinue: ReturnType<typeof useChatContext>['handleContinue'];
|
||||
|
||||
/** Message state management */
|
||||
index: ReturnType<typeof useChatContext>['index'];
|
||||
latestMessageId: ReturnType<typeof useChatContext>['latestMessageId'];
|
||||
latestMessageDepth: ReturnType<typeof useChatContext>['latestMessageDepth'];
|
||||
getMessages: ReturnType<typeof useChatContext>['getMessages'];
|
||||
setMessages: ReturnType<typeof useChatContext>['setMessages'];
|
||||
}
|
||||
|
||||
type MessagesOperations = Pick<
|
||||
export {
|
||||
MessagesViewContext,
|
||||
MessagesViewProvider,
|
||||
useMessagesSubmission,
|
||||
useMessagesOperations,
|
||||
useMessagesViewContext,
|
||||
useMessagesIsSubmitting,
|
||||
useMessagesConversation,
|
||||
MessagesOperationsContext,
|
||||
MessagesSubmittingContext,
|
||||
useIsMessagesViewReadOnly,
|
||||
useOptionalMessagesOperations,
|
||||
useOptionalMessagesConversation,
|
||||
} from '@librechat/chat/react';
|
||||
export type {
|
||||
MessagesEditing,
|
||||
MessagesOperations,
|
||||
MessagesViewContextValue,
|
||||
'ask' | 'regenerate' | 'handleContinue' | 'getMessages' | 'setMessages'
|
||||
>;
|
||||
|
||||
/** The view's conversation with the operations an inline editor writes through. */
|
||||
export type MessagesEditing = Pick<
|
||||
MessagesViewContextValue,
|
||||
'conversation' | 'ask' | 'getMessages' | 'setMessages'
|
||||
>;
|
||||
|
||||
const MessagesViewContext = createContext<MessagesViewContextValue | undefined>(undefined);
|
||||
|
||||
/**
|
||||
* The view's operations alone. They are referentially stable, so components that
|
||||
* only call them (every message row's hover actions) are not re-rendered by the
|
||||
* submission and tail changes the combined context carries on each send.
|
||||
*/
|
||||
const MessagesOperationsContext = createContext<MessagesOperations | undefined>(undefined);
|
||||
|
||||
/**
|
||||
* Whether the view is generating, alone. A boolean context re-renders its consumers
|
||||
* only when a send starts or settles, which is when every row's rerun controls flip.
|
||||
*/
|
||||
const MessagesSubmittingContext = createContext(false);
|
||||
|
||||
// Export the contexts so they can be provided by other providers (e.g., ShareMessagesProvider)
|
||||
export { MessagesViewContext, MessagesOperationsContext, MessagesSubmittingContext };
|
||||
export type { MessagesViewContextValue, MessagesOperations };
|
||||
|
||||
export function MessagesViewProvider({ children }: { children: React.ReactNode }) {
|
||||
const chatContext = useChatContext();
|
||||
|
||||
const {
|
||||
ask,
|
||||
index,
|
||||
regenerate,
|
||||
isSubmitting,
|
||||
conversation,
|
||||
latestMessageId,
|
||||
latestMessageDepth,
|
||||
setAbortScroll,
|
||||
handleContinue,
|
||||
abortScroll,
|
||||
getMessages,
|
||||
setMessages,
|
||||
} = chatContext;
|
||||
|
||||
/** Memoize conversation-related values */
|
||||
const conversationValues = useMemo(
|
||||
() => ({
|
||||
conversation,
|
||||
conversationId: conversation?.conversationId,
|
||||
}),
|
||||
[conversation],
|
||||
);
|
||||
|
||||
/** Memoize submission states */
|
||||
const submissionStates = useMemo(
|
||||
() => ({
|
||||
abortScroll,
|
||||
isSubmitting,
|
||||
setAbortScroll,
|
||||
}),
|
||||
[isSubmitting, abortScroll, setAbortScroll],
|
||||
);
|
||||
|
||||
/** Memoize message operations (these are typically stable references) */
|
||||
const messageOperations = useMemo(
|
||||
() => ({
|
||||
ask,
|
||||
regenerate,
|
||||
getMessages,
|
||||
setMessages,
|
||||
handleContinue,
|
||||
}),
|
||||
[ask, regenerate, handleContinue, getMessages, setMessages],
|
||||
);
|
||||
|
||||
/** Memoize message state values */
|
||||
const messageState = useMemo(
|
||||
() => ({
|
||||
index,
|
||||
latestMessageId,
|
||||
latestMessageDepth,
|
||||
}),
|
||||
[index, latestMessageId, latestMessageDepth],
|
||||
);
|
||||
|
||||
/** Combine all values into final context value */
|
||||
const contextValue = useMemo<MessagesViewContextValue>(
|
||||
() => ({
|
||||
readOnly: false,
|
||||
...conversationValues,
|
||||
...submissionStates,
|
||||
...messageOperations,
|
||||
...messageState,
|
||||
}),
|
||||
[conversationValues, submissionStates, messageOperations, messageState],
|
||||
);
|
||||
|
||||
return (
|
||||
<MessagesOperationsContext.Provider value={messageOperations}>
|
||||
<MessagesSubmittingContext.Provider value={isSubmitting}>
|
||||
<MessagesViewContext.Provider value={contextValue}>{children}</MessagesViewContext.Provider>
|
||||
</MessagesSubmittingContext.Provider>
|
||||
</MessagesOperationsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether the view is generating; false outside a live messages view. */
|
||||
export function useMessagesIsSubmitting(): boolean {
|
||||
return useContext(MessagesSubmittingContext);
|
||||
}
|
||||
|
||||
export function useMessagesViewContext() {
|
||||
const context = useContext(MessagesViewContext);
|
||||
if (!context) {
|
||||
throw new Error('useMessagesViewContext must be used within MessagesViewProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/** Defaults to read-only when no provider is present, so live auth-bearing app actions stay off. */
|
||||
export function useIsMessagesViewReadOnly(): boolean {
|
||||
return useContext(MessagesViewContext)?.readOnly ?? true;
|
||||
}
|
||||
|
||||
/** Hook for components that only need conversation data */
|
||||
export function useMessagesConversation() {
|
||||
const { conversation, conversationId } = useMessagesViewContext();
|
||||
return useMemo(() => ({ conversation, conversationId }), [conversation, conversationId]);
|
||||
}
|
||||
|
||||
/** Hook for components that only need submission states */
|
||||
export function useMessagesSubmission() {
|
||||
const { isSubmitting, abortScroll, setAbortScroll } = useMessagesViewContext();
|
||||
return useMemo(
|
||||
() => ({ isSubmitting, abortScroll, setAbortScroll }),
|
||||
[isSubmitting, abortScroll, setAbortScroll],
|
||||
);
|
||||
}
|
||||
|
||||
/** Hook for components that only need message operations */
|
||||
export function useMessagesOperations(): MessagesOperations {
|
||||
const context = useContext(MessagesOperationsContext);
|
||||
if (!context) {
|
||||
throw new Error('useMessagesOperations must be used within MessagesViewProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
const NOOP_OPS: MessagesOperations = {
|
||||
ask: () => {},
|
||||
regenerate: () => {},
|
||||
handleContinue: () => {},
|
||||
getMessages: () => undefined,
|
||||
setMessages: () => {},
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook for components that need message operations but may render outside MessagesViewProvider
|
||||
* (e.g. the /search route). Returns no-op stubs when the provider is absent — UI actions will
|
||||
* be silently discarded rather than crashing. Callers must use optional chaining on
|
||||
* `getMessages()` results, as it returns `undefined` outside the provider.
|
||||
*/
|
||||
export function useOptionalMessagesOperations(): MessagesOperations {
|
||||
return useContext(MessagesOperationsContext) ?? NOOP_OPS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for components that need conversation data but may render outside MessagesViewProvider
|
||||
* (e.g. the /search route). Returns `undefined` for both fields when the provider is absent.
|
||||
*/
|
||||
export function useOptionalMessagesConversation() {
|
||||
const context = useContext(MessagesViewContext);
|
||||
const conversation = context?.conversation;
|
||||
const conversationId = context?.conversationId;
|
||||
return useMemo(() => ({ conversation, conversationId }), [conversation, conversationId]);
|
||||
}
|
||||
} from '@librechat/chat/react';
|
||||
|
|
|
|||
|
|
@ -13,16 +13,20 @@ import {
|
|||
ComposerRestoreProvider,
|
||||
pendingApprovalActionFamily,
|
||||
} from '@librechat/chat/react';
|
||||
import type { ChatScreenMessagesSlots } from '@librechat/chat/components';
|
||||
import type { TChatProject, TMessage } from 'librechat-data-provider';
|
||||
import type { ChatFormValues } from '~/common';
|
||||
import { AddedChatContext, ChatFormProvider, useChatContext, useFileMapContext } from '~/Providers';
|
||||
import Footer, { useLandingFooter, useConfiguredFooter } from './Footer';
|
||||
import PendingSteers from './Messages/Content/Parts/PendingSteers';
|
||||
import { useChatSettings } from '~/Providers/ChatSettingsContext';
|
||||
import ApprovalProvider from './Messages/Content/ApprovalContext';
|
||||
import ConversationStarters from './Input/ConversationStarters';
|
||||
import ProjectBadge from '~/components/Projects/ProjectBadge';
|
||||
import { OwnerTextProvider } from './Messages/PrivateText';
|
||||
import MessagesView from './Messages/MessagesView';
|
||||
import ScrollButton from './Messages/ScrollButton';
|
||||
import PendingTurn from './Messages/PendingTurn';
|
||||
import { FLAT_THREAD } from './Messages/Thread';
|
||||
import { useAddedResponse } from '~/hooks';
|
||||
import Presentation from './Presentation';
|
||||
import ChatForm from './Input/ChatForm';
|
||||
|
|
@ -32,6 +36,14 @@ import Landing from './Landing';
|
|||
import Header from './Header';
|
||||
import store from '~/store';
|
||||
|
||||
/** The app's pieces of the package message list. */
|
||||
const messagesSlots: ChatScreenMessagesSlots = {
|
||||
flatThread: FLAT_THREAD,
|
||||
renderPendingTurn: (props) => <PendingTurn {...props} />,
|
||||
renderPendingSteers: (props) => <PendingSteers {...props} />,
|
||||
renderScrollButton: (props) => <ScrollButton {...props} />,
|
||||
};
|
||||
|
||||
type ChatViewProps = {
|
||||
index?: number;
|
||||
project?: TChatProject;
|
||||
|
|
@ -101,7 +113,7 @@ function ChatViewPane({
|
|||
<Header parentConversationId={parentConversationId} readOnly={readOnly} />
|
||||
)}
|
||||
renderBadge={(projectId) => <ProjectBadge projectId={projectId} />}
|
||||
renderMessages={(thread) => <MessagesView {...thread} />}
|
||||
messagesSlots={messagesSlots}
|
||||
landing={<Landing centerFormOnLanding={centerFormOnLanding} />}
|
||||
renderColumn={(column) => (
|
||||
<OwnerTextProvider
|
||||
|
|
|
|||
|
|
@ -1,54 +0,0 @@
|
|||
import { useChatSurface } from '@librechat/chat/react';
|
||||
import { MessageNav, MessagesView as MessagesList } from '@librechat/chat/components';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import { MessagePartsHostProvider, appMessagePartsHost } from '~/Providers/MessagePartsHostContext';
|
||||
import PendingSteers from './Content/Parts/PendingSteers';
|
||||
import { MessagesViewProvider } from '~/Providers';
|
||||
import ScrollButton from './ScrollButton';
|
||||
import PendingTurn from './PendingTurn';
|
||||
import { FLAT_THREAD } from './Thread';
|
||||
|
||||
function MessagesViewContent({
|
||||
messagesTree,
|
||||
messages,
|
||||
}: {
|
||||
messagesTree?: TMessage[] | null;
|
||||
messages?: TMessage[] | null;
|
||||
}) {
|
||||
const { showScrollButton, maximizeChatSpace } = useChatSurface();
|
||||
return (
|
||||
<MessagesList
|
||||
messagesTree={messagesTree}
|
||||
messages={messages}
|
||||
showScrollButton={showScrollButton}
|
||||
maximizeChatSpace={maximizeChatSpace}
|
||||
flatThread={FLAT_THREAD}
|
||||
renderPendingTurn={(props) => <PendingTurn {...props} />}
|
||||
renderPendingSteers={(props) => <PendingSteers {...props} />}
|
||||
renderScrollButton={(props) => <ScrollButton {...props} />}
|
||||
renderNav={({ conversationId, scrollableRef }) => (
|
||||
<MessageNav
|
||||
key={conversationId}
|
||||
conversationId={conversationId}
|
||||
scrollableRef={scrollableRef}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function MessagesView({
|
||||
messagesTree,
|
||||
messages,
|
||||
}: {
|
||||
messagesTree?: TMessage[] | null;
|
||||
messages?: TMessage[] | null;
|
||||
}) {
|
||||
return (
|
||||
<MessagesViewProvider>
|
||||
<MessagePartsHostProvider host={appMessagePartsHost}>
|
||||
<MessagesViewContent messagesTree={messagesTree} messages={messages} />
|
||||
</MessagePartsHostProvider>
|
||||
</MessagesViewProvider>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,266 +0,0 @@
|
|||
import React from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { ChatContext, setDefaultMessagesHost } from '@librechat/chat/react';
|
||||
import type { ChatContract, MessagesHost } from '@librechat/chat/react';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import type { ReactElement } from 'react';
|
||||
|
||||
let mockMessagesKey = 'convo-1';
|
||||
let mockMaximizeChatSpace = false;
|
||||
let mockFlatThread = false;
|
||||
const mockNavMount = jest.fn();
|
||||
const mockNavUnmount = jest.fn();
|
||||
|
||||
jest.mock('jotai', () => ({
|
||||
...jest.requireActual('jotai'),
|
||||
useAtomValue: () => false,
|
||||
}));
|
||||
|
||||
jest.mock('recoil', () => ({
|
||||
useRecoilValue: () => false,
|
||||
}));
|
||||
|
||||
jest.mock('~/Providers', () => ({
|
||||
MessagesViewProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
useFileMapContext: () => ({}),
|
||||
}));
|
||||
|
||||
jest.mock('@librechat/chat/react', () => ({
|
||||
...jest.requireActual('@librechat/chat/react'),
|
||||
useChatSurface: () => ({ showScrollButton: false, maximizeChatSpace: mockMaximizeChatSpace }),
|
||||
}));
|
||||
|
||||
jest.mock('~/store', () => ({
|
||||
__esModule: true,
|
||||
default: {},
|
||||
}));
|
||||
|
||||
jest.mock('../Thread', () => ({
|
||||
get FLAT_THREAD() {
|
||||
return mockFlatThread;
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('../Content/Parts/PendingSteers', () => ({
|
||||
__esModule: true,
|
||||
default: ({ conversationId, fullWidth }: { conversationId: string; fullWidth: boolean }) => (
|
||||
<div
|
||||
data-testid="pending-steers"
|
||||
data-conversation-id={conversationId}
|
||||
data-full-width={String(fullWidth)}
|
||||
/>
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock('../PendingTurn', () => ({
|
||||
__esModule: true,
|
||||
default: () => null,
|
||||
}));
|
||||
jest.mock('../ScrollButton', () => ({
|
||||
__esModule: true,
|
||||
default: () => null,
|
||||
}));
|
||||
jest.mock('@librechat/chat/components', () => {
|
||||
const { useEffect } = jest.requireActual('react');
|
||||
return {
|
||||
...jest.requireActual('@librechat/chat/components'),
|
||||
MessageNav: function MessageNav({ conversationId }: { conversationId?: string | null }) {
|
||||
useEffect(() => {
|
||||
mockNavMount();
|
||||
return () => mockNavUnmount();
|
||||
}, []);
|
||||
return <div data-testid="message-nav" data-conversation-id={conversationId} />;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
import MessagesView from '../MessagesView';
|
||||
|
||||
/** Rows render as their message id, so a spec can tell which messages the thread rendered. */
|
||||
const host: MessagesHost = {
|
||||
useHour12: () => undefined,
|
||||
renderFiles: () => null,
|
||||
useForkPreferences: () => ({
|
||||
option: '',
|
||||
setOption: () => undefined,
|
||||
splitAtTarget: false,
|
||||
setSplitAtTarget: () => undefined,
|
||||
rememberOption: false,
|
||||
setRememberOption: () => undefined,
|
||||
}),
|
||||
useForkConversation: () => null,
|
||||
useMessageCopy: () => ({ copyToClipboard: () => undefined, canCopy: false }),
|
||||
useGetMessages: () => () => undefined,
|
||||
useHasPendingTurn: () => false,
|
||||
renderMessageAudio: () => null,
|
||||
useScreenshotTarget: () => undefined,
|
||||
useAutoScroll: () => false,
|
||||
useConversationMessages: () => undefined,
|
||||
useAssistant: () => undefined,
|
||||
useGetAddedConvo: () => () => null,
|
||||
useStreamedAttachments: () => undefined,
|
||||
useMessageFeedback: () => () => undefined,
|
||||
useSeenSource: () => ({
|
||||
readReply: () => undefined,
|
||||
hasReplyProof: () => false,
|
||||
markSeen: () => undefined,
|
||||
consumeFocusSuppression: () => false,
|
||||
}),
|
||||
renderMessageIcon: () => null,
|
||||
renderMessageRow: ({ message }) => <div data-testid="row">{message.messageId}</div>,
|
||||
renderActivity: () => null,
|
||||
useFileMap: () => undefined,
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
setDefaultMessagesHost(host);
|
||||
});
|
||||
|
||||
const renderedRows = () => screen.queryAllByTestId('row').map((row) => row.textContent);
|
||||
|
||||
/** The pane's chat contract, as far as the list reads it: the pane and the routed key. */
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
const withChat = (ui: ReactElement) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<ChatContext.Provider
|
||||
value={
|
||||
{
|
||||
index: 0,
|
||||
isSubmitting: false,
|
||||
latestMessageDepth: 0,
|
||||
messagesKey: mockMessagesKey,
|
||||
} as unknown as ChatContract
|
||||
}
|
||||
>
|
||||
{ui}
|
||||
</ChatContext.Provider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
|
||||
const messageTree = [
|
||||
{
|
||||
messageId: 'assistant-1',
|
||||
conversationId: 'convo-1',
|
||||
},
|
||||
] as unknown as TMessage[];
|
||||
|
||||
describe('MessagesView pending steers', () => {
|
||||
beforeEach(() => {
|
||||
mockMessagesKey = 'convo-1';
|
||||
mockMaximizeChatSpace = false;
|
||||
mockFlatThread = false;
|
||||
});
|
||||
|
||||
it('keeps the failed-steer surface mounted in the recursive renderer', () => {
|
||||
render(withChat(<MessagesView messagesTree={messageTree} messages={messageTree} />));
|
||||
|
||||
expect(renderedRows()).toEqual(['assistant-1']);
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-conversation-id', 'convo-1');
|
||||
});
|
||||
|
||||
it.each([
|
||||
[true, 'from-messages'],
|
||||
[false, 'from-tree'],
|
||||
])('renders the package thread with FLAT_THREAD %s', (flat, rendered) => {
|
||||
mockFlatThread = flat;
|
||||
const tree = [{ ...messageTree[0], messageId: 'from-tree' }];
|
||||
const messages = [{ ...messageTree[0], messageId: 'from-messages' }];
|
||||
render(withChat(<MessagesView messagesTree={tree} messages={messages} />));
|
||||
|
||||
expect(renderedRows()).toEqual([rendered]);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
'passes the host chat width preference (%s) to pending steers',
|
||||
(fullWidth) => {
|
||||
mockMaximizeChatSpace = fullWidth;
|
||||
render(withChat(<MessagesView messagesTree={messageTree} messages={messageTree} />));
|
||||
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute(
|
||||
'data-full-width',
|
||||
String(fullWidth),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it('passes the full-width preference in the flat renderer too', () => {
|
||||
mockMaximizeChatSpace = true;
|
||||
mockFlatThread = true;
|
||||
render(withChat(<MessagesView messagesTree={messageTree} messages={messageTree} />));
|
||||
|
||||
expect(renderedRows()).toEqual(['assistant-1']);
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-full-width', 'true');
|
||||
});
|
||||
|
||||
it('keys the pending surface to the rendered tree, not the lagging context', () => {
|
||||
/** Warm-cache navigation renders the destination tree while the Recoil
|
||||
* conversation still names the source chat. Cancel and Escalate must act
|
||||
* on the run the reader is looking at. */
|
||||
const destinationTree = [
|
||||
{ messageId: 'assistant-2', conversationId: 'convo-2' },
|
||||
] as unknown as TMessage[];
|
||||
|
||||
render(withChat(<MessagesView messagesTree={destinationTree} messages={destinationTree} />));
|
||||
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-conversation-id', 'convo-2');
|
||||
});
|
||||
|
||||
it('shows the empty note instead of a thread while the destination tree is empty', () => {
|
||||
mockFlatThread = true;
|
||||
mockMessagesKey = 'convo-2';
|
||||
render(withChat(<MessagesView messagesTree={[]} messages={[]} />));
|
||||
|
||||
expect(screen.getByText('Nothing found')).toBeInTheDocument();
|
||||
expect(renderedRows()).toEqual([]);
|
||||
});
|
||||
|
||||
it('pins the screenshot identity to the rendered transcript and replaces it on navigation', () => {
|
||||
const view = render(
|
||||
withChat(<MessagesView messagesTree={messageTree} messages={messageTree} />),
|
||||
);
|
||||
const initialTarget = screen.getByTestId('screenshot-target');
|
||||
expect(initialTarget).toHaveAttribute('data-conversation-id', 'convo-1');
|
||||
const destinationTree = [{ ...messageTree[0], conversationId: 'convo-2' }];
|
||||
view.rerender(
|
||||
withChat(<MessagesView messagesTree={destinationTree} messages={destinationTree} />),
|
||||
);
|
||||
const destinationTarget = screen.getByTestId('screenshot-target');
|
||||
expect(destinationTarget).toHaveAttribute('data-conversation-id', 'convo-2');
|
||||
expect(destinationTarget).not.toBe(initialTarget);
|
||||
expect(initialTarget.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('releases the outgoing navigation rail on a warm switch, not on same-chat updates', () => {
|
||||
const view = render(
|
||||
withChat(<MessagesView messagesTree={messageTree} messages={messageTree} />),
|
||||
);
|
||||
expect(mockNavMount).toHaveBeenCalledTimes(1);
|
||||
const updated = [{ ...messageTree[0], text: 'updated' }];
|
||||
view.rerender(withChat(<MessagesView messagesTree={updated} messages={updated} />));
|
||||
expect(mockNavMount).toHaveBeenCalledTimes(1);
|
||||
expect(mockNavUnmount).not.toHaveBeenCalled();
|
||||
|
||||
const destination = [{ ...messageTree[0], conversationId: 'convo-2' }];
|
||||
view.rerender(withChat(<MessagesView messagesTree={destination} messages={destination} />));
|
||||
|
||||
expect(mockNavUnmount).toHaveBeenCalledTimes(1);
|
||||
expect(mockNavMount).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('keeps recovery visible while the message tree is temporarily empty', () => {
|
||||
render(withChat(<MessagesView messagesTree={[]} messages={[]} />));
|
||||
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-conversation-id', 'convo-1');
|
||||
});
|
||||
|
||||
it('keys an empty destination to the route, not the lagging context', () => {
|
||||
/** Warm navigation to a chat whose tree is still empty: no message names the
|
||||
* destination, and the context still names the source run. */
|
||||
mockMessagesKey = 'convo-2';
|
||||
render(withChat(<MessagesView messagesTree={[]} messages={[]} />));
|
||||
|
||||
expect(screen.getByTestId('pending-steers')).toHaveAttribute('data-conversation-id', 'convo-2');
|
||||
});
|
||||
});
|
||||
120
client/src/components/Chat/__tests__/ChatView.slots.spec.tsx
Normal file
120
client/src/components/Chat/__tests__/ChatView.slots.spec.tsx
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
import React, { createRef, isValidElement } from 'react';
|
||||
import '@testing-library/jest-dom';
|
||||
import type { ChatScreenMessagesSlots, ChatScreenProps } from '@librechat/chat/components';
|
||||
import type { ReactElement, ReactNode } from 'react';
|
||||
import PendingSteers from '../Messages/Content/Parts/PendingSteers';
|
||||
import ScrollButton from '../Messages/ScrollButton';
|
||||
import PendingTurn from '../Messages/PendingTurn';
|
||||
import { FLAT_THREAD } from '../Messages/Thread';
|
||||
import { render } from 'test/layout-test-utils';
|
||||
import ChatView from '../ChatView';
|
||||
|
||||
const mockScreenProps = jest.fn<void, [ChatScreenProps]>();
|
||||
|
||||
jest.mock('react-router-dom', () => ({
|
||||
...jest.requireActual('react-router-dom'),
|
||||
useParams: () => ({ conversationId: 'convo-1' }),
|
||||
}));
|
||||
|
||||
jest.mock('~/hooks/AuthContext', () => ({
|
||||
AuthContextProvider: ({ children }: { children: React.ReactNode }) => children,
|
||||
useAuthContext: () => ({ isAuthenticated: false, user: null, roles: {} }),
|
||||
}));
|
||||
|
||||
jest.mock('~/data-provider', () => ({
|
||||
useProjectQuery: () => ({ data: undefined }),
|
||||
}));
|
||||
|
||||
jest.mock('@librechat/chat/react', () => {
|
||||
const actual = jest.requireActual('@librechat/chat/react');
|
||||
return {
|
||||
...actual,
|
||||
ChatSessionProvider: ({
|
||||
children,
|
||||
}: {
|
||||
children: (session: { messages: null; isLoading: boolean }) => React.ReactNode;
|
||||
}) => (
|
||||
<actual.ChatContext.Provider value={{ conversation: null }}>
|
||||
{children({ messages: null, isLoading: false })}
|
||||
</actual.ChatContext.Provider>
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
/** The screen is the subject's receiver: record what ChatView hands it. */
|
||||
jest.mock('@librechat/chat/components', () => ({
|
||||
...jest.requireActual('@librechat/chat/components'),
|
||||
ChatScreen: (props: ChatScreenProps) => {
|
||||
mockScreenProps(props);
|
||||
return null;
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('~/hooks', () => ({
|
||||
useLocalize: () => (key: string) => key,
|
||||
useAddedResponse: () => ({}),
|
||||
}));
|
||||
|
||||
jest.mock('../Presentation', () => ({
|
||||
__esModule: true,
|
||||
default: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
|
||||
}));
|
||||
jest.mock('../Footer', () => ({
|
||||
__esModule: true,
|
||||
default: () => null,
|
||||
useConfiguredFooter: () => false,
|
||||
useLandingFooter: () => true,
|
||||
}));
|
||||
|
||||
const screenSlots = (): ChatScreenMessagesSlots => {
|
||||
render(<ChatView />);
|
||||
const props = mockScreenProps.mock.calls.at(-1)?.[0];
|
||||
expect(props?.renderMessages).toBeUndefined();
|
||||
return props?.messagesSlots ?? {};
|
||||
};
|
||||
|
||||
const scrollableRef = createRef<HTMLDivElement>();
|
||||
|
||||
/** A slot's output as an element, failing the test when the slot rendered nothing. */
|
||||
function element(node: ReactNode): ReactElement {
|
||||
if (!isValidElement(node)) {
|
||||
throw new Error('The slot did not render an element');
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
describe('ChatView message list slots', () => {
|
||||
test('leaves the list to ChatScreen and fills it with the app thread mode', () => {
|
||||
expect(screenSlots().flatThread).toBe(FLAT_THREAD);
|
||||
});
|
||||
|
||||
test('renders the app pending turn, steers and scroll control with what the list passes', () => {
|
||||
const slots = screenSlots();
|
||||
const turn = { scrollableRef, messages: null, maximizeChatSpace: true };
|
||||
const steers = { conversationId: 'convo-1', index: 0, fullWidth: false };
|
||||
const scroll = {
|
||||
conversationId: 'convo-1',
|
||||
enabled: true,
|
||||
maximizeChatSpace: false,
|
||||
scrollableRef,
|
||||
messagesEndRef: createRef<HTMLDivElement>(),
|
||||
scrollHandler: () => undefined,
|
||||
onNearBottomChange: () => undefined,
|
||||
};
|
||||
|
||||
const turnElement = element(slots.renderPendingTurn?.(turn));
|
||||
const steersElement = element(slots.renderPendingSteers?.(steers));
|
||||
const scrollElement = element(slots.renderScrollButton?.(scroll));
|
||||
|
||||
expect(turnElement.type).toBe(PendingTurn);
|
||||
expect(turnElement.props).toEqual(turn);
|
||||
expect(steersElement.type).toBe(PendingSteers);
|
||||
expect(steersElement.props).toEqual(steers);
|
||||
expect(scrollElement.type).toBe(ScrollButton);
|
||||
expect(scrollElement.props).toEqual(scroll);
|
||||
});
|
||||
|
||||
test('leaves the navigation rail to the package list', () => {
|
||||
expect(screenSlots().renderNav).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
@ -67,7 +67,6 @@ jest.mock('../Footer', () => ({
|
|||
}));
|
||||
jest.mock('../Landing', () => ({ __esModule: true, default: () => <div /> }));
|
||||
jest.mock('~/components/Lia', () => ({ __esModule: true, default: () => null }));
|
||||
jest.mock('../Messages/MessagesView', () => ({ __esModule: true, default: () => <div /> }));
|
||||
jest.mock('../Input/ChatForm', () => ({
|
||||
__esModule: true,
|
||||
default: (props: Record<string, unknown>) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
import React from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { MessagesHostTestProvider } from 'test/messagesHost';
|
||||
import { ChatSurfaceHarness } from 'test/harness';
|
||||
import ChatView from '../ChatView';
|
||||
/** The app registers its parts host at boot (App.jsx); the child's list reads it. */
|
||||
import '~/Providers/MessagePartsHostContext';
|
||||
|
||||
let mockConversation: Record<string, unknown> | null;
|
||||
|
||||
|
|
@ -22,6 +26,7 @@ jest.mock('@librechat/client', () => ({
|
|||
Spinner: () => <div data-testid="spinner" />,
|
||||
cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '),
|
||||
useLocalize: () => (key: string) => key,
|
||||
useRemScale: () => 1,
|
||||
}));
|
||||
|
||||
jest.mock('librechat-data-provider', () => ({
|
||||
|
|
@ -72,7 +77,10 @@ jest.mock('~/data-provider', () => ({
|
|||
}));
|
||||
|
||||
jest.mock('../Input/ConversationStarters', () => () => null);
|
||||
jest.mock('../Messages/MessagesView', () => () => <div data-testid="messages" />);
|
||||
/** The package list renders the child's messages; the app's pieces of it are not under test. */
|
||||
jest.mock('../Messages/PendingTurn', () => () => null);
|
||||
jest.mock('../Messages/ScrollButton', () => () => null);
|
||||
jest.mock('../Messages/Content/Parts/PendingSteers', () => () => null);
|
||||
jest.mock('../Presentation', () => ({ children }: { children: React.ReactNode }) => (
|
||||
<>{children}</>
|
||||
));
|
||||
|
|
@ -117,7 +125,13 @@ describe('ChatView child-thread execution identity', () => {
|
|||
},
|
||||
};
|
||||
|
||||
render(<ChatView />);
|
||||
render(
|
||||
<ChatSurfaceHarness>
|
||||
<MessagesHostTestProvider>
|
||||
<ChatView />
|
||||
</MessagesHostTestProvider>
|
||||
</ChatSurfaceHarness>,
|
||||
);
|
||||
|
||||
expect(screen.queryByTestId('chat-form')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('note')).toHaveTextContent('com_ui_subagent_thread_read_only');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue