Fix memoryDiscovery test to work in windows. (#4742)
This commit is contained in:
parent
bbe95f1eaa
commit
4fd7cf9177
|
@ -4,604 +4,363 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { vi, describe, it, expect, beforeEach, Mocked } from 'vitest';
|
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
import * as fsPromises from 'fs/promises';
|
import * as fsPromises from 'fs/promises';
|
||||||
import * as fsSync from 'fs';
|
|
||||||
import { Stats, Dirent } from 'fs';
|
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { loadServerHierarchicalMemory } from './memoryDiscovery.js';
|
import { loadServerHierarchicalMemory } from './memoryDiscovery.js';
|
||||||
import {
|
import {
|
||||||
GEMINI_CONFIG_DIR,
|
GEMINI_CONFIG_DIR,
|
||||||
setGeminiMdFilename,
|
setGeminiMdFilename,
|
||||||
getCurrentGeminiMdFilename,
|
|
||||||
DEFAULT_CONTEXT_FILENAME,
|
DEFAULT_CONTEXT_FILENAME,
|
||||||
} from '../tools/memoryTool.js';
|
} from '../tools/memoryTool.js';
|
||||||
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
||||||
|
|
||||||
const ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST = DEFAULT_CONTEXT_FILENAME;
|
vi.mock('os', async (importOriginal) => {
|
||||||
|
const actualOs = await importOriginal<typeof os>();
|
||||||
// Mock the entire fs/promises module
|
|
||||||
vi.mock('fs/promises');
|
|
||||||
// Mock the parts of fsSync we might use (like constants or existsSync if needed)
|
|
||||||
vi.mock('fs', async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof fsSync>();
|
|
||||||
return {
|
return {
|
||||||
...actual, // Spread actual to get all exports, including Stats and Dirent if they are classes/constructors
|
...actualOs,
|
||||||
constants: { ...actual.constants }, // Preserve constants
|
homedir: vi.fn(),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
vi.mock('os');
|
|
||||||
|
|
||||||
describe('loadServerHierarchicalMemory', () => {
|
describe('loadServerHierarchicalMemory', () => {
|
||||||
const mockFs = fsPromises as Mocked<typeof fsPromises>;
|
let testRootDir: string;
|
||||||
const mockOs = os as Mocked<typeof os>;
|
let cwd: string;
|
||||||
|
let projectRoot: string;
|
||||||
|
let homedir: string;
|
||||||
|
|
||||||
const CWD = '/test/project/src';
|
async function createEmptyDir(fullPath: string) {
|
||||||
const PROJECT_ROOT = '/test/project';
|
await fsPromises.mkdir(fullPath, { recursive: true });
|
||||||
const USER_HOME = '/test/userhome';
|
return fullPath;
|
||||||
|
}
|
||||||
|
|
||||||
let GLOBAL_GEMINI_DIR: string;
|
async function createTestFile(fullPath: string, fileContents: string) {
|
||||||
let GLOBAL_GEMINI_FILE: string; // Defined in beforeEach
|
await fsPromises.mkdir(path.dirname(fullPath), { recursive: true });
|
||||||
|
await fsPromises.writeFile(fullPath, fileContents);
|
||||||
|
return path.resolve(testRootDir, fullPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
testRootDir = await fsPromises.mkdtemp(
|
||||||
|
path.join(os.tmpdir(), 'folder-structure-test-'),
|
||||||
|
);
|
||||||
|
|
||||||
const fileService = new FileDiscoveryService(PROJECT_ROOT);
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.resetAllMocks();
|
vi.resetAllMocks();
|
||||||
// Set environment variables to indicate test environment
|
// Set environment variables to indicate test environment
|
||||||
process.env.NODE_ENV = 'test';
|
process.env.NODE_ENV = 'test';
|
||||||
process.env.VITEST = 'true';
|
process.env.VITEST = 'true';
|
||||||
|
|
||||||
setGeminiMdFilename(DEFAULT_CONTEXT_FILENAME); // Use defined const
|
projectRoot = await createEmptyDir(path.join(testRootDir, 'project'));
|
||||||
mockOs.homedir.mockReturnValue(USER_HOME);
|
cwd = await createEmptyDir(path.join(projectRoot, 'src'));
|
||||||
|
homedir = await createEmptyDir(path.join(testRootDir, 'userhome'));
|
||||||
|
vi.mocked(os.homedir).mockReturnValue(homedir);
|
||||||
|
});
|
||||||
|
|
||||||
// Define these here to use potentially reset/updated values from imports
|
afterEach(async () => {
|
||||||
GLOBAL_GEMINI_DIR = path.join(USER_HOME, GEMINI_CONFIG_DIR);
|
// Some tests set this to a different value.
|
||||||
GLOBAL_GEMINI_FILE = path.join(
|
setGeminiMdFilename(DEFAULT_CONTEXT_FILENAME);
|
||||||
GLOBAL_GEMINI_DIR,
|
// Clean up the temporary directory to prevent resource leaks.
|
||||||
getCurrentGeminiMdFilename(), // Use current filename
|
await fsPromises.rm(testRootDir, { recursive: true, force: true });
|
||||||
);
|
|
||||||
|
|
||||||
mockFs.stat.mockRejectedValue(new Error('File not found'));
|
|
||||||
mockFs.readdir.mockResolvedValue([]);
|
|
||||||
mockFs.readFile.mockRejectedValue(new Error('File not found'));
|
|
||||||
mockFs.access.mockRejectedValue(new Error('File not found'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return empty memory and count if no context files are found', async () => {
|
it('should return empty memory and count if no context files are found', async () => {
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
const result = await loadServerHierarchicalMemory(
|
||||||
CWD,
|
cwd,
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
expect(memoryContent).toBe('');
|
|
||||||
expect(fileCount).toBe(0);
|
expect(result).toEqual({
|
||||||
|
memoryContent: '',
|
||||||
|
fileCount: 0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load only the global context file if present and others are not (default filename)', async () => {
|
it('should load only the global context file if present and others are not (default filename)', async () => {
|
||||||
const globalDefaultFile = path.join(
|
const defaultContextFile = await createTestFile(
|
||||||
GLOBAL_GEMINI_DIR,
|
path.join(homedir, GEMINI_CONFIG_DIR, DEFAULT_CONTEXT_FILENAME),
|
||||||
DEFAULT_CONTEXT_FILENAME,
|
'default context content',
|
||||||
);
|
);
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
|
||||||
if (p === globalDefaultFile) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === globalDefaultFile) {
|
|
||||||
return 'Global memory content';
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
const result = await loadServerHierarchicalMemory(
|
||||||
CWD,
|
cwd,
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(memoryContent).toBe(
|
expect(result).toEqual({
|
||||||
`--- Context from: ${path.relative(CWD, globalDefaultFile)} ---\nGlobal memory content\n--- End of Context from: ${path.relative(CWD, globalDefaultFile)} ---`,
|
memoryContent: `--- Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||||
);
|
default context content
|
||||||
expect(fileCount).toBe(1);
|
--- End of Context from: ${path.relative(cwd, defaultContextFile)} ---`,
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(globalDefaultFile, 'utf-8');
|
fileCount: 1,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load only the global custom context file if present and filename is changed', async () => {
|
it('should load only the global custom context file if present and filename is changed', async () => {
|
||||||
const customFilename = 'CUSTOM_AGENTS.md';
|
const customFilename = 'CUSTOM_AGENTS.md';
|
||||||
setGeminiMdFilename(customFilename);
|
setGeminiMdFilename(customFilename);
|
||||||
const globalCustomFile = path.join(GLOBAL_GEMINI_DIR, customFilename);
|
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
const customContextFile = await createTestFile(
|
||||||
if (p === globalCustomFile) {
|
path.join(homedir, GEMINI_CONFIG_DIR, customFilename),
|
||||||
return undefined;
|
'custom context content',
|
||||||
}
|
);
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === globalCustomFile) {
|
|
||||||
return 'Global custom memory';
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
const result = await loadServerHierarchicalMemory(
|
||||||
CWD,
|
cwd,
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(memoryContent).toBe(
|
expect(result).toEqual({
|
||||||
`--- Context from: ${path.relative(CWD, globalCustomFile)} ---\nGlobal custom memory\n--- End of Context from: ${path.relative(CWD, globalCustomFile)} ---`,
|
memoryContent: `--- Context from: ${path.relative(cwd, customContextFile)} ---
|
||||||
);
|
custom context content
|
||||||
expect(fileCount).toBe(1);
|
--- End of Context from: ${path.relative(cwd, customContextFile)} ---`,
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(globalCustomFile, 'utf-8');
|
fileCount: 1,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load context files by upward traversal with custom filename', async () => {
|
it('should load context files by upward traversal with custom filename', async () => {
|
||||||
const customFilename = 'PROJECT_CONTEXT.md';
|
const customFilename = 'PROJECT_CONTEXT.md';
|
||||||
setGeminiMdFilename(customFilename);
|
setGeminiMdFilename(customFilename);
|
||||||
const projectRootCustomFile = path.join(PROJECT_ROOT, customFilename);
|
|
||||||
const srcCustomFile = path.join(CWD, customFilename);
|
|
||||||
|
|
||||||
mockFs.stat.mockImplementation(async (p) => {
|
const projectContextFile = await createTestFile(
|
||||||
if (p === path.join(PROJECT_ROOT, '.git')) {
|
path.join(projectRoot, customFilename),
|
||||||
return { isDirectory: () => true } as Stats;
|
'project context content',
|
||||||
}
|
);
|
||||||
throw new Error('File not found');
|
const cwdContextFile = await createTestFile(
|
||||||
});
|
path.join(cwd, customFilename),
|
||||||
|
'cwd context content',
|
||||||
|
);
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
const result = await loadServerHierarchicalMemory(
|
||||||
if (p === projectRootCustomFile || p === srcCustomFile) {
|
cwd,
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === projectRootCustomFile) {
|
|
||||||
return 'Project root custom memory';
|
|
||||||
}
|
|
||||||
if (p === srcCustomFile) {
|
|
||||||
return 'Src directory custom memory';
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
const expectedContent =
|
|
||||||
`--- Context from: ${path.relative(CWD, projectRootCustomFile)} ---\nProject root custom memory\n--- End of Context from: ${path.relative(CWD, projectRootCustomFile)} ---\n\n` +
|
|
||||||
`--- Context from: ${customFilename} ---\nSrc directory custom memory\n--- End of Context from: ${customFilename} ---`;
|
|
||||||
|
|
||||||
expect(memoryContent).toBe(expectedContent);
|
expect(result).toEqual({
|
||||||
expect(fileCount).toBe(2);
|
memoryContent: `--- Context from: ${path.relative(cwd, projectContextFile)} ---
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(
|
project context content
|
||||||
projectRootCustomFile,
|
--- End of Context from: ${path.relative(cwd, projectContextFile)} ---
|
||||||
'utf-8',
|
|
||||||
);
|
--- Context from: ${path.relative(cwd, cwdContextFile)} ---
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(srcCustomFile, 'utf-8');
|
cwd context content
|
||||||
|
--- End of Context from: ${path.relative(cwd, cwdContextFile)} ---`,
|
||||||
|
fileCount: 2,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load context files by downward traversal with custom filename', async () => {
|
it('should load context files by downward traversal with custom filename', async () => {
|
||||||
const customFilename = 'LOCAL_CONTEXT.md';
|
const customFilename = 'LOCAL_CONTEXT.md';
|
||||||
setGeminiMdFilename(customFilename);
|
setGeminiMdFilename(customFilename);
|
||||||
const subDir = path.join(CWD, 'subdir');
|
|
||||||
const subDirCustomFile = path.join(subDir, customFilename);
|
|
||||||
const cwdCustomFile = path.join(CWD, customFilename);
|
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
await createTestFile(
|
||||||
if (p === cwdCustomFile || p === subDirCustomFile) return undefined;
|
path.join(cwd, 'subdir', customFilename),
|
||||||
throw new Error('File not found');
|
'Subdir custom memory',
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === cwdCustomFile) return 'CWD custom memory';
|
|
||||||
if (p === subDirCustomFile) return 'Subdir custom memory';
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readdir.mockImplementation((async (
|
|
||||||
p: fsSync.PathLike,
|
|
||||||
): Promise<Dirent[]> => {
|
|
||||||
if (p === CWD) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: customFilename,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
{
|
|
||||||
name: 'subdir',
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
if (p === subDir) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: customFilename,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
return [] as Dirent[];
|
|
||||||
}) as unknown as typeof fsPromises.readdir);
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
|
||||||
fileService,
|
|
||||||
);
|
);
|
||||||
const expectedContent =
|
await createTestFile(path.join(cwd, customFilename), 'CWD custom memory');
|
||||||
`--- Context from: ${customFilename} ---\nCWD custom memory\n--- End of Context from: ${customFilename} ---\n\n` +
|
|
||||||
`--- Context from: ${path.join('subdir', customFilename)} ---\nSubdir custom memory\n--- End of Context from: ${path.join('subdir', customFilename)} ---`;
|
|
||||||
|
|
||||||
expect(memoryContent).toBe(expectedContent);
|
const result = await loadServerHierarchicalMemory(
|
||||||
expect(fileCount).toBe(2);
|
cwd,
|
||||||
|
false,
|
||||||
|
new FileDiscoveryService(projectRoot),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
memoryContent: `--- Context from: ${customFilename} ---
|
||||||
|
CWD custom memory
|
||||||
|
--- End of Context from: ${customFilename} ---
|
||||||
|
|
||||||
|
--- Context from: ${path.join('subdir', customFilename)} ---
|
||||||
|
Subdir custom memory
|
||||||
|
--- End of Context from: ${path.join('subdir', customFilename)} ---`,
|
||||||
|
fileCount: 2,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load ORIGINAL_GEMINI_MD_FILENAME files by upward traversal from CWD to project root', async () => {
|
it('should load ORIGINAL_GEMINI_MD_FILENAME files by upward traversal from CWD to project root', async () => {
|
||||||
const projectRootGeminiFile = path.join(
|
const projectRootGeminiFile = await createTestFile(
|
||||||
PROJECT_ROOT,
|
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
'Project root memory',
|
||||||
);
|
);
|
||||||
const srcGeminiFile = path.join(
|
const srcGeminiFile = await createTestFile(
|
||||||
CWD,
|
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
'Src directory memory',
|
||||||
);
|
);
|
||||||
|
|
||||||
mockFs.stat.mockImplementation(async (p) => {
|
const result = await loadServerHierarchicalMemory(
|
||||||
if (p === path.join(PROJECT_ROOT, '.git')) {
|
cwd,
|
||||||
return { isDirectory: () => true } as Stats;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
|
||||||
if (p === projectRootGeminiFile || p === srcGeminiFile) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === projectRootGeminiFile) {
|
|
||||||
return 'Project root memory';
|
|
||||||
}
|
|
||||||
if (p === srcGeminiFile) {
|
|
||||||
return 'Src directory memory';
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
const expectedContent =
|
|
||||||
`--- Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\nProject root memory\n--- End of Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\n\n` +
|
|
||||||
`--- Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\nSrc directory memory\n--- End of Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---`;
|
|
||||||
|
|
||||||
expect(memoryContent).toBe(expectedContent);
|
expect(result).toEqual({
|
||||||
expect(fileCount).toBe(2);
|
memoryContent: `--- Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(
|
Project root memory
|
||||||
projectRootGeminiFile,
|
--- End of Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||||
'utf-8',
|
|
||||||
);
|
--- Context from: ${path.relative(cwd, srcGeminiFile)} ---
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(srcGeminiFile, 'utf-8');
|
Src directory memory
|
||||||
|
--- End of Context from: ${path.relative(cwd, srcGeminiFile)} ---`,
|
||||||
|
fileCount: 2,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load ORIGINAL_GEMINI_MD_FILENAME files by downward traversal from CWD', async () => {
|
it('should load ORIGINAL_GEMINI_MD_FILENAME files by downward traversal from CWD', async () => {
|
||||||
const subDir = path.join(CWD, 'subdir');
|
await createTestFile(
|
||||||
const subDirGeminiFile = path.join(
|
path.join(cwd, 'subdir', DEFAULT_CONTEXT_FILENAME),
|
||||||
subDir,
|
'Subdir memory',
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
);
|
);
|
||||||
const cwdGeminiFile = path.join(
|
await createTestFile(
|
||||||
CWD,
|
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
'CWD memory',
|
||||||
);
|
);
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
const result = await loadServerHierarchicalMemory(
|
||||||
if (p === cwdGeminiFile || p === subDirGeminiFile) return undefined;
|
cwd,
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === cwdGeminiFile) return 'CWD memory';
|
|
||||||
if (p === subDirGeminiFile) return 'Subdir memory';
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readdir.mockImplementation((async (
|
|
||||||
p: fsSync.PathLike,
|
|
||||||
): Promise<Dirent[]> => {
|
|
||||||
if (p === CWD) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
{
|
|
||||||
name: 'subdir',
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
if (p === subDir) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
return [] as Dirent[];
|
|
||||||
}) as unknown as typeof fsPromises.readdir);
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
const expectedContent =
|
|
||||||
`--- Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\nCWD memory\n--- End of Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\n\n` +
|
|
||||||
`--- Context from: ${path.join('subdir', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---\nSubdir memory\n--- End of Context from: ${path.join('subdir', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---`;
|
|
||||||
|
|
||||||
expect(memoryContent).toBe(expectedContent);
|
expect(result).toEqual({
|
||||||
expect(fileCount).toBe(2);
|
memoryContent: `--- Context from: ${DEFAULT_CONTEXT_FILENAME} ---
|
||||||
|
CWD memory
|
||||||
|
--- End of Context from: ${DEFAULT_CONTEXT_FILENAME} ---
|
||||||
|
|
||||||
|
--- Context from: ${path.join('subdir', DEFAULT_CONTEXT_FILENAME)} ---
|
||||||
|
Subdir memory
|
||||||
|
--- End of Context from: ${path.join('subdir', DEFAULT_CONTEXT_FILENAME)} ---`,
|
||||||
|
fileCount: 2,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load and correctly order global, upward, and downward ORIGINAL_GEMINI_MD_FILENAME files', async () => {
|
it('should load and correctly order global, upward, and downward ORIGINAL_GEMINI_MD_FILENAME files', async () => {
|
||||||
setGeminiMdFilename(ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST); // Explicitly set for this test
|
const defaultContextFile = await createTestFile(
|
||||||
|
path.join(homedir, GEMINI_CONFIG_DIR, DEFAULT_CONTEXT_FILENAME),
|
||||||
const globalFileToUse = path.join(
|
'default context content',
|
||||||
GLOBAL_GEMINI_DIR,
|
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
);
|
);
|
||||||
const projectParentDir = path.dirname(PROJECT_ROOT);
|
const rootGeminiFile = await createTestFile(
|
||||||
const projectParentGeminiFile = path.join(
|
path.join(testRootDir, DEFAULT_CONTEXT_FILENAME),
|
||||||
projectParentDir,
|
'Project parent memory',
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
);
|
);
|
||||||
const projectRootGeminiFile = path.join(
|
const projectRootGeminiFile = await createTestFile(
|
||||||
PROJECT_ROOT,
|
path.join(projectRoot, DEFAULT_CONTEXT_FILENAME),
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
'Project root memory',
|
||||||
);
|
);
|
||||||
const cwdGeminiFile = path.join(
|
const cwdGeminiFile = await createTestFile(
|
||||||
CWD,
|
path.join(cwd, DEFAULT_CONTEXT_FILENAME),
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
'CWD memory',
|
||||||
);
|
);
|
||||||
const subDir = path.join(CWD, 'sub');
|
const subDirGeminiFile = await createTestFile(
|
||||||
const subDirGeminiFile = path.join(
|
path.join(cwd, 'sub', DEFAULT_CONTEXT_FILENAME),
|
||||||
subDir,
|
'Subdir memory',
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
mockFs.stat.mockImplementation(async (p) => {
|
const result = await loadServerHierarchicalMemory(
|
||||||
if (p === path.join(PROJECT_ROOT, '.git')) {
|
cwd,
|
||||||
return { isDirectory: () => true } as Stats;
|
|
||||||
} else if (p === path.join(PROJECT_ROOT, '.gemini')) {
|
|
||||||
return { isDirectory: () => true } as Stats;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
|
||||||
if (
|
|
||||||
p === globalFileToUse || // Use the dynamically set global file path
|
|
||||||
p === projectParentGeminiFile ||
|
|
||||||
p === projectRootGeminiFile ||
|
|
||||||
p === cwdGeminiFile ||
|
|
||||||
p === subDirGeminiFile
|
|
||||||
) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === globalFileToUse) return 'Global memory'; // Use the dynamically set global file path
|
|
||||||
if (p === projectParentGeminiFile) return 'Project parent memory';
|
|
||||||
if (p === projectRootGeminiFile) return 'Project root memory';
|
|
||||||
if (p === cwdGeminiFile) return 'CWD memory';
|
|
||||||
if (p === subDirGeminiFile) return 'Subdir memory';
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readdir.mockImplementation((async (
|
|
||||||
p: fsSync.PathLike,
|
|
||||||
): Promise<Dirent[]> => {
|
|
||||||
if (p === CWD) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: 'sub',
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
if (p === subDir) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
return [] as Dirent[];
|
|
||||||
}) as unknown as typeof fsPromises.readdir);
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
);
|
);
|
||||||
|
|
||||||
const relPathGlobal = path.relative(CWD, GLOBAL_GEMINI_FILE);
|
expect(result).toEqual({
|
||||||
const relPathProjectParent = path.relative(CWD, projectParentGeminiFile);
|
memoryContent: `--- Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||||
const relPathProjectRoot = path.relative(CWD, projectRootGeminiFile);
|
default context content
|
||||||
const relPathCwd = ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST;
|
--- End of Context from: ${path.relative(cwd, defaultContextFile)} ---
|
||||||
const relPathSubDir = path.join(
|
|
||||||
'sub',
|
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
);
|
|
||||||
|
|
||||||
const expectedContent = [
|
--- Context from: ${path.relative(cwd, rootGeminiFile)} ---
|
||||||
`--- Context from: ${relPathGlobal} ---\nGlobal memory\n--- End of Context from: ${relPathGlobal} ---`,
|
Project parent memory
|
||||||
`--- Context from: ${relPathProjectParent} ---\nProject parent memory\n--- End of Context from: ${relPathProjectParent} ---`,
|
--- End of Context from: ${path.relative(cwd, rootGeminiFile)} ---
|
||||||
`--- Context from: ${relPathProjectRoot} ---\nProject root memory\n--- End of Context from: ${relPathProjectRoot} ---`,
|
|
||||||
`--- Context from: ${relPathCwd} ---\nCWD memory\n--- End of Context from: ${relPathCwd} ---`,
|
|
||||||
`--- Context from: ${relPathSubDir} ---\nSubdir memory\n--- End of Context from: ${relPathSubDir} ---`,
|
|
||||||
].join('\n\n');
|
|
||||||
|
|
||||||
expect(memoryContent).toBe(expectedContent);
|
--- Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||||
expect(fileCount).toBe(5);
|
Project root memory
|
||||||
|
--- End of Context from: ${path.relative(cwd, projectRootGeminiFile)} ---
|
||||||
|
|
||||||
|
--- Context from: ${path.relative(cwd, cwdGeminiFile)} ---
|
||||||
|
CWD memory
|
||||||
|
--- End of Context from: ${path.relative(cwd, cwdGeminiFile)} ---
|
||||||
|
|
||||||
|
--- Context from: ${path.relative(cwd, subDirGeminiFile)} ---
|
||||||
|
Subdir memory
|
||||||
|
--- End of Context from: ${path.relative(cwd, subDirGeminiFile)} ---`,
|
||||||
|
fileCount: 5,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ignore specified directories during downward scan', async () => {
|
it('should ignore specified directories during downward scan', async () => {
|
||||||
const ignoredDir = path.join(CWD, 'node_modules');
|
await createEmptyDir(path.join(projectRoot, '.git'));
|
||||||
const ignoredDirGeminiFile = path.join(
|
await createTestFile(path.join(projectRoot, '.gitignore'), 'node_modules');
|
||||||
ignoredDir,
|
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
await createTestFile(
|
||||||
); // Corrected
|
path.join(cwd, 'node_modules', DEFAULT_CONTEXT_FILENAME),
|
||||||
const regularSubDir = path.join(CWD, 'my_code');
|
'Ignored memory',
|
||||||
const regularSubDirGeminiFile = path.join(
|
);
|
||||||
regularSubDir,
|
const regularSubDirGeminiFile = await createTestFile(
|
||||||
ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
path.join(cwd, 'my_code', DEFAULT_CONTEXT_FILENAME),
|
||||||
|
'My code memory',
|
||||||
);
|
);
|
||||||
|
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
const result = await loadServerHierarchicalMemory(
|
||||||
if (p === regularSubDirGeminiFile) return undefined;
|
cwd,
|
||||||
if (p === ignoredDirGeminiFile)
|
|
||||||
throw new Error('Should not access ignored file');
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === regularSubDirGeminiFile) return 'My code memory';
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
mockFs.readdir.mockImplementation((async (
|
|
||||||
p: fsSync.PathLike,
|
|
||||||
): Promise<Dirent[]> => {
|
|
||||||
if (p === CWD) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: 'node_modules',
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent,
|
|
||||||
{
|
|
||||||
name: 'my_code',
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
if (p === regularSubDir) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
name: ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST,
|
|
||||||
isFile: () => true,
|
|
||||||
isDirectory: () => false,
|
|
||||||
} as Dirent,
|
|
||||||
] as Dirent[];
|
|
||||||
}
|
|
||||||
if (p === ignoredDir) {
|
|
||||||
return [] as Dirent[];
|
|
||||||
}
|
|
||||||
return [] as Dirent[];
|
|
||||||
}) as unknown as typeof fsPromises.readdir);
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
|
||||||
CWD,
|
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
|
[],
|
||||||
|
{
|
||||||
|
respectGitIgnore: true,
|
||||||
|
respectGeminiIgnore: true,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const expectedContent = `--- Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---\nMy code memory\n--- End of Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---`;
|
expect(result).toEqual({
|
||||||
|
memoryContent: `--- Context from: ${path.relative(cwd, regularSubDirGeminiFile)} ---
|
||||||
expect(memoryContent).toBe(expectedContent);
|
My code memory
|
||||||
expect(fileCount).toBe(1);
|
--- End of Context from: ${path.relative(cwd, regularSubDirGeminiFile)} ---`,
|
||||||
expect(mockFs.readFile).not.toHaveBeenCalledWith(
|
fileCount: 1,
|
||||||
ignoredDirGeminiFile,
|
});
|
||||||
'utf-8',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY during downward scan', async () => {
|
it('should respect MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY during downward scan', async () => {
|
||||||
const consoleDebugSpy = vi
|
// the max depth is 200 so it will give up before searching all these.
|
||||||
.spyOn(console, 'debug')
|
|
||||||
.mockImplementation(() => {});
|
|
||||||
|
|
||||||
const dirNames: Dirent[] = [];
|
|
||||||
for (let i = 0; i < 250; i++) {
|
for (let i = 0; i < 250; i++) {
|
||||||
dirNames.push({
|
await createEmptyDir(path.join(cwd, `deep_dir_${i}`));
|
||||||
name: `deep_dir_${i}`,
|
|
||||||
isFile: () => false,
|
|
||||||
isDirectory: () => true,
|
|
||||||
} as Dirent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mockFs.readdir.mockImplementation((async (
|
// "much_deeper" is alphabetically after "deep_dir_*" so it won't be loaded
|
||||||
p: fsSync.PathLike,
|
await createTestFile(
|
||||||
): Promise<Dirent[]> => {
|
path.join(cwd, 'much_deeper', DEFAULT_CONTEXT_FILENAME),
|
||||||
if (p === CWD) return dirNames;
|
'Ignored memory',
|
||||||
if (p.toString().startsWith(path.join(CWD, 'deep_dir_')))
|
|
||||||
return [] as Dirent[];
|
|
||||||
return [] as Dirent[];
|
|
||||||
}) as unknown as typeof fsPromises.readdir);
|
|
||||||
mockFs.access.mockRejectedValue(new Error('not found'));
|
|
||||||
|
|
||||||
await loadServerHierarchicalMemory(CWD, true, fileService);
|
|
||||||
|
|
||||||
expect(consoleDebugSpy).toHaveBeenCalledWith(
|
|
||||||
expect.stringContaining('[DEBUG] [BfsFileSearch]'),
|
|
||||||
expect.stringContaining('Scanning [200/200]:'),
|
|
||||||
);
|
);
|
||||||
consoleDebugSpy.mockRestore();
|
|
||||||
|
const result = await loadServerHierarchicalMemory(
|
||||||
|
cwd,
|
||||||
|
false,
|
||||||
|
new FileDiscoveryService(projectRoot),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
memoryContent: '',
|
||||||
|
fileCount: 0,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load extension context file paths', async () => {
|
it('should load extension context file paths', async () => {
|
||||||
const extensionFilePath = '/test/extensions/ext1/GEMINI.md';
|
const extensionFilePath = await createTestFile(
|
||||||
mockFs.access.mockImplementation(async (p) => {
|
path.join(testRootDir, 'extensions/ext1/GEMINI.md'),
|
||||||
if (p === extensionFilePath) {
|
'Extension memory content',
|
||||||
return undefined;
|
);
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
mockFs.readFile.mockImplementation(async (p) => {
|
|
||||||
if (p === extensionFilePath) {
|
|
||||||
return 'Extension memory content';
|
|
||||||
}
|
|
||||||
throw new Error('File not found');
|
|
||||||
});
|
|
||||||
|
|
||||||
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
|
const result = await loadServerHierarchicalMemory(
|
||||||
CWD,
|
cwd,
|
||||||
false,
|
false,
|
||||||
fileService,
|
new FileDiscoveryService(projectRoot),
|
||||||
[extensionFilePath],
|
[extensionFilePath],
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(memoryContent).toBe(
|
expect(result).toEqual({
|
||||||
`--- Context from: ${path.relative(CWD, extensionFilePath)} ---\nExtension memory content\n--- End of Context from: ${path.relative(CWD, extensionFilePath)} ---`,
|
memoryContent: `--- Context from: ${path.relative(cwd, extensionFilePath)} ---
|
||||||
);
|
Extension memory content
|
||||||
expect(fileCount).toBe(1);
|
--- End of Context from: ${path.relative(cwd, extensionFilePath)} ---`,
|
||||||
expect(mockFs.readFile).toHaveBeenCalledWith(extensionFilePath, 'utf-8');
|
fileCount: 1,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue