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