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

@@ -4,24 +4,34 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
createIdeContextStore,
IDE_MAX_OPEN_FILES,
IDE_MAX_SELECTED_TEXT_LENGTH,
} from './constants.js';
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { IdeContextStore } from './ideContext.js';
import {
type IdeContext,
FileSchema,
IdeContextSchema,
} from './ideContext.js';
type File,
} from './types.js';
describe('ideContext', () => {
describe('createIdeContextStore', () => {
let ideContext: ReturnType<typeof createIdeContextStore>;
let ideContextStore: IdeContextStore;
beforeEach(() => {
// Create a fresh, isolated instance for each test
ideContext = createIdeContextStore();
ideContextStore = new IdeContextStore();
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should return undefined initially for ide context', () => {
expect(ideContext.getIdeContext()).toBeUndefined();
expect(ideContextStore.get()).toBeUndefined();
});
it('should set and retrieve the ide context', () => {
@@ -38,9 +48,9 @@ describe('ideContext', () => {
},
};
ideContext.setIdeContext(testFile);
ideContextStore.set(testFile);
const activeFile = ideContext.getIdeContext();
const activeFile = ideContextStore.get();
expect(activeFile).toEqual(testFile);
});
@@ -57,7 +67,7 @@ describe('ideContext', () => {
],
},
};
ideContext.setIdeContext(firstFile);
ideContextStore.set(firstFile);
const secondFile = {
workspaceState: {
@@ -71,9 +81,9 @@ describe('ideContext', () => {
],
},
};
ideContext.setIdeContext(secondFile);
ideContextStore.set(secondFile);
const activeFile = ideContext.getIdeContext();
const activeFile = ideContextStore.get();
expect(activeFile).toEqual(secondFile);
});
@@ -90,16 +100,16 @@ describe('ideContext', () => {
],
},
};
ideContext.setIdeContext(testFile);
expect(ideContext.getIdeContext()).toEqual(testFile);
ideContextStore.set(testFile);
expect(ideContextStore.get()).toEqual(testFile);
});
it('should notify subscribers when ide context changes', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
ideContext.subscribeToIdeContext(subscriber1);
ideContext.subscribeToIdeContext(subscriber2);
ideContextStore.subscribe(subscriber1);
ideContextStore.subscribe(subscriber2);
const testFile = {
workspaceState: {
@@ -113,7 +123,7 @@ describe('ideContext', () => {
],
},
};
ideContext.setIdeContext(testFile);
ideContextStore.set(testFile);
expect(subscriber1).toHaveBeenCalledTimes(1);
expect(subscriber1).toHaveBeenCalledWith(testFile);
@@ -133,7 +143,7 @@ describe('ideContext', () => {
],
},
};
ideContext.setIdeContext(newFile);
ideContextStore.set(newFile);
expect(subscriber1).toHaveBeenCalledTimes(2);
expect(subscriber1).toHaveBeenCalledWith(newFile);
@@ -145,10 +155,10 @@ describe('ideContext', () => {
const subscriber1 = vi.fn();
const subscriber2 = vi.fn();
const unsubscribe1 = ideContext.subscribeToIdeContext(subscriber1);
ideContext.subscribeToIdeContext(subscriber2);
const unsubscribe1 = ideContextStore.subscribe(subscriber1);
ideContextStore.subscribe(subscriber2);
ideContext.setIdeContext({
ideContextStore.set({
workspaceState: {
openFiles: [
{
@@ -165,7 +175,7 @@ describe('ideContext', () => {
unsubscribe1();
ideContext.setIdeContext({
ideContextStore.set({
workspaceState: {
openFiles: [
{
@@ -195,13 +205,152 @@ describe('ideContext', () => {
},
};
ideContext.setIdeContext(testFile);
ideContextStore.set(testFile);
expect(ideContext.getIdeContext()).toEqual(testFile);
expect(ideContextStore.get()).toEqual(testFile);
ideContext.clearIdeContext();
ideContextStore.clear();
expect(ideContext.getIdeContext()).toBeUndefined();
expect(ideContextStore.get()).toBeUndefined();
});
it('should set the context and notify subscribers when no workspaceState is present', () => {
const subscriber = vi.fn();
ideContextStore.subscribe(subscriber);
const context: IdeContext = {};
ideContextStore.set(context);
expect(ideContextStore.get()).toBe(context);
expect(subscriber).toHaveBeenCalledWith(context);
});
it('should handle an empty openFiles array', () => {
const context: IdeContext = {
workspaceState: {
openFiles: [],
},
};
ideContextStore.set(context);
expect(ideContextStore.get()?.workspaceState?.openFiles).toEqual([]);
});
it('should sort openFiles by timestamp in descending order', () => {
const context: IdeContext = {
workspaceState: {
openFiles: [
{ path: 'file1.ts', timestamp: 100, isActive: false },
{ path: 'file2.ts', timestamp: 300, isActive: true },
{ path: 'file3.ts', timestamp: 200, isActive: false },
],
},
};
ideContextStore.set(context);
const openFiles = ideContextStore.get()?.workspaceState?.openFiles;
expect(openFiles?.[0]?.path).toBe('file2.ts');
expect(openFiles?.[1]?.path).toBe('file3.ts');
expect(openFiles?.[2]?.path).toBe('file1.ts');
});
it('should mark only the most recent file as active and clear other active files', () => {
const context: IdeContext = {
workspaceState: {
openFiles: [
{
path: 'file1.ts',
timestamp: 100,
isActive: true,
selectedText: 'hello',
},
{
path: 'file2.ts',
timestamp: 300,
isActive: true,
cursor: { line: 1, character: 1 },
selectedText: 'hello',
},
{
path: 'file3.ts',
timestamp: 200,
isActive: false,
selectedText: 'hello',
},
],
},
};
ideContextStore.set(context);
const openFiles = ideContextStore.get()?.workspaceState?.openFiles;
expect(openFiles?.[0]?.isActive).toBe(true);
expect(openFiles?.[0]?.cursor).toBeDefined();
expect(openFiles?.[0]?.selectedText).toBeDefined();
expect(openFiles?.[1]?.isActive).toBe(false);
expect(openFiles?.[1]?.cursor).toBeUndefined();
expect(openFiles?.[1]?.selectedText).toBeUndefined();
expect(openFiles?.[2]?.isActive).toBe(false);
expect(openFiles?.[2]?.cursor).toBeUndefined();
expect(openFiles?.[2]?.selectedText).toBeUndefined();
});
it('should truncate selectedText if it exceeds the max length', () => {
const longText = 'a'.repeat(IDE_MAX_SELECTED_TEXT_LENGTH + 10);
const context: IdeContext = {
workspaceState: {
openFiles: [
{
path: 'file1.ts',
timestamp: 100,
isActive: true,
selectedText: longText,
},
],
},
};
ideContextStore.set(context);
const selectedText =
ideContextStore.get()?.workspaceState?.openFiles?.[0]?.selectedText;
expect(selectedText).toHaveLength(
IDE_MAX_SELECTED_TEXT_LENGTH + '... [TRUNCATED]'.length,
);
expect(selectedText?.endsWith('... [TRUNCATED]')).toBe(true);
});
it('should not truncate selectedText if it is within the max length', () => {
const shortText = 'a'.repeat(IDE_MAX_SELECTED_TEXT_LENGTH);
const context: IdeContext = {
workspaceState: {
openFiles: [
{
path: 'file1.ts',
timestamp: 100,
isActive: true,
selectedText: shortText,
},
],
},
};
ideContextStore.set(context);
const selectedText =
ideContextStore.get()?.workspaceState?.openFiles?.[0]?.selectedText;
expect(selectedText).toBe(shortText);
});
it('should truncate the openFiles list if it exceeds the max length', () => {
const files: File[] = Array.from(
{ length: IDE_MAX_OPEN_FILES + 5 },
(_, i) => ({
path: `file${i}.ts`,
timestamp: i,
isActive: false,
}),
);
const context: IdeContext = {
workspaceState: {
openFiles: files,
},
};
ideContextStore.set(context);
const openFiles = ideContextStore.get()?.workspaceState?.openFiles;
expect(openFiles).toHaveLength(IDE_MAX_OPEN_FILES);
});
});