|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { MemoryManagerAgent } from './memory-manager-agent.js'; |
| 9 | +import { |
| 10 | + ASK_USER_TOOL_NAME, |
| 11 | + EDIT_TOOL_NAME, |
| 12 | + GLOB_TOOL_NAME, |
| 13 | + GREP_TOOL_NAME, |
| 14 | + LS_TOOL_NAME, |
| 15 | + READ_FILE_TOOL_NAME, |
| 16 | + WRITE_FILE_TOOL_NAME, |
| 17 | +} from '../tools/tool-names.js'; |
| 18 | +import { Storage } from '../config/storage.js'; |
| 19 | +import type { Config } from '../config/config.js'; |
| 20 | +import type { HierarchicalMemory } from '../config/memory.js'; |
| 21 | + |
| 22 | +function createMockConfig(memory: string | HierarchicalMemory = ''): Config { |
| 23 | + return { |
| 24 | + getUserMemory: vi.fn().mockReturnValue(memory), |
| 25 | + } as unknown as Config; |
| 26 | +} |
| 27 | + |
| 28 | +describe('MemoryManagerAgent', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + vi.restoreAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should have the correct name "save_memory"', () => { |
| 38 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 39 | + expect(agent.name).toBe('save_memory'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should be a local agent', () => { |
| 43 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 44 | + expect(agent.kind).toBe('local'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should have a description', () => { |
| 48 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 49 | + expect(agent.description).toBeTruthy(); |
| 50 | + expect(agent.description).toContain('memory'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should have a system prompt with memory management instructions', () => { |
| 54 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 55 | + const prompt = agent.promptConfig.systemPrompt; |
| 56 | + const globalGeminiDir = Storage.getGlobalGeminiDir(); |
| 57 | + expect(prompt).toContain(`Global (${globalGeminiDir}`); |
| 58 | + expect(prompt).toContain('Project (./'); |
| 59 | + expect(prompt).toContain('Memory Hierarchy'); |
| 60 | + expect(prompt).toContain('De-duplicating'); |
| 61 | + expect(prompt).toContain('Adding'); |
| 62 | + expect(prompt).toContain('Removing stale entries'); |
| 63 | + expect(prompt).toContain('Organizing'); |
| 64 | + expect(prompt).toContain('Routing'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should have efficiency guidelines in the system prompt', () => { |
| 68 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 69 | + const prompt = agent.promptConfig.systemPrompt; |
| 70 | + expect(prompt).toContain('Efficiency & Performance'); |
| 71 | + expect(prompt).toContain('Use as few turns as possible'); |
| 72 | + expect(prompt).toContain('Do not perform any exploration'); |
| 73 | + expect(prompt).toContain('Be strategic with your thinking'); |
| 74 | + expect(prompt).toContain('Context Awareness'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should inject hierarchical memory into initial context', () => { |
| 78 | + const config = createMockConfig({ |
| 79 | + global: |
| 80 | + '--- Context from: ../../.gemini/GEMINI.md ---\nglobal context\n--- End of Context from: ../../.gemini/GEMINI.md ---', |
| 81 | + project: |
| 82 | + '--- Context from: .gemini/GEMINI.md ---\nproject context\n--- End of Context from: .gemini/GEMINI.md ---', |
| 83 | + }); |
| 84 | + |
| 85 | + const agent = MemoryManagerAgent(config); |
| 86 | + const query = agent.promptConfig.query; |
| 87 | + |
| 88 | + expect(query).toContain('# Initial Context'); |
| 89 | + expect(query).toContain('global context'); |
| 90 | + expect(query).toContain('project context'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should inject flat string memory into initial context', () => { |
| 94 | + const config = createMockConfig('flat memory content'); |
| 95 | + |
| 96 | + const agent = MemoryManagerAgent(config); |
| 97 | + const query = agent.promptConfig.query; |
| 98 | + |
| 99 | + expect(query).toContain('# Initial Context'); |
| 100 | + expect(query).toContain('flat memory content'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should exclude extension memory from initial context', () => { |
| 104 | + const config = createMockConfig({ |
| 105 | + global: 'global context', |
| 106 | + extension: 'extension context that should be excluded', |
| 107 | + project: 'project context', |
| 108 | + }); |
| 109 | + |
| 110 | + const agent = MemoryManagerAgent(config); |
| 111 | + const query = agent.promptConfig.query; |
| 112 | + |
| 113 | + expect(query).toContain('global context'); |
| 114 | + expect(query).toContain('project context'); |
| 115 | + expect(query).not.toContain('extension context'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not include initial context when memory is empty', () => { |
| 119 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 120 | + const query = agent.promptConfig.query; |
| 121 | + |
| 122 | + expect(query).not.toContain('# Initial Context'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should have file-management and search tools', () => { |
| 126 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 127 | + expect(agent.toolConfig).toBeDefined(); |
| 128 | + expect(agent.toolConfig!.tools).toEqual( |
| 129 | + expect.arrayContaining([ |
| 130 | + READ_FILE_TOOL_NAME, |
| 131 | + EDIT_TOOL_NAME, |
| 132 | + WRITE_FILE_TOOL_NAME, |
| 133 | + LS_TOOL_NAME, |
| 134 | + GLOB_TOOL_NAME, |
| 135 | + GREP_TOOL_NAME, |
| 136 | + ASK_USER_TOOL_NAME, |
| 137 | + ]), |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should require a "request" input parameter', () => { |
| 142 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 143 | + const schema = agent.inputConfig.inputSchema as Record<string, unknown>; |
| 144 | + expect(schema).toBeDefined(); |
| 145 | + expect(schema['properties']).toHaveProperty('request'); |
| 146 | + expect(schema['required']).toContain('request'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should use a fast model', () => { |
| 150 | + const agent = MemoryManagerAgent(createMockConfig()); |
| 151 | + expect(agent.modelConfig.model).toBe('flash'); |
| 152 | + }); |
| 153 | +}); |
0 commit comments