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