feat(ui): pretty JSON rendering tool outputs (#9767)

Co-authored-by: Bryan Morgan <bryanmorgan@google.com>
This commit is contained in:
Aaron Smith
2026-01-27 12:55:06 +00:00
committed by GitHub
parent 88d3df912f
commit 312a72acb8
5 changed files with 304 additions and 2 deletions
+100
View File
@@ -0,0 +1,100 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { checkInput, tryParseJSON } from './jsonoutput.js';
describe('check tools output', () => {
it('accepts object-like JSON strings', () => {
const testJSON = '{"a":1, "b": 2}';
expect(checkInput(testJSON)).toBeTruthy();
});
it('accepts array JSON strings', () => {
expect(checkInput('[1,2,3]')).toBeTruthy();
});
it('rejects primitive strings/plaintext strings', () => {
expect(checkInput('test text')).toBeFalsy();
});
it('rejects empty strings', () => {
expect(checkInput('')).toBeFalsy();
});
it('rejects null and undefined', () => {
expect(checkInput(null)).toBeFalsy();
expect(checkInput(undefined)).toBeFalsy();
});
it('rejects malformed JSON-like strings', () => {
const malformedJSON = '"a":1,}';
expect(checkInput(malformedJSON)).toBeFalsy();
});
it('rejects mixed text and JSON text strings', () => {
const testJSON = 'text {"a":1, "b": 2}';
expect(checkInput(testJSON)).toBeFalsy();
});
it('rejects ANSI-tainted input', () => {
const text = '\u001B[32m{"a":1}\u001B[0m';
expect(checkInput(text)).toBeFalsy();
});
});
describe('check parsing json', () => {
it('returns parsed object for valid JSON', () => {
const testJSON = '{"a":1, "b": 2}';
const parsedTestJSON = JSON.parse(testJSON);
const output = tryParseJSON(testJSON);
expect(output).toEqual(parsedTestJSON);
});
it('returns parsed array for non-empty arrays', () => {
const testJSON = '[1,2,3]';
const parsedTestJSON = JSON.parse(testJSON);
const output = tryParseJSON(testJSON);
expect(output).toEqual(parsedTestJSON);
});
it('returns null for Malformed JSON', () => {
const text = '{"a":1,}';
expect(tryParseJSON(text)).toBeFalsy();
});
it('returns null for empty arrays', () => {
const testArr = '[]';
expect(tryParseJSON(testArr)).toBeFalsy();
});
it('returns null for empty objects', () => {
const testObj = '{}';
expect(tryParseJSON(testObj)).toBeFalsy();
});
it('trims whitespace and parse valid json', () => {
const text = '\n { "a": 1 } \n';
expect(tryParseJSON(text)).toBeTruthy();
});
it('returns null for plaintext', () => {
const testText = 'test plaintext';
const output = tryParseJSON(testText);
expect(output).toBeFalsy();
});
});
+46
View File
@@ -0,0 +1,46 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import stripAnsi from 'strip-ansi';
export function checkInput(input: string | null | undefined): boolean {
if (input === null || input === undefined) {
return false;
}
const trimmed = input.trim();
if (!trimmed) {
return false;
}
if (!/^(?:\[|\{)/.test(trimmed)) {
return false;
}
if (stripAnsi(trimmed) !== trimmed) return false;
return true;
}
export function tryParseJSON(input: string): object | null {
if (!checkInput(input)) return null;
const trimmed = input.trim();
try {
const parsed = JSON.parse(trimmed);
if (parsed === null || typeof parsed !== 'object') {
return null;
}
if (Array.isArray(parsed) && parsed.length === 0) {
return null;
}
if (!Array.isArray(parsed) && Object.keys(parsed).length === 0) return null;
return parsed;
} catch (_err) {
return null;
}
}