Skip to content

Commit

Permalink
ISSUE 282 keep history messages in sync with storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Peelman committed Dec 9, 2024
1 parent 1512edf commit 9d61681
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
19 changes: 18 additions & 1 deletion __tests__/services/ChatHistoryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const saveChatHistory = async (messages: Message[]) => {
parsedMessages = [...historyMessages.slice(-difference), ...parsedMessages]
}

historyMessages = parsedMessages;
setHistoryMessages(parsedMessages);
}

Expand Down

0 comments on commit 9d61681

Please sign in to comment.