| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | import * as sessionModule from "../session.js"; |
| 4 | |
| 5 | import { listSessionsCommand } from "./ls.js"; |
| 6 | |
| 7 | // Mock the session module |
| 8 | vi.mock("../session.js", () => ({ |
| 9 | listSessions: vi.fn(), |
| 10 | loadSessionById: vi.fn(), |
| 11 | })); |
| 12 | |
| 13 | // Mock the TUI components |
| 14 | vi.mock("../ui/SessionSelector.js", () => ({ |
| 15 | SessionSelector: () => null, |
| 16 | })); |
| 17 | |
| 18 | // Mock ink |
| 19 | vi.mock("ink", () => ({ |
| 20 | render: vi.fn(() => ({ unmount: vi.fn() })), |
| 21 | })); |
| 22 | |
| 23 | // Mock react |
| 24 | vi.mock("react", () => ({ |
| 25 | createElement: vi.fn(), |
| 26 | })); |
| 27 | |
| 28 | // Mock the chat command |
| 29 | vi.mock("./chat.js", () => ({ |
| 30 | chat: vi.fn(), |
| 31 | })); |
| 32 | |
| 33 | describe("listSessionsCommand", () => { |
| 34 | const mockListSessions = vi.mocked(sessionModule.listSessions); |
| 35 | |
| 36 | beforeEach(() => { |
| 37 | vi.clearAllMocks(); |
| 38 | }); |
| 39 | |
| 40 | afterEach(() => { |
| 41 | vi.restoreAllMocks(); |
| 42 | }); |
| 43 | |
| 44 | it("should output JSON format when format is json", async () => { |
| 45 | const mockSessions = [ |
| 46 | { |
| 47 | sessionId: "session-1", |
| 48 | title: "Session 1", |
| 49 | dateCreated: "2023-01-01T10:00:00.000Z", |
| 50 | workspaceDirectory: "/workspace", |
| 51 | firstUserMessage: "Hello world", |
| 52 | }, |
| 53 | { |
| 54 | sessionId: "session-2", |
| 55 | title: "Session 2", |
| 56 | dateCreated: "2023-01-01T09:00:00.000Z", |
| 57 | workspaceDirectory: "/workspace", |
| 58 | firstUserMessage: "Test message", |
| 59 | }, |
| 60 | ]; |
| 61 | |
| 62 | mockListSessions.mockReturnValue(mockSessions); |
| 63 | |
| 64 | const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 65 | |
| 66 | await listSessionsCommand({ format: "json" }); |
| 67 | |
| 68 | expect(consoleSpy).toHaveBeenCalledWith( |
| 69 | JSON.stringify( |
| 70 | { |
| 71 | sessions: [ |
| 72 | { |
| 73 | id: "session-1", |
| 74 | timestamp: "2023-01-01T10:00:00.000Z", |
| 75 | workspaceDirectory: "/workspace", |
| 76 | title: "Session 1", |
| 77 | firstUserMessage: "Hello world", |
| 78 | }, |
| 79 | { |
| 80 | id: "session-2", |
| 81 | timestamp: "2023-01-01T09:00:00.000Z", |
| 82 | workspaceDirectory: "/workspace", |
| 83 | title: "Session 2", |
| 84 | firstUserMessage: "Test message", |
| 85 | }, |
| 86 | ], |
| 87 | }, |
| 88 | null, |
| 89 | 2, |
| 90 | ), |
| 91 | ); |
| 92 | |
| 93 | consoleSpy.mockRestore(); |
| 94 | }); |
| 95 | |
| 96 | it("should use limit of 10 for JSON and 20 for TUI", async () => { |
| 97 | mockListSessions.mockReturnValue([]); |
| 98 | |
| 99 | // JSON mode should fetch 10 |
| 100 | await listSessionsCommand({ format: "json" }); |
| 101 | expect(mockListSessions).toHaveBeenCalledWith(10); |
| 102 | |
| 103 | // TUI mode should fetch 20 (so UI can choose how many to display) |
| 104 | await listSessionsCommand({}); |
| 105 | expect(mockListSessions).toHaveBeenCalledWith(20); |
| 106 | }); |
| 107 | |
| 108 | it("should handle empty sessions gracefully in JSON mode", async () => { |
| 109 | mockListSessions.mockReturnValue([]); |
| 110 | |
| 111 | const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 112 | |
| 113 | await listSessionsCommand({ format: "json" }); |
| 114 | |
| 115 | expect(consoleSpy).toHaveBeenCalledWith( |
| 116 | JSON.stringify({ sessions: [] }, null, 2), |
| 117 | ); |
| 118 | |
| 119 | consoleSpy.mockRestore(); |
| 120 | }); |
| 121 | |
| 122 | it("should handle empty sessions gracefully in TUI mode", async () => { |
| 123 | mockListSessions.mockReturnValue([]); |
| 124 | |
| 125 | const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 126 | |
| 127 | await listSessionsCommand({}); |
| 128 | |
| 129 | expect(consoleSpy).toHaveBeenCalledWith( |
| 130 | "No previous sessions found. Start a new conversation with: cn", |
| 131 | ); |
| 132 | |
| 133 | consoleSpy.mockRestore(); |
| 134 | }); |
| 135 | }); |