|
| 1 | +import { vi, describe, it, expect } from 'vitest'; |
| 2 | +import { extractJsonObjectsFromChunk, readChunk } from './getChatResponse'; |
| 3 | +import { ErrorType } from '../components/Chat/types'; |
| 4 | + |
| 5 | +describe('readChunk', () => { |
| 6 | + const decoder = new TextDecoder(); |
| 7 | + const sessionID = 'test-session'; |
| 8 | + |
| 9 | + it('handles an entire JSON object in a single chunk', async () => { |
| 10 | + const handleChatResponse = vi.fn(); |
| 11 | + const handleError = vi.fn(); |
| 12 | + |
| 13 | + const mockReader = { |
| 14 | + read: vi |
| 15 | + .fn() |
| 16 | + .mockResolvedValueOnce({ |
| 17 | + done: false, |
| 18 | + value: new TextEncoder().encode('{"a": {"b": "c"}}\n'), |
| 19 | + }) |
| 20 | + .mockResolvedValueOnce({ done: true }), |
| 21 | + }; |
| 22 | + |
| 23 | + await readChunk( |
| 24 | + mockReader, |
| 25 | + decoder, |
| 26 | + handleChatResponse, |
| 27 | + handleError, |
| 28 | + sessionID, |
| 29 | + ); |
| 30 | + |
| 31 | + expect(handleChatResponse).toHaveBeenCalledTimes(1); |
| 32 | + expect(handleChatResponse).toHaveBeenCalledWith({ a: { b: 'c' } }); |
| 33 | + expect(handleError).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('handles two entire JSON objects in a single chunk', async () => { |
| 37 | + const handleChatResponse = vi.fn(); |
| 38 | + const handleError = vi.fn(); |
| 39 | + |
| 40 | + const mockReader = { |
| 41 | + read: vi |
| 42 | + .fn() |
| 43 | + .mockResolvedValueOnce({ |
| 44 | + done: false, |
| 45 | + value: new TextEncoder().encode( |
| 46 | + '{"a": {"b": "c"}}\n{"d": {"e": "f"}}\n', |
| 47 | + ), |
| 48 | + }) |
| 49 | + .mockResolvedValueOnce({ done: true }), |
| 50 | + }; |
| 51 | + |
| 52 | + await readChunk( |
| 53 | + mockReader, |
| 54 | + decoder, |
| 55 | + handleChatResponse, |
| 56 | + handleError, |
| 57 | + sessionID, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(handleChatResponse).toHaveBeenCalledTimes(2); |
| 61 | + expect(handleChatResponse).toHaveBeenCalledWith({ a: { b: 'c' } }); |
| 62 | + expect(handleChatResponse).toHaveBeenCalledWith({ d: { e: 'f' } }); |
| 63 | + expect(handleError).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('handles a partial JSON object split across two chunks', async () => { |
| 67 | + const handleChatResponse = vi.fn(); |
| 68 | + const handleError = vi.fn(); |
| 69 | + |
| 70 | + const mockReader = { |
| 71 | + read: vi |
| 72 | + .fn() |
| 73 | + .mockResolvedValueOnce({ |
| 74 | + done: false, |
| 75 | + value: new TextEncoder().encode('{"a": {"b": "c"'), |
| 76 | + }) |
| 77 | + .mockResolvedValueOnce({ |
| 78 | + done: false, |
| 79 | + value: new TextEncoder().encode('}}\n'), |
| 80 | + }) |
| 81 | + .mockResolvedValueOnce({ done: true }), |
| 82 | + }; |
| 83 | + |
| 84 | + await readChunk( |
| 85 | + mockReader, |
| 86 | + decoder, |
| 87 | + handleChatResponse, |
| 88 | + handleError, |
| 89 | + sessionID, |
| 90 | + ); |
| 91 | + |
| 92 | + expect(handleChatResponse).toHaveBeenCalledTimes(1); |
| 93 | + expect(handleChatResponse).toHaveBeenCalledWith({ a: { b: 'c' } }); |
| 94 | + expect(handleError).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('handles one entire and one partial JSON object in one chunk with completion in the next', async () => { |
| 98 | + const handleChatResponse = vi.fn(); |
| 99 | + const handleError = vi.fn(); |
| 100 | + |
| 101 | + const mockReader = { |
| 102 | + read: vi |
| 103 | + .fn() |
| 104 | + .mockResolvedValueOnce({ |
| 105 | + done: false, |
| 106 | + value: new TextEncoder().encode('{"a": {"b": "c"}}\n{"d": {"e": "f"'), |
| 107 | + }) |
| 108 | + .mockResolvedValueOnce({ |
| 109 | + done: false, |
| 110 | + value: new TextEncoder().encode('}}\n'), |
| 111 | + }) |
| 112 | + .mockResolvedValueOnce({ done: true }), |
| 113 | + }; |
| 114 | + |
| 115 | + await readChunk( |
| 116 | + mockReader, |
| 117 | + decoder, |
| 118 | + handleChatResponse, |
| 119 | + handleError, |
| 120 | + sessionID, |
| 121 | + ); |
| 122 | + |
| 123 | + expect(handleChatResponse).toHaveBeenCalledTimes(2); |
| 124 | + expect(handleChatResponse).toHaveBeenCalledWith({ a: { b: 'c' } }); |
| 125 | + expect(handleChatResponse).toHaveBeenCalledWith({ d: { e: 'f' } }); |
| 126 | + expect(handleError).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('handles stream ending with incomplete JSON data', async () => { |
| 130 | + const handleChatResponse = vi.fn(); |
| 131 | + const handleError = vi.fn(); |
| 132 | + |
| 133 | + const mockReader = { |
| 134 | + read: vi |
| 135 | + .fn() |
| 136 | + .mockResolvedValueOnce({ |
| 137 | + done: false, |
| 138 | + value: new TextEncoder().encode('{"a": {"b": "c"'), |
| 139 | + }) |
| 140 | + .mockResolvedValueOnce({ done: true }), |
| 141 | + }; |
| 142 | + |
| 143 | + await readChunk( |
| 144 | + mockReader, |
| 145 | + decoder, |
| 146 | + handleChatResponse, |
| 147 | + handleError, |
| 148 | + sessionID, |
| 149 | + ); |
| 150 | + |
| 151 | + expect(handleChatResponse).not.toHaveBeenCalled(); |
| 152 | + expect(handleError).toHaveBeenCalledWith({ |
| 153 | + message: 'Stream ended with incomplete JSON data', |
| 154 | + type: ErrorType.FATAL, |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('handles JSON chunk with error', async () => { |
| 159 | + const handleChatResponse = vi.fn(); |
| 160 | + const handleError = vi.fn(); |
| 161 | + |
| 162 | + const mockReader = { |
| 163 | + read: vi |
| 164 | + .fn() |
| 165 | + .mockResolvedValueOnce({ |
| 166 | + done: false, |
| 167 | + value: new TextEncoder().encode( |
| 168 | + '{"streamingError": {"message": "test message" }}\n', |
| 169 | + ), |
| 170 | + }) |
| 171 | + .mockResolvedValueOnce({ done: true }), |
| 172 | + }; |
| 173 | + |
| 174 | + await readChunk( |
| 175 | + mockReader, |
| 176 | + decoder, |
| 177 | + handleChatResponse, |
| 178 | + handleError, |
| 179 | + sessionID, |
| 180 | + ); |
| 181 | + |
| 182 | + expect(handleChatResponse).not.toHaveBeenCalled(); |
| 183 | + expect(handleError).toHaveBeenCalledWith({ |
| 184 | + message: 'test message', |
| 185 | + type: ErrorType.FATAL, |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe('extractJsonObjectsFromChunk', () => { |
| 191 | + it('extracts entire JSON object', () => { |
| 192 | + const input = '{"a": {"b": "c"}}\n'; |
| 193 | + const result = extractJsonObjectsFromChunk(input); |
| 194 | + expect(result.completeObjects).toEqual(['{"a": {"b": "c"}}']); |
| 195 | + expect(result.remainingBuffer).toBe(''); |
| 196 | + }); |
| 197 | + |
| 198 | + it('extracts two entire JSON objects', () => { |
| 199 | + const input = '{"a": {"b": "c"}}\n{"d": {"e": "f"}}\n'; |
| 200 | + const result = extractJsonObjectsFromChunk(input); |
| 201 | + expect(result.completeObjects).toEqual([ |
| 202 | + '{"a": {"b": "c"}}', |
| 203 | + '{"d": {"e": "f"}}', |
| 204 | + ]); |
| 205 | + expect(result.remainingBuffer).toBe(''); |
| 206 | + }); |
| 207 | + |
| 208 | + it('handles partial JSON object', () => { |
| 209 | + const input = '{"a": {"b": "'; |
| 210 | + const result = extractJsonObjectsFromChunk(input); |
| 211 | + expect(result.completeObjects).toEqual([]); |
| 212 | + expect(result.remainingBuffer).toBe('{"a": {"b": "'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('extracts one entire and one partial JSON object', () => { |
| 216 | + const input = '{"a": {"b": "c"}}\n{"d": {"e": "'; |
| 217 | + const result = extractJsonObjectsFromChunk(input); |
| 218 | + expect(result.completeObjects).toEqual(['{"a": {"b": "c"}}']); |
| 219 | + expect(result.remainingBuffer).toBe('{"d": {"e": "'); |
| 220 | + }); |
| 221 | + |
| 222 | + it('extracts multiple JSON objects with a partial object', () => { |
| 223 | + const input = '{"a": {"b": "c"}}\n{"d": {"e": "f"}}\n{"g": {"h": "'; |
| 224 | + const result = extractJsonObjectsFromChunk(input); |
| 225 | + expect(result.completeObjects).toEqual([ |
| 226 | + '{"a": {"b": "c"}}', |
| 227 | + '{"d": {"e": "f"}}', |
| 228 | + ]); |
| 229 | + expect(result.remainingBuffer).toBe('{"g": {"h": "'); |
| 230 | + }); |
| 231 | + |
| 232 | + it('handles JSON object with internal newlines', () => { |
| 233 | + const input = '{"a": {\n "b": "c"\n}}\n'; |
| 234 | + const result = extractJsonObjectsFromChunk(input); |
| 235 | + expect(result.completeObjects).toEqual(['{"a": {\n "b": "c"\n}}']); |
| 236 | + expect(result.remainingBuffer).toBe(''); |
| 237 | + }); |
| 238 | +}); |
0 commit comments