Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 17.8 KiBRawPermalink
1import { analyzeError } from "./errorAnalysis";
2
3describe("errorAnalysis", () => {
4 describe("analyzeError", () => {
5 describe("basic error analysis", () => {
6 it("should return basic error info when no selectedModel is provided", () => {
7 const error = new Error("Test error message");
8 const result = analyzeError(error, null);
9
10 expect(result).toEqual({
11 parsedError: "Test error message",
12 statusCode: undefined,
13 message: "Test error message",
14 modelTitle: "Chat model",
15 providerName: "the model provider",
16 apiKeyUrl: undefined,
17 });
18 });
19
20 it("should handle undefined error", () => {
21 const result = analyzeError(undefined, null);
22
23 expect(result).toEqual({
24 parsedError: "",
25 statusCode: undefined,
26 message: undefined,
27 modelTitle: "Chat model",
28 providerName: "the model provider",
29 apiKeyUrl: undefined,
30 });
31 });
32
33 it("should handle null error", () => {
34 const result = analyzeError(null, null);
35
36 expect(result).toEqual({
37 parsedError: "",
38 statusCode: undefined,
39 message: undefined,
40 modelTitle: "Chat model",
41 providerName: "the model provider",
42 apiKeyUrl: undefined,
43 });
44 });
45
46 it("should handle error with no message property", () => {
47 const error = { someProperty: "value" };
48 const result = analyzeError(error, null);
49
50 expect(result).toEqual({
51 parsedError: "",
52 statusCode: undefined,
53 message: undefined,
54 modelTitle: "Chat model",
55 providerName: "the model provider",
56 apiKeyUrl: undefined,
57 });
58 });
59
60 it("should handle error with non-string message", () => {
61 const error = { message: 123 };
62 const result = analyzeError(error, null);
63
64 expect(result).toEqual({
65 parsedError: "",
66 statusCode: undefined,
67 message: undefined,
68 modelTitle: "Chat model",
69 providerName: "the model provider",
70 apiKeyUrl: undefined,
71 });
72 });
73 });
74
75 describe("error message parsing", () => {
76 it("should return original message when no double newline is present", () => {
77 const error = new Error("Simple error message");
78 const result = analyzeError(error, null);
79
80 expect(result.parsedError).toBe("Simple error message");
81 });
82
83 it("should parse JSON error message after double newline", () => {
84 const error = new Error(
85 'Header message\n\n{"error": "JSON error message"}',
86 );
87 const result = analyzeError(error, null);
88
89 expect(result.parsedError).toBe('"JSON error message"');
90 });
91
92 it("should parse JSON message property after double newline", () => {
93 const error = new Error(
94 'Header message\n\n{"message": "JSON message content"}',
95 );
96 const result = analyzeError(error, null);
97
98 expect(result.parsedError).toBe('"JSON message content"');
99 });
100
101 it("should return raw JSON when no error or message property", () => {
102 const error = new Error(
103 'Header message\n\n{"code": 404, "details": "Not found"}',
104 );
105 const result = analyzeError(error, null);
106
107 expect(result.parsedError).toBe(
108 '{"code": 404, "details": "Not found"}',
109 );
110 });
111
112 it("should handle invalid JSON after double newline", () => {
113 const error = new Error("Header message\n\n{invalid json}");
114 const result = analyzeError(error, null);
115
116 expect(result.parsedError).toBe("{invalid json}");
117 });
118
119 it("should handle multiple double newlines", () => {
120 const error = new Error("Header\n\nFirst section\n\nSecond section");
121 const result = analyzeError(error, null);
122
123 expect(result.parsedError).toBe("First section\n\nSecond section");
124 });
125
126 it("should handle empty string after double newline", () => {
127 const error = new Error("Header message\n\n");
128 const result = analyzeError(error, null);
129
130 expect(result.parsedError).toBe("");
131 });
132
133 it("should handle complex nested JSON", () => {
134 const complexJson = {
135 error: {
136 code: 400,
137 message: "Bad Request",
138 details: ["Invalid parameter", "Missing field"],
139 },
140 };
141 const error = new Error(`Header\n\n${JSON.stringify(complexJson)}`);
142 const result = analyzeError(error, null);
143
144 expect(result.parsedError).toBe(JSON.stringify(complexJson.error));
145 });
146 });
147
148 describe("status code extraction", () => {
149 it("should extract status code from HTTP error format", () => {
150 const error = new Error("HTTP 404 Not Found");
151 const result = analyzeError(error, null);
152
153 expect(result.statusCode).toBe(404);
154 expect(result.message).toBe("HTTP 404 Not Found");
155 });
156
157 it("should extract status code from direct number format", () => {
158 const error = new Error("401 Unauthorized");
159 const result = analyzeError(error, null);
160
161 expect(result.statusCode).toBe(401);
162 expect(result.message).toBe("401 Unauthorized");
163 });
164
165 it("should extract status code from beginning of message", () => {
166 const error = new Error("500 Internal Server Error occurred");
167 const result = analyzeError(error, null);
168
169 expect(result.statusCode).toBe(500);
170 expect(result.message).toBe("500 Internal Server Error occurred");
171 });
172
173 it("should not extract status code from middle of message", () => {
174 const error = new Error("Error occurred with status 404");
175 const result = analyzeError(error, null);
176
177 expect(result.statusCode).toBe(undefined);
178 expect(result.message).toBe("Error occurred with status 404");
179 });
180
181 it("should handle invalid status codes", () => {
182 const error = new Error("ABC Invalid");
183 const result = analyzeError(error, null);
184
185 expect(result.statusCode).toBe(undefined);
186 expect(result.message).toBe("ABC Invalid");
187 });
188
189 it("should handle single word messages", () => {
190 const error = new Error("404");
191 const result = analyzeError(error, null);
192
193 expect(result.statusCode).toBe(404);
194 expect(result.message).toBe("404");
195 });
196
197 it("should handle empty messages", () => {
198 const error = new Error("");
199 const result = analyzeError(error, null);
200
201 expect(result.statusCode).toBe(undefined);
202 expect(result.message).toBe("");
203 });
204
205 it("should handle messages with only spaces", () => {
206 const error = new Error(" ");
207 const result = analyzeError(error, null);
208
209 expect(result.statusCode).toBe(undefined);
210 expect(result.message).toBe(" ");
211 });
212
213 it("should handle floating point numbers", () => {
214 const error = new Error("HTTP 404.5 Error");
215 const result = analyzeError(error, null);
216
217 expect(result.statusCode).toBe(404.5);
218 expect(result.message).toBe("HTTP 404.5 Error");
219 });
220
221 it("should handle negative numbers", () => {
222 const error = new Error("HTTP -1 Error");
223 const result = analyzeError(error, null);
224
225 expect(result.statusCode).toBe(-1);
226 expect(result.message).toBe("HTTP -1 Error");
227 });
228 });
229
230 describe("selectedModel handling", () => {
231 it("should use selectedModel title and provider", () => {
232 const selectedModel = {
233 title: "GPT-4",
234 provider: "openai",
235 };
236 const error = new Error("Test error");
237 const result = analyzeError(error, selectedModel);
238
239 expect(result.modelTitle).toBe("GPT-4");
240 expect(result.providerName).toBe("OpenAI");
241 expect(result.apiKeyUrl).toBe(
242 "https://platform.openai.com/account/api-keys",
243 );
244 });
245
246 it("should use selectedModel info when no matching provider is found", () => {
247 const selectedModel = {
248 title: "Custom Model",
249 provider: "unknown-provider",
250 };
251 const error = new Error("Test error");
252 const result = analyzeError(error, selectedModel);
253
254 expect(result.modelTitle).toBe("Custom Model");
255 expect(result.providerName).toBe("unknown-provider");
256 expect(result.apiKeyUrl).toBe(undefined);
257 });
258
259 it("should handle selectedModel with missing title", () => {
260 const selectedModel = {
261 provider: "openai",
262 };
263 const error = new Error("Test error");
264 const result = analyzeError(error, selectedModel);
265
266 expect(result.modelTitle).toBe(undefined);
267 expect(result.providerName).toBe("OpenAI");
268 expect(result.apiKeyUrl).toBe(
269 "https://platform.openai.com/account/api-keys",
270 );
271 });
272
273 it("should handle selectedModel with missing provider", () => {
274 const selectedModel = {
275 title: "Custom Model",
276 };
277 const error = new Error("Test error");
278 const result = analyzeError(error, selectedModel);
279
280 expect(result.modelTitle).toBe("Custom Model");
281 expect(result.providerName).toBe(undefined);
282 expect(result.apiKeyUrl).toBe(undefined);
283 });
284
285 it("should handle empty selectedModel", () => {
286 const selectedModel = {};
287 const error = new Error("Test error");
288 const result = analyzeError(error, selectedModel);
289
290 expect(result.modelTitle).toBe(undefined);
291 expect(result.providerName).toBe(undefined);
292 expect(result.apiKeyUrl).toBe(undefined);
293 });
294 });
295
296 describe("provider matching", () => {
297 it("should match Anthropic provider", () => {
298 const selectedModel = {
299 title: "Claude 3.5 Sonnet",
300 provider: "anthropic",
301 };
302 const error = new Error("Test error");
303 const result = analyzeError(error, selectedModel);
304
305 expect(result.providerName).toBe("Anthropic");
306 expect(result.apiKeyUrl).toBe(
307 "https://console.anthropic.com/account/keys",
308 );
309 });
310
311 it("should match Groq provider", () => {
312 const selectedModel = {
313 title: "Llama 3.1 70B",
314 provider: "groq",
315 };
316 const error = new Error("Test error");
317 const result = analyzeError(error, selectedModel);
318
319 expect(result.providerName).toBe("Groq");
320 expect(result.apiKeyUrl).toBe("https://console.groq.com/keys");
321 });
322
323 it("should match provider with no apiKeyUrl", () => {
324 const selectedModel = {
325 title: "Llama 3.1",
326 provider: "ollama",
327 };
328 const error = new Error("Test error");
329 const result = analyzeError(error, selectedModel);
330
331 expect(result.providerName).toBe("Ollama");
332 expect(result.apiKeyUrl).toBe(undefined);
333 });
334
335 it("should be case sensitive for provider matching", () => {
336 const selectedModel = {
337 title: "GPT-4",
338 provider: "OpenAI", // Different case
339 };
340 const error = new Error("Test error");
341 const result = analyzeError(error, selectedModel);
342
343 expect(result.providerName).toBe("OpenAI"); // Should use the original casing
344 expect(result.apiKeyUrl).toBe(undefined); // No match found
345 });
346 });
347
348 describe("complex error scenarios", () => {
349 it("should handle error with JSON message and status code", () => {
350 const error = new Error(
351 'HTTP 429 Rate Limited\n\n{"error": "Too many requests", "retry_after": 60}',
352 );
353 const selectedModel = {
354 title: "GPT-4",
355 provider: "openai",
356 };
357 const result = analyzeError(error, selectedModel);
358
359 expect(result.parsedError).toBe('"Too many requests"');
360 expect(result.statusCode).toBe(429);
361 expect(result.message).toBe(
362 'HTTP 429 Rate Limited\n\n{"error": "Too many requests", "retry_after": 60}',
363 );
364 expect(result.modelTitle).toBe("GPT-4");
365 expect(result.providerName).toBe("OpenAI");
366 expect(result.apiKeyUrl).toBe(
367 "https://platform.openai.com/account/api-keys",
368 );
369 });
370
371 it("should handle error with complex nested JSON structure", () => {
372 const errorObj = {
373 error: {
374 message: "Invalid request",
375 type: "invalid_request_error",
376 param: "model",
377 code: "model_not_found",
378 },
379 };
380 const error = new Error(
381 `400 Bad Request\n\n${JSON.stringify(errorObj)}`,
382 );
383 const selectedModel = {
384 title: "GPT-4",
385 provider: "openai",
386 };
387 const result = analyzeError(error, selectedModel);
388
389 expect(result.parsedError).toBe(JSON.stringify(errorObj.error));
390 expect(result.statusCode).toBe(400);
391 });
392
393 it("should handle Error instance vs plain object", () => {
394 const errorInstance = new Error("Error instance message");
395 const plainObject = { message: "Plain object message" };
396
397 const result1 = analyzeError(errorInstance, null);
398 const result2 = analyzeError(plainObject, null);
399
400 expect(result1.message).toBe("Error instance message");
401 expect(result2.message).toBe("Plain object message");
402 });
403
404 it("should handle malformed JSON gracefully", () => {
405 const error = new Error('Header\n\n{"incomplete": json}');
406 const result = analyzeError(error, null);
407
408 expect(result.parsedError).toBe('{"incomplete": json}');
409 });
410
411 it("should handle empty JSON object", () => {
412 const error = new Error("Header\n\n{}");
413 const result = analyzeError(error, null);
414
415 expect(result.parsedError).toBe("{}");
416 });
417
418 it("should handle JSON with null values", () => {
419 const error = new Error(
420 'Header\n\n{"error": null, "message": "Something went wrong"}',
421 );
422 const result = analyzeError(error, null);
423
424 expect(result.parsedError).toBe('"Something went wrong"');
425 });
426
427 it("should handle JSON with undefined values", () => {
428 const errorObj = { error: undefined, message: "Undefined error" };
429 const error = new Error(`Header\n\n${JSON.stringify(errorObj)}`);
430 const result = analyzeError(error, null);
431
432 expect(result.parsedError).toBe('"Undefined error"');
433 });
434 });
435
436 describe("edge cases", () => {
437 it("should handle very long error messages", () => {
438 const longMessage = "A".repeat(10000);
439 const error = new Error(longMessage);
440 const result = analyzeError(error, null);
441
442 expect(result.parsedError).toBe(longMessage);
443 expect(result.message).toBe(longMessage);
444 });
445
446 it("should handle error messages with special characters", () => {
447 const specialMessage = "Error: 中文 🔥 \n\t Special chars";
448 const error = new Error(specialMessage);
449 const result = analyzeError(error, null);
450
451 expect(result.parsedError).toBe(specialMessage);
452 expect(result.message).toBe(specialMessage);
453 });
454
455 it("should handle error messages with only newlines", () => {
456 const error = new Error("\n\n\n");
457 const result = analyzeError(error, null);
458
459 expect(result.parsedError).toBe("\n");
460 });
461
462 it("should handle circular JSON references gracefully", () => {
463 const obj: any = { name: "test" };
464 obj.self = obj;
465
466 // This would normally cause JSON.stringify to throw
467 const error = new Error('Header\n\n{"error": "circular reference"}');
468 const result = analyzeError(error, null);
469
470 expect(result.parsedError).toBe('"circular reference"');
471 });
472
473 it("should handle error with numeric status codes as strings", () => {
474 const error = new Error("HTTP 404 Not Found");
475 const result = analyzeError(error, null);
476
477 expect(result.statusCode).toBe(404);
478 expect(typeof result.statusCode).toBe("number");
479 });
480 });
481
482 describe("real-world error scenarios", () => {
483 it("should handle OpenAI API key error", () => {
484 const error = new Error(
485 '401 Unauthorized\n\n{"error": {"message": "Invalid API key", "type": "invalid_request_error"}}',
486 );
487 const selectedModel = {
488 title: "GPT-4",
489 provider: "openai",
490 };
491 const result = analyzeError(error, selectedModel);
492
493 expect(result.statusCode).toBe(401);
494 expect(result.apiKeyUrl).toBe(
495 "https://platform.openai.com/account/api-keys",
496 );
497 });
498
499 it("should handle Anthropic rate limit error", () => {
500 const error = new Error(
501 '429 Too Many Requests\n\n{"error": {"type": "rate_limit_error", "message": "Rate limit exceeded"}}',
502 );
503 const selectedModel = {
504 title: "Claude 3.5 Sonnet",
505 provider: "anthropic",
506 };
507 const result = analyzeError(error, selectedModel);
508
509 expect(result.statusCode).toBe(429);
510 expect(result.providerName).toBe("Anthropic");
511 });
512
513 it("should handle network connectivity error", () => {
514 const error = new Error("Network request failed");
515 const selectedModel = {
516 title: "Local Model",
517 provider: "ollama",
518 };
519 const result = analyzeError(error, selectedModel);
520
521 expect(result.parsedError).toBe("Network request failed");
522 expect(result.statusCode).toBe(undefined);
523 expect(result.providerName).toBe("Ollama");
524 });
525
526 it("should handle timeout error", () => {
527 const error = new Error("Request timeout");
528 const result = analyzeError(error, null);
529
530 expect(result.parsedError).toBe("Request timeout");
531 expect(result.statusCode).toBe(undefined);
532 });
533 });
534 });
535});