diff options
Diffstat (limited to 'packages/core/src/tools/memoryTool.test.ts')
| -rw-r--r-- | packages/core/src/tools/memoryTool.test.ts | 76 |
1 files changed, 30 insertions, 46 deletions
diff --git a/packages/core/src/tools/memoryTool.test.ts b/packages/core/src/tools/memoryTool.test.ts index 2a5c4c39..0e382325 100644 --- a/packages/core/src/tools/memoryTool.test.ts +++ b/packages/core/src/tools/memoryTool.test.ts @@ -218,7 +218,8 @@ describe('MemoryTool', () => { it('should call performAddMemoryEntry with correct parameters and return success', async () => { const params = { fact: 'The sky is blue' }; - const result = await memoryTool.execute(params, mockAbortSignal); + const invocation = memoryTool.build(params); + const result = await invocation.execute(mockAbortSignal); // Use getCurrentGeminiMdFilename for the default expectation before any setGeminiMdFilename calls in a test const expectedFilePath = path.join( os.homedir(), @@ -247,14 +248,12 @@ describe('MemoryTool', () => { it('should return an error if fact is empty', async () => { const params = { fact: ' ' }; // Empty fact - const result = await memoryTool.execute(params, mockAbortSignal); - const errorMessage = 'Parameter "fact" must be a non-empty string.'; - - expect(performAddMemoryEntrySpy).not.toHaveBeenCalled(); - expect(result.llmContent).toBe( - JSON.stringify({ success: false, error: errorMessage }), + expect(memoryTool.validateToolParams(params)).toBe( + 'Parameter "fact" must be a non-empty string.', + ); + expect(() => memoryTool.build(params)).toThrow( + 'Parameter "fact" must be a non-empty string.', ); - expect(result.returnDisplay).toBe(`Error: ${errorMessage}`); }); it('should handle errors from performAddMemoryEntry', async () => { @@ -264,7 +263,8 @@ describe('MemoryTool', () => { ); performAddMemoryEntrySpy.mockRejectedValue(underlyingError); - const result = await memoryTool.execute(params, mockAbortSignal); + const invocation = memoryTool.build(params); + const result = await invocation.execute(mockAbortSignal); expect(result.llmContent).toBe( JSON.stringify({ @@ -284,17 +284,17 @@ describe('MemoryTool', () => { beforeEach(() => { memoryTool = new MemoryTool(); // Clear the allowlist before each test - (MemoryTool as unknown as { allowlist: Set<string> }).allowlist.clear(); + const invocation = memoryTool.build({ fact: 'mock-fact' }); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (invocation.constructor as any).allowlist.clear(); // Mock fs.readFile to return empty string (file doesn't exist) vi.mocked(fs.readFile).mockResolvedValue(''); }); it('should return confirmation details when memory file is not allowlisted', async () => { const params = { fact: 'Test fact' }; - const result = await memoryTool.shouldConfirmExecute( - params, - mockAbortSignal, - ); + const invocation = memoryTool.build(params); + const result = await invocation.shouldConfirmExecute(mockAbortSignal); expect(result).toBeDefined(); expect(result).not.toBe(false); @@ -321,15 +321,12 @@ describe('MemoryTool', () => { getCurrentGeminiMdFilename(), ); + const invocation = memoryTool.build(params); // Add the memory file to the allowlist - (MemoryTool as unknown as { allowlist: Set<string> }).allowlist.add( - memoryFilePath, - ); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (invocation.constructor as any).allowlist.add(memoryFilePath); - const result = await memoryTool.shouldConfirmExecute( - params, - mockAbortSignal, - ); + const result = await invocation.shouldConfirmExecute(mockAbortSignal); expect(result).toBe(false); }); @@ -342,10 +339,8 @@ describe('MemoryTool', () => { getCurrentGeminiMdFilename(), ); - const result = await memoryTool.shouldConfirmExecute( - params, - mockAbortSignal, - ); + const invocation = memoryTool.build(params); + const result = await invocation.shouldConfirmExecute(mockAbortSignal); expect(result).toBeDefined(); expect(result).not.toBe(false); @@ -356,9 +351,8 @@ describe('MemoryTool', () => { // Check that the memory file was added to the allowlist expect( - (MemoryTool as unknown as { allowlist: Set<string> }).allowlist.has( - memoryFilePath, - ), + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (invocation.constructor as any).allowlist.has(memoryFilePath), ).toBe(true); } }); @@ -371,10 +365,8 @@ describe('MemoryTool', () => { getCurrentGeminiMdFilename(), ); - const result = await memoryTool.shouldConfirmExecute( - params, - mockAbortSignal, - ); + const invocation = memoryTool.build(params); + const result = await invocation.shouldConfirmExecute(mockAbortSignal); expect(result).toBeDefined(); expect(result).not.toBe(false); @@ -382,18 +374,12 @@ describe('MemoryTool', () => { if (result && result.type === 'edit') { // Simulate the onConfirm callback with different outcomes await result.onConfirm(ToolConfirmationOutcome.ProceedOnce); - expect( - (MemoryTool as unknown as { allowlist: Set<string> }).allowlist.has( - memoryFilePath, - ), - ).toBe(false); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const allowlist = (invocation.constructor as any).allowlist; + expect(allowlist.has(memoryFilePath)).toBe(false); await result.onConfirm(ToolConfirmationOutcome.Cancel); - expect( - (MemoryTool as unknown as { allowlist: Set<string> }).allowlist.has( - memoryFilePath, - ), - ).toBe(false); + expect(allowlist.has(memoryFilePath)).toBe(false); } }); @@ -405,10 +391,8 @@ describe('MemoryTool', () => { // Mock fs.readFile to return existing content vi.mocked(fs.readFile).mockResolvedValue(existingContent); - const result = await memoryTool.shouldConfirmExecute( - params, - mockAbortSignal, - ); + const invocation = memoryTool.build(params); + const result = await invocation.shouldConfirmExecute(mockAbortSignal); expect(result).toBeDefined(); expect(result).not.toBe(false); |
