| 1 | import type { ChatHistoryItem } from "core"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | |
| 4 | import { splitChatHistory } from "./historySplitting.js"; |
| 5 | |
| 6 | // Helper to create a mock chat history item |
| 7 | function createMockChatHistoryItem( |
| 8 | content: string, |
| 9 | role: "user" | "assistant" = "assistant", |
| 10 | ): ChatHistoryItem { |
| 11 | return { |
| 12 | message: { |
| 13 | role, |
| 14 | content, |
| 15 | }, |
| 16 | } as ChatHistoryItem; |
| 17 | } |
| 18 | |
| 19 | describe("messageHistorySplitting", () => { |
| 20 | describe("splitChatHistory", () => { |
| 21 | it("should split multiple large messages in chat history", () => { |
| 22 | const chatHistory = [ |
| 23 | createMockChatHistoryItem("Short message 1"), |
| 24 | createMockChatHistoryItem( |
| 25 | Array(30).fill("Long message 1").join("\n\n"), |
| 26 | ), |
| 27 | createMockChatHistoryItem("Short message 2"), |
| 28 | createMockChatHistoryItem( |
| 29 | Array(25).fill("Long message 2").join("\n\n"), |
| 30 | ), |
| 31 | ]; |
| 32 | |
| 33 | const result = splitChatHistory(chatHistory, 80); |
| 34 | |
| 35 | // Should have more items than the original (due to splits) |
| 36 | expect(result.length).toBeGreaterThan(chatHistory.length); |
| 37 | |
| 38 | // First and third messages should remain unchanged |
| 39 | expect(result[0].message.content).toBe("Short message 1"); |
| 40 | }); |
| 41 | |
| 42 | it("should maintain order after splitting", () => { |
| 43 | const chatHistory = [ |
| 44 | createMockChatHistoryItem("First"), |
| 45 | createMockChatHistoryItem(Array(30).fill("Long middle").join("\n\n")), |
| 46 | createMockChatHistoryItem("Last"), |
| 47 | ]; |
| 48 | |
| 49 | const result = splitChatHistory(chatHistory, 80); |
| 50 | |
| 51 | // First message should still be first |
| 52 | expect(result[0].message.content).toBe("First"); |
| 53 | |
| 54 | // Last message should still be last |
| 55 | expect(result[result.length - 1].message.content).toBe("Last"); |
| 56 | }); |
| 57 | }); |
| 58 | }); |