|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 4 | +import { groupChatsByDate } from "../components/chat/chat-history-utils"; |
| 5 | +import type { Chat } from "../core/ai/state"; |
| 6 | + |
| 7 | +// Mock current time for consistent testing |
| 8 | +const mockNow = new Date("2024-01-15T12:00:00Z").getTime(); |
| 9 | + |
| 10 | +// Mock Date.now to return our fixed time |
| 11 | +const originalDateNow = Date.now; |
| 12 | +beforeAll(() => { |
| 13 | + Date.now = () => mockNow; |
| 14 | +}); |
| 15 | + |
| 16 | +afterAll(() => { |
| 17 | + Date.now = originalDateNow; |
| 18 | +}); |
| 19 | + |
| 20 | +describe("groupChatsByDate", () => { |
| 21 | + const createMockChat = (daysAgo: number, title: string): Chat => ({ |
| 22 | + id: `chat-${daysAgo}` as Chat["id"], |
| 23 | + title, |
| 24 | + messages: [], |
| 25 | + createdAt: mockNow - daysAgo * 24 * 60 * 60 * 1000, |
| 26 | + updatedAt: mockNow - daysAgo * 24 * 60 * 60 * 1000, |
| 27 | + }); |
| 28 | + |
| 29 | + it("should group chats correctly by date periods", () => { |
| 30 | + const chats: Chat[] = [ |
| 31 | + createMockChat(0, "Today chat"), |
| 32 | + createMockChat(1, "Yesterday chat"), |
| 33 | + createMockChat(2, "2 days ago chat"), |
| 34 | + createMockChat(3, "3 days ago chat"), |
| 35 | + createMockChat(5, "5 days ago chat"), // Should go to "This week" |
| 36 | + createMockChat(10, "10 days ago chat"), // Should go to "This month" |
| 37 | + createMockChat(40, "40 days ago chat"), // Should go to "Older" |
| 38 | + ]; |
| 39 | + |
| 40 | + const result = groupChatsByDate(chats); |
| 41 | + |
| 42 | + // Should have 7 groups |
| 43 | + expect(result).toHaveLength(chats.length); |
| 44 | + |
| 45 | + // Check Today group |
| 46 | + const todayGroup = result.find((g) => g.label === "Today"); |
| 47 | + expect(todayGroup?.chats).toHaveLength(1); |
| 48 | + expect(todayGroup?.chats[0].title).toBe("Today chat"); |
| 49 | + |
| 50 | + // Check Yesterday group |
| 51 | + const yesterdayGroup = result.find((g) => g.label === "Yesterday"); |
| 52 | + expect(yesterdayGroup?.chats).toHaveLength(1); |
| 53 | + expect(yesterdayGroup?.chats[0].title).toBe("Yesterday chat"); |
| 54 | + |
| 55 | + // Check 2d ago group |
| 56 | + const twoDaysGroup = result.find((g) => g.label === "2d ago"); |
| 57 | + expect(twoDaysGroup?.chats).toHaveLength(1); |
| 58 | + expect(twoDaysGroup?.chats[0].title).toBe("2 days ago chat"); |
| 59 | + |
| 60 | + // Check 3d ago group |
| 61 | + const threeDaysGroup = result.find((g) => g.label === "3d ago"); |
| 62 | + expect(threeDaysGroup?.chats).toHaveLength(1); |
| 63 | + expect(threeDaysGroup?.chats[0].title).toBe("3 days ago chat"); |
| 64 | + |
| 65 | + // Check This week group (should include 5) |
| 66 | + const thisWeekGroup = result.find((g) => g.label === "This week"); |
| 67 | + expect(thisWeekGroup?.chats).toHaveLength(1); |
| 68 | + expect(thisWeekGroup?.chats.map((c) => c.title)).toContain( |
| 69 | + "5 days ago chat", |
| 70 | + ); |
| 71 | + |
| 72 | + // Check This month group (should include 40 days ago) |
| 73 | + const thisMonthGroup = result.find((g) => g.label === "This month"); |
| 74 | + expect(thisMonthGroup?.chats).toHaveLength(1); |
| 75 | + expect(thisMonthGroup?.chats[0].title).toBe("10 days ago chat"); |
| 76 | + |
| 77 | + // Check Older group (should include 40 days ago) |
| 78 | + const olderGroup = result.find((g) => g.label === "Older"); |
| 79 | + expect(olderGroup?.chats).toHaveLength(1); |
| 80 | + expect(olderGroup?.chats[0].title).toBe("40 days ago chat"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should include all chats in some group", () => { |
| 84 | + const chats: Chat[] = [ |
| 85 | + createMockChat(0, "Today"), |
| 86 | + createMockChat(1, "Yesterday"), |
| 87 | + createMockChat(2, "2 days ago"), |
| 88 | + createMockChat(3, "3 days ago"), |
| 89 | + createMockChat(5, "5 days ago"), |
| 90 | + createMockChat(10, "10 days ago"), |
| 91 | + createMockChat(20, "20 days ago"), |
| 92 | + createMockChat(40, "40 days ago"), |
| 93 | + createMockChat(100, "100 days ago"), |
| 94 | + ]; |
| 95 | + |
| 96 | + const result = groupChatsByDate(chats); |
| 97 | + |
| 98 | + // Count total chats across all groups |
| 99 | + const totalChatsInGroups = result.reduce( |
| 100 | + (sum, group) => sum + group.chats.length, |
| 101 | + 0, |
| 102 | + ); |
| 103 | + expect(totalChatsInGroups).toBe(chats.length); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should handle empty chat list", () => { |
| 107 | + const result = groupChatsByDate([]); |
| 108 | + expect(result).toHaveLength(0); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should filter out empty groups", () => { |
| 112 | + const chats: Chat[] = [ |
| 113 | + createMockChat(0, "Today chat"), |
| 114 | + createMockChat(40, "Old chat"), |
| 115 | + ]; |
| 116 | + |
| 117 | + const result = groupChatsByDate(chats); |
| 118 | + |
| 119 | + // Should only have Today and Older groups, not the empty ones in between |
| 120 | + expect(result).toHaveLength(2); |
| 121 | + expect(result.map((g) => g.label)).toEqual(["Today", "Older"]); |
| 122 | + }); |
| 123 | +}); |
0 commit comments