Sync upstream Gemini-CLI v0.8.2 (#838)

This commit is contained in:
tanzhenxin
2025-10-23 09:27:04 +08:00
committed by GitHub
parent 096fabb5d6
commit eb95c131be
644 changed files with 70389 additions and 23709 deletions

View File

@@ -9,6 +9,13 @@ import { render } from 'ink-testing-library';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { QwenOAuthProgress } from './QwenOAuthProgress.js';
import type { DeviceAuthorizationInfo } from '../hooks/useQwenAuth.js';
import { useKeypress } from '../hooks/useKeypress.js';
import type { Key } from '../contexts/KeypressContext.js';
// Mock useKeypress hook
vi.mock('../hooks/useKeypress.js', () => ({
useKeypress: vi.fn(),
}));
// Mock qrcode-terminal module
vi.mock('qrcode-terminal', () => ({
@@ -31,6 +38,8 @@ vi.mock('ink-link', () => ({
describe('QwenOAuthProgress', () => {
const mockOnTimeout = vi.fn();
const mockOnCancel = vi.fn();
const mockedUseKeypress = vi.mocked(useKeypress);
let keypressHandler: ((key: Key) => void) | null = null;
const createMockDeviceAuth = (
overrides: Partial<DeviceAuthorizationInfo> = {},
@@ -68,6 +77,12 @@ describe('QwenOAuthProgress', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
keypressHandler = null;
// Mock useKeypress to capture the handler
mockedUseKeypress.mockImplementation((handler) => {
keypressHandler = handler;
});
});
afterEach(() => {
@@ -419,7 +434,7 @@ describe('QwenOAuthProgress', () => {
describe('User interactions', () => {
it('should call onCancel when ESC key is pressed', () => {
const { stdin } = render(
render(
<QwenOAuthProgress
onTimeout={mockOnTimeout}
onCancel={mockOnCancel}
@@ -428,24 +443,42 @@ describe('QwenOAuthProgress', () => {
);
// Simulate ESC key press
stdin.write('\u001b'); // ESC character
if (keypressHandler) {
keypressHandler({
name: 'escape',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\u001b',
});
}
expect(mockOnCancel).toHaveBeenCalledTimes(1);
});
it('should call onCancel when ESC is pressed in loading state', () => {
const { stdin } = render(
render(
<QwenOAuthProgress onTimeout={mockOnTimeout} onCancel={mockOnCancel} />,
);
// Simulate ESC key press
stdin.write('\u001b'); // ESC character
if (keypressHandler) {
keypressHandler({
name: 'escape',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\u001b',
});
}
expect(mockOnCancel).toHaveBeenCalledTimes(1);
});
it('should not call onCancel for other key presses', () => {
const { stdin } = render(
render(
<QwenOAuthProgress
onTimeout={mockOnTimeout}
onCancel={mockOnCancel}
@@ -454,9 +487,32 @@ describe('QwenOAuthProgress', () => {
);
// Simulate other key presses
stdin.write('a');
stdin.write('\r'); // Enter
stdin.write(' '); // Space
if (keypressHandler) {
keypressHandler({
name: 'a',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: 'a',
});
keypressHandler({
name: 'return',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\r',
});
keypressHandler({
name: 'space',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: ' ',
});
}
expect(mockOnCancel).not.toHaveBeenCalled();
});
@@ -529,17 +585,35 @@ describe('QwenOAuthProgress', () => {
});
it('should call onCancel for any key press in timeout state', () => {
const { stdin } = renderComponent({
renderComponent({
authStatus: 'timeout',
});
// Simulate any key press
stdin.write('a');
if (keypressHandler) {
keypressHandler({
name: 'a',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: 'a',
});
}
expect(mockOnCancel).toHaveBeenCalledTimes(1);
// Reset mock and try enter key
mockOnCancel.mockClear();
stdin.write('\r');
if (keypressHandler) {
keypressHandler({
name: 'return',
ctrl: false,
meta: false,
shift: false,
paste: false,
sequence: '\r',
});
}
expect(mockOnCancel).toHaveBeenCalledTimes(1);
});
});