|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import { |
| 3 | + StreamingMarkdownParser, |
| 4 | + StreamingParserCallbacks, |
| 5 | +} from "./streamingParser"; |
| 6 | + |
| 7 | +describe("StreamingMarkdownParser", () => { |
| 8 | + let fileNameChanges: Array<{ |
| 9 | + name: string; |
| 10 | + format: string; |
| 11 | + }>; |
| 12 | + let codeLines: string[]; |
| 13 | + let parser: StreamingMarkdownParser; |
| 14 | + |
| 15 | + const callbacks: StreamingParserCallbacks = { |
| 16 | + onFileNameChange: (fileName, format) => { |
| 17 | + fileNameChanges.push({ name: fileName, format }); |
| 18 | + }, |
| 19 | + onCodeLine: (line) => { |
| 20 | + codeLines.push(line); |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + fileNameChanges = []; |
| 26 | + codeLines = []; |
| 27 | + parser = new StreamingMarkdownParser(callbacks); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should process a complete markdown block in one chunk", () => { |
| 31 | + const input = "**index.html**\n```\n<html>\n</html>\n```\n"; |
| 32 | + parser.processChunk(input); |
| 33 | + parser.flushRemaining(); |
| 34 | + |
| 35 | + expect(fileNameChanges).toEqual([ |
| 36 | + { name: "index.html", format: "Bold Format" }, |
| 37 | + ]); |
| 38 | + expect(codeLines).toEqual(["<html>", "</html>"]); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should handle multiple chunks with split lines", () => { |
| 42 | + // Simulate splitting a header and code block across chunks |
| 43 | + parser.processChunk("**inde"); |
| 44 | + parser.processChunk("x.html**\n```\n<ht"); |
| 45 | + parser.processChunk("ml>\n</html>\n```"); |
| 46 | + parser.flushRemaining(); |
| 47 | + |
| 48 | + expect(fileNameChanges).toEqual([ |
| 49 | + { name: "index.html", format: "Bold Format" }, |
| 50 | + ]); |
| 51 | + expect(codeLines).toEqual(["<html>", "</html>"]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should process multiple files and code blocks", () => { |
| 55 | + const input = |
| 56 | + "**index.html**\n```\n<html>\n</html>\n```\n" + |
| 57 | + "**styles.css**\n```\nbody { color: blue; }\n```\n"; |
| 58 | + parser.processChunk(input); |
| 59 | + parser.flushRemaining(); |
| 60 | + |
| 61 | + expect(fileNameChanges).toEqual([ |
| 62 | + { name: "index.html", format: "Bold Format" }, |
| 63 | + { name: "styles.css", format: "Bold Format" }, |
| 64 | + ]); |
| 65 | + expect(codeLines).toEqual(["<html>", "</html>", "body { color: blue; }"]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should handle code fence markers with language specifiers", () => { |
| 69 | + const input = "**script.js**\n```js\nconsole.log('Hello');\n```\n"; |
| 70 | + parser.processChunk(input); |
| 71 | + parser.flushRemaining(); |
| 72 | + |
| 73 | + expect(fileNameChanges).toEqual([ |
| 74 | + { name: "script.js", format: "Bold Format" }, |
| 75 | + ]); |
| 76 | + expect(codeLines).toEqual(["console.log('Hello');"]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should flush remaining partial lines on flushRemaining", () => { |
| 80 | + // Provide a header line without a trailing newline |
| 81 | + parser.processChunk("**partial.html**"); |
| 82 | + parser.flushRemaining(); |
| 83 | + |
| 84 | + expect(fileNameChanges).toEqual([ |
| 85 | + { name: "partial.html", format: "Bold Format" }, |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not trigger callbacks for irrelevant lines outside code fences", () => { |
| 90 | + // Provide a line that doesn't match any header or code fence |
| 91 | + parser.processChunk("This is an irrelevant line\n"); |
| 92 | + parser.flushRemaining(); |
| 93 | + |
| 94 | + expect(fileNameChanges).toEqual([]); |
| 95 | + expect(codeLines).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle nested chunks with header and code block boundaries", () => { |
| 99 | + // Simulate a stream with multiple boundaries and chunk splits |
| 100 | + const chunks = [ |
| 101 | + "**index.html**\n", // Header detected |
| 102 | + "```\n<ht", // Start code fence and partial code |
| 103 | + "ml>\n</ht", // Continuation of code |
| 104 | + "ml>\n```\n", // End code fence |
| 105 | + "Some irrelevant line\n", // Irrelevant line outside code fence |
| 106 | + "**styles.css**\n", // New header |
| 107 | + "```\nbody { color:", // Start second code fence with split code line |
| 108 | + " blue; }\n```\n", // End code fence |
| 109 | + ]; |
| 110 | + |
| 111 | + chunks.forEach((chunk) => parser.processChunk(chunk)); |
| 112 | + parser.flushRemaining(); |
| 113 | + |
| 114 | + expect(fileNameChanges).toEqual([ |
| 115 | + { name: "index.html", format: "Bold Format" }, |
| 116 | + { name: "styles.css", format: "Bold Format" }, |
| 117 | + ]); |
| 118 | + |
| 119 | + expect(codeLines).toEqual(["<html>", "</html>", "body { color: blue; }"]); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should handle bold format with extra text", () => { |
| 123 | + const input = |
| 124 | + "**index.html** (some commentary)\n```\n<html>\n</html>\n```\n"; |
| 125 | + parser.processChunk(input); |
| 126 | + parser.flushRemaining(); |
| 127 | + |
| 128 | + expect(fileNameChanges).toEqual([ |
| 129 | + { name: "index.html", format: "Bold Format" }, |
| 130 | + ]); |
| 131 | + expect(codeLines).toEqual(["<html>", "</html>"]); |
| 132 | + }); |
| 133 | +}); |
0 commit comments