From 8a1501759350eb0685f41b0ea3aa7415c59064b5 Mon Sep 17 00:00:00 2001 From: liqoingyu Date: Wed, 7 Jan 2026 20:07:41 +0800 Subject: [PATCH 1/2] fix(cli): /memory show respects context.fileName --- .../cli/src/ui/commands/memoryCommand.test.ts | 68 +++++++++++++++++++ packages/cli/src/ui/commands/memoryCommand.ts | 14 ++-- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/ui/commands/memoryCommand.test.ts b/packages/cli/src/ui/commands/memoryCommand.test.ts index 233c861b1..6e50136fd 100644 --- a/packages/cli/src/ui/commands/memoryCommand.test.ts +++ b/packages/cli/src/ui/commands/memoryCommand.test.ts @@ -11,9 +11,12 @@ import type { SlashCommand, type CommandContext } from './types.js'; import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; import { MessageType } from '../types.js'; import type { LoadedSettings } from '../../config/settings.js'; +import { readFile } from 'node:fs/promises'; +import os from 'node:os'; import { getErrorMessage, loadServerHierarchicalMemory, + setGeminiMdFilename, type FileDiscoveryService, type LoadServerHierarchicalMemoryResponse, } from '@qwen-code/qwen-code-core'; @@ -31,7 +34,18 @@ vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => { }; }); +vi.mock('node:fs/promises', () => { + const readFile = vi.fn(); + return { + readFile, + default: { + readFile, + }, + }; +}); + const mockLoadServerHierarchicalMemory = loadServerHierarchicalMemory as Mock; +const mockReadFile = readFile as unknown as Mock; describe('memoryCommand', () => { let mockContext: CommandContext; @@ -52,6 +66,10 @@ describe('memoryCommand', () => { let mockGetGeminiMdFileCount: Mock; beforeEach(() => { + setGeminiMdFilename('QWEN.md'); + mockReadFile.mockReset(); + vi.restoreAllMocks(); + showCommand = getSubCommand('show'); mockGetUserMemory = vi.fn(); @@ -102,6 +120,56 @@ describe('memoryCommand', () => { expect.any(Number), ); }); + + it('should show project memory from the configured context file', async () => { + const projectCommand = showCommand.subCommands?.find( + (cmd) => cmd.name === '--project', + ); + if (!projectCommand?.action) throw new Error('Command has no action'); + + setGeminiMdFilename('AGENTS.md'); + vi.spyOn(process, 'cwd').mockReturnValue('/test/project'); + mockReadFile.mockResolvedValue('project memory'); + + await projectCommand.action(mockContext, ''); + + expect(mockReadFile).toHaveBeenCalledWith( + '/test/project/AGENTS.md', + 'utf-8', + ); + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: MessageType.INFO, + text: expect.stringContaining('/test/project/AGENTS.md'), + }, + expect.any(Number), + ); + }); + + it('should show global memory from the configured context file', async () => { + const globalCommand = showCommand.subCommands?.find( + (cmd) => cmd.name === '--global', + ); + if (!globalCommand?.action) throw new Error('Command has no action'); + + setGeminiMdFilename('AGENTS.md'); + vi.spyOn(os, 'homedir').mockReturnValue('/home/user'); + mockReadFile.mockResolvedValue('global memory'); + + await globalCommand.action(mockContext, ''); + + expect(mockReadFile).toHaveBeenCalledWith( + '/home/user/.qwen/AGENTS.md', + 'utf-8', + ); + expect(mockContext.ui.addItem).toHaveBeenCalledWith( + { + type: MessageType.INFO, + text: expect.stringContaining('Global memory content'), + }, + expect.any(Number), + ); + }); }); describe('/memory add', () => { diff --git a/packages/cli/src/ui/commands/memoryCommand.ts b/packages/cli/src/ui/commands/memoryCommand.ts index 013b815d0..05641178c 100644 --- a/packages/cli/src/ui/commands/memoryCommand.ts +++ b/packages/cli/src/ui/commands/memoryCommand.ts @@ -6,12 +6,13 @@ import { getErrorMessage, + getCurrentGeminiMdFilename, loadServerHierarchicalMemory, QWEN_DIR, } from '@qwen-code/qwen-code-core'; import path from 'node:path'; -import os from 'os'; -import fs from 'fs/promises'; +import os from 'node:os'; +import fs from 'node:fs/promises'; import { MessageType } from '../types.js'; import type { SlashCommand, SlashCommandActionReturn } from './types.js'; import { CommandKind } from './types.js'; @@ -56,7 +57,12 @@ export const memoryCommand: SlashCommand = { kind: CommandKind.BUILT_IN, action: async (context) => { try { - const projectMemoryPath = path.join(process.cwd(), 'QWEN.md'); + const workingDir = + context.services.config?.getWorkingDir?.() ?? process.cwd(); + const projectMemoryPath = path.join( + workingDir, + getCurrentGeminiMdFilename(), + ); const memoryContent = await fs.readFile( projectMemoryPath, 'utf-8', @@ -104,7 +110,7 @@ export const memoryCommand: SlashCommand = { const globalMemoryPath = path.join( os.homedir(), QWEN_DIR, - 'QWEN.md', + getCurrentGeminiMdFilename(), ); const globalMemoryContent = await fs.readFile( globalMemoryPath, From 0a0ab64da0e138d3672d3fc071721a65a15b5e3e Mon Sep 17 00:00:00 2001 From: liqoingyu Date: Wed, 7 Jan 2026 20:28:28 +0800 Subject: [PATCH 2/2] test(cli): make memoryCommand path assertions cross-platform --- .../cli/src/ui/commands/memoryCommand.test.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/ui/commands/memoryCommand.test.ts b/packages/cli/src/ui/commands/memoryCommand.test.ts index 6e50136fd..7e20bf11c 100644 --- a/packages/cli/src/ui/commands/memoryCommand.test.ts +++ b/packages/cli/src/ui/commands/memoryCommand.test.ts @@ -13,9 +13,11 @@ import { MessageType } from '../types.js'; import type { LoadedSettings } from '../../config/settings.js'; import { readFile } from 'node:fs/promises'; import os from 'node:os'; +import path from 'node:path'; import { getErrorMessage, loadServerHierarchicalMemory, + QWEN_DIR, setGeminiMdFilename, type FileDiscoveryService, type LoadServerHierarchicalMemoryResponse, @@ -133,14 +135,12 @@ describe('memoryCommand', () => { await projectCommand.action(mockContext, ''); - expect(mockReadFile).toHaveBeenCalledWith( - '/test/project/AGENTS.md', - 'utf-8', - ); + const expectedProjectPath = path.join('/test/project', 'AGENTS.md'); + expect(mockReadFile).toHaveBeenCalledWith(expectedProjectPath, 'utf-8'); expect(mockContext.ui.addItem).toHaveBeenCalledWith( { type: MessageType.INFO, - text: expect.stringContaining('/test/project/AGENTS.md'), + text: expect.stringContaining(expectedProjectPath), }, expect.any(Number), ); @@ -158,10 +158,8 @@ describe('memoryCommand', () => { await globalCommand.action(mockContext, ''); - expect(mockReadFile).toHaveBeenCalledWith( - '/home/user/.qwen/AGENTS.md', - 'utf-8', - ); + const expectedGlobalPath = path.join('/home/user', QWEN_DIR, 'AGENTS.md'); + expect(mockReadFile).toHaveBeenCalledWith(expectedGlobalPath, 'utf-8'); expect(mockContext.ui.addItem).toHaveBeenCalledWith( { type: MessageType.INFO,