File size: 10,524 Bytes
7a1ad33 | 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { expect, describe, it } from 'vitest';
import type { SessionMetrics } from '../telemetry/uiTelemetry.js';
import { JsonFormatter } from './json-formatter.js';
import type { JsonError } from './types.js';
describe('JsonFormatter', () => {
it('should format the response as JSON', () => {
const formatter = new JsonFormatter();
const response = 'This is a test response.';
const formatted = formatter.format(undefined, response);
const expected = {
response,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should format the response as JSON with a session ID', () => {
const formatter = new JsonFormatter();
const response = 'This is a test response.';
const sessionId = 'test-session-id';
const formatted = formatter.format(sessionId, response);
const expected = {
session_id: sessionId,
response,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should strip ANSI escape sequences from response text', () => {
const formatter = new JsonFormatter();
const responseWithAnsi =
'\x1B[31mRed text\x1B[0m and \x1B[32mGreen text\x1B[0m';
const formatted = formatter.format(undefined, responseWithAnsi);
const parsed = JSON.parse(formatted);
expect(parsed.response).toBe('Red text and Green text');
});
it('should strip control characters from response text', () => {
const formatter = new JsonFormatter();
const responseWithControlChars =
'Text with\x07 bell\x08 and\x0B vertical tab';
const formatted = formatter.format(undefined, responseWithControlChars);
const parsed = JSON.parse(formatted);
// Only ANSI codes are stripped, other control chars are preserved
expect(parsed.response).toBe('Text with\x07 bell\x08 and\x0B vertical tab');
});
it('should preserve newlines and tabs in response text', () => {
const formatter = new JsonFormatter();
const responseWithWhitespace = 'Line 1\nLine 2\r\nLine 3\twith tab';
const formatted = formatter.format(undefined, responseWithWhitespace);
const parsed = JSON.parse(formatted);
expect(parsed.response).toBe('Line 1\nLine 2\r\nLine 3\twith tab');
});
it('should format the response as JSON with stats', () => {
const formatter = new JsonFormatter();
const response = 'This is a test response.';
const stats: SessionMetrics = {
models: {
'gemini-2.5-pro': {
api: {
totalRequests: 2,
totalErrors: 0,
totalLatencyMs: 5672,
},
tokens: {
input: 13745,
prompt: 24401,
candidates: 215,
total: 24719,
cached: 10656,
thoughts: 103,
tool: 0,
},
roles: {},
},
'gemini-2.5-flash': {
api: {
totalRequests: 2,
totalErrors: 0,
totalLatencyMs: 5914,
},
tokens: {
input: 20803,
prompt: 20803,
candidates: 716,
total: 21657,
cached: 0,
thoughts: 138,
tool: 0,
},
roles: {},
},
},
tools: {
totalCalls: 1,
totalSuccess: 1,
totalFail: 0,
totalDurationMs: 4582,
totalDecisions: {
accept: 0,
reject: 0,
modify: 0,
auto_accept: 1,
},
byName: {
google_web_search: {
count: 1,
success: 1,
fail: 0,
durationMs: 4582,
decisions: {
accept: 0,
reject: 0,
modify: 0,
auto_accept: 1,
},
},
},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const formatted = formatter.format(undefined, response, stats);
const expected = {
response,
stats,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should format error as JSON', () => {
const formatter = new JsonFormatter();
const error: JsonError = {
type: 'ValidationError',
message: 'Invalid input provided',
code: 400,
};
const formatted = formatter.format(undefined, undefined, undefined, error);
const expected = {
error,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should format response with error as JSON', () => {
const formatter = new JsonFormatter();
const response = 'Partial response';
const error: JsonError = {
type: 'TimeoutError',
message: 'Request timed out',
code: 'TIMEOUT',
};
const formatted = formatter.format(undefined, response, undefined, error);
const expected = {
response,
error,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should format error using formatError method', () => {
const formatter = new JsonFormatter();
const error = new Error('Something went wrong');
const formatted = formatter.formatError(error, 500);
const parsed = JSON.parse(formatted);
expect(parsed).toEqual({
error: {
type: 'Error',
message: 'Something went wrong',
code: 500,
},
});
});
it('should format error using formatError method with a session ID', () => {
const formatter = new JsonFormatter();
const error = new Error('Something went wrong');
const sessionId = 'test-session-id';
const formatted = formatter.formatError(error, 500, sessionId);
const parsed = JSON.parse(formatted);
expect(parsed).toEqual({
session_id: sessionId,
error: {
type: 'Error',
message: 'Something went wrong',
code: 500,
},
});
});
it('should format custom error using formatError method', () => {
class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = 'CustomError';
}
}
const formatter = new JsonFormatter();
const error = new CustomError('Custom error occurred');
const formatted = formatter.formatError(error, undefined);
const parsed = JSON.parse(formatted);
expect(parsed).toEqual({
error: {
type: 'CustomError',
message: 'Custom error occurred',
},
});
});
it('should format complete JSON output with response, stats, and error', () => {
const formatter = new JsonFormatter();
const response = 'Partial response before error';
const stats: SessionMetrics = {
models: {},
tools: {
totalCalls: 0,
totalSuccess: 0,
totalFail: 1,
totalDurationMs: 0,
totalDecisions: {
accept: 0,
reject: 0,
modify: 0,
auto_accept: 0,
},
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const error: JsonError = {
type: 'ApiError',
message: 'Rate limit exceeded',
code: 429,
};
const formatted = formatter.format(undefined, response, stats, error);
const expected = {
response,
stats,
error,
};
expect(JSON.parse(formatted)).toEqual(expected);
});
it('should handle error messages containing JSON content', () => {
const formatter = new JsonFormatter();
const errorWithJson = new Error(
'API returned: {"error": "Invalid request", "code": 400}',
);
const formatted = formatter.formatError(errorWithJson, 'API_ERROR');
const parsed = JSON.parse(formatted);
expect(parsed).toEqual({
error: {
type: 'Error',
message: 'API returned: {"error": "Invalid request", "code": 400}',
code: 'API_ERROR',
},
});
// Verify the entire output is valid JSON
expect(() => JSON.parse(formatted)).not.toThrow();
});
it('should handle error messages with quotes and special characters', () => {
const formatter = new JsonFormatter();
const errorWithQuotes = new Error('Error: "quoted text" and \\backslash');
const formatted = formatter.formatError(errorWithQuotes);
const parsed = JSON.parse(formatted);
expect(parsed).toEqual({
error: {
type: 'Error',
message: 'Error: "quoted text" and \\backslash',
},
});
// Verify the entire output is valid JSON
expect(() => JSON.parse(formatted)).not.toThrow();
});
it('should handle error messages with control characters', () => {
const formatter = new JsonFormatter();
const errorWithControlChars = new Error('Error with\n newline and\t tab');
const formatted = formatter.formatError(errorWithControlChars);
const parsed = JSON.parse(formatted);
// Should preserve newlines and tabs as they are common whitespace characters
expect(parsed.error.message).toBe('Error with\n newline and\t tab');
// Verify the entire output is valid JSON
expect(() => JSON.parse(formatted)).not.toThrow();
});
it('should strip ANSI escape sequences from error messages', () => {
const formatter = new JsonFormatter();
const errorWithAnsi = new Error('\x1B[31mRed error\x1B[0m message');
const formatted = formatter.formatError(errorWithAnsi);
const parsed = JSON.parse(formatted);
expect(parsed.error.message).toBe('Red error message');
expect(() => JSON.parse(formatted)).not.toThrow();
});
it('should strip unsafe control characters from error messages', () => {
const formatter = new JsonFormatter();
const errorWithControlChars = new Error(
'Error\x07 with\x08 control\x0B chars',
);
const formatted = formatter.formatError(errorWithControlChars);
const parsed = JSON.parse(formatted);
// Only ANSI codes are stripped, other control chars are preserved
expect(parsed.error.message).toBe('Error\x07 with\x08 control\x0B chars');
expect(() => JSON.parse(formatted)).not.toThrow();
});
it('should format warnings as JSON', () => {
const formatter = new JsonFormatter();
const warnings = ['Warning 1', '\x1B[33mWarning 2 with ANSI\x1B[0m'];
const formatted = formatter.format(
undefined,
undefined,
undefined,
undefined,
warnings,
);
const parsed = JSON.parse(formatted);
expect(parsed.warnings).toEqual(['Warning 1', 'Warning 2 with ANSI']);
});
});
|