|
1 |
| -import { describe, expect, it } from 'vitest'; |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
2 | 2 |
|
3 |
| -// Skip testing for now |
4 |
| -describe.skip('shellExecuteTool', () => { |
5 |
| - it('should execute a shell command', async () => { |
| 3 | +import { shellExecuteTool } from './shellExecute'; |
| 4 | + |
| 5 | +// Mock child_process.exec |
| 6 | +vi.mock('child_process', () => ({ |
| 7 | + exec: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +// Mock util.promisify to return our mocked exec function |
| 11 | +vi.mock('util', () => ({ |
| 12 | + promisify: vi.fn((fn) => fn), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('shellExecuteTool', () => { |
| 16 | + // Original test - skipped |
| 17 | + it.skip('should execute a shell command', async () => { |
6 | 18 | // This is a dummy test that will be skipped
|
7 | 19 | expect(true).toBe(true);
|
8 | 20 | });
|
| 21 | + |
| 22 | + // New test for newline conversion |
| 23 | + it('should properly convert literal newlines in stdinContent', async () => { |
| 24 | + // Setup |
| 25 | + const { exec } = await import('child_process'); |
| 26 | + const stdinWithLiteralNewlines = 'Line 1\\nLine 2\\nLine 3'; |
| 27 | + const expectedProcessedContent = 'Line 1\nLine 2\nLine 3'; |
| 28 | + |
| 29 | + // Create a minimal mock context |
| 30 | + const mockContext = { |
| 31 | + logger: { |
| 32 | + debug: vi.fn(), |
| 33 | + error: vi.fn(), |
| 34 | + log: vi.fn(), |
| 35 | + warn: vi.fn(), |
| 36 | + info: vi.fn(), |
| 37 | + }, |
| 38 | + workingDirectory: '/test', |
| 39 | + headless: false, |
| 40 | + userSession: false, |
| 41 | + tokenTracker: { trackTokens: vi.fn() }, |
| 42 | + githubMode: false, |
| 43 | + provider: 'anthropic', |
| 44 | + maxTokens: 4000, |
| 45 | + temperature: 0, |
| 46 | + agentTracker: { registerAgent: vi.fn() }, |
| 47 | + shellTracker: { registerShell: vi.fn(), processStates: new Map() }, |
| 48 | + browserTracker: { registerSession: vi.fn() }, |
| 49 | + }; |
| 50 | + |
| 51 | + // Create a real Buffer but spy on the toString method |
| 52 | + const realBuffer = Buffer.from('test'); |
| 53 | + const bufferSpy = vi |
| 54 | + .spyOn(Buffer, 'from') |
| 55 | + .mockImplementationOnce((content) => { |
| 56 | + // Store the actual content for verification |
| 57 | + if (typeof content === 'string') { |
| 58 | + // This is where we verify the content has been transformed |
| 59 | + expect(content).toEqual(expectedProcessedContent); |
| 60 | + } |
| 61 | + return realBuffer; |
| 62 | + }); |
| 63 | + |
| 64 | + // Mock exec to resolve with empty stdout/stderr |
| 65 | + (exec as any).mockImplementationOnce((cmd, opts, callback) => { |
| 66 | + callback(null, { stdout: '', stderr: '' }); |
| 67 | + }); |
| 68 | + |
| 69 | + // Execute the tool with literal newlines in stdinContent |
| 70 | + await shellExecuteTool.execute( |
| 71 | + { |
| 72 | + command: 'cat', |
| 73 | + description: 'Testing literal newline conversion', |
| 74 | + stdinContent: stdinWithLiteralNewlines, |
| 75 | + }, |
| 76 | + mockContext as any, |
| 77 | + ); |
| 78 | + |
| 79 | + // Verify the Buffer.from was called |
| 80 | + expect(bufferSpy).toHaveBeenCalled(); |
| 81 | + |
| 82 | + // Reset mocks |
| 83 | + bufferSpy.mockRestore(); |
| 84 | + }); |
9 | 85 | });
|
0 commit comments