Improve code coverage for cli package (#13724)

This commit is contained in:
Megha Bansal
2025-11-24 23:11:46 +05:30
committed by GitHub
parent 569c6f1dd0
commit 95693e265e
47 changed files with 5115 additions and 489 deletions
@@ -0,0 +1,71 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi } from 'vitest';
import { calculateMainAreaWidth } from './ui-sizing.js';
import { type LoadedSettings } from '../../config/settings.js';
// Mock dependencies
const mocks = vi.hoisted(() => ({
isAlternateBufferEnabled: vi.fn(),
}));
vi.mock('../hooks/useAlternateBuffer.js', () => ({
isAlternateBufferEnabled: mocks.isAlternateBufferEnabled,
}));
describe('ui-sizing', () => {
const createSettings = (useFullWidth?: boolean): LoadedSettings =>
({
merged: {
ui: {
useFullWidth,
},
},
}) as unknown as LoadedSettings;
describe('calculateMainAreaWidth', () => {
it.each([
// width, useFullWidth, alternateBuffer, expected
[80, true, false, 80],
[100, true, false, 100],
[80, true, true, 79], // -1 for alternate buffer
[100, true, true, 99],
// Default behavior (useFullWidth undefined or true)
[100, undefined, false, 100],
// useFullWidth: false (Smart sizing)
[80, false, false, 78], // 98% of 80
[132, false, false, 119], // 90% of 132
[200, false, false, 180], // 90% of 200 (>= 132)
// Interpolation check
[106, false, false, 100], // Approx middle
])(
'should return %i when width=%i, useFullWidth=%s, altBuffer=%s',
(width, useFullWidth, altBuffer, expected) => {
mocks.isAlternateBufferEnabled.mockReturnValue(altBuffer);
const settings = createSettings(useFullWidth);
expect(calculateMainAreaWidth(width, settings)).toBe(expected);
},
);
it('should match snapshot for interpolation range', () => {
mocks.isAlternateBufferEnabled.mockReturnValue(false);
const settings = createSettings(false);
const results: Record<number, number> = {};
// Test range from 80 to 132
for (let w = 80; w <= 132; w += 4) {
results[w] = calculateMainAreaWidth(w, settings);
}
expect(results).toMatchSnapshot();
});
});
});