| 1 | import { subDays, subMonths, subYears, startOfDay, addHours } from "date-fns"; |
| 2 | import { render } from "ink-testing-library"; |
| 3 | import React from "react"; |
| 4 | import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 5 | |
| 6 | import type { SessionMetadata } from "../session.js"; |
| 7 | |
| 8 | import { SessionSelector } from "./SessionSelector.js"; |
| 9 | |
| 10 | describe("SessionSelector", () => { |
| 11 | const mockOnSelect = vi.fn(); |
| 12 | const mockOnExit = vi.fn(); |
| 13 | |
| 14 | beforeEach(() => { |
| 15 | vi.clearAllMocks(); |
| 16 | }); |
| 17 | |
| 18 | it("formats timestamps correctly for different time periods", () => { |
| 19 | const now = new Date(); |
| 20 | const today = addHours(startOfDay(now), 14); // 2 PM today |
| 21 | const yesterday = subDays(today, 1); |
| 22 | const thisWeek = subDays(today, 3); // 3 days ago (this week) |
| 23 | const thisYear = subMonths(today, 2); // 2 months ago (this year) |
| 24 | const lastYear = subYears(today, 1); // 1 year ago |
| 25 | |
| 26 | const sessions: SessionMetadata[] = [ |
| 27 | { |
| 28 | id: "session-1", |
| 29 | path: "/test/session-1.json", |
| 30 | timestamp: today, |
| 31 | messageCount: 10, |
| 32 | firstUserMessage: "Today's session", |
| 33 | }, |
| 34 | { |
| 35 | id: "session-2", |
| 36 | path: "/test/session-2.json", |
| 37 | timestamp: yesterday, |
| 38 | messageCount: 5, |
| 39 | firstUserMessage: "Yesterday's session", |
| 40 | }, |
| 41 | { |
| 42 | id: "session-3", |
| 43 | path: "/test/session-3.json", |
| 44 | timestamp: thisWeek, |
| 45 | messageCount: 8, |
| 46 | firstUserMessage: "This week's session", |
| 47 | }, |
| 48 | { |
| 49 | id: "session-4", |
| 50 | path: "/test/session-4.json", |
| 51 | timestamp: thisYear, |
| 52 | messageCount: 12, |
| 53 | firstUserMessage: "This year's session", |
| 54 | }, |
| 55 | { |
| 56 | id: "session-5", |
| 57 | path: "/test/session-5.json", |
| 58 | timestamp: lastYear, |
| 59 | messageCount: 3, |
| 60 | firstUserMessage: "Last year's session", |
| 61 | }, |
| 62 | ]; |
| 63 | |
| 64 | const { lastFrame } = render( |
| 65 | <SessionSelector |
| 66 | sessions={sessions} |
| 67 | onSelect={mockOnSelect} |
| 68 | onExit={mockOnExit} |
| 69 | />, |
| 70 | ); |
| 71 | |
| 72 | // Check that the output contains expected date formats |
| 73 | expect(lastFrame()).toContain("2:00 PM"); // Today should show time |
| 74 | expect(lastFrame()).toContain("yesterday"); // Yesterday should show "yesterday" |
| 75 | |
| 76 | // This week should show day name (we can't predict exact day without knowing when test runs) |
| 77 | // but we can check it doesn't contain "ago" which was the old format |
| 78 | expect(lastFrame()).not.toContain("days ago"); |
| 79 | |
| 80 | // This year should show month/day format like "Oct 18" (no "ago") |
| 81 | expect(lastFrame()).toMatch(/[A-Z][a-z]{2} \d{1,2}/); // MMM d format |
| 82 | |
| 83 | // Last year should include the year |
| 84 | expect(lastFrame()).toMatch(/[A-Z][a-z]{2} \d{1,2}, \d{4}/); // MMM d, yyyy format |
| 85 | }); |
| 86 | |
| 87 | it("handles empty sessions gracefully", () => { |
| 88 | const { lastFrame } = render( |
| 89 | <SessionSelector |
| 90 | sessions={[]} |
| 91 | onSelect={mockOnSelect} |
| 92 | onExit={mockOnExit} |
| 93 | />, |
| 94 | ); |
| 95 | |
| 96 | expect(lastFrame()).toContain("No previous sessions found"); |
| 97 | expect(lastFrame()).toContain("Start a new conversation with: cn"); |
| 98 | }); |
| 99 | |
| 100 | it("handles sessions without first user message", () => { |
| 101 | const sessions: SessionMetadata[] = [ |
| 102 | { |
| 103 | id: "session-1", |
| 104 | path: "/test/session-1.json", |
| 105 | timestamp: new Date(), |
| 106 | messageCount: 0, |
| 107 | firstUserMessage: undefined, |
| 108 | }, |
| 109 | ]; |
| 110 | |
| 111 | const { lastFrame } = render( |
| 112 | <SessionSelector |
| 113 | sessions={sessions} |
| 114 | onSelect={mockOnSelect} |
| 115 | onExit={mockOnExit} |
| 116 | />, |
| 117 | ); |
| 118 | |
| 119 | expect(lastFrame()).toContain("(no messages)"); |
| 120 | }); |
| 121 | }); |