| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { |
| 3 | collectAllLines, |
| 4 | headerIsMarkdown, |
| 5 | isMarkdownFile, |
| 6 | MarkdownBlockStateTracker, |
| 7 | } from "./markdownUtils"; |
| 8 | |
| 9 | describe("headerIsMarkdown", () => { |
| 10 | // Test exact match with common Markdown identifiers |
| 11 | it("should identify exact matches with common Markdown identifiers", () => { |
| 12 | expect(headerIsMarkdown("md")).toBe(true); |
| 13 | expect(headerIsMarkdown("markdown")).toBe(true); |
| 14 | expect(headerIsMarkdown("gfm")).toBe(true); |
| 15 | expect(headerIsMarkdown("github-markdown")).toBe(true); |
| 16 | }); |
| 17 | |
| 18 | // Test identifiers preceded by a space |
| 19 | it("should identify identifiers preceded by a space", () => { |
| 20 | expect(headerIsMarkdown("lang md")).toBe(true); |
| 21 | expect(headerIsMarkdown("something markdown")).toBe(true); |
| 22 | expect(headerIsMarkdown("language gfm")).toBe(true); |
| 23 | expect(headerIsMarkdown("spec github-markdown")).toBe(true); |
| 24 | }); |
| 25 | |
| 26 | // Test file extensions |
| 27 | it("should identify file names with markdown extensions", () => { |
| 28 | expect(headerIsMarkdown("example.md")).toBe(true); |
| 29 | expect(headerIsMarkdown("document.markdown")).toBe(true); |
| 30 | expect(headerIsMarkdown("readme.gfm")).toBe(true); |
| 31 | }); |
| 32 | |
| 33 | // Test more complex cases with extensions |
| 34 | it("should identify file names with markdown extensions followed by other text", () => { |
| 35 | expect(headerIsMarkdown("example.md additional text")).toBe(true); |
| 36 | expect(headerIsMarkdown("document.markdown with some description")).toBe( |
| 37 | true, |
| 38 | ); |
| 39 | expect(headerIsMarkdown("readme.gfm v2.0")).toBe(true); |
| 40 | }); |
| 41 | |
| 42 | // Test non-markdown cases |
| 43 | it("should not identify non-markdown headers", () => { |
| 44 | expect(headerIsMarkdown("javascript")).toBe(false); |
| 45 | expect(headerIsMarkdown("typescript")).toBe(false); |
| 46 | expect(headerIsMarkdown("plain")).toBe(false); |
| 47 | expect(headerIsMarkdown("python")).toBe(false); |
| 48 | }); |
| 49 | |
| 50 | // Test edge cases |
| 51 | it("should handle edge cases correctly", () => { |
| 52 | expect(headerIsMarkdown("")).toBe(false); |
| 53 | expect(headerIsMarkdown("mdx")).toBe(false); // Similar but not exactly "md" |
| 54 | expect(headerIsMarkdown("readme md")).toBe(true); |
| 55 | expect(headerIsMarkdown("md.js")).toBe(false); // "md" is not the extension |
| 56 | }); |
| 57 | |
| 58 | // Test case sensitivity |
| 59 | it("should respect case sensitivity", () => { |
| 60 | expect(headerIsMarkdown("MD")).toBe(false); |
| 61 | expect(headerIsMarkdown("MARKDOWN")).toBe(false); |
| 62 | expect(headerIsMarkdown("example.MD")).toBe(false); |
| 63 | expect(headerIsMarkdown("lang MD")).toBe(false); |
| 64 | }); |
| 65 | |
| 66 | // Test with special characters and spacing |
| 67 | it("should handle special characters and spacing correctly", () => { |
| 68 | expect(headerIsMarkdown(" md")).toBe(true); // Space before "md" |
| 69 | expect(headerIsMarkdown("md ")).toBe(true); // Space after "md" |
| 70 | expect(headerIsMarkdown("hello-md")).toBe(false); // "md" with hyphen prefix |
| 71 | expect(headerIsMarkdown("markdown:")).toBe(false); // "markdown" with suffix |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | describe("isMarkdownFile", () => { |
| 76 | it("should identify markdown files by .md extension", () => { |
| 77 | expect(isMarkdownFile("README.md")).toBe(true); |
| 78 | expect(isMarkdownFile("docs/guide.md")).toBe(true); |
| 79 | expect(isMarkdownFile("/path/to/file.md")).toBe(true); |
| 80 | }); |
| 81 | |
| 82 | it("should identify markdown files by .markdown extension", () => { |
| 83 | expect(isMarkdownFile("README.markdown")).toBe(true); |
| 84 | expect(isMarkdownFile("docs/guide.markdown")).toBe(true); |
| 85 | expect(isMarkdownFile("/path/to/file.markdown")).toBe(true); |
| 86 | }); |
| 87 | |
| 88 | it("should identify markdown files by .gfm extension", () => { |
| 89 | expect(isMarkdownFile("README.gfm")).toBe(true); |
| 90 | expect(isMarkdownFile("docs/guide.gfm")).toBe(true); |
| 91 | expect(isMarkdownFile("/path/to/file.gfm")).toBe(true); |
| 92 | }); |
| 93 | |
| 94 | it("should handle case insensitive extensions", () => { |
| 95 | expect(isMarkdownFile("README.MD")).toBe(true); |
| 96 | expect(isMarkdownFile("guide.MARKDOWN")).toBe(true); |
| 97 | expect(isMarkdownFile("file.GFM")).toBe(true); |
| 98 | }); |
| 99 | |
| 100 | it("should return false for non-markdown files", () => { |
| 101 | expect(isMarkdownFile("script.js")).toBe(false); |
| 102 | expect(isMarkdownFile("style.css")).toBe(false); |
| 103 | expect(isMarkdownFile("data.json")).toBe(false); |
| 104 | expect(isMarkdownFile("document.txt")).toBe(false); |
| 105 | }); |
| 106 | |
| 107 | it("should return false for undefined or empty filepath", () => { |
| 108 | expect(isMarkdownFile(undefined)).toBe(false); |
| 109 | expect(isMarkdownFile("")).toBe(false); |
| 110 | }); |
| 111 | |
| 112 | it("should return false for files without extensions", () => { |
| 113 | expect(isMarkdownFile("README")).toBe(false); |
| 114 | expect(isMarkdownFile("Dockerfile")).toBe(false); |
| 115 | }); |
| 116 | |
| 117 | it("should handle files with multiple dots", () => { |
| 118 | expect(isMarkdownFile("file.backup.md")).toBe(true); |
| 119 | expect(isMarkdownFile("config.test.js")).toBe(false); |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | describe("MarkdownBlockStateTracker", () => { |
| 124 | it("should initialize correctly with empty lines", () => { |
| 125 | const tracker = new MarkdownBlockStateTracker([]); |
| 126 | expect(tracker.shouldStopAtPosition(0)).toBe(false); |
| 127 | }); |
| 128 | |
| 129 | it("should initialize correctly with non-markdown lines", () => { |
| 130 | const tracker = new MarkdownBlockStateTracker([ |
| 131 | "const x = 5;", |
| 132 | "console.log(x);", |
| 133 | ]); |
| 134 | expect(tracker.shouldStopAtPosition(0)).toBe(false); |
| 135 | expect(tracker.shouldStopAtPosition(1)).toBe(false); |
| 136 | }); |
| 137 | |
| 138 | it("should detect simple markdown blocks", () => { |
| 139 | const tracker = new MarkdownBlockStateTracker([ |
| 140 | "```markdown", |
| 141 | "# Title", |
| 142 | "```", |
| 143 | ]); |
| 144 | expect(tracker.shouldStopAtPosition(0)).toBe(false); // Opening |
| 145 | expect(tracker.shouldStopAtPosition(1)).toBe(false); // Content |
| 146 | expect(tracker.shouldStopAtPosition(2)).toBe(true); // Closing |
| 147 | }); |
| 148 | |
| 149 | it("should handle nested code blocks within markdown", () => { |
| 150 | const tracker = new MarkdownBlockStateTracker([ |
| 151 | "```md", |
| 152 | "Here's some code:", |
| 153 | "```javascript", |
| 154 | "console.log('hello');", |
| 155 | "```", |
| 156 | "More content", |
| 157 | "```", |
| 158 | ]); |
| 159 | expect(tracker.shouldStopAtPosition(2)).toBe(false); // Inner opening |
| 160 | expect(tracker.shouldStopAtPosition(4)).toBe(false); // Inner closing |
| 161 | expect(tracker.shouldStopAtPosition(6)).toBe(true); // Outer closing |
| 162 | }); |
| 163 | |
| 164 | it("should handle multiple bare backticks", () => { |
| 165 | const tracker = new MarkdownBlockStateTracker([ |
| 166 | "```markdown", |
| 167 | "Content with ```inline``` code", |
| 168 | "```", |
| 169 | "More content", |
| 170 | "```", |
| 171 | ]); |
| 172 | expect(tracker.shouldStopAtPosition(2)).toBe(false); // First bare backticks |
| 173 | expect(tracker.shouldStopAtPosition(4)).toBe(true); // Last bare backticks |
| 174 | }); |
| 175 | |
| 176 | it("should handle non-markdown code blocks", () => { |
| 177 | const tracker = new MarkdownBlockStateTracker([ |
| 178 | "```javascript", |
| 179 | "console.log('hello');", |
| 180 | "```", |
| 181 | ]); |
| 182 | expect(tracker.shouldStopAtPosition(2)).toBe(false); // Not a markdown block |
| 183 | }); |
| 184 | |
| 185 | it("should handle mixed markdown types", () => { |
| 186 | const tracker = new MarkdownBlockStateTracker([ |
| 187 | "```gfm", |
| 188 | "# GitHub Flavored Markdown", |
| 189 | "```", |
| 190 | ]); |
| 191 | expect(tracker.shouldStopAtPosition(2)).toBe(true); |
| 192 | }); |
| 193 | |
| 194 | it("should handle state persistence across multiple calls", () => { |
| 195 | const tracker = new MarkdownBlockStateTracker([ |
| 196 | "```markdown", |
| 197 | "# Documentation", |
| 198 | "```typescript", |
| 199 | "interface Config {}", |
| 200 | "```", |
| 201 | "Final notes", |
| 202 | "```", |
| 203 | ]); |
| 204 | |
| 205 | // Process incrementally |
| 206 | expect(tracker.shouldStopAtPosition(2)).toBe(false); // Inner opening |
| 207 | expect(tracker.shouldStopAtPosition(4)).toBe(false); // Inner closing |
| 208 | expect(tracker.shouldStopAtPosition(6)).toBe(true); // Outer closing |
| 209 | }); |
| 210 | |
| 211 | it("should handle lines that are not closing backticks", () => { |
| 212 | const tracker = new MarkdownBlockStateTracker([ |
| 213 | "```markdown", |
| 214 | "Regular content line", |
| 215 | "```", |
| 216 | ]); |
| 217 | expect(tracker.shouldStopAtPosition(1)).toBe(false); // Not backticks |
| 218 | }); |
| 219 | |
| 220 | it("should provide access to trimmed lines", () => { |
| 221 | const tracker = new MarkdownBlockStateTracker([ |
| 222 | " ```markdown ", |
| 223 | " # Title ", |
| 224 | " ``` ", |
| 225 | ]); |
| 226 | const trimmedLines = tracker.getTrimmedLines(); |
| 227 | expect(trimmedLines).toEqual(["```markdown", "# Title", "```"]); |
| 228 | }); |
| 229 | |
| 230 | it("should identify bare backtick lines correctly", () => { |
| 231 | const tracker = new MarkdownBlockStateTracker([ |
| 232 | "```markdown", |
| 233 | "```", |
| 234 | "content", |
| 235 | "```", |
| 236 | ]); |
| 237 | expect(tracker.isBareBacktickLine(0)).toBe(false); // Has language |
| 238 | expect(tracker.isBareBacktickLine(1)).toBe(true); // Bare backticks |
| 239 | expect(tracker.isBareBacktickLine(2)).toBe(false); // Content |
| 240 | expect(tracker.isBareBacktickLine(3)).toBe(true); // Bare backticks |
| 241 | }); |
| 242 | |
| 243 | it("should count remaining bare backticks correctly", () => { |
| 244 | const tracker = new MarkdownBlockStateTracker([ |
| 245 | "```markdown", |
| 246 | "```", |
| 247 | "content", |
| 248 | "```", |
| 249 | "more content", |
| 250 | "```", |
| 251 | ]); |
| 252 | expect(tracker.getRemainingBareBackticksAfter(0)).toBe(3); // After opening |
| 253 | expect(tracker.getRemainingBareBackticksAfter(1)).toBe(2); // After first bare |
| 254 | expect(tracker.getRemainingBareBackticksAfter(3)).toBe(1); // After second bare |
| 255 | expect(tracker.getRemainingBareBackticksAfter(5)).toBe(0); // After last bare |
| 256 | }); |
| 257 | }); |
| 258 | |
| 259 | describe("collectAllLines", () => { |
| 260 | async function* createAsyncGenerator<T>(items: T[]): AsyncGenerator<T> { |
| 261 | for (const item of items) { |
| 262 | yield item; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | it("should collect all lines from a stream", async () => { |
| 267 | const stream = createAsyncGenerator(["line1", "line2", "line3"]); |
| 268 | const result = await collectAllLines(stream); |
| 269 | expect(result).toEqual(["line1", "line2", "line3"]); |
| 270 | }); |
| 271 | |
| 272 | it("should handle empty streams", async () => { |
| 273 | const stream = createAsyncGenerator([]); |
| 274 | const result = await collectAllLines(stream); |
| 275 | expect(result).toEqual([]); |
| 276 | }); |
| 277 | |
| 278 | it("should handle streams with mixed types", async () => { |
| 279 | const stream = createAsyncGenerator([1, "text", null, true]); |
| 280 | const result = await collectAllLines(stream); |
| 281 | expect(result).toEqual([1, "text", null, true]); |
| 282 | }); |
| 283 | |
| 284 | it("should handle streams with empty strings", async () => { |
| 285 | const stream = createAsyncGenerator(["", "line2", "", "line4"]); |
| 286 | const result = await collectAllLines(stream); |
| 287 | expect(result).toEqual(["", "line2", "", "line4"]); |
| 288 | }); |
| 289 | |
| 290 | it("should handle large streams", async () => { |
| 291 | const largeArray = Array.from({ length: 1000 }, (_, i) => `line${i}`); |
| 292 | const stream = createAsyncGenerator(largeArray); |
| 293 | const result = await collectAllLines(stream); |
| 294 | expect(result).toEqual(largeArray); |
| 295 | }); |
| 296 | |
| 297 | it("should handle object streams", async () => { |
| 298 | const objects = [ |
| 299 | { type: "new", line: "const x = 5;" }, |
| 300 | { type: "old", line: "let y = 10;" }, |
| 301 | ]; |
| 302 | const stream = createAsyncGenerator(objects); |
| 303 | const result = await collectAllLines(stream); |
| 304 | expect(result).toEqual(objects); |
| 305 | }); |
| 306 | }); |