Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions src/filesystem/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,23 @@ describe('Lib Functions', () => {
});

describe('tailFile', () => {
const mockTailReadableFile = (content: string) => {
const fileBuffer = Buffer.from(content, 'utf-8');
mockFs.stat.mockResolvedValue({ size: fileBuffer.length } as any);

const mockFileHandle = {
read: vi.fn(async (buffer: Buffer, offset: number, length: number, position: number) => {
const chunk = fileBuffer.subarray(position, position + length);
chunk.copy(buffer, offset);
return { bytesRead: chunk.length, buffer };
}),
close: vi.fn().mockResolvedValue(undefined)
} as any;

mockFs.open.mockResolvedValue(mockFileHandle);
return mockFileHandle;
};

it('handles empty files', async () => {
mockFs.stat.mockResolvedValue({ size: 0 } as any);

Expand Down Expand Up @@ -583,23 +600,29 @@ describe('Lib Functions', () => {
});

it('handles files with content and returns last lines', async () => {
mockFs.stat.mockResolvedValue({ size: 50 } as any);

const mockFileHandle = {
read: vi.fn(),
close: vi.fn()
} as any;

// Simulate reading file content in chunks
mockFileHandle.read
.mockResolvedValueOnce({ bytesRead: 20, buffer: Buffer.from('line3\nline4\nline5\n') })
.mockResolvedValueOnce({ bytesRead: 0 });
mockFileHandle.close.mockResolvedValue(undefined);

mockFs.open.mockResolvedValue(mockFileHandle);

const mockFileHandle = mockTailReadableFile('line1\nline2\nline3\nline4\nline5');

const result = await tailFile('/test/file.txt', 2);


expect(result).toBe('line4\nline5');
expect(mockFileHandle.close).toHaveBeenCalled();
});

it('does not treat a final newline as an empty tail line', async () => {
const mockFileHandle = mockTailReadableFile('line1\nline2\nline3\n');

const result = await tailFile('/test/file.txt', 1);

expect(result).toBe('line3');
expect(mockFileHandle.close).toHaveBeenCalled();
});

it('returns the requested lines when content ends in a newline', async () => {
const mockFileHandle = mockTailReadableFile('line1\nline2\nline3\n');

const result = await tailFile('/test/file.txt', 2);

expect(result).toBe('line2\nline3');
expect(mockFileHandle.close).toHaveBeenCalled();
});

Expand Down
6 changes: 6 additions & 0 deletions src/filesystem/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export async function tailFile(filePath: string, numLines: number): Promise<stri
let chunk = Buffer.alloc(CHUNK_SIZE);
let linesFound = 0;
let remainingText = '';
let isEndChunk = true;

// Read chunks from the end of the file until we have enough lines
while (position > 0 && linesFound < numLines) {
Expand All @@ -289,6 +290,11 @@ export async function tailFile(filePath: string, numLines: number): Promise<stri

// Split by newlines and count
const chunkLines = normalizeLineEndings(chunkText).split('\n');

if (isEndChunk && chunkLines[chunkLines.length - 1] === '') {
chunkLines.pop();
}
isEndChunk = false;

// If this isn't the end of the file, the first line is likely incomplete
// Save it to prepend to the next chunk
Expand Down
Loading