Elffuss BraidHub

kikocis/continue public

660ff1a44709 · 4.9 KiBRawPermalink
1import { convertToUnifiedHistory } from "core/util/messageConversion.js";
2import { ChatCompletionMessageParam } from "openai/resources.mjs";
3import { beforeEach, describe, expect, it, vi } from "vitest";
4
5import { startNewSession } from "../../session.js";
6
7import { processSlashCommandResult } from "./useChat.helpers.js";
8
9// Mock the session module
10vi.mock("../../session.js", () => ({
11 loadSession: vi.fn(),
12 updateSessionHistory: vi.fn(),
13 startNewSession: vi.fn(),
14}));
15
16describe("useChat clear command", () => {
17 let mockSetChatHistory: ReturnType<typeof vi.fn>;
18 let mockExit: ReturnType<typeof vi.fn>;
19 let mockOnClear: ReturnType<typeof vi.fn>;
20
21 beforeEach(() => {
22 // Clear all mocks to prevent call history leaking between tests
23 vi.clearAllMocks();
24
25 mockSetChatHistory = vi.fn();
26 mockExit = vi.fn();
27 mockOnClear = vi.fn();
28 });
29
30 it("should call onClear callback when processing clear command", () => {
31 const legacyHistory: ChatCompletionMessageParam[] = [
32 { role: "system", content: "You are a helpful assistant" },
33 { role: "user", content: "Hello" },
34 { role: "assistant", content: "Hi there!" },
35 ];
36 const chatHistory = convertToUnifiedHistory(legacyHistory);
37
38 const result = {
39 clear: true,
40 output: "Chat history cleared",
41 };
42
43 processSlashCommandResult({
44 result,
45 chatHistory,
46 setChatHistory: mockSetChatHistory,
47 exit: mockExit,
48 onShowConfigSelector: vi.fn(),
49 onClear: mockOnClear,
50 });
51
52 // Verify that onClear was called
53 expect(mockOnClear).toHaveBeenCalledOnce();
54
55 // Verify that a new session was started with the system message
56 expect(startNewSession).toHaveBeenCalledWith([
57 chatHistory[0], // The system message ChatHistoryItem
58 ]);
59
60 // Verify that chat history was reset (keeping only system message)
61 expect(mockSetChatHistory).toHaveBeenCalledWith([
62 chatHistory[0], // The system message ChatHistoryItem
63 ]);
64
65 // Second call should be with the "Chat history cleared" message
66 expect(mockSetChatHistory).toHaveBeenLastCalledWith([
67 {
68 message: {
69 role: "system",
70 content: "Chat history cleared",
71 },
72 contextItems: [],
73 },
74 ]);
75 });
76
77 it("should not call onClear if callback is not provided", () => {
78 const legacyHistory: ChatCompletionMessageParam[] = [
79 { role: "system", content: "You are a helpful assistant" },
80 { role: "user", content: "Hello" },
81 ];
82 const chatHistory = convertToUnifiedHistory(legacyHistory);
83
84 const result = {
85 clear: true,
86 };
87
88 // Should not throw when onClear is undefined
89 expect(() => {
90 processSlashCommandResult({
91 result,
92 chatHistory,
93 setChatHistory: mockSetChatHistory,
94 exit: mockExit,
95 onShowConfigSelector: vi.fn(),
96 // onClear is not provided
97 });
98 }).not.toThrow();
99
100 // Verify that a new session was started with the system message
101 expect(startNewSession).toHaveBeenCalledWith([
102 chatHistory[0], // The system message ChatHistoryItem
103 ]);
104
105 // Verify that history is still reset
106 expect(mockSetChatHistory).toHaveBeenCalledWith([
107 chatHistory[0], // The system message ChatHistoryItem
108 ]);
109 });
110
111 it("should preserve system message when clearing", () => {
112 const systemMessage: ChatCompletionMessageParam = {
113 role: "system",
114 content: "Custom system message",
115 };
116 const legacyHistory: ChatCompletionMessageParam[] = [
117 systemMessage,
118 { role: "user", content: "Test message" },
119 { role: "assistant", content: "Test response" },
120 ];
121 const chatHistory = convertToUnifiedHistory(legacyHistory);
122 const systemHistoryItem = chatHistory[0];
123
124 const result = { clear: true };
125
126 processSlashCommandResult({
127 result,
128 chatHistory,
129 setChatHistory: mockSetChatHistory,
130 exit: mockExit,
131 onShowConfigSelector: vi.fn(),
132 onClear: mockOnClear,
133 });
134
135 // Verify that a new session was started with the system message
136 expect(startNewSession).toHaveBeenCalledWith([systemHistoryItem]);
137
138 // Should keep only the system message
139 expect(mockSetChatHistory).toHaveBeenCalledWith([systemHistoryItem]);
140 expect(mockOnClear).toHaveBeenCalledOnce();
141 });
142
143 it("should handle clear with empty chat history", () => {
144 const legacyHistory: ChatCompletionMessageParam[] = [];
145 const chatHistory = convertToUnifiedHistory(legacyHistory);
146 const result = { clear: true };
147
148 processSlashCommandResult({
149 result,
150 chatHistory,
151 setChatHistory: mockSetChatHistory,
152 exit: mockExit,
153 onShowConfigSelector: vi.fn(),
154 onClear: mockOnClear,
155 });
156
157 // Verify that a new session was started with empty history
158 expect(startNewSession).toHaveBeenCalledWith([]);
159
160 // Should set empty history when no system message exists
161 expect(mockSetChatHistory).toHaveBeenCalledWith([]);
162 expect(mockOnClear).toHaveBeenCalledOnce();
163 });
164});