diff --git a/client/src/components/Conversations/Conversations.tsx b/client/src/components/Conversations/Conversations.tsx index f7a9a54101..75bc305066 100644 --- a/client/src/components/Conversations/Conversations.tsx +++ b/client/src/components/Conversations/Conversations.tsx @@ -541,8 +541,12 @@ const Conversations: FC = ({ if (item.type === 'convo') { const isGenerating = activeJobIds.has(item.convo.conversationId ?? ''); + /** Keyed by chat, not by the row index react-virtualized passes: an index key + * keeps a row's local state (an open delete dialog, a rename field) in its slot + * while a different chat moves into it. */ + const convoKey = item.convo.conversationId ? `convo-${item.convo.conversationId}` : key; return ( - + ({ }: { conversation: TConversation; showProjectBadge?: boolean; - }) => ( -
- {conversation.title} -
- ), + }) => { + /* Stands in for the row's local state (an open delete dialog, a rename field): + * captured on mount, so it shows which chat the mounted row was created for. */ + const [openedFor] = jest.requireActual('react').useState(conversation.title); + return ( +
+ {conversation.title} +
+ ); + }, })); const pinnedConvo = { @@ -215,6 +224,61 @@ describe('Conversations: live running order', () => { }); }); +describe('Conversations: row state follows its chat', () => { + const containerRef = createRef(); + const chats = ['A', 'B', 'C'].map( + (name) => + ({ + conversationId: `chat-${name}`, + title: `Chat ${name}`, + createdAt: '2026-09-26T00:00:00.000Z', + updatedAt: '2026-09-26T00:00:00.000Z', + }) as TConversation, + ); + + it('keeps each mounted row on the chat it was created for when rows shift', () => { + const renderList = (conversations: TConversation[]) => ( + + + + + + + + ); + const rows = () => + screen + .getAllByTestId('convo') + .map((row) => [row.textContent, row.getAttribute('data-opened-for')]); + + const view = render(renderList(chats)); + expect(rows()).toEqual([ + ['Chat A', 'Chat A'], + ['Chat B', 'Chat B'], + ['Chat C', 'Chat C'], + ]); + + /* Archiving or deleting the first chat moves every row below it up one slot. */ + view.rerender(renderList(chats.slice(1))); + expect(rows()).toEqual([ + ['Chat B', 'Chat B'], + ['Chat C', 'Chat C'], + ]); + }); +}); + describe('Conversations: pinned chats live in PinnedSection', () => { const containerRef = createRef(); diff --git a/client/src/components/Projects/ProjectChatList.tsx b/client/src/components/Projects/ProjectChatList.tsx index 2623470ed8..238956f71c 100644 --- a/client/src/components/Projects/ProjectChatList.tsx +++ b/client/src/components/Projects/ProjectChatList.tsx @@ -227,8 +227,10 @@ const ProjectChatList = ({ ); } + /** Keyed by chat so a row's local state stays with its chat when rows move. */ + const convoKey = item.convo.conversationId ? `convo-${item.convo.conversationId}` : key; return ( - + + page.getByTestId('convo-item').filter({ hasText: title }).filter({ visible: true }).first(); + +/** Only the open menu: a row keeps its menu mounted after closing it. */ +const menuItem = (page: Page, name: string) => + page.getByRole('menuitem', { name, exact: true }).filter({ visible: true }); + +type Chat = { id: string; title: string }; + +/** Three chats, newest first, so archiving the first moves the other two up a slot. */ +function makeChats(label: string): Chat[] { + return ['first', 'second', 'third'].map((name) => { + const id = randomUUID(); + return { id, title: `${label} ${name} ${id.slice(0, 8)}` }; + }); +} + +async function seed(chats: Chat[]) { + const now = Date.now(); + await seedConversations( + userEmail, + chats.map((chat, i) => ({ + conversationId: chat.id, + title: chat.title, + updatedAt: new Date(now - i * 1_000), + })), + ); +} + +async function readChats(ids: string[]) { + return withMongo((db) => + db + .collection('conversations') + .find({ conversationId: { $in: ids } }) + .project<{ conversationId: string; title: string; isArchived?: boolean }>({ + _id: 0, + conversationId: 1, + title: 1, + isArchived: 1, + }) + .toArray(), + ); +} + +/** Archives `chat` with its reply held, so the rows move only when `release` is called. */ +async function archiveHeld(page: Page, chat: Chat) { + let held: Route | undefined; + let arrived!: () => void; + const reached = new Promise((resolve) => (arrived = resolve)); + await page.route('**/api/convos/archive', (route) => { + held = route; + arrived(); + }); + await openConversationMenu(row(page, chat.title)); + await menuItem(page, 'Archive').click(); + await reached; + if (await menuItem(page, 'Archive').isVisible()) { + await page.keyboard.press('Escape'); + } + await expect(menuItem(page, 'Archive')).toHaveCount(0); + return async () => { + await held?.continue(); + await page.unroute('**/api/convos/archive'); + await expect(page.getByTestId('convo-item').filter({ hasText: chat.title })).toHaveCount(0); + }; +} + +test.describe('sidebar row state follows its chat', () => { + test('the delete dialog deletes the chat it was opened for when the rows above it move @scenario:delete-dialog-deletes-the-chat-it-was-opened-for', async ({ + page, + }) => { + test.setTimeout(60_000); + const [archived, picked, below] = makeChats('Row delete'); + const ids = [archived.id, picked.id, below.id]; + try { + await seed([archived, picked, below]); + await page.goto(NEW_CHAT_PATH, { timeout: 15_000 }); + await openSidebar(page); + await expect(row(page, below.title)).toBeVisible(); + + const release = await archiveHeld(page, archived); + await openConversationMenu(row(page, picked.title)); + await menuItem(page, 'Delete').click(); + const dialog = page.getByRole('dialog', { name: 'Delete chat?' }); + await expect(dialog).toContainText(picked.title); + + await release(); + await expect(dialog).toBeVisible(); + await expect(dialog).toContainText(picked.title); + await expect(dialog).not.toContainText(below.title); + + const [deleteRequest] = await Promise.all([ + page.waitForRequest( + (request) => request.method() === 'DELETE' && request.url().includes('/api/convos'), + ), + dialog.getByRole('button', { name: 'Delete', exact: true }).click(), + ]); + expect(deleteRequest.postDataJSON()?.arg?.conversationId).toBe(picked.id); + await expect(page.getByTestId('convo-item').filter({ hasText: picked.title })).toHaveCount(0); + await expect(row(page, below.title)).toBeVisible(); + + await expect + .poll(async () => + (await readChats(ids)).map((chat) => [chat.conversationId, chat.isArchived === true]), + ) + .toEqual( + expect.arrayContaining([ + [archived.id, true], + [below.id, false], + ]), + ); + expect((await readChats(ids)).map((chat) => chat.conversationId).sort()).toEqual( + [archived.id, below.id].sort(), + ); + } finally { + await page.unroute('**/api/convos/archive').catch(() => undefined); + await deleteConversations(ids); + } + }); + + test('a rename in progress keeps its text and saves to its own chat when the rows above it move @scenario:rename-field-stays-on-its-chat-when-rows-move', async ({ + page, + }) => { + test.setTimeout(60_000); + const [archived, picked, below] = makeChats('Row rename'); + const ids = [archived.id, picked.id, below.id]; + const typed = `Renamed ${picked.id.slice(0, 8)}`; + try { + await seed([archived, picked, below]); + await page.goto(NEW_CHAT_PATH, { timeout: 15_000 }); + await openSidebar(page); + await expect(row(page, below.title)).toBeVisible(); + + const release = await archiveHeld(page, archived); + await openConversationMenu(row(page, picked.title)); + await menuItem(page, 'Rename').click(); + const titleInput = page.getByRole('textbox', { name: 'New Conversation Title' }); + await titleInput.fill(typed); + + await release(); + await expect(titleInput).toHaveValue(typed); + await titleInput.press('Enter'); + await expect(row(page, typed)).toBeVisible(); + await expect(row(page, below.title)).toBeVisible(); + + await expect + .poll(async () => + Object.fromEntries( + (await readChats(ids)).map((chat) => [chat.conversationId, chat.title]), + ), + ) + .toEqual({ + [archived.id]: archived.title, + [picked.id]: typed, + [below.id]: below.title, + }); + } finally { + await page.unroute('**/api/convos/archive').catch(() => undefined); + await deleteConversations(ids); + } + }); +});