Elffuss BraidHub

kikocis/continue public

57d99ced5e24 · 3.3 KiBRawPermalink
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
3import * as sessionModule from "../session.js";
4
5import { listSessionsCommand } from "./ls.js";
6
7// Mock the session module
8vi.mock("../session.js", () => ({
9 listSessions: vi.fn(),
10 loadSessionById: vi.fn(),
11}));
12
13// Mock the TUI components
14vi.mock("../ui/SessionSelector.js", () => ({
15 SessionSelector: () => null,
16}));
17
18// Mock ink
19vi.mock("ink", () => ({
20 render: vi.fn(() => ({ unmount: vi.fn() })),
21}));
22
23// Mock react
24vi.mock("react", () => ({
25 createElement: vi.fn(),
26}));
27
28// Mock the chat command
29vi.mock("./chat.js", () => ({
30 chat: vi.fn(),
31}));
32
33describe("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 id: "session-1",
48 path: "/path/to/session1.json",
49 timestamp: new Date("2023-01-01T10:00:00Z"),
50 messageCount: 5,
51 firstUserMessage: "Hello world",
52 },
53 {
54 id: "session-2",
55 path: "/path/to/session2.json",
56 timestamp: new Date("2023-01-01T09:00:00Z"),
57 messageCount: 3,
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 messageCount: 5,
76 firstUserMessage: "Hello world",
77 },
78 {
79 id: "session-2",
80 timestamp: "2023-01-01T09:00:00.000Z",
81 messageCount: 3,
82 firstUserMessage: "Test message",
83 },
84 ],
85 },
86 null,
87 2,
88 ),
89 );
90
91 consoleSpy.mockRestore();
92 });
93
94 it("should use limit of 10 for JSON and 20 for TUI", async () => {
95 mockListSessions.mockReturnValue([]);
96
97 // JSON mode should fetch 10
98 await listSessionsCommand({ format: "json" });
99 expect(mockListSessions).toHaveBeenCalledWith(10);
100
101 // TUI mode should fetch 20 (so UI can choose how many to display)
102 await listSessionsCommand({});
103 expect(mockListSessions).toHaveBeenCalledWith(20);
104 });
105
106 it("should handle empty sessions gracefully in JSON mode", async () => {
107 mockListSessions.mockReturnValue([]);
108
109 const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
110
111 await listSessionsCommand({ format: "json" });
112
113 expect(consoleSpy).toHaveBeenCalledWith(
114 JSON.stringify({ sessions: [] }, null, 2),
115 );
116
117 consoleSpy.mockRestore();
118 });
119
120 it("should handle empty sessions gracefully in TUI mode", async () => {
121 mockListSessions.mockReturnValue([]);
122
123 const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
124
125 await listSessionsCommand({});
126
127 expect(consoleSpy).toHaveBeenCalledWith(
128 "No previous sessions found. Start a new conversation with: cn",
129 );
130
131 consoleSpy.mockRestore();
132 });
133});