|
| 1 | +import { describe, expect, test, vi } from 'vitest'; |
| 2 | +import { readFileTool } from './readFile'; |
| 3 | + |
| 4 | +vi.mock('@/features/git', () => ({ |
| 5 | + getFileSource: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('../logger', () => ({ |
| 9 | + logger: { debug: vi.fn() }, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('./readFile.txt', () => ({ default: 'description' })); |
| 13 | + |
| 14 | +import { getFileSource } from '@/features/git'; |
| 15 | + |
| 16 | +const mockGetFileSource = vi.mocked(getFileSource); |
| 17 | + |
| 18 | +function makeSource(source: string) { |
| 19 | + mockGetFileSource.mockResolvedValue({ |
| 20 | + source, |
| 21 | + path: 'test.ts', |
| 22 | + repo: 'github.com/org/repo', |
| 23 | + language: 'typescript', |
| 24 | + revision: 'HEAD', |
| 25 | + } as any); |
| 26 | +} |
| 27 | + |
| 28 | +describe('readFileTool byte cap', () => { |
| 29 | + test('truncates output at 5KB and shows byte cap message', async () => { |
| 30 | + // Each line is ~100 bytes; 60 lines = ~6KB, over the 5KB cap |
| 31 | + const lines = Array.from({ length: 60 }, (_, i) => `line${i + 1}: ${'x'.repeat(90)}`).join('\n'); |
| 32 | + makeSource(lines); |
| 33 | + |
| 34 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 35 | + expect('source' in result && result.source).toContain('Output capped at 5KB'); |
| 36 | + expect('source' in result && result.source).toContain('Use offset='); |
| 37 | + expect('source' in result && result.source).toContain('Output capped at 5KB'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('does not cap output under 5KB', async () => { |
| 41 | + makeSource('short line\n'.repeat(10).trimEnd()); |
| 42 | + |
| 43 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 44 | + expect('source' in result && result.source).not.toContain('Output capped at 5KB'); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('readFileTool hasMoreLines message', () => { |
| 49 | + test('appends continuation message when file is truncated', async () => { |
| 50 | + const lines = Array.from({ length: 600 }, (_, i) => `line${i + 1}`).join('\n'); |
| 51 | + makeSource(lines); |
| 52 | + |
| 53 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 54 | + expect('source' in result && result.source).toContain('Showing lines 1-500 of 600'); |
| 55 | + expect('source' in result && result.source).toContain('offset=501'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('shows end of file message when all lines fit', async () => { |
| 59 | + makeSource('line1\nline2\nline3'); |
| 60 | + |
| 61 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 62 | + expect('source' in result && result.source).not.toContain('Showing lines'); |
| 63 | + expect('source' in result && result.source).toContain('End of file - 3 lines total'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('continuation message reflects offset parameter', async () => { |
| 67 | + const lines = Array.from({ length: 600 }, (_, i) => `line${i + 1}`).join('\n'); |
| 68 | + makeSource(lines); |
| 69 | + |
| 70 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo', offset: 100 }, {} as any); |
| 71 | + expect('source' in result && result.source).toContain('Showing lines 100-599 of 600'); |
| 72 | + expect('source' in result && result.source).toContain('offset=600'); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('readFileTool line truncation', () => { |
| 77 | + test('does not truncate lines under the limit', async () => { |
| 78 | + const line = 'x'.repeat(100); |
| 79 | + makeSource(line); |
| 80 | + |
| 81 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 82 | + expect('source' in result && result.source).toContain(line); |
| 83 | + expect('source' in result && result.source).not.toContain('line truncated'); |
| 84 | + }); |
| 85 | + |
| 86 | + test('truncates lines longer than 2000 chars', async () => { |
| 87 | + const line = 'x'.repeat(3000); |
| 88 | + makeSource(line); |
| 89 | + |
| 90 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 91 | + expect('source' in result && result.source).toContain('... (line truncated to 2000 chars)'); |
| 92 | + expect('source' in result && result.source).not.toContain('x'.repeat(2001)); |
| 93 | + }); |
| 94 | + |
| 95 | + test('truncates only the long lines, leaving normal lines intact', async () => { |
| 96 | + const longLine = 'a'.repeat(3000); |
| 97 | + const normalLine = 'normal line'; |
| 98 | + makeSource(`${normalLine}\n${longLine}\n${normalLine}`); |
| 99 | + |
| 100 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 101 | + expect('source' in result && result.source).toContain(normalLine); |
| 102 | + expect('source' in result && result.source).toContain('... (line truncated to 2000 chars)'); |
| 103 | + }); |
| 104 | + |
| 105 | + test('truncates a line at exactly 2001 chars', async () => { |
| 106 | + makeSource('b'.repeat(2001)); |
| 107 | + |
| 108 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 109 | + expect('source' in result && result.source).toContain('... (line truncated to 2000 chars)'); |
| 110 | + }); |
| 111 | + |
| 112 | + test('does not truncate a line at exactly 2000 chars', async () => { |
| 113 | + makeSource('c'.repeat(2000)); |
| 114 | + |
| 115 | + const result = await readFileTool.execute!({ path: 'test.ts', repository: 'github.com/org/repo' }, {} as any); |
| 116 | + expect('source' in result && result.source).not.toContain('line truncated'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments