mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 17:27:54 +00:00
Refactor(cli): Move memory add logic to server tool call (#493)
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeEach,
|
||||
// afterEach, // Removed as it's not used
|
||||
type Mocked,
|
||||
type Mock,
|
||||
} from 'vitest';
|
||||
import * as path from 'path';
|
||||
import { homedir } from 'os';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getGlobalMemoryFilePath, addMemoryEntry } from './memoryUtils.js';
|
||||
import { SETTINGS_DIRECTORY_NAME } from './settings.js';
|
||||
import {
|
||||
MemoryTool,
|
||||
GEMINI_MD_FILENAME,
|
||||
// MEMORY_SECTION_HEADER, // Removed as it's not used
|
||||
// getErrorMessage, // Removed as it's not used
|
||||
} from '@gemini-code/server';
|
||||
|
||||
// Mock the entire fs/promises module
|
||||
vi.mock('fs/promises');
|
||||
// Mock MemoryTool static method
|
||||
vi.mock('@gemini-code/server', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@gemini-code/server')>();
|
||||
return {
|
||||
...actual,
|
||||
MemoryTool: {
|
||||
...actual.MemoryTool,
|
||||
performAddMemoryEntry: vi.fn(),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe('memoryUtils', () => {
|
||||
beforeEach(() => {
|
||||
// Reset mocks before each test
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('getGlobalMemoryFilePath', () => {
|
||||
it('should return the correct global memory file path', () => {
|
||||
const expectedPath = path.join(
|
||||
homedir(),
|
||||
SETTINGS_DIRECTORY_NAME,
|
||||
GEMINI_MD_FILENAME,
|
||||
);
|
||||
expect(getGlobalMemoryFilePath()).toBe(expectedPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addMemoryEntry', () => {
|
||||
const mockFs = fs as Mocked<typeof fs>; // Type cast for mocked fs
|
||||
const mockPerformAddMemoryEntry = MemoryTool.performAddMemoryEntry as Mock;
|
||||
|
||||
it('should call MemoryTool.performAddMemoryEntry with correct parameters', async () => {
|
||||
const testText = 'Remember this important fact.';
|
||||
const expectedFilePath = getGlobalMemoryFilePath();
|
||||
|
||||
await addMemoryEntry(testText);
|
||||
|
||||
expect(mockPerformAddMemoryEntry).toHaveBeenCalledOnce();
|
||||
expect(mockPerformAddMemoryEntry).toHaveBeenCalledWith(
|
||||
testText,
|
||||
expectedFilePath,
|
||||
{
|
||||
readFile: mockFs.readFile,
|
||||
writeFile: mockFs.writeFile,
|
||||
mkdir: mockFs.mkdir,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should propagate errors from MemoryTool.performAddMemoryEntry', async () => {
|
||||
const testText = 'This will fail.';
|
||||
const expectedError = new Error('Failed to add memory entry');
|
||||
mockPerformAddMemoryEntry.mockRejectedValueOnce(expectedError);
|
||||
|
||||
await expect(addMemoryEntry(testText)).rejects.toThrow(expectedError);
|
||||
});
|
||||
});
|
||||
|
||||
// More tests will be added here
|
||||
});
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { SETTINGS_DIRECTORY_NAME } from './settings.js';
|
||||
import {
|
||||
// getErrorMessage, // Removed as it's not used
|
||||
MemoryTool,
|
||||
GEMINI_MD_FILENAME,
|
||||
// MEMORY_SECTION_HEADER, // Removed as it's not used
|
||||
} from '@gemini-code/server';
|
||||
|
||||
/**
|
||||
* Gets the absolute path to the global GEMINI.md file.
|
||||
*/
|
||||
export function getGlobalMemoryFilePath(): string {
|
||||
return path.join(homedir(), SETTINGS_DIRECTORY_NAME, GEMINI_MD_FILENAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new memory entry to the global GEMINI.md file under the specified header.
|
||||
*/
|
||||
export async function addMemoryEntry(text: string): Promise<void> {
|
||||
const filePath = getGlobalMemoryFilePath();
|
||||
// The performAddMemoryEntry method from MemoryTool will handle its own errors
|
||||
// and throw an appropriately formatted error if needed.
|
||||
await MemoryTool.performAddMemoryEntry(text, filePath, {
|
||||
readFile: fs.readFile,
|
||||
writeFile: fs.writeFile,
|
||||
mkdir: fs.mkdir,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user