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