🧪 test: Prove the @librechat/chat Public Entries With a Consumer Spec (#17522)

* feat: Add a Minimal Consumer Example for @librechat/chat

A Vite React page under packages/chat/example renders a themed chat from the package's public entries and the @librechat/client theme, with an in-memory transport standing in for the server. Its jsdom smoke test runs in the package's jest suite, the package typecheck covers it, and ESLint keeps it to public imports and semantic theme roles.

* fix: Let Enter Confirm an IME Composition in the Example Composer

* fix: Report a Failed Turn in the Example and Restore Its Text

* fix: Localize the Example Copy and Guard the Safari IME Enter

* fix: Drop a Rejected Turn, Load the Component CSS, and Keep the i18n Instance in the Example Build

* refactor: Replace the Example App With a Public-Entry Consumer Spec

The owner does not want an example app in the repository. packages/chat/test/consumer.spec.tsx keeps what it proved: a chat built only from @librechat/chat, /react, /components and @librechat/client, over an in-memory transport, streams a reply into the message list under the ClickHouse theme. It sits outside src because chat/boundary rejects self-imports there; jest maps the entry names to the sources and the package typecheck covers it. The example app, its Vite, Tailwind and PostCSS config, its lint block, script and dependency lines are removed.

* chore: Declare the Consumer Spec's i18next Dev Dependencies

* chore: Retest Against Current Dev
This commit is contained in:
Marco Beretta 2026-10-29 09:54:20 +01:00 • committed by GitHub
parent 06065af0f3
commit b657c40e4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 332 additions and 1 deletions

View file

@ -453,6 +453,7 @@
"@types/jest": "^29.5.2",
"@types/react": "^18.2.11",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"i18next": "^24.2.2",
"jest": "^30.2.0",
"jest-environment-jsdom": "^30.2.0",
"jotai": "^2.12.5",
@ -461,6 +462,7 @@
"lucide-react": "^0.528.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^15.4.0",
"rimraf": "^6.1.3",
"tsdown": "0.22.2",
"typescript": "^5.9.3",

2
package-lock.json generated
View file

@ -41458,6 +41458,7 @@
"@types/jest": "^29.5.2",
"@types/react": "^18.2.11",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"i18next": "^24.2.2",
"jest": "^30.2.0",
"jest-environment-jsdom": "^30.2.0",
"jotai": "^2.12.5",
@ -41466,6 +41467,7 @@
"lucide-react": "^0.528.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^15.4.0",
"rimraf": "^6.1.3",
"tsdown": "0.22.2",
"typescript": "^5.9.3",

View file

@ -13,6 +13,12 @@ module.exports = {
],
// The component library's resolver: its ESM-only @ariakit packages need the `import` condition.
resolver: '<rootDir>/../client/jest.resolver.cjs',
// `test/` imports the package by its published entry names, as a consumer would; this points
// those names at the sources so the suite needs no package build.
moduleNameMapper: {
'^@librechat/chat$': '<rootDir>/src/index.ts',
'^@librechat/chat/(react|components)$': '<rootDir>/src/$1.ts',
},
// The core runs without a DOM; a React spec opts into jsdom with a docblock.
testEnvironment: 'node',
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],

View file

@ -53,7 +53,7 @@
"build": "npm run clean && tsdown",
"b:build": "bun run b:clean && bun run tsdown",
"build:watch": "tsdown --watch",
"typecheck": "node ../../scripts/typecheck.mts",
"typecheck": "node ../../scripts/typecheck.mts && node ../../scripts/typecheck.mts -p test/tsconfig.json",
"test": "jest",
"test:ci": "jest --ci"
},
@ -81,6 +81,7 @@
"@types/jest": "^29.5.2",
"@types/react": "^18.2.11",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"i18next": "^24.2.2",
"jest": "^30.2.0",
"jest-environment-jsdom": "^30.2.0",
"jotai": "^2.12.5",
@ -89,6 +90,7 @@
"lucide-react": "^0.528.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^15.4.0",
"rimraf": "^6.1.3",
"tsdown": "0.22.2",
"typescript": "^5.9.3",

View file

@ -0,0 +1,304 @@
/**
* @jest-environment jsdom
*/
import { useEffect, useMemo, useState, useSyncExternalStore } from 'react';
import { Chat } from '@librechat/chat';
import { createInstance } from 'i18next';
import { ContentTypes } from 'librechat-data-provider';
import { Part, Text } from '@librechat/chat/components';
import { initReactI18next, I18nextProvider } from 'react-i18next';
import { act, fireEvent, render, screen, within } from '@testing-library/react';
import { Button, Textarea, ThemeProvider, clickHouseTheme } from '@librechat/client';
import { MessageContext, MessagePartIndex, MessagePartsHostProvider } from '@librechat/chat/react';
import type { ChatEvent, TMessage, TMessageContentParts } from 'librechat-data-provider';
import type { ChatState, ChatStream, ChatTransport } from '@librechat/chat';
import type { PartRenderers } from '@librechat/chat/components';
import type { MessagePartsHost } from '@librechat/chat/react';
import type { ReactElement } from 'react';
/**
* An outside consumer of the package: it reaches LibreChat's chat only through the published
* entries (`@librechat/chat`, `/react`, `/components`) and the `@librechat/client` theme, with an
* in-memory transport in place of a server. It grows as the session, message list and composer
* move into the package.
*/
/** A stream the test feeds frame by frame. */
function createStream(): ChatStream & { push(event: ChatEvent): void } {
const queued: ChatEvent[] = [];
let wake: (() => void) | undefined;
let closed = false;
return {
push(event) {
queued.push(event);
wake?.();
},
close() {
closed = true;
wake?.();
},
async *[Symbol.asyncIterator]() {
while (true) {
while (queued.length > 0) {
yield queued.shift() as ChatEvent;
}
if (closed) {
return;
}
await new Promise<void>((resolve) => {
wake = resolve;
});
}
},
};
}
/** Serves each turn from a stream the test drives. */
function createMemoryTransport(): ChatTransport & { stream?: ReturnType<typeof createStream> } {
const transport: ChatTransport & { stream?: ReturnType<typeof createStream> } = {
async sendMessages() {
transport.stream = createStream();
return { status: 'stream', stream: transport.stream };
},
async reconnectToStream() {
return null;
},
async abort() {
return { success: true };
},
};
return transport;
}
function createState(): ChatState {
let messages: TMessage[] = [];
return {
status: 'ready',
error: undefined,
connection: 'idle',
get messages() {
return messages;
},
replaceMessages(next) {
messages = next;
},
};
}
let ids = 0;
/** The consumer's own turn building: the request plus its optimistic user message and reply. */
function createChat(transport: ChatTransport): Chat {
return new Chat({
id: 'convo',
state: createState(),
transport,
prepareRequest({ input, messages }) {
const text = input?.parts.map((part) => part.text).join('') ?? '';
const parentMessageId = messages[messages.length - 1]?.messageId ?? null;
const messageId = `user-${++ids}`;
const responseMessageId = `reply-${ids}`;
const base = { conversationId: 'convo' };
const user: TMessage = { ...base, messageId, parentMessageId, isCreatedByUser: true, text };
const reply: TMessage = {
...base,
messageId: responseMessageId,
parentMessageId: messageId,
isCreatedByUser: false,
text: '',
};
return {
payload: {
...base,
text,
messageId,
parentMessageId,
isContinued: false,
isTemporary: true,
},
messages: (current) => [...current, user, reply],
responseMessageId,
};
},
/** Cumulative `text` frames fill the reply as it streams; the `final` frame settles it. */
applyEvent(event, context) {
if (event.type === 'text') {
const text = event.data.text ?? event.data.response ?? '';
context.write(
context.messages.map((message) =>
message.messageId === event.data.messageId ? { ...message, text } : message,
),
);
}
const response = event.type === 'final' ? event.data.responseMessage : undefined;
if (response) {
context.write(
context.messages.map((message) =>
message.messageId === response.messageId ? { ...message, ...response } : message,
),
);
}
},
});
}
const host: MessagePartsHost = {
useMessage: () => ({ messageId: '' }),
renderMarkdown: ({ content }) => <p>{content}</p>,
useSmoothStreaming: () => false,
useAgent: () => undefined,
renderAgentIcon: () => null,
useFontSize: () => 'text-base',
useShowThinking: () => false,
useAutoExpandTools: () => false,
useLaTeXParsing: () => false,
useChatDirection: () => 'auto',
useUserTextPreferences: () => ({
enableUserMsgMarkdown: false,
collapseLongUserMessages: false,
usernameDisplay: false,
}),
useUser: () => undefined,
useFileMap: () => undefined,
useToast: () => () => undefined,
useSandboxStarting: () => false,
useToolArtifactClaim: () => [null, () => undefined],
useSteerEscalating: () => false,
usePaneConversationId: () => null,
useLiveAppliedSteer: () => [false, () => undefined],
};
const renderers: PartRenderers = {
parts: {
[ContentTypes.TEXT]: ({ part, showCursor, isCreatedByUser }) => (
<Text
text={typeof part.text === 'string' ? part.text : (part.text?.value ?? '')}
showCursor={showCursor}
isCreatedByUser={isCreatedByUser}
/>
),
},
tools: {},
defaultTool: () => null,
};
const partsOf = (message: TMessage): TMessageContentParts[] =>
message.content?.length
? message.content
: [{ type: ContentTypes.TEXT, text: message.text ?? '' }];
function ChatView({ chat }: { chat: Chat }): ReactElement {
const subscribe = useMemo(() => chat.subscribe.bind(chat), [chat]);
const messages = useSyncExternalStore(subscribe, () => chat.messages);
const status = useSyncExternalStore(subscribe, () => chat.status);
const [text, setText] = useState('');
const send = () => {
setText('');
void chat.sendMessage({ parts: [{ type: 'text', text }] });
};
return (
<MessagePartsHostProvider host={host}>
<ol aria-label="Messages">
{messages.map((message) => (
<li key={message.messageId} aria-label={message.isCreatedByUser ? 'You' : 'Assistant'}>
<MessageContext.Provider value={{ messageId: message.messageId, isExpanded: false }}>
{partsOf(message).map((part, index) => (
<MessagePartIndex key={index} index={index}>
<Part
part={part}
renderers={renderers}
isSubmitting={status !== 'ready'}
showCursor={false}
isCreatedByUser={message.isCreatedByUser}
/>
</MessagePartIndex>
))}
</MessageContext.Provider>
</li>
))}
</ol>
<p role="status">{status}</p>
<label htmlFor="composer">Message</label>
<Textarea id="composer" value={text} onChange={(event) => setText(event.target.value)} />
<Button type="button" onClick={send}>
Send
</Button>
</MessagePartsHostProvider>
);
}
function Consumer({ transport }: { transport: ChatTransport }): ReactElement | null {
const [chat, setChat] = useState<Chat | null>(null);
useEffect(() => {
const next = createChat(transport);
setChat(next);
return () => next.dispose();
}, [transport]);
return chat ? <ChatView chat={chat} /> : null;
}
const i18n = createInstance();
void i18n.use(initReactI18next).init({ lng: 'en', resources: { en: { translation: {} } } });
const renderConsumer = (transport: ChatTransport) =>
render(
<I18nextProvider i18n={i18n}>
<ThemeProvider themeDefinition={clickHouseTheme} persistThemeDefinition={false}>
<Consumer transport={transport} />
</ThemeProvider>
</I18nextProvider>,
);
describe('a consumer built on the public entries', () => {
it('sends a turn and streams the reply into the message list', async () => {
const transport = createMemoryTransport();
renderConsumer(transport);
fireEvent.change(await screen.findByLabelText('Message'), { target: { value: 'Hi' } });
fireEvent.click(screen.getByRole('button', { name: 'Send' }));
const list = await screen.findByRole('list', { name: 'Messages' });
expect(await within(list).findByRole('listitem', { name: 'You' })).toHaveTextContent('Hi');
await act(async () => {
transport.stream?.push({ type: 'open' });
transport.stream?.push({
type: 'text',
data: { message: true, text: 'Hel', messageId: 'reply-1' },
});
});
const reply = within(list).getByRole('listitem', { name: 'Assistant' });
expect(reply).toHaveTextContent('Hel');
expect(screen.getByRole('status')).toHaveTextContent('streaming');
await act(async () => {
transport.stream?.push({
type: 'text',
data: { message: true, text: 'Hello there', messageId: 'reply-1' },
});
});
expect(reply).toHaveTextContent('Hello there');
await act(async () => {
transport.stream?.push({
type: 'final',
data: {
final: true,
responseMessage: {
messageId: 'reply-1',
parentMessageId: 'user-1',
conversationId: 'convo',
isCreatedByUser: false,
text: 'Hello there.',
},
},
});
transport.stream?.close();
});
expect(await within(list).findByText('Hello there.')).toBeInTheDocument();
expect(within(list).getAllByRole('listitem')).toHaveLength(2);
expect(await screen.findByText('ready')).toBeInTheDocument();
expect(document.documentElement.style.getPropertyValue('--theme-mono-font-family')).toContain(
'Inconsolata',
);
});
});

View file

@ -0,0 +1,15 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"declaration": false,
"declarationMap": false,
"isolatedDeclarations": false,
"rootDir": "..",
"paths": {
"@librechat/chat": ["../src/index.ts"],
"@librechat/chat/*": ["../src/*.ts"]
}
},
"include": ["**/*"]
}