🗂️ fix: Optimize Conversation Grouping and Sorting (#4173)

* chore: remove double import of TrashIcon

* fix(convos): eslint warnings

* ci(convos): add test for month sorting

* fix(convos): grouping by month in chronological order instead of alphabetical, optimize sort

* ci: additional tests for conversation sorting

* chore: fix eslint disable rule

* chore: imports, use constant enum for 'new' value

* fix: test dependent on current date
This commit is contained in:
Danny Avila 2024-09-21 10:20:30 -04:00 committed by GitHub
parent 44458d3832
commit c1c13a69dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 243 additions and 39 deletions

View file

@ -1,4 +1,5 @@
import { convoData } from './convos.fakeData';
import { Constants } from 'librechat-data-provider';
import type { TConversation, ConversationData } from 'librechat-data-provider';
import {
dateKeys,
addConversation,
@ -8,7 +9,7 @@ import {
findPageForConversation,
groupConversationsByDate,
} from './convos';
import type { TConversation, ConversationData } from 'librechat-data-provider';
import { convoData } from './convos.fakeData';
import { normalizeData } from './collection';
describe('Conversation Utilities', () => {
@ -60,18 +61,182 @@ describe('Conversation Utilities', () => {
const uniqueIds = [...new Set(allGroupedIds)];
expect(allGroupedIds.length).toBe(uniqueIds.length);
});
it('sorts conversations by month correctly', () => {
const conversations = [
{ conversationId: '1', updatedAt: '2023-01-01T12:00:00Z' }, // January 2023
{ conversationId: '2', updatedAt: '2023-12-01T12:00:00Z' }, // December 2023
{ conversationId: '3', updatedAt: '2023-02-01T12:00:00Z' }, // February 2023
{ conversationId: '4', updatedAt: '2023-11-01T12:00:00Z' }, // November 2023
{ conversationId: '5', updatedAt: '2022-12-01T12:00:00Z' }, // December 2022
];
const grouped = groupConversationsByDate(conversations as TConversation[]);
// Check if the years are in the correct order (most recent first)
expect(grouped.map(([key]) => key)).toEqual([' 2023', ' 2022']);
// Check if conversations within 2023 are sorted correctly by month
const conversationsIn2023 = grouped[0][1];
const monthsIn2023 = conversationsIn2023.map((c) => new Date(c.updatedAt).getMonth());
expect(monthsIn2023).toEqual([11, 10, 1, 0]); // December (11), November (10), February (1), January (0)
// Check if the conversation from 2022 is in its own group
expect(grouped[1][1].length).toBe(1);
expect(new Date(grouped[1][1][0].updatedAt).getFullYear()).toBe(2022);
});
it('handles conversations from multiple years correctly', () => {
const conversations = [
{ conversationId: '1', updatedAt: '2023-01-01T12:00:00Z' }, // January 2023
{ conversationId: '2', updatedAt: '2022-12-01T12:00:00Z' }, // December 2022
{ conversationId: '3', updatedAt: '2021-06-01T12:00:00Z' }, // June 2021
{ conversationId: '4', updatedAt: '2023-06-01T12:00:00Z' }, // June 2023
{ conversationId: '5', updatedAt: '2021-12-01T12:00:00Z' }, // December 2021
];
const grouped = groupConversationsByDate(conversations as TConversation[]);
expect(grouped.map(([key]) => key)).toEqual([' 2023', ' 2022', ' 2021']);
expect(grouped[0][1].map((c) => new Date(c.updatedAt).getMonth())).toEqual([5, 0]); // June, January
expect(grouped[1][1].map((c) => new Date(c.updatedAt).getMonth())).toEqual([11]); // December
expect(grouped[2][1].map((c) => new Date(c.updatedAt).getMonth())).toEqual([11, 5]); // December, June
});
it('handles conversations from the same month correctly', () => {
const conversations = [
{ conversationId: '1', updatedAt: '2023-06-01T12:00:00Z' },
{ conversationId: '2', updatedAt: '2023-06-15T12:00:00Z' },
{ conversationId: '3', updatedAt: '2023-06-30T12:00:00Z' },
];
const grouped = groupConversationsByDate(conversations as TConversation[]);
expect(grouped.length).toBe(1);
expect(grouped[0][0]).toBe(' 2023');
expect(grouped[0][1].map((c) => c.conversationId)).toEqual(['3', '2', '1']);
});
it('handles conversations from today, yesterday, and previous days correctly', () => {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const twoDaysAgo = new Date(today);
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
const conversations = [
{ conversationId: '1', updatedAt: today.toISOString() },
{ conversationId: '2', updatedAt: yesterday.toISOString() },
{ conversationId: '3', updatedAt: twoDaysAgo.toISOString() },
];
const grouped = groupConversationsByDate(conversations as TConversation[]);
expect(grouped.map(([key]) => key)).toEqual([
dateKeys.today,
dateKeys.yesterday,
dateKeys.previous7Days,
]);
});
it('handles conversations with null or undefined updatedAt correctly', () => {
const conversations = [
{ conversationId: '1', updatedAt: '2023-06-01T12:00:00Z' },
{ conversationId: '2', updatedAt: null },
{ conversationId: '3', updatedAt: undefined },
];
const grouped = groupConversationsByDate(conversations as TConversation[]);
expect(grouped.length).toBe(2); // One group for 2023 and one for today (null/undefined dates)
expect(grouped[0][0]).toBe(dateKeys.today);
expect(grouped[0][1].length).toBe(2); // Two conversations with null/undefined dates
expect(grouped[1][0]).toBe(' 2023');
expect(grouped[1][1].length).toBe(1); // One conversation from 2023
});
it('handles an empty array of conversations', () => {
const grouped = groupConversationsByDate([]);
expect(grouped).toEqual([]);
});
it('correctly groups and sorts conversations for every month of the year', () => {
const months = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
];
// Create conversations for each month in both 2023 and 2022
const conversations = months.flatMap((month, index) => [
{
conversationId: `2023-${month}`,
updatedAt: `2023-${String(index + 1).padStart(2, '0')}-15T12:00:00Z`,
},
{
conversationId: `2022-${month}`,
updatedAt: `2022-${String(index + 1).padStart(2, '0')}-15T12:00:00Z`,
},
]);
const grouped = groupConversationsByDate(conversations as TConversation[]);
// Check that we have two year groups
expect(grouped.length).toBe(2);
// Check 2023 months
const group2023 = grouped.find(([key]) => key === ' 2023') ?? [];
expect(group2023).toBeDefined();
const grouped2023 = group2023[1];
expect(grouped2023?.length).toBe(12);
expect(grouped2023?.map((c) => new Date(c.updatedAt).getMonth())).toEqual([
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
]);
// Check 2022 months
const group2022 = grouped.find(([key]) => key === ' 2022') ?? [];
expect(group2022).toBeDefined();
const grouped2022 = group2022[1];
expect(grouped2022?.length).toBe(12);
expect(grouped2022?.map((c) => new Date(c.updatedAt).getMonth())).toEqual([
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
]);
// Check that all conversations are accounted for
const totalGroupedConversations =
// eslint-disable-next-line @typescript-eslint/no-unused-vars
grouped.reduce((total, [_, convos]) => total + convos.length, 0);
expect(totalGroupedConversations).toBe(conversations.length);
// Check that the years are in the correct order
const yearOrder = grouped.map(([key]) => key);
expect(yearOrder).toEqual([' 2023', ' 2022']);
});
});
describe('addConversation', () => {
it('adds a new conversation to the top of the list', () => {
const data = { pages: [{ conversations: [] }] };
const newConversation = { conversationId: 'new', updatedAt: '2023-04-02T12:00:00Z' };
const newConversation = {
conversationId: Constants.NEW_CONVO,
updatedAt: '2023-04-02T12:00:00Z',
};
const newData = addConversation(
data as unknown as ConversationData,
newConversation as TConversation,
);
expect(newData.pages[0].conversations).toHaveLength(1);
expect(newData.pages[0].conversations[0].conversationId).toBe('new');
expect(newData.pages[0].conversations[0].conversationId).toBe(Constants.NEW_CONVO);
});
});
@ -171,13 +336,13 @@ describe('Conversation Utilities with Fake Data', () => {
describe('addConversation', () => {
it('adds a new conversation to the existing fake data', () => {
const newConversation = {
conversationId: 'new',
conversationId: Constants.NEW_CONVO,
updatedAt: new Date().toISOString(),
} as TConversation;
const initialLength = convoData.pages[0].conversations.length;
const newData = addConversation(convoData, newConversation);
expect(newData.pages[0].conversations.length).toBe(initialLength + 1);
expect(newData.pages[0].conversations[0].conversationId).toBe('new');
expect(newData.pages[0].conversations[0].conversationId).toBe(Constants.NEW_CONVO);
});
});