| 1 | import { expect, test } from "vitest"; |
| 2 | import { LineStream } from "../diff/util"; |
| 3 | import { |
| 4 | processBlockNesting, |
| 5 | stopAtLinesWithMarkdownSupport, |
| 6 | } from "./streamMarkdownUtils"; |
| 7 | |
| 8 | test("processBlockNesting correctly handles starting blocks", () => { |
| 9 | // Mock function to determine which lines should be removed |
| 10 | const shouldRemoveLineBeforeStart = (line: string) => |
| 11 | line === "[CODE]" || line === "```"; |
| 12 | |
| 13 | // Test not seeing first fence yet |
| 14 | const result1 = processBlockNesting( |
| 15 | "[CODE]", |
| 16 | false, |
| 17 | shouldRemoveLineBeforeStart, |
| 18 | ); |
| 19 | expect(result1.newSeenFirstFence).toBe(false); |
| 20 | expect(result1.shouldSkip).toBe(true); |
| 21 | |
| 22 | // Test not seeing first fence but line shouldn't be removed |
| 23 | const result2 = processBlockNesting( |
| 24 | "normal code", |
| 25 | false, |
| 26 | shouldRemoveLineBeforeStart, |
| 27 | ); |
| 28 | expect(result2.newSeenFirstFence).toBe(true); |
| 29 | expect(result2.shouldSkip).toBe(false); |
| 30 | |
| 31 | // Test already seen first fence |
| 32 | const result3 = processBlockNesting( |
| 33 | "more code", |
| 34 | true, |
| 35 | shouldRemoveLineBeforeStart, |
| 36 | ); |
| 37 | expect(result3.newSeenFirstFence).toBe(true); |
| 38 | expect(result3.shouldSkip).toBe(false); |
| 39 | }); |
| 40 | |
| 41 | test("stopAtLinesWithMarkdownSupport handles non-markdown files", async () => { |
| 42 | // Create a mock line stream |
| 43 | const mockLines = ["line 1", "line 2", "```", "line 4"]; |
| 44 | const lineStream: LineStream = (async function* () { |
| 45 | for (const line of mockLines) { |
| 46 | yield line; |
| 47 | } |
| 48 | })(); |
| 49 | |
| 50 | // Test with a non-markdown file |
| 51 | const filename = "test.js"; |
| 52 | const result = stopAtLinesWithMarkdownSupport(lineStream, filename); |
| 53 | |
| 54 | // Should only yield lines up to the backticks |
| 55 | const collected = []; |
| 56 | for await (const line of result) { |
| 57 | collected.push(line); |
| 58 | } |
| 59 | |
| 60 | expect(collected).toEqual(["line 1", "line 2"]); |
| 61 | }); |
| 62 | |
| 63 | test("stopAtLinesWithMarkdownSupport handles markdown files with no nested blocks", async () => { |
| 64 | // Create a mock line stream with markdown content - but NOT nested markdown blocks |
| 65 | const mockLines = [ |
| 66 | "Some markdown text", |
| 67 | "", |
| 68 | "```javascript", |
| 69 | "function test() {", |
| 70 | " return true;", |
| 71 | "}", |
| 72 | "```", |
| 73 | "More text", |
| 74 | ]; |
| 75 | |
| 76 | const lineStream: LineStream = (async function* () { |
| 77 | for (const line of mockLines) { |
| 78 | yield line; |
| 79 | } |
| 80 | })(); |
| 81 | |
| 82 | // Test with a markdown file |
| 83 | const filename = "test.md"; |
| 84 | const result = stopAtLinesWithMarkdownSupport(lineStream, filename); |
| 85 | |
| 86 | // Should yield everything since there's no nested markdown blocks |
| 87 | const collected = []; |
| 88 | for await (const line of result) { |
| 89 | collected.push(line); |
| 90 | } |
| 91 | |
| 92 | // The function should stop at the standalone ``` line (not yield it or lines after) |
| 93 | // This is the correct behavior - it stops when it encounters standalone backticks |
| 94 | expect(collected).toEqual([ |
| 95 | "Some markdown text", |
| 96 | "", |
| 97 | "```javascript", |
| 98 | "function test() {", |
| 99 | " return true;", |
| 100 | "}", |
| 101 | ]); |
| 102 | }); |