File size: 9,824 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { extractJson, extractJsonWithKey, extractJsonWithArray } from '@/lib/json-extractor.js';

describe('json-extractor.ts', () => {
  const mockLogger = {
    debug: vi.fn(),
    warn: vi.fn(),
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('extractJson', () => {
    describe('Strategy 1: JSON in ```json code block', () => {
      it('should extract JSON from ```json code block', () => {
        const responseText = `Here is the result:
\`\`\`json
{"name": "test", "value": 42}
\`\`\`
That's all!`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ name: 'test', value: 42 });
        expect(mockLogger.debug).toHaveBeenCalledWith('Extracting JSON from ```json code block');
      });

      it('should handle multiline JSON in code block', () => {
        const responseText = `\`\`\`json
{
  "items": [
    {"id": 1},
    {"id": 2}
  ]
}
\`\`\``;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ items: [{ id: 1 }, { id: 2 }] });
      });
    });

    describe('Strategy 2: JSON in ``` code block (no language)', () => {
      it('should extract JSON from unmarked code block', () => {
        const responseText = `Result:
\`\`\`
{"status": "ok"}
\`\`\``;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ status: 'ok' });
        expect(mockLogger.debug).toHaveBeenCalledWith('Extracting JSON from ``` code block');
      });

      it('should handle array JSON in unmarked code block', () => {
        const responseText = `\`\`\`
[1, 2, 3]
\`\`\``;

        const result = extractJson<number[]>(responseText, { logger: mockLogger });

        expect(result).toEqual([1, 2, 3]);
      });

      it('should skip non-JSON code blocks and find JSON via brace matching', () => {
        // When code block contains non-JSON, later strategies will try to extract
        // The first { in the response is in the function code, so brace matching
        // will try that and fail. The JSON after the code block is found via strategy 5.
        const responseText = `\`\`\`
return true;
\`\`\`
Here is the JSON: {"actual": "json"}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ actual: 'json' });
      });
    });

    describe('Strategy 3: Find JSON with required key', () => {
      it('should find JSON containing required key', () => {
        const responseText = `Some text before {"features": ["a", "b"]} and after`;

        const result = extractJson(responseText, {
          logger: mockLogger,
          requiredKey: 'features',
        });

        expect(result).toEqual({ features: ['a', 'b'] });
        expect(mockLogger.debug).toHaveBeenCalledWith(
          'Extracting JSON with required key "features"'
        );
      });

      it('should skip JSON without required key', () => {
        const responseText = `{"wrong": "key"} {"features": ["correct"]}`;

        const result = extractJson(responseText, {
          logger: mockLogger,
          requiredKey: 'features',
        });

        expect(result).toEqual({ features: ['correct'] });
      });
    });

    describe('Strategy 4: Find any JSON by brace matching', () => {
      it('should extract JSON by matching braces', () => {
        const responseText = `Let me provide the response: {"result": "success", "data": {"nested": true}}. Done.`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ result: 'success', data: { nested: true } });
        expect(mockLogger.debug).toHaveBeenCalledWith('Extracting JSON by brace matching');
      });

      it('should handle deeply nested objects', () => {
        const responseText = `{"a": {"b": {"c": {"d": "deep"}}}}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ a: { b: { c: { d: 'deep' } } } });
      });
    });

    describe('Strategy 5: First { to last }', () => {
      it('should extract from first to last brace when other strategies fail', () => {
        // Create malformed JSON that brace matching fails but first-to-last works
        const responseText = `Prefix {"key": "value"} suffix text`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ key: 'value' });
      });
    });

    describe('Strategy 6: Parse entire response as JSON', () => {
      it('should parse entire response when it is valid JSON object', () => {
        const responseText = `{"complete": "json"}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ complete: 'json' });
      });

      it('should parse entire response when it is valid JSON array', () => {
        const responseText = `["a", "b", "c"]`;

        const result = extractJson<string[]>(responseText, { logger: mockLogger });

        expect(result).toEqual(['a', 'b', 'c']);
      });

      it('should handle whitespace around JSON', () => {
        const responseText = `
  {"trimmed": true}
  `;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ trimmed: true });
      });
    });

    describe('requireArray option', () => {
      it('should validate required key contains array', () => {
        const responseText = `{"items": ["a", "b", "c"]}`;

        const result = extractJson(responseText, {
          logger: mockLogger,
          requiredKey: 'items',
          requireArray: true,
        });

        expect(result).toEqual({ items: ['a', 'b', 'c'] });
      });

      it('should reject when required key is not an array', () => {
        const responseText = `{"items": "not an array"}`;

        const result = extractJson(responseText, {
          logger: mockLogger,
          requiredKey: 'items',
          requireArray: true,
        });

        expect(result).toBeNull();
      });
    });

    describe('error handling', () => {
      it('should return null for invalid JSON', () => {
        const responseText = `This is not JSON at all`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toBeNull();
        expect(mockLogger.debug).toHaveBeenCalledWith('Failed to extract JSON from response');
      });

      it('should return null for malformed JSON', () => {
        const responseText = `{"broken": }`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toBeNull();
      });

      it('should return null for empty input', () => {
        const result = extractJson('', { logger: mockLogger });

        expect(result).toBeNull();
      });

      it('should return null when required key is missing', () => {
        const responseText = `{"other": "key"}`;

        const result = extractJson(responseText, {
          logger: mockLogger,
          requiredKey: 'missing',
        });

        expect(result).toBeNull();
      });
    });

    describe('edge cases', () => {
      it('should handle JSON with escaped characters', () => {
        const responseText = `{"text": "Hello \\"World\\"", "path": "C:\\\\Users"}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ text: 'Hello "World"', path: 'C:\\Users' });
      });

      it('should handle JSON with unicode', () => {
        const responseText = `{"emoji": "🚀", "japanese": "日本語"}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ emoji: '🚀', japanese: '日本語' });
      });

      it('should work without custom logger', () => {
        const responseText = `{"simple": "test"}`;

        const result = extractJson(responseText);

        expect(result).toEqual({ simple: 'test' });
      });

      it('should handle multiple JSON objects in text - takes first valid one', () => {
        const responseText = `First: {"a": 1} Second: {"b": 2}`;

        const result = extractJson(responseText, { logger: mockLogger });

        expect(result).toEqual({ a: 1 });
      });
    });
  });

  describe('extractJsonWithKey', () => {
    it('should extract JSON with specified required key', () => {
      const responseText = `{"suggestions": [{"title": "Test"}]}`;

      const result = extractJsonWithKey(responseText, 'suggestions', { logger: mockLogger });

      expect(result).toEqual({ suggestions: [{ title: 'Test' }] });
    });

    it('should return null when key is missing', () => {
      const responseText = `{"other": "data"}`;

      const result = extractJsonWithKey(responseText, 'suggestions', { logger: mockLogger });

      expect(result).toBeNull();
    });
  });

  describe('extractJsonWithArray', () => {
    it('should extract JSON with array at specified key', () => {
      const responseText = `{"features": ["feature1", "feature2"]}`;

      const result = extractJsonWithArray(responseText, 'features', { logger: mockLogger });

      expect(result).toEqual({ features: ['feature1', 'feature2'] });
    });

    it('should return null when key value is not an array', () => {
      const responseText = `{"features": "not an array"}`;

      const result = extractJsonWithArray(responseText, 'features', { logger: mockLogger });

      expect(result).toBeNull();
    });

    it('should return null when key is missing', () => {
      const responseText = `{"other": ["array"]}`;

      const result = extractJsonWithArray(responseText, 'features', { logger: mockLogger });

      expect(result).toBeNull();
    });
  });
});