Migrate console to coreEvents.emitFeedback or debugLogger (#15219)

This commit is contained in:
Adib234
2025-12-29 15:46:10 -05:00
committed by GitHub
parent dcd2449b1a
commit 10ae84869a
66 changed files with 564 additions and 425 deletions
+39 -43
View File
@@ -10,6 +10,11 @@ import { ChatRecordingService } from '@google/gemini-cli-core';
import { listSessions, deleteSession } from './sessions.js';
import { SessionSelector, type SessionInfo } from './sessionUtils.js';
const mocks = vi.hoisted(() => ({
writeToStdout: vi.fn(),
writeToStderr: vi.fn(),
}));
// Mock the SessionSelector and ChatRecordingService
vi.mock('./sessionUtils.js', () => ({
SessionSelector: vi.fn(),
@@ -22,13 +27,14 @@ vi.mock('@google/gemini-cli-core', async () => {
...actual,
ChatRecordingService: vi.fn(),
generateSummary: vi.fn().mockResolvedValue(undefined),
writeToStdout: mocks.writeToStdout,
writeToStderr: mocks.writeToStderr,
};
});
describe('listSessions', () => {
let mockConfig: Config;
let mockListSessions: ReturnType<typeof vi.fn>;
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
// Create mock config
@@ -49,14 +55,12 @@ describe('listSessions', () => {
listSessions: mockListSessions,
}) as unknown as InstanceType<typeof SessionSelector>,
);
// Spy on console.log
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
});
afterEach(() => {
vi.clearAllMocks();
consoleLogSpy.mockRestore();
mocks.writeToStdout.mockClear();
mocks.writeToStderr.mockClear();
});
it('should display message when no previous sessions were found', async () => {
@@ -68,7 +72,7 @@ describe('listSessions', () => {
// Assert
expect(mockListSessions).toHaveBeenCalledOnce();
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
'No previous sessions found for this project.',
);
});
@@ -127,32 +131,32 @@ describe('listSessions', () => {
expect(mockListSessions).toHaveBeenCalledOnce();
// Check that the header was displayed
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
'\nAvailable sessions for this project (3):\n',
);
// Check that each session was logged
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('1. First user message'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('[session-1]'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('2. Second user message'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('[session-2]'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('3. Current session'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining(', current)'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('[current-session-id]'),
);
});
@@ -209,7 +213,7 @@ describe('listSessions', () => {
// Assert
// Get all the session log calls (skip the header)
const sessionCalls = consoleLogSpy.mock.calls.filter(
const sessionCalls = mocks.writeToStdout.mock.calls.filter(
(call): call is [string] =>
typeof call[0] === 'string' &&
call[0].includes('[session-') &&
@@ -246,13 +250,13 @@ describe('listSessions', () => {
await listSessions(mockConfig);
// Assert
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('1. Test message'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('some time ago'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('[abc123def456]'),
);
});
@@ -281,13 +285,13 @@ describe('listSessions', () => {
await listSessions(mockConfig);
// Assert
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
'\nAvailable sessions for this project (1):\n',
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('1. Only session'),
);
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining(', current)'),
);
});
@@ -318,10 +322,10 @@ describe('listSessions', () => {
await listSessions(mockConfig);
// Assert: Should show the summary (displayName), not the first user message
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('1. Add dark mode to the app'),
);
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect(mocks.writeToStdout).not.toHaveBeenCalledWith(
expect.stringContaining('How do I add dark mode to my React application'),
);
});
@@ -331,8 +335,6 @@ describe('deleteSession', () => {
let mockConfig: Config;
let mockListSessions: ReturnType<typeof vi.fn>;
let mockDeleteSession: ReturnType<typeof vi.fn>;
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
// Create mock config
@@ -362,16 +364,10 @@ describe('deleteSession', () => {
deleteSession: mockDeleteSession,
}) as unknown as InstanceType<typeof ChatRecordingService>,
);
// Spy on console methods
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.clearAllMocks();
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
});
it('should display error when no sessions are found', async () => {
@@ -383,7 +379,7 @@ describe('deleteSession', () => {
// Assert
expect(mockListSessions).toHaveBeenCalledOnce();
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'No sessions found for this project.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -416,10 +412,10 @@ describe('deleteSession', () => {
// Assert
expect(mockListSessions).toHaveBeenCalledOnce();
expect(mockDeleteSession).toHaveBeenCalledWith('session-file-123');
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
'Deleted session 1: Test session (some time ago)',
);
expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(mocks.writeToStderr).not.toHaveBeenCalled();
});
it('should delete session by index', async () => {
@@ -463,7 +459,7 @@ describe('deleteSession', () => {
// Assert
expect(mockListSessions).toHaveBeenCalledOnce();
expect(mockDeleteSession).toHaveBeenCalledWith('session-file-2');
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
'Deleted session 2: Second session (some time ago)',
);
});
@@ -492,7 +488,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, 'invalid-id');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Invalid session identifier "invalid-id". Use --list-sessions to see available sessions.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -522,7 +518,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, '999');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Invalid session identifier "999". Use --list-sessions to see available sessions.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -552,7 +548,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, '0');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Invalid session identifier "0". Use --list-sessions to see available sessions.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -582,7 +578,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, '1');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Cannot delete the current active session.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -612,7 +608,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, 'current-session-id');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Cannot delete the current active session.',
);
expect(mockDeleteSession).not.toHaveBeenCalled();
@@ -646,7 +642,7 @@ describe('deleteSession', () => {
// Assert
expect(mockDeleteSession).toHaveBeenCalledWith('session-file-1');
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Failed to delete session: File deletion failed',
);
});
@@ -679,7 +675,7 @@ describe('deleteSession', () => {
await deleteSession(mockConfig, '1');
// Assert
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(mocks.writeToStderr).toHaveBeenCalledWith(
'Failed to delete session: Unknown error',
);
});
@@ -737,7 +733,7 @@ describe('deleteSession', () => {
// Assert
expect(mockDeleteSession).toHaveBeenCalledWith('session-file-1');
expect(consoleLogSpy).toHaveBeenCalledWith(
expect(mocks.writeToStdout).toHaveBeenCalledWith(
expect.stringContaining('Oldest session'),
);
});