| |
| |
| |
| |
| |
|
|
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| import { initializeOutputListenersAndFlush } from './gemini.js'; |
| import { coreEvents, CoreEvent, type Config } from '@google/gemini-cli-core'; |
|
|
| |
| vi.mock('@google/gemini-cli-core', async (importOriginal) => { |
| const actual = |
| await importOriginal<typeof import('@google/gemini-cli-core')>(); |
| return { |
| ...actual, |
| writeToStdout: vi.fn(), |
| writeToStderr: vi.fn(), |
| }; |
| }); |
|
|
| import { writeToStdout, writeToStderr } from '@google/gemini-cli-core'; |
|
|
| describe('Output Redirection', () => { |
| beforeEach(() => { |
| vi.clearAllMocks(); |
| |
| coreEvents.removeAllListeners(); |
| }); |
|
|
| afterEach(() => { |
| coreEvents.removeAllListeners(); |
| }); |
|
|
| it('should redirect buffered stdout to stderr when output format is json', () => { |
| const mockConfig = { |
| getOutputFormat: () => 'json', |
| } as unknown as Config; |
|
|
| |
| coreEvents.emitOutput(false, 'informational message'); |
| coreEvents.emitOutput(true, 'error message'); |
|
|
| |
| initializeOutputListenersAndFlush(mockConfig); |
|
|
| |
| expect(writeToStderr).toHaveBeenCalledWith( |
| 'informational message', |
| undefined, |
| ); |
| expect(writeToStderr).toHaveBeenCalledWith('error message', undefined); |
| expect(writeToStdout).not.toHaveBeenCalled(); |
| }); |
|
|
| it('should NOT redirect buffered stdout to stderr when output format is NOT json', () => { |
| const mockConfig = { |
| getOutputFormat: () => 'text', |
| } as unknown as Config; |
|
|
| |
| coreEvents.emitOutput(false, 'regular message'); |
|
|
| |
| initializeOutputListenersAndFlush(mockConfig); |
|
|
| |
| expect(writeToStdout).toHaveBeenCalledWith('regular message', undefined); |
| expect(writeToStderr).not.toHaveBeenCalled(); |
| }); |
|
|
| it('should NOT force stdout to stderr when config is undefined (early init/version)', () => { |
| |
| coreEvents.emitOutput(false, 'early init message'); |
|
|
| |
| initializeOutputListenersAndFlush(undefined); |
|
|
| |
| expect(writeToStdout).toHaveBeenCalledWith('early init message', undefined); |
| expect(writeToStderr).not.toHaveBeenCalled(); |
| }); |
|
|
| it('should attach ConsoleLog and UserFeedback listeners even if Output already has one', () => { |
| |
| coreEvents.on(CoreEvent.Output, vi.fn()); |
|
|
| |
| initializeOutputListenersAndFlush(undefined); |
|
|
| |
| coreEvents.emitConsoleLog('info', 'stray log'); |
| coreEvents.emitFeedback('info', 'stray feedback'); |
|
|
| |
| |
| expect(writeToStderr).toHaveBeenCalledWith('stray log\n'); |
| expect(writeToStderr).toHaveBeenCalledWith('stray feedback\n'); |
| }); |
| }); |
|
|