|
| 1 | +import { assert, describe, test } from "@rezi-ui/testkit"; |
| 2 | +import { createTestRenderer } from "../../../testing/index.js"; |
| 3 | +import { ui } from "../../ui.js"; |
| 4 | +import { parseMarkdown } from "../parse.js"; |
| 5 | +import { createMarkdownStream } from "../stream.js"; |
| 6 | + |
| 7 | +const KITCHEN_SINK = [ |
| 8 | + "# Title", |
| 9 | + "", |
| 10 | + "Some **bold** text with `code` and [a link](https://rezitui.dev).", |
| 11 | + "", |
| 12 | + "- item one", |
| 13 | + "- item two", |
| 14 | + " - nested", |
| 15 | + "", |
| 16 | + "> quoted", |
| 17 | + "", |
| 18 | + "```ts", |
| 19 | + "const a = 1; // comment", |
| 20 | + "```", |
| 21 | + "", |
| 22 | + "| a | b |", |
| 23 | + "| - | -: |", |
| 24 | + "| 1 | 2 |", |
| 25 | + "", |
| 26 | + "Tail paragraph https://example.com.", |
| 27 | +].join("\n"); |
| 28 | + |
| 29 | +function renderText(vnode: Parameters<ReturnType<typeof createTestRenderer>["render"]>[0]): string { |
| 30 | + return createTestRenderer({ viewport: { cols: 44, rows: 36 } }).render(vnode).toText(); |
| 31 | +} |
| 32 | + |
| 33 | +describe("createMarkdownStream", () => { |
| 34 | + test("char-by-char appends match whole-source parsing", () => { |
| 35 | + const stream = createMarkdownStream(); |
| 36 | + for (const ch of KITCHEN_SINK) stream.append(ch); |
| 37 | + assert.deepEqual(stream.document(), parseMarkdown(KITCHEN_SINK)); |
| 38 | + assert.equal(stream.source(), KITCHEN_SINK); |
| 39 | + }); |
| 40 | + |
| 41 | + test("chunked appends match whole-source parsing at several widths", () => { |
| 42 | + for (const width of [3, 7, 16, 64]) { |
| 43 | + const stream = createMarkdownStream(); |
| 44 | + for (let i = 0; i < KITCHEN_SINK.length; i += width) { |
| 45 | + stream.append(KITCHEN_SINK.slice(i, i + width)); |
| 46 | + } |
| 47 | + assert.deepEqual(stream.document(), parseMarkdown(KITCHEN_SINK), `width ${width}`); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + test("CRLF pairs split across chunk boundaries normalize correctly", () => { |
| 52 | + const stream = createMarkdownStream(); |
| 53 | + stream.append("alpha\r"); |
| 54 | + stream.append("\nbeta\r"); |
| 55 | + stream.append("\n\r\ngamma"); |
| 56 | + assert.deepEqual(stream.document(), parseMarkdown("alpha\r\nbeta\r\n\r\ngamma")); |
| 57 | + }); |
| 58 | + |
| 59 | + test("appending can extend the last block across blank lines", () => { |
| 60 | + const stream = createMarkdownStream(); |
| 61 | + stream.append("- item\n\n"); |
| 62 | + const before = stream.document(); |
| 63 | + assert.equal(before.blocks.length, 1); |
| 64 | + stream.append(" continuation"); |
| 65 | + assert.deepEqual(stream.document(), parseMarkdown("- item\n\n continuation")); |
| 66 | + }); |
| 67 | + |
| 68 | + test("an unclosed fence keeps absorbing appends until it closes", () => { |
| 69 | + const stream = createMarkdownStream(); |
| 70 | + stream.append("```ts\nconst a"); |
| 71 | + const open = stream.document().blocks[0]; |
| 72 | + assert.ok(open !== undefined && open.kind === "codeBlock"); |
| 73 | + assert.equal(open.text, "const a"); |
| 74 | + stream.append(" = 1;\n```\nafter"); |
| 75 | + assert.deepEqual(stream.document(), parseMarkdown("```ts\nconst a = 1;\n```\nafter")); |
| 76 | + }); |
| 77 | + |
| 78 | + test("completed blocks keep referential identity across appends", () => { |
| 79 | + const stream = createMarkdownStream(); |
| 80 | + stream.append("# one\n\ntwo\n\n# three\n\n"); |
| 81 | + const before = stream.document().blocks; |
| 82 | + stream.append("growing tail"); |
| 83 | + stream.append(" with more words"); |
| 84 | + const after = stream.document().blocks; |
| 85 | + assert.equal(after[0], before[0]); |
| 86 | + assert.equal(after[1], before[1]); |
| 87 | + assert.equal(after[2], before[2]); |
| 88 | + }); |
| 89 | + |
| 90 | + test("vnode() reuses cached VNodes for completed blocks", () => { |
| 91 | + const stream = createMarkdownStream(); |
| 92 | + stream.append("# head\n\nstable paragraph\n\ntail"); |
| 93 | + const first = stream.vnode(); |
| 94 | + stream.append(" grows"); |
| 95 | + const second = stream.vnode(); |
| 96 | + assert.ok(first.kind === "column" && second.kind === "column"); |
| 97 | + assert.equal(second.children?.[0], first.children?.[0], "heading vnode cached"); |
| 98 | + assert.equal(second.children?.[1], first.children?.[1], "paragraph vnode cached"); |
| 99 | + assert.notEqual(second.children?.[2], first.children?.[2], "tail re-rendered"); |
| 100 | + }); |
| 101 | + |
| 102 | + test("vnode() renders identically to ui.markdown over the same source", () => { |
| 103 | + const stream = createMarkdownStream({ blockGap: 2 }); |
| 104 | + for (let i = 0; i < KITCHEN_SINK.length; i += 11) { |
| 105 | + stream.append(KITCHEN_SINK.slice(i, i + 11)); |
| 106 | + } |
| 107 | + const streamed = renderText(stream.vnode()); |
| 108 | + const whole = renderText(ui.markdown(stream.source(), { blockGap: 2 })); |
| 109 | + assert.equal(streamed, whole); |
| 110 | + }); |
| 111 | + |
| 112 | + test("reset clears state and accepts replacement source", () => { |
| 113 | + const stream = createMarkdownStream(); |
| 114 | + stream.append("# old\n\ncontent"); |
| 115 | + stream.reset("# fresh"); |
| 116 | + assert.deepEqual(stream.document(), parseMarkdown("# fresh")); |
| 117 | + assert.equal(stream.source(), "# fresh"); |
| 118 | + stream.reset(); |
| 119 | + assert.equal(stream.source(), ""); |
| 120 | + assert.equal(stream.document().blocks.length, 0); |
| 121 | + }); |
| 122 | + |
| 123 | + test("empty appends and empty streams are safe", () => { |
| 124 | + const stream = createMarkdownStream(); |
| 125 | + stream.append(""); |
| 126 | + assert.equal(stream.document().blocks.length, 0); |
| 127 | + assert.equal(renderText(stream.vnode()).trim(), ""); |
| 128 | + stream.append("text"); |
| 129 | + stream.append(""); |
| 130 | + assert.deepEqual(stream.document(), parseMarkdown("text")); |
| 131 | + }); |
| 132 | + |
| 133 | + test("document results stay deeply frozen", () => { |
| 134 | + const stream = createMarkdownStream(); |
| 135 | + stream.append("# a\n\nb"); |
| 136 | + const doc = stream.document(); |
| 137 | + assert.ok(Object.isFrozen(doc)); |
| 138 | + assert.ok(Object.isFrozen(doc.blocks)); |
| 139 | + assert.ok(Object.isFrozen(doc.blocks[0])); |
| 140 | + }); |
| 141 | +}); |
0 commit comments