|
| 1 | +import { |
| 2 | + buildChatSections, |
| 3 | + getRelativeDateSection, |
| 4 | + sortChatsByRecency, |
| 5 | +} from '../components/drawer/chatSections'; |
| 6 | +import { Chat } from '../database/chatRepository'; |
| 7 | + |
| 8 | +const HOUR = 60 * 60 * 1000; |
| 9 | +const DAY = 24 * HOUR; |
| 10 | +const NOW = new Date('2026-03-10T12:00:00Z').getTime(); |
| 11 | + |
| 12 | +const makeChat = (over: Partial<Chat>): Chat => ({ |
| 13 | + id: 1, |
| 14 | + modelId: 1, |
| 15 | + title: 'Chat', |
| 16 | + lastUsed: NOW, |
| 17 | + ...over, |
| 18 | +}); |
| 19 | + |
| 20 | +describe('getRelativeDateSection', () => { |
| 21 | + const now = new Date(NOW); |
| 22 | + |
| 23 | + it('labels the same day as Today', () => { |
| 24 | + expect(getRelativeDateSection(new Date(NOW - HOUR), now)).toBe('Today'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('labels the previous day as Yesterday', () => { |
| 28 | + expect(getRelativeDateSection(new Date(NOW - DAY), now)).toBe('Yesterday'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('labels 2-6 days back as "<n> days ago"', () => { |
| 32 | + expect(getRelativeDateSection(new Date(NOW - 2 * DAY), now)).toBe( |
| 33 | + '2 days ago' |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('labels anything older than a year as "More than a year ago"', () => { |
| 38 | + expect(getRelativeDateSection(new Date(NOW - 400 * DAY), now)).toBe( |
| 39 | + 'More than a year ago' |
| 40 | + ); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('sortChatsByRecency', () => { |
| 45 | + it('orders chats most-recent first without mutating the input', () => { |
| 46 | + const input = [ |
| 47 | + makeChat({ id: 1, lastUsed: NOW - DAY }), |
| 48 | + makeChat({ id: 2, lastUsed: NOW - HOUR }), |
| 49 | + makeChat({ id: 3, lastUsed: NOW - 2 * DAY }), |
| 50 | + ]; |
| 51 | + |
| 52 | + const sorted = sortChatsByRecency(input); |
| 53 | + |
| 54 | + expect(sorted.map((chat) => chat.id)).toEqual([2, 1, 3]); |
| 55 | + expect(input.map((chat) => chat.id)).toEqual([1, 2, 3]); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('buildChatSections', () => { |
| 60 | + const chats = [ |
| 61 | + makeChat({ id: 1, title: 'Pizza recipe', lastUsed: NOW - HOUR }), |
| 62 | + makeChat({ id: 2, title: 'Meeting notes', lastUsed: NOW - 2 * HOUR }), |
| 63 | + makeChat({ id: 3, title: 'Trip to Rome', lastUsed: NOW - DAY }), |
| 64 | + ]; |
| 65 | + |
| 66 | + it('groups pre-sorted chats by their relative date section', () => { |
| 67 | + const sections = buildChatSections(chats, '', NOW); |
| 68 | + |
| 69 | + expect(sections).toEqual([ |
| 70 | + ['Today', [chats[0], chats[1]]], |
| 71 | + ['Yesterday', [chats[2]]], |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('filters by a case-insensitive label match', () => { |
| 76 | + const sections = buildChatSections(chats, 'rome', NOW); |
| 77 | + |
| 78 | + expect(sections).toEqual([['Yesterday', [chats[2]]]]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('matches the "Chat <id>" fallback label of untitled chats', () => { |
| 82 | + const untitled = makeChat({ id: 42, title: '', lastUsed: NOW - HOUR }); |
| 83 | + |
| 84 | + const sections = buildChatSections([untitled], 'chat 42', NOW); |
| 85 | + |
| 86 | + expect(sections).toEqual([['Today', [untitled]]]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns no sections when nothing matches', () => { |
| 90 | + expect(buildChatSections(chats, 'nonexistent', NOW)).toEqual([]); |
| 91 | + }); |
| 92 | +}); |
0 commit comments