mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-21 10:34:35 -07:00
feat(core): Implement JIT context memory loading and UI sync (#14469)
This commit is contained in:
@@ -8,7 +8,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { ContextManager } from './contextManager.js';
|
||||
import * as memoryDiscovery from '../utils/memoryDiscovery.js';
|
||||
import type { Config } from '../config/config.js';
|
||||
import type { ExtensionLoader } from '../utils/extensionLoader.js';
|
||||
import { coreEvents, CoreEvent } from '../utils/events.js';
|
||||
|
||||
// Mock memoryDiscovery module
|
||||
vi.mock('../utils/memoryDiscovery.js', async (importOriginal) => {
|
||||
@@ -19,6 +19,9 @@ vi.mock('../utils/memoryDiscovery.js', async (importOriginal) => {
|
||||
loadGlobalMemory: vi.fn(),
|
||||
loadEnvironmentMemory: vi.fn(),
|
||||
loadJitSubdirectoryMemory: vi.fn(),
|
||||
concatenateInstructions: vi
|
||||
.fn()
|
||||
.mockImplementation(actual.concatenateInstructions),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -30,58 +33,84 @@ describe('ContextManager', () => {
|
||||
mockConfig = {
|
||||
getDebugMode: vi.fn().mockReturnValue(false),
|
||||
getWorkingDir: vi.fn().mockReturnValue('/app'),
|
||||
getWorkspaceContext: vi.fn().mockReturnValue({
|
||||
getDirectories: vi.fn().mockReturnValue(['/app']),
|
||||
}),
|
||||
getExtensionLoader: vi.fn().mockReturnValue({}),
|
||||
getMcpClientManager: vi.fn().mockReturnValue({
|
||||
getMcpInstructions: vi.fn().mockReturnValue('MCP Instructions'),
|
||||
}),
|
||||
} as unknown as Config;
|
||||
|
||||
contextManager = new ContextManager(mockConfig);
|
||||
vi.clearAllMocks();
|
||||
vi.spyOn(coreEvents, 'emit');
|
||||
});
|
||||
|
||||
describe('loadGlobalMemory', () => {
|
||||
it('should load and format global memory', async () => {
|
||||
const mockResult: memoryDiscovery.MemoryLoadResult = {
|
||||
describe('refresh', () => {
|
||||
it('should load and format global and environment memory', async () => {
|
||||
const mockGlobalResult: memoryDiscovery.MemoryLoadResult = {
|
||||
files: [
|
||||
{ path: '/home/user/.gemini/GEMINI.md', content: 'Global Content' },
|
||||
],
|
||||
};
|
||||
vi.mocked(memoryDiscovery.loadGlobalMemory).mockResolvedValue(mockResult);
|
||||
|
||||
const result = await contextManager.loadGlobalMemory();
|
||||
|
||||
expect(memoryDiscovery.loadGlobalMemory).toHaveBeenCalledWith(false);
|
||||
// The path will be relative to CWD (/app), so it might contain ../
|
||||
expect(result).toMatch(/--- Context from: .*GEMINI.md ---/);
|
||||
expect(result).toContain('Global Content');
|
||||
expect(contextManager.getLoadedPaths()).toContain(
|
||||
'/home/user/.gemini/GEMINI.md',
|
||||
vi.mocked(memoryDiscovery.loadGlobalMemory).mockResolvedValue(
|
||||
mockGlobalResult,
|
||||
);
|
||||
expect(contextManager.getGlobalMemory()).toBe(result);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadEnvironmentMemory', () => {
|
||||
it('should load and format environment memory', async () => {
|
||||
const mockResult: memoryDiscovery.MemoryLoadResult = {
|
||||
const mockEnvResult: memoryDiscovery.MemoryLoadResult = {
|
||||
files: [{ path: '/app/GEMINI.md', content: 'Env Content' }],
|
||||
};
|
||||
vi.mocked(memoryDiscovery.loadEnvironmentMemory).mockResolvedValue(
|
||||
mockResult,
|
||||
mockEnvResult,
|
||||
);
|
||||
const mockExtensionLoader = {} as unknown as ExtensionLoader;
|
||||
|
||||
const result = await contextManager.loadEnvironmentMemory(
|
||||
['/app'],
|
||||
mockExtensionLoader,
|
||||
await contextManager.refresh();
|
||||
|
||||
expect(memoryDiscovery.loadGlobalMemory).toHaveBeenCalledWith(false);
|
||||
expect(contextManager.getGlobalMemory()).toMatch(
|
||||
/--- Context from: .*GEMINI.md ---/,
|
||||
);
|
||||
expect(contextManager.getGlobalMemory()).toContain('Global Content');
|
||||
|
||||
expect(memoryDiscovery.loadEnvironmentMemory).toHaveBeenCalledWith(
|
||||
['/app'],
|
||||
mockExtensionLoader,
|
||||
expect.anything(),
|
||||
false,
|
||||
);
|
||||
expect(result).toContain('--- Context from: GEMINI.md ---');
|
||||
expect(result).toContain('Env Content');
|
||||
expect(contextManager.getEnvironmentMemory()).toContain(
|
||||
'--- Context from: GEMINI.md ---',
|
||||
);
|
||||
expect(contextManager.getEnvironmentMemory()).toContain('Env Content');
|
||||
expect(contextManager.getEnvironmentMemory()).toContain(
|
||||
'MCP Instructions',
|
||||
);
|
||||
|
||||
expect(contextManager.getLoadedPaths()).toContain(
|
||||
'/home/user/.gemini/GEMINI.md',
|
||||
);
|
||||
expect(contextManager.getLoadedPaths()).toContain('/app/GEMINI.md');
|
||||
expect(contextManager.getEnvironmentMemory()).toBe(result);
|
||||
});
|
||||
|
||||
it('should emit MemoryChanged event when memory is refreshed', async () => {
|
||||
const mockGlobalResult = {
|
||||
files: [{ path: '/app/GEMINI.md', content: 'content' }],
|
||||
};
|
||||
const mockEnvResult = {
|
||||
files: [{ path: '/app/src/GEMINI.md', content: 'env content' }],
|
||||
};
|
||||
vi.mocked(memoryDiscovery.loadGlobalMemory).mockResolvedValue(
|
||||
mockGlobalResult,
|
||||
);
|
||||
vi.mocked(memoryDiscovery.loadEnvironmentMemory).mockResolvedValue(
|
||||
mockEnvResult,
|
||||
);
|
||||
|
||||
await contextManager.refresh();
|
||||
|
||||
expect(coreEvents.emit).toHaveBeenCalledWith(CoreEvent.MemoryChanged, {
|
||||
fileCount: 2,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -122,27 +151,4 @@ describe('ContextManager', () => {
|
||||
expect(result).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('reset', () => {
|
||||
it('should clear loaded paths and memory', async () => {
|
||||
// Setup some state
|
||||
const mockResult: memoryDiscovery.MemoryLoadResult = {
|
||||
files: [
|
||||
{ path: '/home/user/.gemini/GEMINI.md', content: 'Global Content' },
|
||||
],
|
||||
};
|
||||
vi.mocked(memoryDiscovery.loadGlobalMemory).mockResolvedValue(mockResult);
|
||||
await contextManager.loadGlobalMemory();
|
||||
|
||||
expect(contextManager.getLoadedPaths().size).toBeGreaterThan(0);
|
||||
expect(contextManager.getGlobalMemory()).toBeTruthy();
|
||||
|
||||
// Reset
|
||||
contextManager.reset();
|
||||
|
||||
expect(contextManager.getLoadedPaths().size).toBe(0);
|
||||
expect(contextManager.getGlobalMemory()).toBe('');
|
||||
expect(contextManager.getEnvironmentMemory()).toBe('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user