Elffuss BraidHub

kikocis/continue public

main · 4.0 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 with createContext
24vi.mock("react", async (importOriginal) => {
25 const actual: any = await importOriginal();
26 return {
27 ...actual,
28 createElement: vi.fn(),
29 createContext: vi.fn(() => ({ Provider: vi.fn(), Consumer: vi.fn() })),
30 };
31});
32
33// Mock the chat command
34vi.mock("./chat.js", () => ({
35 chat: vi.fn(),
36}));
37
38// Mock the remote command
39vi.mock("./remote.js", () => ({
40 remote: vi.fn(),
41}));
42
43describe("listSessionsCommand", () => {
44 const mockListSessions = vi.mocked(sessionModule.listSessions);
45
46 beforeEach(() => {
47 vi.clearAllMocks();
48 });
49
50 afterEach(() => {
51 vi.restoreAllMocks();
52 });
53
54 it("should output JSON format when format is json", async () => {
55 const mockSessions = [
56 {
57 sessionId: "session-1",
58 title: "Session 1",
59 dateCreated: "2023-01-01T10:00:00.000Z",
60 workspaceDirectory: "/workspace",
61 firstUserMessage: "Hello world",
62 isRemote: false,
63 },
64 {
65 sessionId: "session-2",
66 title: "Session 2",
67 dateCreated: "2023-01-01T09:00:00.000Z",
68 workspaceDirectory: "/workspace",
69 firstUserMessage: "Test message",
70 isRemote: true,
71 remoteId: "agent-123",
72 },
73 ];
74
75 mockListSessions.mockResolvedValue(mockSessions);
76
77 const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
78
79 await listSessionsCommand({ format: "json" });
80
81 expect(consoleSpy).toHaveBeenCalledWith(
82 JSON.stringify(
83 {
84 sessions: [
85 {
86 id: "session-1",
87 timestamp: "2023-01-01T10:00:00.000Z",
88 workspaceDirectory: "/workspace",
89 title: "Session 1",
90 firstUserMessage: "Hello world",
91 isRemote: false,
92 remoteId: undefined,
93 },
94 {
95 id: "session-2",
96 timestamp: "2023-01-01T09:00:00.000Z",
97 workspaceDirectory: "/workspace",
98 title: "Session 2",
99 firstUserMessage: "Test message",
100 isRemote: true,
101 remoteId: "agent-123",
102 },
103 ],
104 },
105 null,
106 2,
107 ),
108 );
109
110 consoleSpy.mockRestore();
111 });
112
113 it("should call listSessions without limit restrictions", async () => {
114 mockListSessions.mockResolvedValue([]);
115
116 // JSON mode - should call listSessions (implementation decides limit)
117 await listSessionsCommand({ format: "json" });
118 expect(mockListSessions).toHaveBeenCalled();
119
120 // TUI mode - should call listSessions (implementation decides limit)
121 await listSessionsCommand({});
122 expect(mockListSessions).toHaveBeenCalled();
123
124 // Verify it was called twice total
125 expect(mockListSessions).toHaveBeenCalledTimes(2);
126 });
127
128 it("should handle empty sessions gracefully in JSON mode", async () => {
129 mockListSessions.mockResolvedValue([]);
130
131 const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
132
133 await listSessionsCommand({ format: "json" });
134
135 expect(consoleSpy).toHaveBeenCalledWith(
136 JSON.stringify({ sessions: [] }, null, 2),
137 );
138
139 consoleSpy.mockRestore();
140 });
141
142 it("should handle empty sessions gracefully in TUI mode", async () => {
143 mockListSessions.mockResolvedValue([]);
144
145 const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
146
147 await listSessionsCommand({});
148
149 expect(consoleSpy).toHaveBeenCalledWith(
150 "No previous sessions found. Start a new conversation with: cn",
151 );
152
153 consoleSpy.mockRestore();
154 });
155});