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

View File

@@ -6,6 +6,13 @@
import { vi, describe, expect, it, beforeEach, afterEach } from 'vitest';
import { readStdin } from './readStdin.js';
import { debugLogger } from '@google/gemini-cli-core';
vi.mock('@google/gemini-cli-core', () => ({
debugLogger: {
warn: vi.fn(),
},
}));
// Mock process.stdin
const mockStdin = {
@@ -20,6 +27,7 @@ describe('readStdin', () => {
let originalStdin: typeof process.stdin;
let onReadableHandler: () => void;
let onEndHandler: () => void;
let onErrorHandler: (err: Error) => void;
beforeEach(() => {
vi.clearAllMocks();
@@ -33,10 +41,13 @@ describe('readStdin', () => {
});
// Capture event handlers
mockStdin.on.mockImplementation((event: string, handler: () => void) => {
if (event === 'readable') onReadableHandler = handler;
if (event === 'end') onEndHandler = handler;
});
mockStdin.on.mockImplementation(
(event: string, handler: (...args: unknown[]) => void) => {
if (event === 'readable') onReadableHandler = handler as () => void;
if (event === 'end') onEndHandler = handler as () => void;
if (event === 'error') onErrorHandler = handler as (err: Error) => void;
},
);
});
afterEach(() => {
@@ -109,4 +120,26 @@ describe('readStdin', () => {
await expect(promise).resolves.toBe('chunk1chunk2');
});
it('should truncate input if it exceeds MAX_STDIN_SIZE', async () => {
const MAX_STDIN_SIZE = 8 * 1024 * 1024;
const largeChunk = 'a'.repeat(MAX_STDIN_SIZE + 100);
mockStdin.read.mockReturnValueOnce(largeChunk).mockReturnValueOnce(null);
const promise = readStdin();
onReadableHandler();
await expect(promise).resolves.toBe('a'.repeat(MAX_STDIN_SIZE));
expect(debugLogger.warn).toHaveBeenCalledWith(
`Warning: stdin input truncated to ${MAX_STDIN_SIZE} bytes.`,
);
expect(mockStdin.destroy).toHaveBeenCalled();
});
it('should handle stdin error', async () => {
const promise = readStdin();
const error = new Error('stdin error');
onErrorHandler(error);
await expect(promise).rejects.toThrow('stdin error');
});
});