| 1 | import { streamLazyApply } from "./streamLazyApply"; |
| 2 | |
| 3 | // Mock LLM for testing |
| 4 | class MockLLM { |
| 5 | model = "claude-3-5-sonnet-20241022"; |
| 6 | providerName = "anthropic"; |
| 7 | |
| 8 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 9 | // Simulate LLM response for markdown file with nested blocks |
| 10 | const lines = [ |
| 11 | "# Project Structure", |
| 12 | "", |
| 13 | "```markdown README.md", |
| 14 | "# Project Structure", |
| 15 | "", |
| 16 | "```", |
| 17 | ".", |
| 18 | "├── src/", |
| 19 | "│ └── main.js", |
| 20 | "└── package.json", |
| 21 | "```", |
| 22 | "```", |
| 23 | ]; |
| 24 | |
| 25 | // Yield each line separately (more realistic streaming) |
| 26 | for (const line of lines) { |
| 27 | yield line; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | class MockJavaScriptLLM { |
| 33 | model = "claude-3-5-sonnet-20241022"; |
| 34 | providerName = "anthropic"; |
| 35 | |
| 36 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 37 | // Simulate LLM response for JavaScript file |
| 38 | const lines = [ |
| 39 | "function calculateSum(a, b) {", |
| 40 | " return a + b;", |
| 41 | "}", |
| 42 | "", |
| 43 | "function calculateProduct(a, b) {", |
| 44 | " return a * b;", |
| 45 | "}", |
| 46 | ]; |
| 47 | |
| 48 | for (const line of lines) { |
| 49 | yield line; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | class MockPythonLLM { |
| 55 | model = "claude-3-5-sonnet-20241022"; |
| 56 | providerName = "anthropic"; |
| 57 | |
| 58 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 59 | // Simulate LLM response for Python file |
| 60 | const lines = [ |
| 61 | "def calculate_sum(a, b):", |
| 62 | " return a + b", |
| 63 | "", |
| 64 | "def calculate_product(a, b):", |
| 65 | " return a * b", |
| 66 | ]; |
| 67 | |
| 68 | for (const line of lines) { |
| 69 | yield line; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | class MockWithUnchangedCodeLLM { |
| 75 | model = "claude-3-5-sonnet-20241022"; |
| 76 | providerName = "anthropic"; |
| 77 | |
| 78 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 79 | // Simulate LLM response with UNCHANGED_CODE markers |
| 80 | const lines = [ |
| 81 | "function newFunction() {", |
| 82 | " return 'new';", |
| 83 | "}", |
| 84 | "", |
| 85 | "// ... unchanged code ...", |
| 86 | "", |
| 87 | "function anotherFunction() {", |
| 88 | " return 'updated';", |
| 89 | "}", |
| 90 | ]; |
| 91 | |
| 92 | for (const line of lines) { |
| 93 | yield line; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | class MockTypeScriptLLM { |
| 99 | model = "claude-3-5-sonnet-20241022"; |
| 100 | providerName = "anthropic"; |
| 101 | |
| 102 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 103 | const lines = [ |
| 104 | "interface User {", |
| 105 | " id: number;", |
| 106 | " name: string;", |
| 107 | "}", |
| 108 | "", |
| 109 | "function getUser(id: number): User {", |
| 110 | " return { id, name: 'Test User' };", |
| 111 | "}", |
| 112 | ]; |
| 113 | |
| 114 | for (const line of lines) { |
| 115 | yield line; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | class MockEmptyLLM { |
| 121 | model = "claude-3-5-sonnet-20241022"; |
| 122 | providerName = "anthropic"; |
| 123 | |
| 124 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 125 | // Empty response |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | class MockSimpleLLM { |
| 131 | model = "claude-3-5-sonnet-20241022"; |
| 132 | providerName = "anthropic"; |
| 133 | |
| 134 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 135 | const lines = ["function test() {", " return true;", "}"]; |
| 136 | for (const line of lines) { |
| 137 | yield line; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | class MockRegularMarkdownLLM { |
| 143 | model = "claude-3-5-sonnet-20241022"; |
| 144 | providerName = "anthropic"; |
| 145 | |
| 146 | async *streamChat(messages: any[], signal?: AbortSignal) { |
| 147 | // Simulate regular markdown with simple code block |
| 148 | const lines = [ |
| 149 | "# Debug Test Folder", |
| 150 | "The sole purpose of this folder is to open it when debugging.", |
| 151 | "", |
| 152 | "## File Structure", |
| 153 | "```", |
| 154 | "debug-test-folder/", |
| 155 | "├── AdvancedPage.tsx", |
| 156 | "├── Calculator.java", |
| 157 | "├── Dockerfile", |
| 158 | "└── test.ts", |
| 159 | "```", |
| 160 | ]; |
| 161 | |
| 162 | for (const line of lines) { |
| 163 | yield line; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | describe("streamLazyApply", () => { |
| 169 | test("should handle markdown files with nested blocks without stopping early", async () => { |
| 170 | const oldCode = "# Project\n\nBasic readme content."; |
| 171 | const newCode = `# Project Structure |
| 172 | |
| 173 | \`\`\`markdown README.md |
| 174 | # Project Structure |
| 175 | |
| 176 | \`\`\` |
| 177 | . |
| 178 | ├── src/ |
| 179 | │ └── main.js |
| 180 | └── package.json |
| 181 | \`\`\` |
| 182 | \`\`\``; |
| 183 | const filename = "README.md"; |
| 184 | const llm = new MockLLM() as any; |
| 185 | const abortController = new AbortController(); |
| 186 | |
| 187 | const diffLines = []; |
| 188 | for await (const diffLine of streamLazyApply( |
| 189 | oldCode, |
| 190 | filename, |
| 191 | newCode, |
| 192 | llm, |
| 193 | abortController, |
| 194 | )) { |
| 195 | diffLines.push(diffLine); |
| 196 | } |
| 197 | |
| 198 | // Should have received diff lines (not empty) |
| 199 | expect(diffLines.length).toBeGreaterThan(0); |
| 200 | |
| 201 | // Should contain the nested markdown content |
| 202 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 203 | expect(allContent).toContain("```markdown README.md"); |
| 204 | expect(allContent).toContain("└── package.json"); |
| 205 | |
| 206 | // Should not have been truncated at the first ``` |
| 207 | expect(allContent).toContain("src/"); |
| 208 | }); |
| 209 | |
| 210 | test("should handle JavaScript files correctly", async () => { |
| 211 | const oldCode = "function calculateSum(a, b) {\n return a + b;\n}"; |
| 212 | const newCode = `function calculateSum(a, b) { |
| 213 | return a + b; |
| 214 | } |
| 215 | |
| 216 | function calculateProduct(a, b) { |
| 217 | return a * b; |
| 218 | }`; |
| 219 | const filename = "calculator.js"; |
| 220 | const llm = new MockJavaScriptLLM() as any; |
| 221 | const abortController = new AbortController(); |
| 222 | |
| 223 | const diffLines = []; |
| 224 | for await (const diffLine of streamLazyApply( |
| 225 | oldCode, |
| 226 | filename, |
| 227 | newCode, |
| 228 | llm, |
| 229 | abortController, |
| 230 | )) { |
| 231 | diffLines.push(diffLine); |
| 232 | } |
| 233 | |
| 234 | // Should have received diff lines |
| 235 | expect(diffLines.length).toBeGreaterThan(0); |
| 236 | |
| 237 | // Should contain the JavaScript functions |
| 238 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 239 | expect(allContent).toContain("calculateSum"); |
| 240 | expect(allContent).toContain("calculateProduct"); |
| 241 | }); |
| 242 | |
| 243 | test("should handle Python files correctly", async () => { |
| 244 | const oldCode = "def calculate_sum(a, b):\n return a + b"; |
| 245 | const newCode = `def calculate_sum(a, b): |
| 246 | return a + b |
| 247 | |
| 248 | def calculate_product(a, b): |
| 249 | return a * b`; |
| 250 | const filename = "calculator.py"; |
| 251 | const llm = new MockPythonLLM() as any; |
| 252 | const abortController = new AbortController(); |
| 253 | |
| 254 | const diffLines = []; |
| 255 | for await (const diffLine of streamLazyApply( |
| 256 | oldCode, |
| 257 | filename, |
| 258 | newCode, |
| 259 | llm, |
| 260 | abortController, |
| 261 | )) { |
| 262 | diffLines.push(diffLine); |
| 263 | } |
| 264 | |
| 265 | // Should have received diff lines |
| 266 | expect(diffLines.length).toBeGreaterThan(0); |
| 267 | |
| 268 | // Should contain the Python functions |
| 269 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 270 | expect(allContent).toContain("calculate_sum"); |
| 271 | expect(allContent).toContain("calculate_product"); |
| 272 | }); |
| 273 | |
| 274 | test("should handle UNCHANGED_CODE markers in responses", async () => { |
| 275 | const oldCode = `function oldFunction() { |
| 276 | return 'old'; |
| 277 | } |
| 278 | |
| 279 | function existingFunction() { |
| 280 | return 'existing'; |
| 281 | }`; |
| 282 | const newCode = `function newFunction() { |
| 283 | return 'new'; |
| 284 | } |
| 285 | |
| 286 | function existingFunction() { |
| 287 | return 'existing'; |
| 288 | } |
| 289 | |
| 290 | function anotherFunction() { |
| 291 | return 'updated'; |
| 292 | }`; |
| 293 | const filename = "example.js"; |
| 294 | const llm = new MockWithUnchangedCodeLLM() as any; |
| 295 | const abortController = new AbortController(); |
| 296 | |
| 297 | const diffLines = []; |
| 298 | for await (const diffLine of streamLazyApply( |
| 299 | oldCode, |
| 300 | filename, |
| 301 | newCode, |
| 302 | llm, |
| 303 | abortController, |
| 304 | )) { |
| 305 | diffLines.push(diffLine); |
| 306 | } |
| 307 | |
| 308 | // Should have received diff lines |
| 309 | expect(diffLines.length).toBeGreaterThan(0); |
| 310 | |
| 311 | // Should contain new and updated functions |
| 312 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 313 | expect(allContent).toContain("newFunction"); |
| 314 | expect(allContent).toContain("anotherFunction"); |
| 315 | }); |
| 316 | |
| 317 | test("should handle TypeScript files correctly", async () => { |
| 318 | const oldCode = "interface User {\n id: number;\n}"; |
| 319 | const newCode = `interface User { |
| 320 | id: number; |
| 321 | name: string; |
| 322 | } |
| 323 | |
| 324 | function getUser(id: number): User { |
| 325 | return { id, name: 'Test User' }; |
| 326 | }`; |
| 327 | const filename = "user.ts"; |
| 328 | const llm = new MockTypeScriptLLM() as any; |
| 329 | const abortController = new AbortController(); |
| 330 | |
| 331 | const diffLines = []; |
| 332 | for await (const diffLine of streamLazyApply( |
| 333 | oldCode, |
| 334 | filename, |
| 335 | newCode, |
| 336 | llm, |
| 337 | abortController, |
| 338 | )) { |
| 339 | diffLines.push(diffLine); |
| 340 | } |
| 341 | |
| 342 | // Should have received diff lines |
| 343 | expect(diffLines.length).toBeGreaterThan(0); |
| 344 | |
| 345 | // Should contain TypeScript interface and function |
| 346 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 347 | expect(allContent).toContain("interface User"); |
| 348 | expect(allContent).toContain("getUser"); |
| 349 | expect(allContent).toContain("name: string"); |
| 350 | }); |
| 351 | |
| 352 | test("should work with empty LLM responses", async () => { |
| 353 | const oldCode = "function test() { return 1; }"; |
| 354 | const newCode = "function test() { return 2; }"; |
| 355 | const filename = "test.js"; |
| 356 | const llm = new MockEmptyLLM() as any; |
| 357 | const abortController = new AbortController(); |
| 358 | |
| 359 | const diffLines = []; |
| 360 | for await (const diffLine of streamLazyApply( |
| 361 | oldCode, |
| 362 | filename, |
| 363 | newCode, |
| 364 | llm, |
| 365 | abortController, |
| 366 | )) { |
| 367 | diffLines.push(diffLine); |
| 368 | } |
| 369 | |
| 370 | // Should handle empty response gracefully |
| 371 | expect(diffLines).toBeDefined(); |
| 372 | }); |
| 373 | |
| 374 | test("should correctly identify markdown vs non-markdown files", async () => { |
| 375 | // Test that our isMarkdownFile logic works |
| 376 | const markdownFiles = ["README.md", "docs.markdown", "example.gfm"]; |
| 377 | const nonMarkdownFiles = [ |
| 378 | "script.js", |
| 379 | "code.py", |
| 380 | "style.css", |
| 381 | "component.tsx", |
| 382 | ]; |
| 383 | |
| 384 | // This is more of a unit test, but verifies our file detection logic |
| 385 | // We can test this indirectly by checking that different file types |
| 386 | // are processed through different code paths |
| 387 | |
| 388 | const oldCode = "function test() { return false; }"; |
| 389 | const newCode = "function test() { return true; }"; |
| 390 | const llm = new MockSimpleLLM() as any; |
| 391 | |
| 392 | // Test each file type |
| 393 | for (const filename of [...markdownFiles, ...nonMarkdownFiles]) { |
| 394 | const abortController = new AbortController(); |
| 395 | const diffLines = []; |
| 396 | |
| 397 | for await (const diffLine of streamLazyApply( |
| 398 | oldCode, |
| 399 | filename, |
| 400 | newCode, |
| 401 | llm, |
| 402 | abortController, |
| 403 | )) { |
| 404 | diffLines.push(diffLine); |
| 405 | } |
| 406 | |
| 407 | // Should work for all file types |
| 408 | expect(diffLines.length).toBeGreaterThan(0); |
| 409 | |
| 410 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 411 | expect(allContent).toContain("test"); |
| 412 | } |
| 413 | }); |
| 414 | |
| 415 | test("should handle regular markdown files with simple code blocks", async () => { |
| 416 | const oldCode = "# Old Content"; |
| 417 | const newCode = `# Debug Test Folder |
| 418 | The sole purpose of this folder is to open it when debugging. |
| 419 | |
| 420 | ## File Structure |
| 421 | \`\`\` |
| 422 | debug-test-folder/ |
| 423 | ├── AdvancedPage.tsx |
| 424 | ├── Calculator.java |
| 425 | ├── Dockerfile |
| 426 | └── test.ts |
| 427 | \`\`\``; |
| 428 | |
| 429 | const filename = "README.md"; |
| 430 | const llm = new MockRegularMarkdownLLM() as any; |
| 431 | const abortController = new AbortController(); |
| 432 | |
| 433 | const diffLines = []; |
| 434 | for await (const diffLine of streamLazyApply( |
| 435 | oldCode, |
| 436 | filename, |
| 437 | newCode, |
| 438 | llm, |
| 439 | abortController, |
| 440 | )) { |
| 441 | diffLines.push(diffLine); |
| 442 | } |
| 443 | |
| 444 | // Should have received diff lines (not empty) |
| 445 | expect(diffLines.length).toBeGreaterThan(0); |
| 446 | |
| 447 | // Should contain the full file structure |
| 448 | const allContent = diffLines.map((d) => d.line).join("\n"); |
| 449 | expect(allContent).toContain("Debug Test Folder"); |
| 450 | expect(allContent).toContain("File Structure"); |
| 451 | expect(allContent).toContain("debug-test-folder/"); |
| 452 | expect(allContent).toContain("├── AdvancedPage.tsx"); |
| 453 | expect(allContent).toContain("└── test.ts"); |
| 454 | |
| 455 | // Should not have been truncated at the opening ``` |
| 456 | expect(allContent).toContain("Calculator.java"); |
| 457 | expect(allContent).toContain("Dockerfile"); |
| 458 | }); |
| 459 | }); |