The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { MemoryRouterAppNavigatorProvider } from '~/testing/components/MemoryRouterAppNavigatorProvider';
import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { atom, Provider as JotaiProvider } from 'jotai';
import { type ReactNode } from 'react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { AppPath } from 'twenty-shared/types';
import { SOURCE_LOCALE } from 'twenty-shared/translations';
import { getCommandMenuDropdownIdFromCommandMenuId } from '@/command-menu-item/utils/getCommandMenuDropdownIdFromCommandMenuId';
import { CommandMenuComponentInstanceContext } from '@/command-menu/states/contexts/CommandMenuComponentInstanceContext';
import { MAIN_CONTEXT_STORE_INSTANCE_ID } from '@/context-store/constants/MainContextStoreInstanceId';
import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/states/contextStoreTargetedRecordsRuleComponentState';
import { ContextStoreComponentInstanceContext } from '@/context-store/states/contexts/ContextStoreComponentInstanceContext';
import { AI_CHAT_INBOX_INSTANCE_ID } from '@/ai/constants/AiChatInboxInstanceId';
import { AI_CHAT_INBOX_LAYOUT } from '@/ai/constants/AiChatInboxLayout';
import { AI_CHAT_NEW_THREAD_PATH_SEGMENT } from '@/ai/constants/AiChatNewThreadPathSegment';
import { AGENT_CHAT_THREAD_FILTER_STATUS } from '@/ai/constants/AgentChatThreadFilterStatus';
import { useOptimisticallyRestoreOnSend } from '@/ai/hooks/useOptimisticallyRestoreOnSend';
import { agentChatThreadFilterStatusState } from '@/ai/states/agentChatThreadFilterStatusState';
import { agentChatThreadListState } from '@/ai/states/agentChatThreadListState';
import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';
import { aiChatInboxLayoutState } from '@/ai/states/aiChatInboxLayoutState';
import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isDropdownOpenComponentState } from '@/ui/layout/dropdown/states/isDropdownOpenComponentState';
import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
import {
resetJotaiStore,
jotaiStore,
} from '@/ui/utilities/state/jotai/jotaiStore';
import { messages } from '~/locales/generated/en';
import { AiChatInboxPage } from '~/pages/ai-chat/AiChatInboxPage';
i18n.load({ [SOURCE_LOCALE]: messages });
i18n.activate(SOURCE_LOCALE);
const buildThread = (id: string, title: string): AgentChatThreadRecord => ({
__typename: 'AgentChatThread',
id,
title,
deletedAt: null,
createdAt: '2026-09-07T00:00:00.000Z',
updatedAt: '2026-09-07T00:00:00.000Z',
});
const THREADS = [
buildThread('6f1c2b0e-7a4d-4e8b-9c3f-2d5a1b8e7c60', 'First chat'),
buildThread('0b6e3f1a-2c4d-4b8e-9a7f-1d3c5e7a9b20', 'Second chat'),
buildThread('9d2a4c6e-8b1f-4e3a-8c5d-7f9b1a3c5e40', 'Third chat'),
];
const chatObjectMetadataItemAtom = atom<Pick<EnrichedObjectMetadataItem, 'id'>>(
{ id: 'chat-object' },
);
jest.mock('@/object-metadata/states/objectMetadataItemFamilySelector', () => ({
objectMetadataItemFamilySelector: {
selectorFamily: () => chatObjectMetadataItemAtom,
},
}));
jest.mock('@/ai/components/AiChatThreadList', () => ({
AiChatThreadList: ({
threads,
selectedThreadIds,
checkedThreadIds,
onThreadClick,
onThreadCheckboxClick,
onThreadContextMenu,
}: {
threads: AgentChatThreadRecord[];
selectedThreadIds: string[];
checkedThreadIds: string[];
onThreadClick: (
thread: AgentChatThreadRecord,
event: React.MouseEvent<HTMLElement>,
) => void;
onThreadCheckboxClick?: (
thread: AgentChatThreadRecord,
event: React.MouseEvent<HTMLElement>,
) => void;
onThreadContextMenu?: (
thread: AgentChatThreadRecord,
event: React.MouseEvent<HTMLElement>,
) => void;
}) =>
threads.map((thread) => (
<div key={thread.id} data-selectable-id={thread.id}>
<input
type="checkbox"
aria-label={`Select ${thread.title}`}
checked={checkedThreadIds.includes(thread.id)}
disabled={!onThreadCheckboxClick}
readOnly
onClick={(event) => onThreadCheckboxClick?.(thread, event)}
/>
<button
aria-pressed={selectedThreadIds.includes(thread.id)}
onClick={(event) => onThreadClick(thread, event)}
onContextMenu={(event) => onThreadContextMenu?.(thread, event)}
>
{thread.title}
</button>
</div>
)),
}));
jest.mock('@/ai/components/AgentChatThreadsFetchMoreTrigger', () => ({
AgentChatThreadsFetchMoreTrigger: () => null,
}));
jest.mock('@/ai/components/AgentChatChannelThreadsLoadEffect', () => ({
AgentChatChannelThreadsLoadEffect: () => null,
}));
jest.mock('@/ai/components/AgentChatChannelThreadsFetchMoreTrigger', () => ({
AgentChatChannelThreadsFetchMoreTrigger: () => null,
}));
jest.mock('@/ai/hooks/useLoadAgentChatChannelThreads', () => ({
useLoadAgentChatChannelThreads: () => ({
loadAgentChatChannelThreads: jest.fn(),
}),
}));
jest.mock('@/ai/hooks/useRefreshAgentChatThreads', () => ({
useRefreshAgentChatThreads: () => ({ fetchMoreAgentChatThreads: jest.fn() }),
}));
jest.mock('@/ai/hooks/useAgentChatOpenThreadsSummary', () => ({
useAgentChatOpenThreadsSummary: () => ({
openThreadCount: 0,
unreadOpenThreadCount: 0,
needsInputThreadCount: 0,
mentionThreadCount: 0,
assignedToMeThreadCount: 0,
assignedThreadCount: 0,
unassignedThreadCount: 0,
hasUnreadOpenThread: false,
hasUnreadMentionThread: false,
hasUnreadAssignedThread: false,
}),
}));
jest.mock('@/ai/hooks/useSwitchToNewAiChat', () => ({
useSwitchToNewAiChat: () => ({ switchToNewChat: jest.fn() }),
}));
jest.mock('~/pages/ai-chat/AiChatPageEffects', () => ({
AiChatPageEffects: () => null,
}));
jest.mock('@/ai/components/AiChatTab', () => ({
AiChatTab: () => <div>New conversation composer</div>,
}));
jest.mock('@/ai/components/AiChatPageHeader', () => ({
AiChatPageHeader: ({ actionButton }: { actionButton?: ReactNode }) => (
<div>
New chat header
{actionButton}
</div>
),
}));
// Sending stands in for the composer with what a send applies right away
jest.mock('~/pages/ai-chat/AiChatThreadPageContent', () => ({
AiChatThreadPageContent: ({
threadId,
headerTitlePrefix,
headerActions,
}: {
threadId: string;
headerTitlePrefix?: ReactNode;
headerActions?: ReactNode;
}) => {
const { applyOptimisticRestore } = useOptimisticallyRestoreOnSend();
return (
<div>
{headerTitlePrefix}
Chat page {threadId}
{headerActions}
<button
onClick={() =>
applyOptimisticRestore({
threadId,
optimisticUpdatedAt: '2026-09-09T00:00:00.000Z',
})
}
>
Send
</button>
</div>
);
},
}));
let isMobile = false;
jest.mock('twenty-ui/utilities', () => ({
...jest.requireActual('twenty-ui/utilities'),
useIsMobile: () => isMobile,
}));
jest.mock('@/information-banner/components/InformationBannerWrapper', () => ({
InformationBannerWrapper: () => null,
}));
// Command menu items throw without the context store and command menu
// instances, so the stand-in requires them too
jest.mock('@/command-menu-item/contexts/CommandMenuContextProvider', () => ({
CommandMenuContextProvider: ({ children }: { children: ReactNode }) => {
useAvailableComponentInstanceIdOrThrow(
ContextStoreComponentInstanceContext,
);
useAvailableComponentInstanceIdOrThrow(CommandMenuComponentInstanceContext);
return children;
},
}));
jest.mock(
'@/command-menu-item/display/components/PinnedCommandMenuItemButtons',
() => ({ PinnedCommandMenuItemButtons: () => null }),
);
let mockIsAiChatInboxEnabled = true;
jest.mock('@/workspace/hooks/useIsFeatureEnabled', () => ({
useIsFeatureEnabled: () => mockIsAiChatInboxEnabled,
}));
const [firstThread, secondThread, thirdThread] = THREADS;
const renderInbox = (path = `/inbox/${firstThread.id}`) =>
render(
<JotaiProvider store={jotaiStore}>
<I18nProvider i18n={i18n}>
<MemoryRouter initialEntries={[path]}>
<MemoryRouterAppNavigatorProvider>
<Routes>
<Route path={AppPath.AiChatInbox} element={<AiChatInboxPage />} />
<Route
path={AppPath.AiChat}
element={<div>Chat page without the inbox</div>}
/>
</Routes>
</MemoryRouterAppNavigatorProvider>
</MemoryRouter>
</I18nProvider>
</JotaiProvider>,
);
const isRowOpen = (title: string) =>
screen.getByRole('button', { name: title }).getAttribute('aria-pressed') ===
'true';
const getRowCheckbox = (title: string) =>
screen.getByRole('checkbox', { name: `Select ${title}` });
const getTargetedThreadIds = (contextStoreInstanceId: string) =>
jotaiStore.get(
contextStoreTargetedRecordsRuleComponentState.atomFamily({
instanceId: contextStoreInstanceId,
}),
);
describe('AiChatInboxPage', () => {
beforeAll(() => {
Element.prototype.scrollIntoView = jest.fn();
});
beforeEach(() => {
// The list filter is kept in local storage
localStorage.clear();
resetJotaiStore();
THREADS.forEach((thread) =>
jotaiStore.set(recordStoreFamilyState.atomFamily(thread.id), thread),
);
jotaiStore.set(agentChatThreadListState.atom, {
threadIds: THREADS.map(({ id }) => id),
hasNextPage: false,
endCursor: null,
});
// The member's own chats, which they follow
jotaiStore.set(
agentChatThreadParticipantsState.atom,
Object.fromEntries(
THREADS.map(({ id, updatedAt }) => [
id,
{
__typename: 'AgentChatThreadParticipant' as const,
id: `participant-${id}`,
threadId: id,
lastReadAt: updatedAt,
archivedAt: null,
snoozedUntil: null,
isSubscribed: true,
lastMentionedAt: null,
mentionsResolvedAt: null,
updatedAt,
},
]),
),
);
isMobile = false;
mockIsAiChatInboxEnabled = true;
});
it('sends the inbox to the chat page while the inbox feature flag is off', () => {
mockIsAiChatInboxEnabled = false;
renderInbox();
expect(screen.getByText('Chat page without the inbox')).toBeInTheDocument();
});
it('shows the list without selecting a chat on the base inbox route', () => {
renderInbox('/inbox');
expect(
screen.getByRole('region', { name: 'Conversation list' }),
).toBeInTheDocument();
expect(screen.queryByText(/^Chat page /)).not.toBeInTheDocument();
expect(
screen.queryByText('No conversation selected'),
).not.toBeInTheDocument();
});
it('shows a new chat beside the list without opening the first saved chat', () => {
renderInbox(`/inbox/${AI_CHAT_NEW_THREAD_PATH_SEGMENT}`);
expect(screen.getByText('New conversation composer')).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'First chat' }),
).toBeInTheDocument();
expect(isRowOpen('First chat')).toBe(false);
expect(screen.queryByText(/^Chat page /)).not.toBeInTheDocument();
});
it('opens the clicked chat', () => {
renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Second chat' }));
expect(
screen.getByText(`Chat page ${secondThread.id}`),
).toBeInTheDocument();
});
it('stays on a done chat the member writes in, though it leaves the done list', () => {
jotaiStore.set(
agentChatThreadFilterStatusState.atom,
AGENT_CHAT_THREAD_FILTER_STATUS.DONE,
);
jotaiStore.set(
agentChatThreadParticipantsState.atom,
Object.fromEntries(
THREADS.map(({ id }) => [
id,
{
__typename: 'AgentChatThreadParticipant' as const,
id: `participant-${id}`,
threadId: id,
lastReadAt: '2026-09-07T00:00:00.000Z',
archivedAt: '2026-09-08T00:00:00.000Z',
snoozedUntil: null,
isSubscribed: true,
lastMentionedAt: null,
mentionsResolvedAt: null,
updatedAt: '2026-09-08T00:00:00.000Z',
},
]),
),
);
renderInbox(`/inbox/${secondThread.id}`);
expect(isRowOpen('Second chat')).toBe(true);
fireEvent.click(screen.getByRole('button', { name: 'Send' }));
expect(
screen.queryByRole('button', { name: 'Second chat' }),
).not.toBeInTheDocument();
expect(
screen.getByText(`Chat page ${secondThread.id}`),
).toBeInTheDocument();
});
it('adds a chat to the one on screen with cmd+click', () => {
renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Third chat' }), {
metaKey: true,
});
expect(screen.getByText('2 chats selected')).toBeInTheDocument();
expect(getTargetedThreadIds(AI_CHAT_INBOX_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [firstThread.id, thirdThread.id],
});
expect(getRowCheckbox('First chat')).toBeChecked();
expect(getRowCheckbox('Second chat')).not.toBeChecked();
expect(getRowCheckbox('Third chat')).toBeChecked();
expect(isRowOpen('First chat')).toBe(false);
});
it('checks a chat from its checkbox, leaving the chat on screen out', () => {
renderInbox();
fireEvent.click(
screen.getByRole('checkbox', { name: 'Select Third chat' }),
);
expect(screen.getByText('1 chat selected')).toBeInTheDocument();
expect(getRowCheckbox('First chat')).not.toBeChecked();
expect(getRowCheckbox('Third chat')).toBeChecked();
});
it('checks the chat on screen from its checkbox', () => {
renderInbox();
fireEvent.click(
screen.getByRole('checkbox', { name: 'Select First chat' }),
);
expect(screen.getByText('1 chat selected')).toBeInTheDocument();
expect(getRowCheckbox('First chat')).toBeChecked();
expect(screen.getByText('1 selected')).toBeInTheDocument();
});
it('counts the selection in the list header instead of offering a new chat', () => {
renderInbox();
expect(
screen.getByRole('button', { name: 'New chat' }),
).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Third chat' }), {
metaKey: true,
});
expect(screen.getByText('2 selected')).toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'New chat' }),
).not.toBeInTheDocument();
});
it('lets the command menu act on the selection while it is shown', () => {
renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Third chat' }), {
metaKey: true,
});
expect(getTargetedThreadIds(MAIN_CONTEXT_STORE_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [firstThread.id, thirdThread.id],
});
fireEvent.click(screen.getByRole('button', { name: 'Clear selection' }));
expect(getTargetedThreadIds(MAIN_CONTEXT_STORE_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [],
});
});
it('opens the command menu on the right-clicked chats', () => {
renderInbox();
fireEvent.contextMenu(screen.getByRole('button', { name: 'Second chat' }));
fireEvent.contextMenu(screen.getByRole('button', { name: 'Third chat' }));
expect(screen.getByText('2 chats selected')).toBeInTheDocument();
expect(getTargetedThreadIds(AI_CHAT_INBOX_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [secondThread.id, thirdThread.id],
});
expect(
jotaiStore.get(
isDropdownOpenComponentState.atomFamily({
instanceId: getCommandMenuDropdownIdFromCommandMenuId(
AI_CHAT_INBOX_INSTANCE_ID,
),
}),
),
).toBe(true);
});
it('selects every chat from the one on screen with shift+click', () => {
renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Third chat' }), {
shiftKey: true,
});
expect(screen.getByText('3 chats selected')).toBeInTheDocument();
});
it('moves between chats with the arrow keys', async () => {
renderInbox();
await userEvent.keyboard('{ArrowDown}{ArrowDown}');
expect(screen.getByText(`Chat page ${thirdThread.id}`)).toBeInTheDocument();
await userEvent.keyboard('{ArrowUp}');
expect(
screen.getByText(`Chat page ${secondThread.id}`),
).toBeInTheDocument();
});
it('selects chats from the one on screen with shift+arrow keys', async () => {
renderInbox();
await userEvent.keyboard('{Shift>}{ArrowDown}{ArrowDown}{/Shift}');
expect(screen.getByText('3 chats selected')).toBeInTheDocument();
await userEvent.keyboard('{Shift>}{ArrowUp}{/Shift}');
expect(screen.getByText('2 chats selected')).toBeInTheDocument();
await userEvent.keyboard('{ArrowDown}');
expect(screen.getByText(`Chat page ${thirdThread.id}`)).toBeInTheDocument();
});
it('goes back to the chat on screen once the selection is cleared', () => {
renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Second chat' }), {
ctrlKey: true,
});
fireEvent.click(screen.getByRole('button', { name: 'Clear selection' }));
expect(screen.getByText(`Chat page ${firstThread.id}`)).toBeInTheDocument();
expect(isRowOpen('First chat')).toBe(true);
});
it('starts with no selection when coming back to the inbox', () => {
const { unmount } = renderInbox();
fireEvent.click(screen.getByRole('button', { name: 'Second chat' }), {
metaKey: true,
});
unmount();
expect(getTargetedThreadIds(AI_CHAT_INBOX_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [],
});
renderInbox();
expect(screen.getByText(`Chat page ${firstThread.id}`)).toBeInTheDocument();
expect(getRowCheckbox('Second chat')).not.toBeChecked();
});
it('switches to the record page layout from the list header', async () => {
renderInbox();
await userEvent.click(
screen.getByRole('button', { name: 'Open chats in' }),
);
await userEvent.click(await screen.findByText('Record page'));
expect(jotaiStore.get(aiChatInboxLayoutState.atom)).toBe(
AI_CHAT_INBOX_LAYOUT.RECORD_PAGE,
);
expect(screen.queryByText(/Chat page/)).not.toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'First chat' }),
).toBeInTheDocument();
});
describe('in record page layout', () => {
beforeEach(() => {
jotaiStore.set(
aiChatInboxLayoutState.atom,
AI_CHAT_INBOX_LAYOUT.RECORD_PAGE,
);
});
it('shows a new chat in place of the list', () => {
renderInbox(`/inbox/${AI_CHAT_NEW_THREAD_PATH_SEGMENT}`);
expect(screen.getByText('New conversation composer')).toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'First chat' }),
).not.toBeInTheDocument();
expect(screen.queryByText(/^Chat page /)).not.toBeInTheDocument();
});
it('shows the list alone, then the clicked chat alone', () => {
renderInbox('/inbox');
expect(screen.queryByText(/Chat page/)).not.toBeInTheDocument();
expect(
screen.queryByText('No conversation selected'),
).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Second chat' }));
expect(
screen.getByText(`Chat page ${secondThread.id}`),
).toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'First chat' }),
).not.toBeInTheDocument();
});
it('moves between chats and back to the list from the chat', async () => {
renderInbox(`/inbox/${secondThread.id}`);
fireEvent.click(screen.getByRole('button', { name: 'Next chat' }));
expect(
screen.getByText(`Chat page ${thirdThread.id}`),
).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Next chat' })).toBeDisabled();
fireEvent.click(screen.getByRole('link', { name: /Open/ }));
await waitFor(() =>
expect(screen.queryByText(/Chat page/)).not.toBeInTheDocument(),
);
expect(
screen.getByRole('button', { name: 'Third chat' }),
).toBeInTheDocument();
});
it('moves between chats with the arrow keys without selecting any from the chat', async () => {
renderInbox(`/inbox/${firstThread.id}`);
await userEvent.keyboard('{Shift>}{ArrowDown}{/Shift}');
expect(
screen.getByText(`Chat page ${firstThread.id}`),
).toBeInTheDocument();
expect(screen.queryByText(/selected/)).not.toBeInTheDocument();
await userEvent.keyboard('{ArrowDown}');
expect(
screen.getByText(`Chat page ${secondThread.id}`),
).toBeInTheDocument();
});
it('selects chats in the list, with the command menu acting on them', () => {
renderInbox('/inbox');
fireEvent.click(screen.getByRole('button', { name: 'Second chat' }), {
metaKey: true,
});
expect(screen.getByText('1 selected')).toBeInTheDocument();
expect(screen.queryByText('1 chat selected')).not.toBeInTheDocument();
expect(getTargetedThreadIds(MAIN_CONTEXT_STORE_INSTANCE_ID)).toEqual({
mode: 'selection',
selectedRecordIds: [secondThread.id],
});
});
});
describe('on a phone', () => {
beforeEach(() => {
isMobile = true;
});
it('shows a new chat with a way back to the inbox', () => {
renderInbox(`/inbox/${AI_CHAT_NEW_THREAD_PATH_SEGMENT}`);
expect(screen.getByText('New conversation composer')).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Back to inbox' }),
).toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'First chat' }),
).not.toBeInTheDocument();
});
it('opens the tapped chat in place of the list', () => {
renderInbox('/inbox');
fireEvent.click(screen.getByRole('button', { name: 'First chat' }));
expect(
screen.getByText(`Chat page ${firstThread.id}`),
).toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'Second chat' }),
).not.toBeInTheDocument();
});
it('adds tapped chats to a selection started from a checkbox', () => {
renderInbox('/inbox');
fireEvent.click(
screen.getByRole('checkbox', { name: 'Select First chat' }),
);
fireEvent.click(screen.getByRole('button', { name: 'Third chat' }));
expect(screen.getByText('2 selected')).toBeInTheDocument();
expect(getRowCheckbox('First chat')).toBeChecked();
expect(getRowCheckbox('Third chat')).toBeChecked();
expect(screen.queryByText(/Chat page/)).not.toBeInTheDocument();
});
});
});