File size: 18,729 Bytes
9332ccf | 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { TableRenderer } from './TableRenderer.js';
import { renderWithProviders } from '../../test-utils/render.js';
describe('TableRenderer', () => {
it('renders a 3x3 table correctly', async () => {
const headers = ['Header 1', 'Header 2', 'Header 3'];
const rows = [
['Row 1, Col 1', 'Row 1, Col 2', 'Row 1, Col 3'],
['Row 2, Col 1', 'Row 2, Col 2', 'Row 2, Col 3'],
['Row 3, Col 1', 'Row 3, Col 2', 'Row 3, Col 3'],
];
const terminalWidth = 80;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('Header 1');
expect(output).toContain('Row 1, Col 1');
expect(output).toContain('Row 3, Col 3');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('renders a table with long headers and 4 columns correctly', async () => {
const headers = [
'Very Long Column Header One',
'Very Long Column Header Two',
'Very Long Column Header Three',
'Very Long Column Header Four',
];
const rows = [
['Data 1.1', 'Data 1.2', 'Data 1.3', 'Data 1.4'],
['Data 2.1', 'Data 2.2', 'Data 2.3', 'Data 2.4'],
['Data 3.1', 'Data 3.2', 'Data 3.3', 'Data 3.4'],
];
const terminalWidth = 80;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
// Since terminalWidth is 80 and headers are long, they might be truncated.
// We just check for some of the content.
expect(output).toContain('Data 1.1');
expect(output).toContain('Data 3.4');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('wraps long cell content correctly', async () => {
const headers = ['Col 1', 'Col 2', 'Col 3'];
const rows = [
[
'Short',
'This is a very long cell content that should wrap to multiple lines',
'Short',
],
];
const terminalWidth = 50;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('This is a very');
expect(output).toContain('long cell');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('wraps all long columns correctly', async () => {
const headers = ['Col 1', 'Col 2', 'Col 3'];
const rows = [
[
'This is a very long text that needs wrapping in column 1',
'This is also a very long text that needs wrapping in column 2',
'And this is the third long text that needs wrapping in column 3',
],
];
const terminalWidth = 60;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('wrapping in');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('wraps mixed long and short columns correctly', async () => {
const headers = ['Short', 'Long', 'Medium'];
const rows = [
[
'Tiny',
'This is a very long text that definitely needs to wrap to the next line',
'Not so long',
],
];
const terminalWidth = 50;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('Tiny');
expect(output).toContain('definitely needs');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
// The snapshot looks weird but checked on VS Code terminal and it looks fine
it('wraps columns with punctuation correctly', async () => {
const headers = ['Punctuation 1', 'Punctuation 2', 'Punctuation 3'];
const rows = [
[
'Start. Stop. Comma, separated. Exclamation! Question? hyphen-ated',
'Semi; colon: Pipe| Slash/ Backslash\\',
'At@ Hash# Dollar$ Percent% Caret^ Ampersand& Asterisk*',
],
];
const terminalWidth = 60;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('Start. Stop.');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('strips bold markers from headers and renders them correctly', async () => {
const headers = ['**Bold Header**', 'Normal Header', '**Another Bold**'];
const rows = [['Data 1', 'Data 2', 'Data 3']];
const terminalWidth = 50;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
// The output should NOT contain the literal '**'
expect(output).not.toContain('**Bold Header**');
expect(output).toContain('Bold Header');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('handles wrapped bold headers without showing markers', async () => {
const headers = [
'**Very Long Bold Header That Will Wrap**',
'Short',
'**Another Long Header**',
];
const rows = [['Data 1', 'Data 2', 'Data 3']];
const terminalWidth = 40;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
// Markers should be gone
expect(output).not.toContain('**');
expect(output).toContain('Very Long');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('renders a complex table with mixed content lengths correctly', async () => {
const headers = [
'Comprehensive Architectural Specification for the Distributed Infrastructure Layer',
'Implementation Details for the High-Throughput Asynchronous Message Processing Pipeline with Extended Scalability Features and Redundancy Protocols',
'Longitudinal Performance Analysis Across Multi-Regional Cloud Deployment Clusters',
'Strategic Security Framework for Mitigating Sophisticated Cross-Site Scripting Vulnerabilities',
'Key',
'Status',
'Version',
'Owner',
];
const rows = [
[
'The primary architecture utilizes a decoupled microservices approach, leveraging container orchestration for scalability and fault tolerance in high-load scenarios.\n\nThis layer provides the fundamental building blocks for service discovery, load balancing, and inter-service communication via highly efficient protocol buffers.\n\nAdvanced telemetry and logging integrations allow for real-time monitoring of system health and rapid identification of bottlenecks within the service mesh.',
'Each message is processed through a series of specialized workers that handle data transformation, validation, and persistent storage using a persistent queue.\n\nThe pipeline features built-in retry mechanisms with exponential backoff to ensure message delivery integrity even during transient network or service failures.\n\nHorizontal autoscaling is triggered automatically based on the depth of the processing queue, ensuring consistent performance during unexpected traffic spikes.',
'Historical data indicates a significant reduction in tail latency when utilizing edge computing nodes closer to the geographic location of the end-user base.\n\nMonitoring tools have captured a steady increase in throughput efficiency since the introduction of the vectorized query engine in the primary data warehouse.\n\nResource utilization metrics demonstrate that the transition to serverless compute for intermittent tasks has resulted in a thirty percent cost optimization.',
'A multi-layered defense strategy incorporates content security policies, input sanitization libraries, and regular automated penetration testing routines.\n\nDevelopers are required to undergo mandatory security training focusing on the OWASP Top Ten to ensure that security is integrated into the initial design phase.\n\nThe implementation of a robust Identity and Access Management system ensures that the principle of least privilege is strictly enforced across all environments.',
'INF',
'Active',
'v2.4',
'J. Doe',
],
];
const terminalWidth = 160;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
{ width: terminalWidth },
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toContain('Comprehensive Architectural');
expect(output).toContain('protocol buffers');
expect(output).toContain('exponential backoff');
expect(output).toContain('vectorized query engine');
expect(output).toContain('OWASP Top Ten');
expect(output).toContain('INF');
expect(output).toContain('Active');
expect(output).toContain('v2.4');
// "J. Doe" might wrap due to column width constraints
expect(output).toContain('J.');
expect(output).toContain('Doe');
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it('handles extremely small terminal widths without crashing', async () => {
const headers = ['Col 1', 'Col 2'];
const rows = [['Data 1', 'Data 2']];
// This width is much smaller than the overhead, which could lead to negative column widths
const terminalWidth = 1;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { unmount } = renderResult;
// If it didn't throw RangeError: Invalid count value, the test passes
unmount();
});
it.each([
{
name: 'handles non-ASCII characters (emojis and Asian scripts) correctly',
headers: ['Emoji π', 'Asian ζ±ε', 'Mixed π Text'],
rows: [
['Start π End', 'δ½ ε₯½δΈη', 'Rocket π Man'],
['Thumbs π Up', 'γγγ«γ‘γ―', 'Fire π₯'],
],
terminalWidth: 60,
expected: ['Emoji π', 'Asian ζ±ε', 'δ½ ε₯½δΈη'],
},
{
name: 'renders a table with only emojis and text correctly',
headers: ['Happy π', 'Rocket π', 'Heart β€οΈ'],
rows: [
['Smile π', 'Fire π₯', 'Love π'],
['Cool π', 'Star β', 'Blue π'],
],
terminalWidth: 60,
expected: ['Happy π', 'Smile π', 'Fire π₯'],
},
{
name: 'renders a table with only Asian characters and text correctly',
headers: ['Chinese δΈζ', 'Japanese ζ₯ζ¬θͺ', 'Korean νκ΅μ΄'],
rows: [
['δ½ ε₯½', 'γγγ«γ‘γ―', 'μλ
νμΈμ'],
['δΈη', 'δΈη', 'μΈκ³'],
],
terminalWidth: 60,
expected: ['Chinese δΈζ', 'δ½ ε₯½', 'γγγ«γ‘γ―'],
},
{
name: 'renders a table with mixed emojis, Asian characters, and text correctly',
headers: ['Mixed π δΈζ', 'Complex π ζ₯ζ¬θͺ', 'Text π νκ΅μ΄'],
rows: [
['δ½ ε₯½ π', 'γγγ«γ‘γ― π', 'μλ
νμΈμ π'],
['World π', 'Code π»', 'Pizza π'],
],
terminalWidth: 80,
expected: ['Mixed π δΈζ', 'δ½ ε₯½ π', 'γγγ«γ‘γ― π'],
},
])('$name', async ({ headers, rows, terminalWidth, expected }) => {
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
{ width: terminalWidth },
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expected.forEach((text) => {
expect(output).toContain(text);
});
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it.each([
{
name: 'renders correctly when headers are empty but rows have data',
headers: [] as string[],
rows: [['Data 1', 'Data 2']],
expected: ['Data 1', 'Data 2'],
},
{
name: 'renders correctly when there are more headers than columns in rows',
headers: ['Header 1', 'Header 2', 'Header 3'],
rows: [['Data 1', 'Data 2']],
expected: ['Header 1', 'Header 2', 'Header 3', 'Data 1', 'Data 2'],
},
])('$name', async ({ headers, rows, expected }) => {
const terminalWidth = 50;
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expected.forEach((text) => {
expect(output).toContain(text);
});
await expect(renderResult).toMatchSvgSnapshot();
unmount();
});
it.each([
{
name: 'renders complex markdown in rows and calculates widths correctly',
headers: ['Feature', 'Markdown'],
rows: [
['Bold', '**Bold Text**'],
['Italic', '_Italic Text_'],
['Combined', '***Bold and Italic***'],
['Link', '[Google](https://google.com)'],
['Code', '`const x = 1`'],
['Strikethrough', '~~Strike~~'],
['Underline', '<u>Underline</u>'],
],
terminalWidth: 80,
waitForText: 'Bold Text',
assertions: (output: string) => {
expect(output).not.toContain('**Bold Text**');
expect(output).toContain('Bold Text');
expect(output).not.toContain('_Italic Text_');
expect(output).toContain('Italic Text');
expect(output).toContain('Bold and Italic');
expect(output).toContain('Google');
expect(output).toContain('https://google.com');
expect(output).toContain('(https://google.com)');
expect(output).toContain('const x = 1');
expect(output).not.toContain('`const x = 1`');
expect(output).toContain('Strike');
expect(output).toContain('Underline');
},
},
{
name: 'calculates column widths based on rendered text, not raw markdown',
headers: ['Col 1', 'Col 2', 'Col 3'],
rows: [
['**123456**', 'Normal', 'Short'],
['Short', '**123456**', 'Normal'],
['Normal', 'Short', '**123456**'],
],
terminalWidth: 40,
waitForText: '123456',
assertions: (output: string) => {
expect(output).toContain('123456');
const dataLines = output.split('\n').filter((l) => /123456/.test(l));
expect(dataLines.length).toBe(3);
},
},
{
name: 'handles nested markdown styles recursively',
headers: ['Header 1', 'Header 2', 'Header 3'],
rows: [
['**Bold with _Italic_ and ~~Strike~~**', 'Normal', 'Short'],
['Short', '**Bold with _Italic_ and ~~Strike~~**', 'Normal'],
['Normal', 'Short', '**Bold with _Italic_ and ~~Strike~~**'],
],
terminalWidth: 100,
waitForText: 'Bold with Italic and Strike',
assertions: (output: string) => {
expect(output).not.toContain('**');
expect(output).not.toContain('_');
expect(output).not.toContain('~~');
expect(output).toContain('Bold with Italic and Strike');
},
},
{
name: 'calculates width correctly for content with URLs and styles',
headers: ['Col 1', 'Col 2', 'Col 3'],
rows: [
['Visit [Google](https://google.com)', 'Plain Text', 'More Info'],
['Info Here', 'Visit [Bing](https://bing.com)', 'Links'],
['Check This', 'Search', 'Visit [Yahoo](https://yahoo.com)'],
],
terminalWidth: 120,
waitForText: 'Visit Google',
assertions: (output: string) => {
expect(output).toContain('Visit Google');
expect(output).toContain('Visit Bing');
expect(output).toContain('Visit Yahoo');
expect(output).toContain('https://google.com');
expect(output).toContain('https://bing.com');
expect(output).toContain('https://yahoo.com');
expect(output).toContain('(https://google.com)');
const dataLine = output
.split('\n')
.find((l) => l.includes('Visit Google'));
expect(dataLine).toContain('Visit Google');
},
},
{
name: 'does not parse markdown inside code snippets',
headers: ['Col 1', 'Col 2', 'Col 3'],
rows: [
['`**not bold**`', '`_not italic_`', '`~~not strike~~`'],
['`[not link](url)`', '`<u>not underline</u>`', '`https://not.link`'],
['Normal Text', 'More Code: `*test*`', '`***nested***`'],
],
terminalWidth: 100,
waitForText: '**not bold**',
assertions: (output: string) => {
expect(output).toContain('**not bold**');
expect(output).toContain('_not italic_');
expect(output).toContain('~~not strike~~');
expect(output).toContain('[not link](url)');
expect(output).toContain('<u>not underline</u>');
expect(output).toContain('https://not.link');
expect(output).toContain('***nested***');
},
},
])(
'$name',
async ({ headers, rows, terminalWidth, waitForText, assertions }) => {
const renderResult = await renderWithProviders(
<TableRenderer
headers={headers}
rows={rows}
terminalWidth={terminalWidth}
/>,
{ width: terminalWidth },
);
const { lastFrame, unmount } = renderResult;
const output = lastFrame();
expect(output).toBeDefined();
expect(output).toContain(waitForText);
assertions(output);
await expect(renderResult).toMatchSvgSnapshot();
unmount();
},
);
});
|