2026-03-16 21:30:16 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-16 21:45:11 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
import { ConsolePatcher } from './ConsolePatcher.js';
|
|
|
|
|
|
|
|
|
|
describe('ConsolePatcher', () => {
|
|
|
|
|
let patcher: ConsolePatcher;
|
|
|
|
|
const onNewMessage = vi.fn();
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
if (patcher) {
|
|
|
|
|
patcher.cleanup();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should suppress output when suppressConsoleOutput is true and debugMode is false', () => {
|
2026-03-16 22:04:06 +00:00
|
|
|
const originalError = console.error;
|
|
|
|
|
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher = new ConsolePatcher({
|
|
|
|
|
debugMode: false,
|
|
|
|
|
suppressConsoleOutput: true,
|
|
|
|
|
stderr: true,
|
|
|
|
|
});
|
|
|
|
|
patcher.patch();
|
|
|
|
|
|
|
|
|
|
console.log('test log');
|
|
|
|
|
|
2026-03-16 22:04:06 +00:00
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher.cleanup();
|
2026-03-16 22:04:06 +00:00
|
|
|
errorSpy.mockRestore();
|
|
|
|
|
console.error = originalError;
|
2026-03-16 21:30:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should NOT suppress output when suppressConsoleOutput is true but debugMode is true', () => {
|
|
|
|
|
const originalError = console.error;
|
|
|
|
|
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher = new ConsolePatcher({
|
|
|
|
|
debugMode: true,
|
|
|
|
|
suppressConsoleOutput: true,
|
|
|
|
|
stderr: true,
|
|
|
|
|
});
|
|
|
|
|
patcher.patch();
|
|
|
|
|
|
|
|
|
|
console.log('test log');
|
|
|
|
|
// When stderr is true, log goes to originalConsoleError
|
|
|
|
|
expect(errorSpy).toHaveBeenCalled();
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher.cleanup();
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
|
console.error = originalError;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should NOT suppress output when suppressConsoleOutput is false', () => {
|
|
|
|
|
const originalError = console.error;
|
|
|
|
|
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher = new ConsolePatcher({
|
|
|
|
|
debugMode: false,
|
|
|
|
|
suppressConsoleOutput: false,
|
|
|
|
|
stderr: true,
|
|
|
|
|
});
|
|
|
|
|
patcher.patch();
|
|
|
|
|
|
|
|
|
|
console.error('test error');
|
|
|
|
|
expect(errorSpy).toHaveBeenCalled();
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher.cleanup();
|
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
|
console.error = originalError;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should call onNewMessage when stderr is false and not suppressed', () => {
|
|
|
|
|
patcher = new ConsolePatcher({
|
|
|
|
|
debugMode: false,
|
|
|
|
|
suppressConsoleOutput: false,
|
|
|
|
|
stderr: false,
|
|
|
|
|
onNewMessage,
|
|
|
|
|
});
|
|
|
|
|
patcher.patch();
|
|
|
|
|
|
|
|
|
|
console.log('test log');
|
2026-03-16 21:45:11 +00:00
|
|
|
expect(onNewMessage).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
type: 'log',
|
|
|
|
|
content: 'test log',
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
patcher.cleanup();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should NOT suppress output when suppressConsoleOutput is true but debugMode is true (explicit check)', () => {
|
|
|
|
|
const onNewMessage = vi.fn();
|
|
|
|
|
patcher = new ConsolePatcher({
|
|
|
|
|
debugMode: true,
|
|
|
|
|
suppressConsoleOutput: true,
|
|
|
|
|
stderr: false,
|
|
|
|
|
onNewMessage,
|
|
|
|
|
});
|
|
|
|
|
patcher.patch();
|
|
|
|
|
|
|
|
|
|
console.log('test log');
|
2026-03-16 21:45:11 +00:00
|
|
|
|
2026-03-16 21:30:16 +00:00
|
|
|
expect(onNewMessage).toHaveBeenCalled();
|
|
|
|
|
patcher.cleanup();
|
|
|
|
|
});
|
|
|
|
|
});
|