@@ -3,9 +3,10 @@ import { mkdtemp, readFile } from 'fs/promises';
33import { tmpdir } from 'os' ;
44import { join } from 'path' ;
55
6- import { describe , it , expect , beforeEach , afterEach } from 'vitest' ;
6+ import { describe , it , expect , beforeEach , afterEach , vi } from 'vitest' ;
77
88import { ToolContext } from '../../core/types.js' ;
9+ import { MockLogger } from '../../utils/mockLogger.js' ;
910import { getMockToolContext } from '../getTools.test.js' ;
1011import { shellExecuteTool } from '../system/shellExecute.js' ;
1112
@@ -384,4 +385,63 @@ describe('textEditor', () => {
384385 content = await readFile ( testPath , 'utf8' ) ;
385386 expect ( content ) . toBe ( initialContent ) ;
386387 } ) ;
388+
389+ it ( 'should convert absolute paths to relative paths in log messages' , ( ) => {
390+ // Create a mock logger with a spy on the info method
391+ const mockLogger = new MockLogger ( ) ;
392+ const infoSpy = vi . spyOn ( mockLogger , 'info' ) ;
393+
394+ // Create a context with a specific working directory
395+ const contextWithWorkingDir : ToolContext = {
396+ ...toolContext ,
397+ logger : mockLogger ,
398+ workingDirectory : '/home/user/project' ,
399+ } ;
400+
401+ // Test with an absolute path within the working directory
402+ const absolutePath = '/home/user/project/packages/agent/src/file.ts' ;
403+ textEditorTool . logParameters ?.(
404+ {
405+ command : 'view' ,
406+ path : absolutePath ,
407+ description : 'test path conversion' ,
408+ } ,
409+ contextWithWorkingDir ,
410+ ) ;
411+
412+ // Verify the log message contains the relative path
413+ expect ( infoSpy ) . toHaveBeenCalledWith (
414+ expect . stringContaining ( './packages/agent/src/file.ts' ) ,
415+ ) ;
416+
417+ // Test with an absolute path outside the working directory
418+ infoSpy . mockClear ( ) ;
419+ const externalPath = '/etc/config.json' ;
420+ textEditorTool . logParameters ?.(
421+ {
422+ command : 'view' ,
423+ path : externalPath ,
424+ description : 'test external path' ,
425+ } ,
426+ contextWithWorkingDir ,
427+ ) ;
428+
429+ // Verify the log message keeps the absolute path
430+ expect ( infoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( externalPath ) ) ;
431+
432+ // Test with a relative path
433+ infoSpy . mockClear ( ) ;
434+ const relativePath = 'src/file.ts' ;
435+ textEditorTool . logParameters ?.(
436+ {
437+ command : 'view' ,
438+ path : relativePath ,
439+ description : 'test relative path' ,
440+ } ,
441+ contextWithWorkingDir ,
442+ ) ;
443+
444+ // Verify the log message keeps the relative path as is
445+ expect ( infoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( relativePath ) ) ;
446+ } ) ;
387447} ) ;
0 commit comments