Elffuss BraidHub

kikocis/continue public

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