|
| 1 | +// npx vitest run integrations/misc/__tests__/extract-text-large-files.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach, Mock } from "vitest" |
| 4 | +import * as fs from "fs/promises" |
| 5 | +import { extractTextFromFile } from "../extract-text" |
| 6 | +import { countFileLines } from "../line-counter" |
| 7 | +import { readLines } from "../read-lines" |
| 8 | +import { isBinaryFile } from "isbinaryfile" |
| 9 | + |
| 10 | +// Mock all dependencies |
| 11 | +vi.mock("fs/promises") |
| 12 | +vi.mock("../line-counter") |
| 13 | +vi.mock("../read-lines") |
| 14 | +vi.mock("isbinaryfile") |
| 15 | + |
| 16 | +describe("extractTextFromFile - Large File Handling", () => { |
| 17 | + // Type the mocks |
| 18 | + const mockedFs = vi.mocked(fs) |
| 19 | + const mockedCountFileLines = vi.mocked(countFileLines) |
| 20 | + const mockedReadLines = vi.mocked(readLines) |
| 21 | + const mockedIsBinaryFile = vi.mocked(isBinaryFile) |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks() |
| 25 | + // Set default mock behavior |
| 26 | + mockedFs.access.mockResolvedValue(undefined) |
| 27 | + mockedIsBinaryFile.mockResolvedValue(false) |
| 28 | + }) |
| 29 | + |
| 30 | + it("should truncate files that exceed maxReadFileLine limit", async () => { |
| 31 | + const largeFileContent = Array(150) |
| 32 | + .fill(null) |
| 33 | + .map((_, i) => `Line ${i + 1}: This is a test line with some content`) |
| 34 | + .join("\n") |
| 35 | + |
| 36 | + mockedCountFileLines.mockResolvedValue(150) |
| 37 | + mockedReadLines.mockResolvedValue( |
| 38 | + Array(100) |
| 39 | + .fill(null) |
| 40 | + .map((_, i) => `Line ${i + 1}: This is a test line with some content`) |
| 41 | + .join("\n"), |
| 42 | + ) |
| 43 | + |
| 44 | + const result = await extractTextFromFile("/test/large-file.ts", 100) |
| 45 | + |
| 46 | + // Should only include first 100 lines with line numbers |
| 47 | + expect(result).toContain(" 1 | Line 1: This is a test line with some content") |
| 48 | + expect(result).toContain("100 | Line 100: This is a test line with some content") |
| 49 | + expect(result).not.toContain("101 | Line 101: This is a test line with some content") |
| 50 | + |
| 51 | + // Should include truncation message |
| 52 | + expect(result).toContain( |
| 53 | + "[File truncated: showing 100 of 150 total lines. The file is too large and may exhaust the context window if read in full.]", |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it("should not truncate files within the maxReadFileLine limit", async () => { |
| 58 | + const smallFileContent = Array(50) |
| 59 | + .fill(null) |
| 60 | + .map((_, i) => `Line ${i + 1}: This is a test line`) |
| 61 | + .join("\n") |
| 62 | + |
| 63 | + mockedCountFileLines.mockResolvedValue(50) |
| 64 | + mockedFs.readFile.mockResolvedValue(smallFileContent as any) |
| 65 | + |
| 66 | + const result = await extractTextFromFile("/test/small-file.ts", 100) |
| 67 | + |
| 68 | + // Should include all lines with line numbers |
| 69 | + expect(result).toContain(" 1 | Line 1: This is a test line") |
| 70 | + expect(result).toContain("50 | Line 50: This is a test line") |
| 71 | + |
| 72 | + // Should not include truncation message |
| 73 | + expect(result).not.toContain("[File truncated:") |
| 74 | + }) |
| 75 | + |
| 76 | + it("should handle files with exactly maxReadFileLine lines", async () => { |
| 77 | + const exactFileContent = Array(100) |
| 78 | + .fill(null) |
| 79 | + .map((_, i) => `Line ${i + 1}`) |
| 80 | + .join("\n") |
| 81 | + |
| 82 | + mockedCountFileLines.mockResolvedValue(100) |
| 83 | + mockedFs.readFile.mockResolvedValue(exactFileContent as any) |
| 84 | + |
| 85 | + const result = await extractTextFromFile("/test/exact-file.ts", 100) |
| 86 | + |
| 87 | + // Should include all lines with line numbers |
| 88 | + expect(result).toContain(" 1 | Line 1") |
| 89 | + expect(result).toContain("100 | Line 100") |
| 90 | + |
| 91 | + // Should not include truncation message |
| 92 | + expect(result).not.toContain("[File truncated:") |
| 93 | + }) |
| 94 | + |
| 95 | + it("should handle undefined maxReadFileLine by not truncating", async () => { |
| 96 | + const largeFileContent = Array(200) |
| 97 | + .fill(null) |
| 98 | + .map((_, i) => `Line ${i + 1}`) |
| 99 | + .join("\n") |
| 100 | + |
| 101 | + mockedFs.readFile.mockResolvedValue(largeFileContent as any) |
| 102 | + |
| 103 | + const result = await extractTextFromFile("/test/large-file.ts", undefined) |
| 104 | + |
| 105 | + // Should include all lines with line numbers when maxReadFileLine is undefined |
| 106 | + expect(result).toContain(" 1 | Line 1") |
| 107 | + expect(result).toContain("200 | Line 200") |
| 108 | + |
| 109 | + // Should not include truncation message |
| 110 | + expect(result).not.toContain("[File truncated:") |
| 111 | + }) |
| 112 | + |
| 113 | + it("should handle empty files", async () => { |
| 114 | + mockedFs.readFile.mockResolvedValue("" as any) |
| 115 | + |
| 116 | + const result = await extractTextFromFile("/test/empty-file.ts", 100) |
| 117 | + |
| 118 | + expect(result).toBe("") |
| 119 | + expect(result).not.toContain("[File truncated:") |
| 120 | + }) |
| 121 | + |
| 122 | + it("should handle files with only newlines", async () => { |
| 123 | + const newlineOnlyContent = "\n\n\n\n\n" |
| 124 | + |
| 125 | + mockedCountFileLines.mockResolvedValue(6) // 5 newlines = 6 lines |
| 126 | + mockedReadLines.mockResolvedValue("\n\n") |
| 127 | + |
| 128 | + const result = await extractTextFromFile("/test/newline-file.ts", 3) |
| 129 | + |
| 130 | + // Should truncate at line 3 |
| 131 | + expect(result).toContain("[File truncated: showing 3 of 6 total lines") |
| 132 | + }) |
| 133 | + |
| 134 | + it("should handle very large files efficiently", async () => { |
| 135 | + // Simulate a 10,000 line file |
| 136 | + mockedCountFileLines.mockResolvedValue(10000) |
| 137 | + mockedReadLines.mockResolvedValue( |
| 138 | + Array(500) |
| 139 | + .fill(null) |
| 140 | + .map((_, i) => `Line ${i + 1}: Some content here`) |
| 141 | + .join("\n"), |
| 142 | + ) |
| 143 | + |
| 144 | + const result = await extractTextFromFile("/test/very-large-file.ts", 500) |
| 145 | + |
| 146 | + // Should only include first 500 lines with line numbers |
| 147 | + expect(result).toContain(" 1 | Line 1: Some content here") |
| 148 | + expect(result).toContain("500 | Line 500: Some content here") |
| 149 | + expect(result).not.toContain("501 | Line 501: Some content here") |
| 150 | + |
| 151 | + // Should show truncation message |
| 152 | + expect(result).toContain("[File truncated: showing 500 of 10000 total lines") |
| 153 | + }) |
| 154 | + |
| 155 | + it("should handle maxReadFileLine of 0 by not truncating", async () => { |
| 156 | + const fileContent = "Line 1\nLine 2\nLine 3" |
| 157 | + |
| 158 | + mockedFs.readFile.mockResolvedValue(fileContent as any) |
| 159 | + |
| 160 | + const result = await extractTextFromFile("/test/file.ts", 0) |
| 161 | + |
| 162 | + // maxReadFileLine of 0 or negative means no limit |
| 163 | + expect(result).toContain("1 | Line 1") |
| 164 | + expect(result).toContain("2 | Line 2") |
| 165 | + expect(result).toContain("3 | Line 3") |
| 166 | + expect(result).not.toContain("[File truncated:") |
| 167 | + }) |
| 168 | + |
| 169 | + it("should handle negative maxReadFileLine by treating as undefined", async () => { |
| 170 | + const fileContent = "Line 1\nLine 2\nLine 3" |
| 171 | + |
| 172 | + mockedFs.readFile.mockResolvedValue(fileContent as any) |
| 173 | + |
| 174 | + const result = await extractTextFromFile("/test/file.ts", -1) |
| 175 | + |
| 176 | + // Should include all content with line numbers when negative |
| 177 | + expect(result).toContain("1 | Line 1") |
| 178 | + expect(result).toContain("2 | Line 2") |
| 179 | + expect(result).toContain("3 | Line 3") |
| 180 | + expect(result).not.toContain("[File truncated:") |
| 181 | + }) |
| 182 | + |
| 183 | + it("should preserve file content structure when truncating", async () => { |
| 184 | + const structuredContent = [ |
| 185 | + "function example() {", |
| 186 | + " const x = 1;", |
| 187 | + " const y = 2;", |
| 188 | + " return x + y;", |
| 189 | + "}", |
| 190 | + "", |
| 191 | + "// More code below", |
| 192 | + ].join("\n") |
| 193 | + |
| 194 | + mockedCountFileLines.mockResolvedValue(7) |
| 195 | + mockedReadLines.mockResolvedValue(["function example() {", " const x = 1;", " const y = 2;"].join("\n")) |
| 196 | + |
| 197 | + const result = await extractTextFromFile("/test/structured.ts", 3) |
| 198 | + |
| 199 | + // Should preserve the first 3 lines with line numbers |
| 200 | + expect(result).toContain("1 | function example() {") |
| 201 | + expect(result).toContain("2 | const x = 1;") |
| 202 | + expect(result).toContain("3 | const y = 2;") |
| 203 | + expect(result).not.toContain("4 | return x + y;") |
| 204 | + |
| 205 | + // Should include truncation info |
| 206 | + expect(result).toContain("[File truncated: showing 3 of 7 total lines") |
| 207 | + }) |
| 208 | + |
| 209 | + it("should handle binary files by throwing an error", async () => { |
| 210 | + mockedIsBinaryFile.mockResolvedValue(true) |
| 211 | + |
| 212 | + await expect(extractTextFromFile("/test/binary.bin", 100)).rejects.toThrow( |
| 213 | + "Cannot read text for file type: .bin", |
| 214 | + ) |
| 215 | + }) |
| 216 | + |
| 217 | + it("should handle file not found errors", async () => { |
| 218 | + mockedFs.access.mockRejectedValue(new Error("ENOENT")) |
| 219 | + |
| 220 | + await expect(extractTextFromFile("/test/nonexistent.ts", 100)).rejects.toThrow( |
| 221 | + "File not found: /test/nonexistent.ts", |
| 222 | + ) |
| 223 | + }) |
| 224 | +}) |
0 commit comments