|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type { ChatHistoryData, DisplayMessage } from '../chatHistoryStorage'; |
| 3 | +import type { ChatMessage } from '../llmClient'; |
| 4 | + |
| 5 | +const fetchMock = vi.fn(); |
| 6 | +vi.stubGlobal('fetch', fetchMock); |
| 7 | + |
| 8 | +const STORAGE_KEY = 'webuiapps-chat-history'; |
| 9 | + |
| 10 | +const sampleMessages: DisplayMessage[] = [ |
| 11 | + { id: '1', role: 'user', content: 'Hello' }, |
| 12 | + { id: '2', role: 'assistant', content: 'Hi there!' }, |
| 13 | +]; |
| 14 | + |
| 15 | +const sampleChatHistory: ChatMessage[] = [ |
| 16 | + { role: 'user', content: 'Hello' }, |
| 17 | + { role: 'assistant', content: 'Hi there!' }, |
| 18 | +]; |
| 19 | + |
| 20 | +function makeSavedData(msgs = sampleMessages, history = sampleChatHistory): ChatHistoryData { |
| 21 | + return { version: 1, savedAt: Date.now(), messages: msgs, chatHistory: history }; |
| 22 | +} |
| 23 | + |
| 24 | +describe('chatHistoryStorage', () => { |
| 25 | + beforeEach(() => { |
| 26 | + fetchMock.mockReset(); |
| 27 | + localStorage.clear(); |
| 28 | + vi.resetModules(); |
| 29 | + }); |
| 30 | + |
| 31 | + // ============ loadChatHistorySync ============ |
| 32 | + |
| 33 | + describe('loadChatHistorySync', () => { |
| 34 | + it('returns null when localStorage is empty', async () => { |
| 35 | + const { loadChatHistorySync } = await import('../chatHistoryStorage'); |
| 36 | + expect(loadChatHistorySync()).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns data from localStorage', async () => { |
| 40 | + const data = makeSavedData(); |
| 41 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); |
| 42 | + const { loadChatHistorySync } = await import('../chatHistoryStorage'); |
| 43 | + const result = loadChatHistorySync(); |
| 44 | + expect(result).not.toBeNull(); |
| 45 | + expect(result!.messages).toHaveLength(2); |
| 46 | + expect(result!.chatHistory).toHaveLength(2); |
| 47 | + expect(result!.version).toBe(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns null for invalid JSON', async () => { |
| 51 | + localStorage.setItem(STORAGE_KEY, 'not-json'); |
| 52 | + const { loadChatHistorySync } = await import('../chatHistoryStorage'); |
| 53 | + expect(loadChatHistorySync()).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns null for wrong version', async () => { |
| 57 | + localStorage.setItem( |
| 58 | + STORAGE_KEY, |
| 59 | + JSON.stringify({ version: 99, savedAt: 0, messages: [], chatHistory: [] }), |
| 60 | + ); |
| 61 | + const { loadChatHistorySync } = await import('../chatHistoryStorage'); |
| 62 | + expect(loadChatHistorySync()).toBeNull(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + // ============ loadChatHistory (async) ============ |
| 67 | + |
| 68 | + describe('loadChatHistory', () => { |
| 69 | + it('loads from API and syncs to localStorage', async () => { |
| 70 | + const data = makeSavedData(); |
| 71 | + fetchMock.mockResolvedValueOnce({ |
| 72 | + ok: true, |
| 73 | + json: () => Promise.resolve(data), |
| 74 | + }); |
| 75 | + const { loadChatHistory } = await import('../chatHistoryStorage'); |
| 76 | + |
| 77 | + const result = await loadChatHistory(); |
| 78 | + |
| 79 | + expect(fetchMock).toHaveBeenCalledWith('/api/chat-history'); |
| 80 | + expect(result).not.toBeNull(); |
| 81 | + expect(result!.messages).toEqual(sampleMessages); |
| 82 | + // Verify synced to localStorage |
| 83 | + const stored = JSON.parse(localStorage.getItem(STORAGE_KEY)!); |
| 84 | + expect(stored.version).toBe(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it('falls back to localStorage when API returns non-ok', async () => { |
| 88 | + const data = makeSavedData(); |
| 89 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); |
| 90 | + fetchMock.mockResolvedValueOnce({ ok: false, status: 404 }); |
| 91 | + const { loadChatHistory } = await import('../chatHistoryStorage'); |
| 92 | + |
| 93 | + const result = await loadChatHistory(); |
| 94 | + |
| 95 | + expect(result).not.toBeNull(); |
| 96 | + expect(result!.messages).toEqual(sampleMessages); |
| 97 | + }); |
| 98 | + |
| 99 | + it('falls back to localStorage when fetch throws', async () => { |
| 100 | + const data = makeSavedData(); |
| 101 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); |
| 102 | + fetchMock.mockRejectedValueOnce(new Error('network error')); |
| 103 | + const { loadChatHistory } = await import('../chatHistoryStorage'); |
| 104 | + |
| 105 | + const result = await loadChatHistory(); |
| 106 | + |
| 107 | + expect(result).not.toBeNull(); |
| 108 | + expect(result!.messages).toEqual(sampleMessages); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns null when both API and localStorage are empty', async () => { |
| 112 | + fetchMock.mockResolvedValueOnce({ ok: false, status: 404 }); |
| 113 | + const { loadChatHistory } = await import('../chatHistoryStorage'); |
| 114 | + |
| 115 | + const result = await loadChatHistory(); |
| 116 | + expect(result).toBeNull(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + // ============ saveChatHistory ============ |
| 121 | + |
| 122 | + describe('saveChatHistory', () => { |
| 123 | + it('saves to localStorage and POSTs to API', async () => { |
| 124 | + fetchMock.mockResolvedValueOnce({ ok: true }); |
| 125 | + const { saveChatHistory } = await import('../chatHistoryStorage'); |
| 126 | + |
| 127 | + await saveChatHistory(sampleMessages, sampleChatHistory); |
| 128 | + |
| 129 | + // Check localStorage |
| 130 | + const stored = JSON.parse(localStorage.getItem(STORAGE_KEY)!); |
| 131 | + expect(stored.version).toBe(1); |
| 132 | + expect(stored.messages).toEqual(sampleMessages); |
| 133 | + expect(stored.chatHistory).toEqual(sampleChatHistory); |
| 134 | + expect(typeof stored.savedAt).toBe('number'); |
| 135 | + |
| 136 | + // Check fetch call |
| 137 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 138 | + const [url, options] = fetchMock.mock.calls[0]; |
| 139 | + expect(url).toBe('/api/chat-history'); |
| 140 | + expect(options.method).toBe('POST'); |
| 141 | + const body = JSON.parse(options.body); |
| 142 | + expect(body.version).toBe(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('saves to localStorage even when fetch fails', async () => { |
| 146 | + fetchMock.mockRejectedValueOnce(new Error('network error')); |
| 147 | + const { saveChatHistory } = await import('../chatHistoryStorage'); |
| 148 | + |
| 149 | + await saveChatHistory(sampleMessages, sampleChatHistory); |
| 150 | + |
| 151 | + const stored = JSON.parse(localStorage.getItem(STORAGE_KEY)!); |
| 152 | + expect(stored.messages).toEqual(sampleMessages); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + // ============ clearChatHistory ============ |
| 157 | + |
| 158 | + describe('clearChatHistory', () => { |
| 159 | + it('removes from localStorage and sends DELETE to API', async () => { |
| 160 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(makeSavedData())); |
| 161 | + fetchMock.mockResolvedValueOnce({ ok: true }); |
| 162 | + const { clearChatHistory } = await import('../chatHistoryStorage'); |
| 163 | + |
| 164 | + await clearChatHistory(); |
| 165 | + |
| 166 | + expect(localStorage.getItem(STORAGE_KEY)).toBeNull(); |
| 167 | + expect(fetchMock).toHaveBeenCalledWith('/api/chat-history', { method: 'DELETE' }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('clears localStorage even when DELETE fetch fails', async () => { |
| 171 | + localStorage.setItem(STORAGE_KEY, JSON.stringify(makeSavedData())); |
| 172 | + fetchMock.mockRejectedValueOnce(new Error('network error')); |
| 173 | + const { clearChatHistory } = await import('../chatHistoryStorage'); |
| 174 | + |
| 175 | + await clearChatHistory(); |
| 176 | + |
| 177 | + expect(localStorage.getItem(STORAGE_KEY)).toBeNull(); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments