From 9d61681921b1e6d7a502c43d5dbde30fa53bc5c5 Mon Sep 17 00:00:00 2001 From: Alex Peelman Date: Mon, 9 Dec 2024 11:57:37 +0100 Subject: [PATCH] ISSUE 282 keep history messages in sync with storage --- __tests__/services/ChatHistoryService.test.ts | 19 ++++++++++++++++++- src/services/ChatHistoryService.tsx | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/__tests__/services/ChatHistoryService.test.ts b/__tests__/services/ChatHistoryService.test.ts index 9b6f494..c4ced6e 100644 --- a/__tests__/services/ChatHistoryService.test.ts +++ b/__tests__/services/ChatHistoryService.test.ts @@ -61,13 +61,30 @@ describe("ChatHistoryService", () => { setHistoryStorageValues(settings); await saveChatHistory([mockMessage]); expect(storage.setItem).not.toHaveBeenCalled(); + + const historyMessages = getHistoryMessages(); + expect(historyMessages).toEqual([]); }); it("should save history if not disabled", async () => { setHistoryStorageValues(mockSettings(storageType)); await saveChatHistory([mockMessage]); - + expect(storage.setItem).toHaveBeenCalled(); + + const historyMessages = getHistoryMessages(); + expect(historyMessages).toEqual([mockMessage]); + }); + + it("should retain previous history if not disabled and empty message array is passed", async () => { + setHistoryStorageValues(mockSettings(storageType)); + await saveChatHistory([mockMessage]); + await saveChatHistory([]); + + expect(storage.setItem).toHaveBeenCalledTimes(2); + + const historyMessages = getHistoryMessages(); + expect(historyMessages).toEqual([mockMessage]); }); it("should handle empty message array without errors", async () => { diff --git a/src/services/ChatHistoryService.tsx b/src/services/ChatHistoryService.tsx index 4edb816..1aa65fc 100644 --- a/src/services/ChatHistoryService.tsx +++ b/src/services/ChatHistoryService.tsx @@ -51,6 +51,7 @@ const saveChatHistory = async (messages: Message[]) => { parsedMessages = [...historyMessages.slice(-difference), ...parsedMessages] } + historyMessages = parsedMessages; setHistoryMessages(parsedMessages); }