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
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
import { renderHook, act } from '@testing-library/react';
import { Provider as JotaiProvider, createStore } from 'jotai';
import { pendingReasoningOverrideFamily } from '@librechat/chat/react';
import {
Constants,
ContentTypes,
EModelEndpoint,
QueryKeys,
createPayload,
} from 'librechat-data-provider';
import {
revealedQueuedTurnFamily,
submissionStartFamily,
showStopButtonByIndex,
} from '@librechat/chat/react';
import type {
Agent,
CodeEnvironmentMode,
CodeWorkspaceSelection,
TConversation,
TMessage,
TSubmission,
TReasoningOverride,
TEphemeralAgent,
} from 'librechat-data-provider';
import type { ReactNode } from 'react';
import type { ExtendedFile } from '~/common';
import {
activeUsageResponseIdFamily,
pendingUsageFamily,
} from '~/components/Chat/Input/TokenUsage/store';
import { ChatSettingsContext, defaultChatSettings } from '~/Providers/ChatSettingsContext';
import useChatFunctions from '../useChatFunctions';
import { isPasteSubmitted } from '~/utils';
const mockNavigate = jest.fn();
const mockSetIsSubmitting = jest.fn();
const mockGetEphemeralAgent = jest.fn((): TEphemeralAgent | null => null);
const mockSetFilesToDelete = jest.fn();
const mockGetSender = jest.fn(() => 'Assistant');
const mockGetExpiry = jest.fn(() => 'expiry-key');
const mockAgentQueryData: { current?: Agent } = {};
const mockEndpointsQueryData: { current?: Record<string, unknown> } = {};
const mockCapabilitiesData: { current?: Record<string, unknown> } = {};
const mockCapabilitiesState: {
current?: { status: string; fetchStatus?: string; dataUpdatedAt?: number };
} = {};
/** A query that holds data always has a timestamp: loaded data is fresh unless a test says otherwise. */
const mockGetQueryState = jest.fn((queryKey: readonly unknown[]) =>
queryKey[0] === QueryKeys.reasoningCapabilities
? { dataUpdatedAt: Date.now(), ...mockCapabilitiesState.current }
: undefined,
);
const mockGetQueryData = jest.fn((queryKey: readonly unknown[]) => {
if (queryKey[0] === QueryKeys.reasoningCapabilities) {
return mockCapabilitiesData.current;
}
if (queryKey[0] === QueryKeys.agent) {
return mockAgentQueryData.current;
}
if (queryKey[0] === QueryKeys.endpoints) {
return mockEndpointsQueryData.current ?? {};
}
return {};
});
const mockLoggerWarn = jest.fn();
const mockGetLatestConversation = jest.fn(() => null as TConversation | null);
const mockSetConversation = jest.fn();
const mockResolveCodeWorkspaceSubmission = jest.fn<
| { codeEnvironmentMode?: CodeEnvironmentMode; codeWorkspaces?: CodeWorkspaceSelection[] }
| undefined,
[CodeWorkspaceSelection[]?, CodeEnvironmentMode?]
>(() => ({}));
const mockCodeWorkspace = {
resolveSubmission: mockResolveCodeWorkspaceSubmission,
rememberSelection: jest.fn(),
};
jest.mock('react-router-dom', () => ({
useNavigate: () => mockNavigate,
}));
jest.mock('@tanstack/react-query', () => ({
useQueryClient: () => ({
getQueryData: mockGetQueryData,
getQueryState: (queryKey: readonly unknown[]) => mockGetQueryState(queryKey),
}),
}));
jest.mock('recoil', () => ({
useRecoilValue: () => false,
useSetRecoilState: (atom: unknown) =>
String(atom).includes('isSubmitting') ? mockSetIsSubmitting : jest.fn(),
useRecoilCallback: (factory: any) =>
factory({
snapshot: {
getLoadable: (_atom: unknown) => ({
state: 'hasValue',
contents: [],
}),
},
set: jest.fn(),
reset: jest.fn(),
}),
}));
jest.mock('~/hooks/Files/useSetFilesToDelete', () => () => mockSetFilesToDelete);
jest.mock('~/hooks/Agents/useCodeApprovalMode', () => () => ({
modes: ['ask', 'acceptEdits'],
selected: 'ask',
}));
jest.mock('~/hooks/Agents/useCodeWorkspace', () => () => mockCodeWorkspace);
jest.mock('~/hooks/Conversations/useGetConversation', () => () => mockGetLatestConversation);
jest.mock('~/hooks/Conversations/useGetSender', () => () => mockGetSender);
jest.mock('~/hooks/Input/useUserKey', () => () => ({ getExpiry: mockGetExpiry }));
jest.mock('~/hooks', () => ({
useAuthContext: () => ({ user: null }),
}));
jest.mock('~/Providers/AgentsMapContext', () => ({
useAgentsMapContext: () => ({
'agent-1': {
provider: 'openAI',
model: 'gpt-5.1',
},
}),
}));
jest.mock('~/store', () => ({
__esModule: true,
default: {
messagesSiblingIdxFamily: () => 'messagesSiblingIdx',
conversationByKeySelector: () => 'conversation',
},
}));
jest.mock('~/utils', () => ({
...jest.requireActual('~/utils'),
logger: {
log: jest.fn(),
dir: jest.fn(),
warn: (...args: unknown[]) => mockLoggerWarn(...args),
},
createDualMessageContent: jest.fn(() => []),
getRouteChatProjectId: jest.fn(() => null),
requestChatFocus: jest.fn(),
}));
const userMessage = (messageId: string, parentMessageId = '00000000-0000-0000-0000-000000000000') =>
({
messageId,
parentMessageId,
conversationId: 'conversation-1',
isCreatedByUser: true,
sender: 'User',
text: messageId,
}) as TMessage;
const assistantMessage = (messageId: string, parentMessageId: string) =>
({
messageId,
parentMessageId,
conversationId: 'conversation-1',
isCreatedByUser: false,
sender: 'Assistant',
text: messageId,
}) as TMessage;
const conversation = (conversationId: string) =>
({
conversationId,
endpoint: EModelEndpoint.agents,
model: 'gpt-4o',
agent_id: 'agent-1',
}) as TConversation;
const settingsWrapper = ({ children }: { children: ReactNode }) => (
<ChatSettingsContext.Provider
value={{ ...defaultChatSettings, getEphemeralAgent: mockGetEphemeralAgent }}
>
{children}
</ChatSettingsContext.Provider>
);
function renderAsk(
messages: TMessage[] | undefined,
conversationId = 'conversation-1',
options: {
endpoint?: TConversation['endpoint'];
model?: string;
isSubmitting?: boolean;
reasoningOverride?: TReasoningOverride;
agentId?: string;
files?: Map<string, ExtendedFile>;
} = {},
) {
const setMessages = jest.fn();
const setFiles = jest.fn();
const setSubmission = jest.fn();
const getMessages = jest.fn(() => messages);
const immutableConversation = conversation(conversationId);
if ('endpoint' in options) {
immutableConversation.endpoint = options.endpoint ?? null;
}
if (options.model != null) {
immutableConversation.model = options.model;
}
if (options.agentId != null) {
immutableConversation.agent_id = options.agentId;
}
const reasoningStore = createStore();
reasoningStore.set(pendingReasoningOverrideFamily(conversationId), options.reasoningOverride);
const wrapper = ({ children }: { children: ReactNode }) => (
<ChatSettingsContext.Provider
value={{ ...defaultChatSettings, getEphemeralAgent: mockGetEphemeralAgent }}
>
<JotaiProvider store={reasoningStore}>{children}</JotaiProvider>
</ChatSettingsContext.Provider>
);
const hook = renderHook(
() =>
useChatFunctions({
isSubmitting: options.isSubmitting ?? false,
latestMessage: messages?.at(-1) ?? null,
conversation: immutableConversation,
getMessages,
setMessages,
setSubmission,
files: options.files,
setFiles,
setConversation: mockSetConversation,
}),
{ wrapper },
);
return { ...hook, getMessages, setMessages, setFiles, setSubmission, reasoningStore };
}
describe('useChatFunctions ask', () => {
beforeEach(() => {
mockEndpointsQueryData.current = undefined;
mockCapabilitiesData.current = undefined;
mockCapabilitiesState.current = undefined;
localStorage.clear();
jest.clearAllMocks();
mockAgentQueryData.current = undefined;
mockGetLatestConversation.mockReturnValue(null);
mockResolveCodeWorkspaceSubmission.mockReturnValue({});
});
it.each([EModelEndpoint.agents, EModelEndpoint.openAI])(
'binds the optimistic %s response before publishing its messages',
(endpoint) => {
/* The helper renders under its own Jotai store; the hook writes there. */
const {
result,
setMessages,
setSubmission,
reasoningStore: store,
} = renderAsk([], 'conversation-1', { endpoint });
store.set(activeUsageResponseIdFamily('conversation-1'), null);
setMessages.mockImplementation((messages: TMessage[]) => {
expect(store.get(activeUsageResponseIdFamily('conversation-1'))).toBe(
messages.at(-1)?.messageId,
);
});
act(() => result.current.ask({ text: 'Hello', conversationId: 'conversation-1' }));
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(store.get(activeUsageResponseIdFamily('conversation-1'))).toBe(
submission.initialResponse?.messageId,
);
expect(store.get(pendingUsageFamily('conversation-1')).eventCount).toBe(0);
},
);
it.each([false, true])(
'publishes document aliases once before consuming files (override=%s)',
(override) => {
const file: ExtendedFile = {
file_id: 'server-document',
temp_file_id: 'upload-document',
filename: 'report.pdf',
type: 'application/pdf',
filepath: '/uploads/report.pdf',
llmDeliveryPath: 'text',
size: 100,
progress: 1,
};
const { result, setFiles, setSubmission } = renderAsk([], 'new', {
files: new Map([['map-document', file]]),
});
const write = jest.spyOn(Storage.prototype, 'setItem');
setFiles.mockImplementation(() => {
expect(isPasteSubmitted('map-document')).toBe(true);
expect(isPasteSubmitted(file.file_id)).toBe(true);
expect(isPasteSubmitted(file.temp_file_id)).toBe(true);
});
act(() =>
result.current.ask(
{ text: 'Read this document' },
override ? { overrideFiles: [file] } : undefined,
),
);
expect(
write.mock.calls.filter(([key]) => key === 'librechat-submitted-paste-file-ids'),
).toHaveLength(1);
expect(setFiles).toHaveBeenCalledTimes(override ? 0 : 1);
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.files?.[0]).toMatchObject({
file_id: file.file_id,
llmDeliveryPath: 'text',
});
expect(isPasteSubmitted(file.file_id)).toBe(true);
expect(isPasteSubmitted(file.temp_file_id)).toBe(true);
},
);
it('reads an approval-mode selection made immediately before send', () => {
mockGetLatestConversation.mockReturnValue({
...conversation('conversation-1'),
codeApprovalMode: 'acceptEdits',
});
const { result, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Edit the file', conversationId: 'conversation-1' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.codeApprovalMode).toBe('acceptEdits');
expect(submission.conversation.codeApprovalMode).toBe('acceptEdits');
});
it('sends the gated ask for a turn without a workspace while keeping the chat pick', () => {
mockResolveCodeWorkspaceSubmission.mockReturnValue({ codeEnvironmentMode: 'without_attached' });
mockGetLatestConversation.mockReturnValue({
...conversation('conversation-1'),
codeApprovalMode: 'acceptEdits',
codeEnvironmentMode: 'without_attached',
});
const { result, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Just chat', conversationId: 'conversation-1' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.codeEnvironmentMode).toBe('without_attached');
expect(submission.codeApprovalMode).toBe('ask');
expect(createPayload(submission).payload.codeApprovalMode).toBe('ask');
expect(submission.conversation.codeApprovalMode).toBe('acceptEdits');
});
it('preallocates a durable Agents user id for the optimistic response anchor', () => {
const { result, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Queue safely', conversationId: 'conversation-1' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
const userMessageId = submission.userMessage.messageId;
expect(submission.endpointOption.overrideUserMessageId).toBe(
`${userMessageId}${Constants.COMMON_DIVIDER}0`,
);
expect(createPayload(submission).payload.overrideUserMessageId).toBe(
`${userMessageId}${Constants.COMMON_DIVIDER}0`,
);
expect(submission.initialResponse?.clientQueueParentMessageId).toBe(userMessageId);
});
it('submits the latest validated workspace selection', () => {
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
mockResolveCodeWorkspaceSubmission.mockReturnValue({ codeWorkspaces: [selection] });
mockGetLatestConversation.mockReturnValue({
...conversation('conversation-1'),
codeEnvironmentMode: 'attached',
codeWorkspaces: [selection],
});
const { result, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Edit the file', conversationId: 'conversation-1' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(mockResolveCodeWorkspaceSubmission).toHaveBeenCalledWith([selection], 'attached');
expect(submission.codeWorkspaces).toEqual([selection]);
});
/* The server seals the decision this run establishes. A chat that becomes saved mid-run must hold
* it too, or the composer re-derives an agent default the seal refuses and reports "choose a
* workspace" with Send disabled until the page reloads. */
it('records the decision the run establishes on the conversation it belongs to', () => {
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
mockResolveCodeWorkspaceSubmission.mockReturnValue({
codeEnvironmentMode: 'attached',
codeWorkspaces: [selection],
});
const { result } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Edit the file', conversationId: 'conversation-1' });
});
expect(mockSetConversation).toHaveBeenCalledTimes(1);
const update = mockSetConversation.mock.calls[0][0];
const current = conversation('conversation-1');
expect(update(current)).toEqual({
...current,
codeEnvironmentMode: 'attached',
codeWorkspaces: [selection],
});
/** Another chat's conversation is never stamped with this run's decision. */
expect(update(conversation('conversation-2'))).toEqual(conversation('conversation-2'));
expect(update(null)).toBeNull();
});
it('leaves a conversation that already holds the decision untouched', () => {
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
mockResolveCodeWorkspaceSubmission.mockReturnValue({
codeEnvironmentMode: 'attached',
codeWorkspaces: [selection],
});
const sealed = {
...conversation('conversation-1'),
codeEnvironmentMode: 'attached' as const,
codeWorkspaces: [selection],
};
mockGetLatestConversation.mockReturnValue(sealed);
const { result } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Edit the file', conversationId: 'conversation-1' });
});
const update = mockSetConversation.mock.calls[0][0];
expect(update(sealed)).toBe(sealed);
});
it('refuses every direct send before consuming composer context during handoff', () => {
const family = revealedQueuedTurnFamily('conversation-1');
const { result, setSubmission, getMessages, reasoningStore } = renderAsk([]);
reasoningStore.set(family, {
clientRequestId: 'queued',
parentMessageId: 'response',
generationCreatedAt: 41,
text: 'queued',
revealedAt: new Date().toISOString(),
});
try {
expect(result.current.ask({ text: 'direct' })).toBe(false);
expect(
result.current.ask({ text: 'rerun', parentMessageId: 'earlier' }, { isRegenerate: true }),
).toBe(false);
expect(mockResolveCodeWorkspaceSubmission).not.toHaveBeenCalled();
expect(getMessages).not.toHaveBeenCalled();
expect(setSubmission).not.toHaveBeenCalled();
expect(mockSetFilesToDelete).not.toHaveBeenCalled();
} finally {
reasoningStore.set(family, null);
}
});
it('refuses to send when workspace submission resolution is not ready', () => {
mockResolveCodeWorkspaceSubmission.mockReturnValue(undefined);
const { result, setSubmission } = renderAsk([]);
expect(result.current.ask({ text: 'Edit the file', conversationId: 'conversation-1' })).toBe(
false,
);
expect(setSubmission).not.toHaveBeenCalled();
});
it('refuses to send to an existing conversation before its history loads', () => {
const { result, getMessages, setMessages, setSubmission } = renderAsk(undefined);
let askResult: ReturnType<typeof result.current.ask>;
act(() => {
askResult = result.current.ask({ text: 'Hello', conversationId: 'conversation-1' });
});
expect(askResult!).toBe(false);
expect(getMessages).toHaveBeenCalledWith('conversation-1');
expect(setMessages).not.toHaveBeenCalled();
expect(setSubmission).not.toHaveBeenCalled();
expect(mockLoggerWarn).toHaveBeenCalledWith(
'[useTurns] Refusing to send before existing conversation history loads',
);
});
it('synchronously reports a refusal while another submit is in flight', () => {
const {
result,
setMessages,
setSubmission,
reasoningStore: store,
} = renderAsk([], 'conversation-1', {
isSubmitting: true,
});
let askResult: ReturnType<typeof result.current.ask>;
act(() => {
askResult = result.current.ask({ text: 'queued follow-up' });
});
expect(askResult!).toBe(false);
expect(setMessages).not.toHaveBeenCalled();
expect(setSubmission).not.toHaveBeenCalled();
expect(store.get(showStopButtonByIndex(0))).toBe(false);
expect(store.get(submissionStartFamily(0))).toBeNull();
});
it('shows the stop button and stamps the elapsed baseline for an accepted submit', () => {
const { result, reasoningStore: store } = renderAsk([]);
const before = Date.now();
act(() => {
result.current.ask({ text: 'first turn', conversationId: 'conversation-1' });
});
expect(store.get(showStopButtonByIndex(0))).toBe(true);
expect(store.get(submissionStartFamily(0))).toBeGreaterThanOrEqual(before);
});
it('refuses a second submit fired in the same task, before isSubmitting commits', () => {
const { result, setSubmission } = renderAsk([]);
let first: ReturnType<typeof result.current.ask>;
let second: ReturnType<typeof result.current.ask>;
act(() => {
first = result.current.ask({ text: 'double enter', conversationId: 'conversation-1' });
second = result.current.ask({ text: 'double enter', conversationId: 'conversation-1' });
});
expect(first!).not.toBe(false);
expect(second!).toBe(false);
expect(setSubmission).toHaveBeenCalledTimes(1);
});
it('releases the in-flight guard on the next commit rather than latching it', () => {
const { result, rerender, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'first turn', conversationId: 'conversation-1' });
});
/* `isSubmitting` never turns true here, standing in for a start that fails
outright: the next commit has to release the guard on its own instead of
latching the composer shut. */
act(() => {
rerender();
});
act(() => {
result.current.ask({ text: 'second turn', conversationId: 'conversation-1' });
});
expect(setSubmission).toHaveBeenCalledTimes(2);
});
it('reports a refusal when no endpoint is available', () => {
const {
result,
setMessages,
setSubmission,
reasoningStore: store,
} = renderAsk([], 'conversation-1', {
endpoint: null,
});
let askResult: ReturnType<typeof result.current.ask>;
act(() => {
askResult = result.current.ask({ text: 'queued follow-up' });
});
expect(askResult!).toBe(false);
expect(setMessages).not.toHaveBeenCalled();
expect(setSubmission).not.toHaveBeenCalled();
expect(store.get(showStopButtonByIndex(0))).toBe(false);
expect(store.get(submissionStartFamily(0))).toBeNull();
});
it.each([
['empty text', { text: ' ' }, undefined],
['the search view', { text: 'queued follow-up', conversationId: 'search' }, undefined],
['a continue without a latest message', { text: 'continue' }, { isContinued: true }],
])('reports a refusal for %s', (_label, props, askOptions) => {
const { result, setMessages, setSubmission } = renderAsk([]);
let askResult: ReturnType<typeof result.current.ask>;
act(() => {
askResult = result.current.ask(props, askOptions);
});
expect(askResult!).toBe(false);
expect(setMessages).not.toHaveBeenCalled();
expect(setSubmission).not.toHaveBeenCalled();
});
it('allows an existing conversation whose loaded history is empty', () => {
const { result, setMessages, setSubmission } = renderAsk([]);
act(() => {
result.current.ask({ text: 'Hello', conversationId: 'conversation-1' });
});
expect(setMessages).toHaveBeenCalled();
expect(setSubmission).toHaveBeenCalled();
});
it('allows a new conversation before its message cache exists', () => {
const newConversationId = Constants.NEW_CONVO as string;
const { result, setMessages, setSubmission } = renderAsk(undefined, newConversationId);
act(() => {
result.current.ask({ text: 'Hello', conversationId: newConversationId });
});
expect(setMessages).toHaveBeenCalled();
expect(setSubmission).toHaveBeenCalled();
});
it('allows explicit override messages before the cache exists', () => {
const { result, setMessages, setSubmission } = renderAsk(undefined);
act(() => {
result.current.ask(
{ text: 'Hello', conversationId: 'conversation-1' },
{ overrideMessages: [] },
);
});
expect(setMessages).toHaveBeenCalled();
expect(setSubmission).toHaveBeenCalled();
});
it('stores an explicit reasoning override on only the submitted user turn', () => {
const { result, setSubmission } = renderAsk([]);
const override = { key: 'reasoning_effort', value: 'high' } as TReasoningOverride;
act(() => {
result.current.ask({ text: 'Think carefully' }, { overrideReasoning: override });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(override);
expect(submission.conversation).not.toHaveProperty('reasoning_effort', 'high');
expect(submission.endpointOption).not.toHaveProperty('reasoning_effort', 'high');
});
/* A manual compaction is an internal summarization turn, like a regenerate:
the staged choice belongs to the user's next real message. */
it('leaves a staged reasoning override for the next message across a manual compaction', () => {
const override = { key: 'reasoning_effort', value: 'high' } as TReasoningOverride;
const { result, setSubmission, reasoningStore } = renderAsk(
[{ messageId: 'msg-1', parentMessageId: Constants.NO_PARENT } as TMessage],
'conversation-1',
{ reasoningOverride: override },
);
act(() => {
result.current.ask(
{
text: '',
conversationId: 'conversation-1',
messageId: 'msg-1',
parentMessageId: 'msg-1',
},
{ compact: true },
);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission | undefined;
expect(submission?.userMessage?.reasoningOverride).toBeUndefined();
expect(reasoningStore.get(pendingReasoningOverrideFamily('conversation-1'))).toEqual(override);
});
it('drains a staged reasoning override onto a fresh submission exactly once', () => {
const override = { key: 'reasoning_effort', value: 'high' } as TReasoningOverride;
const { result, setSubmission, reasoningStore } = renderAsk([], 'conversation-1', {
reasoningOverride: override,
});
act(() => {
result.current.ask({ text: 'Think carefully' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(override);
expect(reasoningStore.get(pendingReasoningOverrideFamily('conversation-1'))).toBeUndefined();
});
it('keeps a staged override for an ephemeral agent resolved from its encoded target', () => {
const override = { key: 'reasoning_effort', value: 'high' } as TReasoningOverride;
const { result, setSubmission } = renderAsk([], 'conversation-1', {
reasoningOverride: override,
agentId: 'openAI__gpt-5___GPT-5',
});
act(() => {
result.current.ask({ text: 'Think carefully' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(override);
});
it('submits the declared effort override for the mock custom endpoint', () => {
mockEndpointsQueryData.current = {
'Mock Provider A': {
type: 'custom',
customParams: {
defaultParamsEndpoint: 'anthropic',
paramDefinitions: [{ key: 'effort' }],
},
},
};
const override = { key: 'effort', value: 'high' } as TReasoningOverride;
const { result, setSubmission } = renderAsk([], 'mock-provider-conversation', {
/* The lab's endpoint is a named custom endpoint, so its label is not an
`EModelEndpoint` member; that is the configuration under test. */
endpoint: 'Mock Provider A' as TConversation['endpoint'],
model: 'mock-model-a',
reasoningOverride: override,
});
act(() => {
result.current.ask({ text: 'Think with effort' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(override);
});
describe('replaying a reasoning override on an OpenRouter endpoint', () => {
const model = 'openai/gpt-6.1-sol';
const override = { key: 'reasoning_effort', value: 'max' } as TReasoningOverride;
const openRouter = (paramDefinitions?: unknown[]) => ({
OpenRouter: {
type: 'custom',
customParams: {
defaultParamsEndpoint: 'openrouter',
reasoningFormat: 'reasoning_effort',
...(paramDefinitions && { paramDefinitions }),
},
},
});
const replay = (value: TReasoningOverride = override) => {
const { result, setSubmission } = renderAsk([], 'openrouter-conversation', {
endpoint: 'OpenRouter' as TConversation['endpoint'],
model,
});
act(() => {
result.current.ask({ text: 'Think' }, { overrideReasoning: value });
});
return (setSubmission.mock.calls.at(-1)?.[0] as TSubmission).userMessage.reasoningOverride;
};
beforeEach(() => {
mockEndpointsQueryData.current = openRouter();
});
it('omits the override while the capabilities have not loaded', () => {
mockCapabilitiesState.current = { status: 'loading' };
expect(replay()).toBeUndefined();
});
it('omits the override when no capabilities request has run', () => {
expect(replay()).toBeUndefined();
});
it('keeps an override the loaded capabilities support', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
expect(replay()).toEqual(override);
});
it('omits an override the loaded capabilities do not support', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['low'] } } },
expiresInMs: 60000,
};
expect(replay()).toBeUndefined();
});
it('omits the override when the capabilities request failed, as its efforts are unknown', () => {
mockCapabilitiesState.current = { status: 'error' };
expect(replay()).toBeUndefined();
});
it('omits the override when a refresh failed although the previous data is still cached', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
mockCapabilitiesState.current = { status: 'error' };
expect(replay()).toBeUndefined();
});
it('omits the override while an expired entry is refreshed, although its data is cached', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
mockCapabilitiesState.current = { status: 'success', fetchStatus: 'fetching' } as never;
expect(replay()).toBeUndefined();
});
it('omits the override when the cached entry is past its lifetime and idle', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
mockCapabilitiesState.current = {
status: 'success',
fetchStatus: 'idle',
dataUpdatedAt: Date.now() - 61_000,
};
expect(replay()).toBeUndefined();
});
it('keeps the override while the cached entry is still within its lifetime', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
mockCapabilitiesState.current = {
status: 'success',
fetchStatus: 'idle',
dataUpdatedAt: Date.now() - 30_000,
};
expect(replay()).toEqual(override);
});
describe('a staged effort submitted while the capabilities are unknown', () => {
const submitStaged = () => {
const { result, setSubmission, reasoningStore } = renderAsk([], 'openrouter-conversation', {
endpoint: 'OpenRouter' as TConversation['endpoint'],
model,
reasoningOverride: override,
});
act(() => {
result.current.ask({ text: 'Think' });
});
const sent = (setSubmission.mock.calls.at(-1)?.[0] as TSubmission).userMessage
.reasoningOverride;
return {
sent,
left: reasoningStore.get(pendingReasoningOverrideFamily('openrouter-conversation')),
};
};
it('leaves it staged for the next message instead of losing it', () => {
mockCapabilitiesState.current = { status: 'error' };
const { sent, left } = submitStaged();
expect(sent).toBeUndefined();
expect(left).toEqual(override);
});
it('leaves it staged while the capabilities are loading', () => {
mockCapabilitiesState.current = { status: 'loading' };
expect(submitStaged().left).toEqual(override);
});
it('consumes it once it is sent', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['max', 'high'] } } },
expiresInMs: 60000,
};
const { sent, left } = submitStaged();
expect(sent).toEqual(override);
expect(left).toBeUndefined();
});
it('consumes it when the loaded catalog refuses it', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: ['low'] } } },
expiresInMs: 60000,
};
const { sent, left } = submitStaged();
expect(sent).toBeUndefined();
expect(left).toBeUndefined();
});
});
describe('an Auto override, which sends no effort', () => {
const auto = { key: 'reasoning_effort', value: '' } as TReasoningOverride;
it('is kept while the capabilities have not loaded', () => {
mockCapabilitiesState.current = { status: 'loading' };
expect(replay(auto)).toEqual(auto);
});
it('is kept when the capabilities request failed', () => {
mockCapabilitiesState.current = { status: 'error' };
expect(replay(auto)).toEqual(auto);
});
it('is kept for a model the loaded catalog lists without effort selection', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { [model]: { efforts: [] } } },
expiresInMs: 60000,
};
expect(replay(auto)).toEqual(auto);
});
});
it('keeps an override for a model the loaded catalog does not list', () => {
mockCapabilitiesData.current = {
capabilities: { OpenRouter: { 'meta/other': { efforts: ['low'] } } },
expiresInMs: 60000,
};
expect(replay()).toEqual(override);
});
it('keeps an administrator-defined effort override without waiting for the catalog', () => {
mockEndpointsQueryData.current = openRouter([{ key: 'reasoning_effort' }]);
mockCapabilitiesState.current = { status: 'loading' };
expect(replay()).toEqual(override);
});
});
it('uses hydrated per-agent query data when the agent catalog is unavailable', () => {
const agentId = 'agent-uncatalogued';
const override = { key: 'reasoning_effort', value: 'high' } as TReasoningOverride;
mockAgentQueryData.current = {
id: agentId,
provider: 'openAI',
model: 'gpt-5.1',
} as Agent;
const { result, setSubmission, reasoningStore } = renderAsk([], 'conversation-uncatalogued', {
agentId,
reasoningOverride: override,
});
act(() => {
result.current.ask({ text: 'Think carefully' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(override);
expect(
reasoningStore.get(pendingReasoningOverrideFamily('conversation-uncatalogued')),
).toBeUndefined();
expect(mockGetQueryData).toHaveBeenCalledWith([QueryKeys.agent, agentId]);
});
});
describe('useChatFunctions regenerate', () => {
beforeEach(() => {
jest.clearAllMocks();
mockAgentQueryData.current = undefined;
});
it('keys a non-tail regenerate to the selected assistant response', () => {
let messages = [
userMessage('user-1'),
assistantMessage('assistant-1', 'user-1'),
userMessage('user-2', 'assistant-1'),
assistantMessage('assistant-2', 'user-2'),
userMessage('user-3', 'assistant-2'),
assistantMessage('assistant-3', 'user-3'),
];
const setMessages = jest.fn((nextMessages: TMessage[]) => {
messages = nextMessages;
});
const setSubmission = jest.fn();
const conversation = {
conversationId: 'conversation-1',
endpoint: EModelEndpoint.agents,
model: 'gpt-4o',
agent_id: 'agent-1',
} as TConversation;
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: messages[5],
conversation,
getMessages: () => messages,
setMessages,
setSubmission,
setConversation: mockSetConversation,
}),
);
act(() => {
result.current.regenerate(messages[1]);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.codeApprovalMode).toBe('ask');
expect(submission.userMessage.overrideParentMessageId).toBe('user-1');
expect(submission.userMessage.responseMessageId).toBe('assistant-1_');
expect(submission.initialResponse?.messageId).toBe('assistant-1_');
expect(submission.initialResponse?.parentMessageId).toBe('user-1');
expect(submission.initialResponse?.clientQueueParentMessageId).toBe('user-1');
expect(submission.messages.map((message) => message.messageId)).toEqual(['user-1']);
expect(submission.regenerateMessages?.map((message) => message.messageId)).toEqual([
'user-1',
'assistant-1',
'user-2',
'assistant-2',
'user-3',
'assistant-3',
]);
expect(
setMessages.mock.calls.at(-1)?.[0].map((message: TMessage) => message.messageId),
).toEqual(['user-1', 'assistant-1_']);
expect(messages.at(-1)?.messageId).toBe('assistant-1_');
});
it.each(['assistant-1', 'assistant_1_'])(
'marks an edited optimistic response %s with its durable user anchor',
(responseMessageId) => {
const parent = userMessage('user-1');
const response = {
...assistantMessage(responseMessageId, parent.messageId),
content: [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'before' }],
} as TMessage;
const { result, setSubmission } = renderAsk([parent, response]);
act(() => {
result.current.ask(
{ ...parent },
{
isRegenerate: true,
isEdited: true,
editedMessageId: responseMessageId,
editedContent: {
index: 0,
type: ContentTypes.TEXT,
[ContentTypes.TEXT]: 'after',
},
},
);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.initialResponse?.messageId).toBe(responseMessageId);
expect(submission.initialResponse?.clientQueueParentMessageId).toBe('user-1');
},
);
it('replays the original user turn reasoning override on regenerate', () => {
const parent = {
...userMessage('user-reasoning'),
reasoningOverride: { key: 'reasoning_effort', value: 'high' },
} as TMessage;
const response = assistantMessage('assistant-reasoning', parent.messageId);
const { result, setSubmission } = renderAsk([parent, response]);
act(() => {
result.current.regenerate(response);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toEqual(parent.reasoningOverride);
});
it('drops a replayed reasoning override that the selected model no longer supports', () => {
const parent = {
...userMessage('user-reasoning'),
reasoningOverride: { key: 'effort', value: 'max' },
} as TMessage;
const response = assistantMessage('assistant-reasoning', parent.messageId);
const { result, setSubmission } = renderAsk([parent, response]);
act(() => result.current.regenerate(response));
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.reasoningOverride).toBeUndefined();
});
});
describe('useChatFunctions ask attachments', () => {
beforeEach(() => {
jest.clearAllMocks();
mockAgentQueryData.current = undefined;
});
afterEach(() => {
mockGetEphemeralAgent.mockReturnValue(null);
});
/** The server titles an attachment-only turn from the submitted filenames
* (getAttachmentTitleText), so the fresh-file mapping must carry them. */
it('carries the filename on freshly attached files', () => {
const setMessages = jest.fn();
const setSubmission = jest.fn();
const setFiles = jest.fn();
const files = new Map([
[
'file-1',
{
file_id: 'file-1',
filepath: '/uploads/file-1',
filename: 'quarterly-report.pdf',
type: 'application/pdf',
},
],
]) as unknown as Parameters<typeof useChatFunctions>[0]['files'];
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: null,
conversation: conversation(Constants.NEW_CONVO as string),
getMessages: () => [],
setMessages,
setSubmission,
setConversation: mockSetConversation,
files,
setFiles,
}),
);
act(() => {
result.current.ask({ text: '' });
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.files?.[0]).toMatchObject({
file_id: 'file-1',
filename: 'quarterly-report.pdf',
});
});
it('keeps the next composer file staged when a text-only App message uses empty overrides', () => {
const setMessages = jest.fn();
const setSubmission = jest.fn();
const setFiles = jest.fn();
const files = new Map([
[
'app-next-draft-file',
{
file_id: 'app-next-draft-file',
filepath: '/uploads/app-next-draft-file',
filename: 'private-next-message.pdf',
type: 'application/pdf',
},
],
]) as unknown as Parameters<typeof useChatFunctions>[0]['files'];
const { result } = renderHook(
() =>
useChatFunctions({
isSubmitting: false,
latestMessage: null,
conversation: conversation(Constants.NEW_CONVO as string),
getMessages: () => [],
setMessages,
setSubmission,
setConversation: mockSetConversation,
files,
setFiles,
}),
{ wrapper: settingsWrapper },
);
const selectedConversationTools: TEphemeralAgent = {
skills: true,
mcp: ['selected-server'],
};
mockGetEphemeralAgent.mockReturnValue(selectedConversationTools);
act(() => {
result.current.ask(
{ text: 'approved App message' },
{
overrideFiles: [],
overrideManualSkills: [],
overrideQuotes: [],
},
);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.userMessage.text).toBe('approved App message');
expect(submission.userMessage.files).toBeUndefined();
expect(submission.userMessage.manualSkills).toBeUndefined();
expect(submission.userMessage.quotes).toBeUndefined();
expect(submission.ephemeralAgent).toEqual(selectedConversationTools);
expect(mockGetEphemeralAgent).toHaveBeenCalledTimes(1);
expect(files?.has('app-next-draft-file')).toBe(true);
expect(setFiles).not.toHaveBeenCalled();
expect(isPasteSubmitted('app-next-draft-file')).toBe(false);
});
it('rejects a blank text-only App turn even when the composer contains a staged file', () => {
const setSubmission = jest.fn();
const files = new Map([
['staged-file', { file_id: 'staged-file', filename: 'private-draft.pdf' }],
]) as unknown as Parameters<typeof useChatFunctions>[0]['files'];
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: null,
conversation: conversation(Constants.NEW_CONVO as string),
getMessages: () => [],
setMessages: jest.fn(),
setSubmission,
setConversation: mockSetConversation,
files,
setFiles: jest.fn(),
}),
);
let accepted: ReturnType<typeof result.current.ask>;
act(() => {
accepted = result.current.ask(
{ text: ' ' },
{
overrideFiles: [],
overrideManualSkills: [],
overrideQuotes: [],
},
);
});
expect(accepted!).toBe(false);
expect(setSubmission).not.toHaveBeenCalled();
expect(files?.has('staged-file')).toBe(true);
});
it('treats a legacy null file override as an ordinary staged-file submission', () => {
const files = new Map([
['legacy-draft-file', { file_id: 'legacy-draft-file', filename: 'draft.pdf' }],
]) as unknown as Parameters<typeof useChatFunctions>[0]['files'];
const setSubmission = jest.fn();
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: null,
conversation: conversation(Constants.NEW_CONVO as string),
getMessages: () => [],
setMessages: jest.fn(),
setSubmission,
setConversation: mockSetConversation,
files,
setFiles: jest.fn(),
}),
);
act(() => {
result.current.ask(
{ text: '' },
{
overrideFiles: null as unknown as NonNullable<
Parameters<typeof result.current.ask>[1]
>['overrideFiles'],
},
);
});
expect(setSubmission).toHaveBeenCalledTimes(1);
expect((setSubmission.mock.calls[0][0] as TSubmission).userMessage.files).toEqual([
expect.objectContaining({ file_id: 'legacy-draft-file' }),
]);
});
it('still uses a staged ephemeral agent for an ordinary composer submission', () => {
const stagedAgent: TEphemeralAgent = { skills: true, mcp: ['selected-tool'] };
mockGetEphemeralAgent.mockReturnValue(stagedAgent);
const { result, setSubmission } = renderAsk([], 'conversation-1');
act(() => {
result.current.ask({ text: 'normal user draft' });
});
expect(mockGetEphemeralAgent).toHaveBeenCalledTimes(1);
expect((setSubmission.mock.calls.at(-1)?.[0] as TSubmission).ephemeralAgent).toEqual(
stagedAgent,
);
});
it('marks files consumed through overrideFiles as submitted', () => {
const overrideFiles = [
{
file_id: 'queued-override-file',
temp_file_id: 'queued-override-temp-file',
filepath: '/uploads/queued-override-file',
filename: 'queued-override.txt',
type: 'text/plain',
},
];
const setMessages = jest.fn();
const setSubmission = jest.fn();
const setFiles = jest.fn();
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: null,
conversation: conversation(Constants.NEW_CONVO as string),
getMessages: () => [],
setMessages,
setSubmission,
setConversation: mockSetConversation,
files: new Map(),
setFiles,
}),
);
act(() => {
result.current.ask(
{ text: 'queued override' },
{
overrideFiles,
},
);
});
expect(isPasteSubmitted('queued-override-file')).toBe(true);
expect(isPasteSubmitted('queued-override-temp-file')).toBe(true);
});
});
describe('useChatFunctions ask compaction', () => {
beforeEach(() => {
jest.clearAllMocks();
mockGetQueryData.mockReturnValue({});
});
it('submits a summarize-only turn hung off the leaf without a new user bubble', () => {
const messages = [userMessage('u1'), assistantMessage('a1', 'u1')];
const { result, setMessages, setSubmission } = renderAsk(messages);
act(() => {
result.current.ask(
{ text: '', conversationId: 'conversation-1', messageId: 'a1', parentMessageId: 'u1' },
{ compact: true },
);
});
expect(setSubmission).toHaveBeenCalledTimes(1);
const submission = setSubmission.mock.calls[0][0] as TSubmission;
expect(submission.compact).toBe(true);
expect(submission.isRegenerate).toBe(true);
expect(submission.initialResponse?.parentMessageId).toBe('a1');
expect(submission.initialResponse?.messageId).toBe('a1_');
/** The leaf stands in for the user message so an error lands under it. */
expect(submission.userMessage.messageId).toBe('a1');
expect(submission.userMessage.overrideParentMessageId).toBeNull();
expect(submission.userMessage.files).toBeUndefined();
expect(submission.messages.map((message) => message.messageId)).toEqual(['u1', 'a1']);
expect(submission.regenerateMessages?.map((message) => message.messageId)).toEqual([
'u1',
'a1',
]);
const rendered = setMessages.mock.calls[0][0] as TMessage[];
expect(rendered.map((message) => message.messageId)).toEqual(['u1', 'a1', 'a1_']);
});
it('does not re-attach the leaf files a user-message leaf carries', () => {
const leaf = { ...userMessage('u2', 'a1'), files: [{ file_id: 'f1' }] } as TMessage;
const messages = [userMessage('u1'), assistantMessage('a1', 'u1'), leaf];
const { result, setSubmission } = renderAsk(messages);
act(() => {
result.current.ask(
{ text: '', conversationId: 'conversation-1', messageId: 'u2', parentMessageId: 'a1' },
{ compact: true },
);
});
const submission = setSubmission.mock.calls[0][0] as TSubmission;
expect(submission.userMessage.files).toBeUndefined();
expect(submission.initialResponse?.parentMessageId).toBe('u2');
});
});
describe('useChatFunctions ask compaction and the composer', () => {
it('leaves files staged in the composer untouched', () => {
const setMessages = jest.fn();
const setSubmission = jest.fn();
const setFiles = jest.fn();
const files = new Map([
[
'staged-file',
{
file_id: 'staged-file',
filepath: '/uploads/staged-file',
filename: 'next-message.pdf',
type: 'application/pdf',
},
],
]) as unknown as Parameters<typeof useChatFunctions>[0]['files'];
const messages = [userMessage('u1'), assistantMessage('a1', 'u1')];
const { result } = renderHook(() =>
useChatFunctions({
isSubmitting: false,
latestMessage: messages[1],
conversation: conversation('conversation-1'),
getMessages: () => messages,
setMessages,
setSubmission,
setConversation: mockSetConversation,
files,
setFiles,
}),
);
act(() => {
result.current.ask(
{ text: '', conversationId: 'conversation-1', messageId: 'a1', parentMessageId: 'a1' },
{ compact: true },
);
});
const submission = setSubmission.mock.calls.at(-1)?.[0] as TSubmission;
expect(submission.compact).toBe(true);
expect(submission.userMessage.files).toBeUndefined();
expect(submission.userMessage.parentMessageId).toBe('a1');
expect(setFiles).not.toHaveBeenCalled();
expect(isPasteSubmitted('staged-file')).toBe(false);
});
});