Refactor: Migrate CLI appEvents to Core coreEvents (#15737)

This commit is contained in:
Adib234
2026-01-23 11:45:46 -05:00
committed by GitHub
parent 0b7d26c9e3
commit 488d5fc439
13 changed files with 90 additions and 93 deletions

View File

@@ -5,12 +5,25 @@
*/
import { act } from 'react';
import type { EventEmitter } from 'node:events';
import { render } from '../../test-utils/render.js';
import { waitFor } from '../../test-utils/async.js';
import { ConfigInitDisplay } from './ConfigInitDisplay.js';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { AppEvent } from '../../utils/events.js';
import { MCPServerStatus, type McpClient } from '@google/gemini-cli-core';
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
type MockInstance,
} from 'vitest';
import {
CoreEvent,
MCPServerStatus,
type McpClient,
coreEvents,
} from '@google/gemini-cli-core';
import { Text } from 'ink';
// Mock GeminiSpinner
@@ -18,30 +31,11 @@ vi.mock('./GeminiRespondingSpinner.js', () => ({
GeminiSpinner: () => <Text>Spinner</Text>,
}));
// Mock appEvents
const { mockOn, mockOff, mockEmit } = vi.hoisted(() => ({
mockOn: vi.fn(),
mockOff: vi.fn(),
mockEmit: vi.fn(),
}));
vi.mock('../../utils/events.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../utils/events.js')>();
return {
...actual,
appEvents: {
on: mockOn,
off: mockOff,
emit: mockEmit,
},
};
});
describe('ConfigInitDisplay', () => {
let onSpy: MockInstance<EventEmitter['on']>;
beforeEach(() => {
mockOn.mockClear();
mockOff.mockClear();
mockEmit.mockClear();
onSpy = vi.spyOn(coreEvents as EventEmitter, 'on');
});
afterEach(() => {
@@ -55,10 +49,11 @@ describe('ConfigInitDisplay', () => {
it('updates message on McpClientUpdate event', async () => {
let listener: ((clients?: Map<string, McpClient>) => void) | undefined;
mockOn.mockImplementation((event, fn) => {
if (event === AppEvent.McpClientUpdate) {
listener = fn;
onSpy.mockImplementation((event: unknown, fn: unknown) => {
if (event === CoreEvent.McpClientUpdate) {
listener = fn as (clients?: Map<string, McpClient>) => void;
}
return coreEvents;
});
const { lastFrame } = render(<ConfigInitDisplay />);
@@ -92,10 +87,11 @@ describe('ConfigInitDisplay', () => {
it('truncates list of waiting servers if too many', async () => {
let listener: ((clients?: Map<string, McpClient>) => void) | undefined;
mockOn.mockImplementation((event, fn) => {
if (event === AppEvent.McpClientUpdate) {
listener = fn;
onSpy.mockImplementation((event: unknown, fn: unknown) => {
if (event === CoreEvent.McpClientUpdate) {
listener = fn as (clients?: Map<string, McpClient>) => void;
}
return coreEvents;
});
const { lastFrame } = render(<ConfigInitDisplay />);
@@ -127,10 +123,11 @@ describe('ConfigInitDisplay', () => {
it('handles empty clients map', async () => {
let listener: ((clients?: Map<string, McpClient>) => void) | undefined;
mockOn.mockImplementation((event, fn) => {
if (event === AppEvent.McpClientUpdate) {
listener = fn;
onSpy.mockImplementation((event: unknown, fn: unknown) => {
if (event === CoreEvent.McpClientUpdate) {
listener = fn as (clients?: Map<string, McpClient>) => void;
}
return coreEvents;
});
const { lastFrame } = render(<ConfigInitDisplay />);

View File

@@ -5,9 +5,13 @@
*/
import { useEffect, useState } from 'react';
import { AppEvent, appEvents } from './../../utils/events.js';
import { Box, Text } from 'ink';
import { type McpClient, MCPServerStatus } from '@google/gemini-cli-core';
import {
CoreEvent,
coreEvents,
type McpClient,
MCPServerStatus,
} from '@google/gemini-cli-core';
import { GeminiSpinner } from './GeminiRespondingSpinner.js';
import { theme } from '../semantic-colors.js';
@@ -45,9 +49,9 @@ export const ConfigInitDisplay = () => {
}
};
appEvents.on(AppEvent.McpClientUpdate, onChange);
coreEvents.on(CoreEvent.McpClientUpdate, onChange);
return () => {
appEvents.off(AppEvent.McpClientUpdate, onChange);
coreEvents.off(CoreEvent.McpClientUpdate, onChange);
};
}, []);