xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / components / src / markdown.test.ts

14.5 KB
1import { describe, expect, test } from "bun:test";
2import type { ContainerNode, Node, TextNode } from "@cel-tui/types";
3import { Markdown } from "./markdown";
4
5// ─── Helpers ────────────────────────────────────────────────────
6
7/** Extract the text content of a Text node. */
8function textContent(node: Node): string {
9  if (node.type === "text") return node.content;
10  throw new Error(`Expected text node, got ${node.type}`);
11}
12
13/** Assert a node is a Text node and return it. */
14function asText(node: Node): TextNode {
15  expect(node.type).toBe("text");
16  return node as TextNode;
17}
18
19/** Assert a node is a container (vstack/hstack) and return it. */
20function asContainer(node: Node): ContainerNode {
21  expect(["vstack", "hstack"]).toContain(node.type);
22  return node as ContainerNode;
23}
24
25function item<T>(items: readonly T[], index: number): T {
26  const value = items[index];
27  expect(value).toBeDefined();
28  if (value === undefined) {
29    throw new Error(`Missing item at index ${index}`);
30  }
31  return value;
32}
33
34// ─── Markdown component ─────────────────────────────────────────
35
36describe("Markdown", () => {
37  test("returns empty array for empty string", () => {
38    const nodes = Markdown("");
39    expect(nodes).toHaveLength(0);
40  });
41
42  test("heading renders as styled Text", () => {
43    const nodes = Markdown("# Hello");
44    expect(nodes).toHaveLength(1);
45    const t = asText(item(nodes, 0));
46    expect(t.content).toBe("Hello");
47    expect(t.props.bold).toBe(true);
48    expect(t.props.fgColor).toBe("color06");
49  });
50
51  test("heading levels have different styles", () => {
52    const nodes = Markdown("# H1\n## H2\n### H3");
53    const h1 = asText(item(nodes, 0));
54    const h2 = asText(item(nodes, 1));
55    const h3 = asText(item(nodes, 2));
56    expect(h1.props.fgColor).toBe("color06");
57    expect(h2.props.fgColor).toBe("color03");
58    expect(h3.props.fgColor).toBe("color02");
59  });
60
61  test("plain paragraph renders as wrapping Text", () => {
62    const nodes = Markdown("Hello world");
63    expect(nodes).toHaveLength(1);
64    const t = asText(item(nodes, 0));
65    expect(t.content).toBe("Hello world");
66    expect(t.props.wrap).toBe("word");
67  });
68
69  test("paragraph with bold renders as wrapping HStack", () => {
70    const nodes = Markdown("Hello **bold** world");
71    expect(nodes).toHaveLength(1);
72    const hstack = asContainer(item(nodes, 0));
73    expect(hstack.type).toBe("hstack");
74    expect(hstack.props.flexWrap).toBe("wrap");
75    // Children: "Hello", " ", bold("bold"), " ", "world"
76    expect(hstack.children).toHaveLength(5);
77    expect(asText(item(hstack.children, 0)).content).toBe("Hello");
78    expect(asText(item(hstack.children, 1)).content).toBe(" ");
79    const boldNode = asText(item(hstack.children, 2));
80    expect(boldNode.content).toBe("bold");
81    expect(boldNode.props.bold).toBe(true);
82    expect(asText(item(hstack.children, 3)).content).toBe(" ");
83    expect(asText(item(hstack.children, 4)).content).toBe("world");
84  });
85
86  test("paragraph with italic renders styled nodes", () => {
87    const nodes = Markdown("some *italic* text");
88    const hstack = asContainer(item(nodes, 0));
89    expect(hstack.type).toBe("hstack");
90    // "some", " ", italic("italic"), " ", "text"
91    const italicNode = asText(item(hstack.children, 2));
92    expect(italicNode.content).toBe("italic");
93    expect(italicNode.props.italic).toBe(true);
94  });
95
96  test("paragraph with inline code keeps code span atomic", () => {
97    const nodes = Markdown("use `foo bar` here");
98    const hstack = asContainer(item(nodes, 0));
99    expect(hstack.type).toBe("hstack");
100    // "use", " ", code("foo bar"), " ", "here"
101    expect(hstack.children).toHaveLength(5);
102    const codeNode = asText(item(hstack.children, 2));
103    expect(codeNode.content).toBe("foo bar");
104    expect(codeNode.props.fgColor).toBe("color03");
105  });
106
107  test("paragraph with link keeps link text atomic", () => {
108    const nodes = Markdown("see [the docs](https://example.com) here");
109    const hstack = asContainer(item(nodes, 0));
110    // "see", " ", link("the docs"), " ", "here"
111    expect(hstack.children).toHaveLength(5);
112    const linkNode = asText(item(hstack.children, 2));
113    expect(linkNode.content).toBe("the docs");
114    expect(linkNode.props.fgColor).toBe("color06");
115    expect(linkNode.props.underline).toBe(true);
116  });
117
118  test("paragraph with mixed formatting", () => {
119    const nodes = Markdown("Hello, **bold**!");
120    const hstack = asContainer(item(nodes, 0));
121    // "Hello,", " ", bold("bold"), "!"
122    expect(hstack.children).toHaveLength(4);
123    expect(asText(item(hstack.children, 0)).content).toBe("Hello,");
124    expect(asText(item(hstack.children, 1)).content).toBe(" ");
125    expect(asText(item(hstack.children, 2)).props.bold).toBe(true);
126    // No space before "!" — punctuation is adjacent
127    expect(asText(item(hstack.children, 3)).content).toBe("!");
128  });
129
130  test("code block renders as VStack with Text", () => {
131    const nodes = Markdown("```js\nconst x = 1;\n```");
132    expect(nodes).toHaveLength(1);
133    const container = asContainer(item(nodes, 0));
134    expect(container.type).toBe("vstack");
135    expect(container.props.padding).toEqual({ x: 1 });
136    expect(container.props.bgColor).toBe("color08");
137    expect(container.children).toHaveLength(1);
138    expect(textContent(item(container.children, 0))).toBe("const x = 1;");
139  });
140
141  test("unordered list item renders as HStack with marker", () => {
142    const nodes = Markdown("- hello world");
143    expect(nodes).toHaveLength(1);
144    const row = asContainer(item(nodes, 0));
145    expect(row.type).toBe("hstack");
146    expect(row.children).toHaveLength(2);
147
148    // Marker
149    const marker = asText(item(row.children, 0));
150    expect(marker.content).toBe("• ");
151    expect(marker.props.fgColor).toBe("color03");
152
153    // Content in flex VStack
154    const wrapper = asContainer(item(row.children, 1));
155    expect(wrapper.type).toBe("vstack");
156    expect(wrapper.props.flex).toBe(1);
157    const content = asText(item(wrapper.children, 0));
158    expect(content.content).toBe("hello world");
159    expect(content.props.wrap).toBe("word");
160  });
161
162  test("ordered list item uses index as marker", () => {
163    const nodes = Markdown("3. third item");
164    const row = asContainer(item(nodes, 0));
165    const marker = asText(item(row.children, 0));
166    expect(marker.content).toBe("3. ");
167  });
168
169  test("blockquote renders with bar and italic content", () => {
170    const nodes = Markdown("> quoted text");
171    expect(nodes).toHaveLength(1);
172    const row = asContainer(item(nodes, 0));
173    expect(row.type).toBe("hstack");
174
175    // Bar
176    const bar = asText(item(row.children, 0));
177    expect(bar.content).toBe("│ ");
178    expect(bar.props.fgColor).toBe("color02");
179
180    // Content
181    const wrapper = asContainer(item(row.children, 1));
182    const content = asText(item(wrapper.children, 0));
183    expect(content.content).toBe("quoted text");
184    expect(content.props.italic).toBe(true);
185    expect(content.props.wrap).toBe("word");
186  });
187
188  test("horizontal rule renders as Divider", () => {
189    const nodes = Markdown("---");
190    expect(nodes).toHaveLength(1);
191    const t = asText(item(nodes, 0));
192    // Divider is a Text with repeat: "fill"
193    expect(t.content).toBe("─");
194    expect(t.props.repeat).toBe("fill");
195    expect(t.props.fgColor).toBe("color08");
196  });
197
198  test("blank line renders as empty Text", () => {
199    const nodes = Markdown("hello\n\nworld");
200    expect(nodes).toHaveLength(3);
201    expect(textContent(item(nodes, 1))).toBe("");
202  });
203
204  test("custom theme overrides defaults", () => {
205    const nodes = Markdown("# Hello", {
206      theme: { heading1: { bold: true, fgColor: "color05" } },
207    });
208    const t = asText(item(nodes, 0));
209    expect(t.props.fgColor).toBe("color05");
210  });
211
212  test("custom theme partial — unset properties use defaults", () => {
213    const nodes = Markdown("# H1\n> quote", {
214      theme: { heading1: { fgColor: "color01" } },
215    });
216    // Heading uses custom
217    expect(asText(item(nodes, 0)).props.fgColor).toBe("color01");
218    // Blockquote bar uses default
219    const row = asContainer(item(nodes, 1));
220    expect(asText(item(row.children, 0)).props.fgColor).toBe("color02");
221  });
222
223  test("heading levels preserve their base style with inline content", () => {
224    const nodes = Markdown(
225      "# H1 **bold**\n## H2 *italic*\n### H3 `code` [link](https://example.com)",
226    );
227
228    const h1 = asContainer(item(nodes, 0));
229    const h2 = asContainer(item(nodes, 1));
230    const h3 = asContainer(item(nodes, 2));
231
232    expect(h1.type).toBe("hstack");
233    expect(h1.props).toMatchObject({
234      flexWrap: "wrap",
235      bold: true,
236      fgColor: "color06",
237    });
238    expect(h2.props).toMatchObject({
239      flexWrap: "wrap",
240      bold: true,
241      fgColor: "color03",
242    });
243    expect(h3.props).toMatchObject({
244      flexWrap: "wrap",
245      bold: true,
246      fgColor: "color02",
247    });
248
249    expect(asText(item(h1.children, 2)).props.bold).toBe(true);
250    expect(asText(item(h2.children, 2)).props.italic).toBe(true);
251    expect(asText(item(h3.children, 2)).props.fgColor).toBe("color03");
252    expect(asText(item(h3.children, 4)).props).toMatchObject({
253      fgColor: "color06",
254      underline: true,
255    });
256  });
257
258  test("heading inline styles compose with a custom heading theme", () => {
259    const nodes = Markdown(
260      "# plain **bold** *italic* `code value` [the link](https://example.com)",
261      {
262        theme: {
263          heading1: { bold: true, fgColor: "color05", bgColor: "color00" },
264          bold: { bold: true, fgColor: "color01" },
265          italic: { italic: true, fgColor: "color02" },
266          inlineCode: { fgColor: "color03" },
267          link: { fgColor: "color04", underline: true },
268        },
269      },
270    );
271
272    const heading = asContainer(item(nodes, 0));
273    expect(heading.props).toMatchObject({
274      flexWrap: "wrap",
275      bold: true,
276      fgColor: "color05",
277      bgColor: "color00",
278    });
279    expect(heading.children.map(textContent)).toEqual([
280      "plain",
281      " ",
282      "bold",
283      " ",
284      "italic",
285      " ",
286      "code value",
287      " ",
288      "the link",
289    ]);
290    expect(asText(item(heading.children, 2)).props).toEqual({
291      bold: true,
292      fgColor: "color01",
293    });
294    expect(asText(item(heading.children, 4)).props).toEqual({
295      italic: true,
296      fgColor: "color02",
297    });
298    expect(asText(item(heading.children, 6)).props).toEqual({
299      fgColor: "color03",
300    });
301    expect(asText(item(heading.children, 8)).props).toEqual({
302      fgColor: "color04",
303      underline: true,
304    });
305  });
306
307  test("link text is used, url is stripped", () => {
308    const nodes = Markdown("See [the docs](https://example.com) here.");
309    const hstack = asContainer(item(nodes, 0));
310    expect(hstack.type).toBe("hstack");
311    expect(hstack.props.flexWrap).toBe("wrap");
312    // "See", " ", link("the docs"), " ", "here."
313    expect(hstack.children).toHaveLength(5);
314    expect(asText(item(hstack.children, 0)).content).toBe("See");
315    const link = asText(item(hstack.children, 2));
316    expect(link.content).toBe("the docs");
317    expect(link.props.fgColor).toBe("color06");
318    expect(asText(item(hstack.children, 4)).content).toBe("here.");
319  });
320
321  test("streaming: unclosed code block renders as code", () => {
322    const nodes = Markdown("```js\nconst x = 1;");
323    expect(nodes).toHaveLength(1);
324    const container = asContainer(item(nodes, 0));
325    expect(container.props.bgColor).toBe("color08");
326    expect(textContent(item(container.children, 0))).toBe("const x = 1;");
327  });
328
329  test("streaming: unclosed bold appears as plain text", () => {
330    // Unclosed ** is not parsed as bold — stays as text span
331    const nodes = Markdown("hello **world");
332    const t = asText(item(nodes, 0));
333    expect(t.content).toBe("hello **world");
334    expect(t.props.wrap).toBe("word");
335  });
336
337  test("list item with inline formatting", () => {
338    const nodes = Markdown("- hello **bold** world");
339    const row = asContainer(item(nodes, 0));
340    expect(row.type).toBe("hstack");
341    const wrapper = asContainer(item(row.children, 1));
342    expect(wrapper.type).toBe("vstack");
343    // Content is a wrapping HStack
344    const content = asContainer(item(wrapper.children, 0));
345    expect(content.type).toBe("hstack");
346    expect(content.props.flexWrap).toBe("wrap");
347    // "hello", " ", bold("bold"), " ", "world"
348    expect(content.children).toHaveLength(5);
349    expect(asText(item(content.children, 2)).props.bold).toBe(true);
350  });
351
352  test("blockquote with inline formatting", () => {
353    const nodes = Markdown("> hello **bold** text");
354    const row = asContainer(item(nodes, 0));
355    const wrapper = asContainer(item(row.children, 1));
356    const content = asContainer(item(wrapper.children, 0));
357    expect(content.type).toBe("hstack");
358    expect(content.props.flexWrap).toBe("wrap");
359    // blockquoteText style (italic) applied to the wrapping HStack
360    expect(content.props.italic).toBe(true);
361    // bold node inside inherits italic from parent, also has bold
362    expect(asText(item(content.children, 2)).props.bold).toBe(true);
363  });
364
365  test("inline code theme is customizable", () => {
366    const nodes = Markdown("use `code` here", {
367      theme: { inlineCode: { fgColor: "color01", bgColor: "color00" } },
368    });
369    const hstack = asContainer(item(nodes, 0));
370    const codeNode = asText(item(hstack.children, 2));
371    expect(codeNode.props.fgColor).toBe("color01");
372    expect(codeNode.props.bgColor).toBe("color00");
373  });
374
375  test("full document produces correct node sequence", () => {
376    const md = [
377      "# Title",
378      "",
379      "Some text here.",
380      "",
381      "```",
382      "code",
383      "```",
384      "",
385      "- item",
386      "",
387      "> quote",
388      "",
389      "---",
390    ].join("\n");
391
392    const nodes = Markdown(md);
393    const types = nodes.map((n) => {
394      if (n.type === "text") {
395        if (n.content === "") return "blank";
396        if (n.props.repeat === "fill") return "hr";
397        if (n.props.bold && n.props.fgColor) return "heading";
398        return "paragraph";
399      }
400      if (n.type === "vstack") return "code_block";
401      if (n.type === "hstack") {
402        const first = (n as ContainerNode).children[0];
403        if (first?.type === "text" && (first as TextNode).content === "│ ")
404          return "blockquote";
405        return "list_item";
406      }
407      return n.type;
408    });
409
410    expect(types).toEqual([
411      "heading",
412      "blank",
413      "paragraph",
414      "blank",
415      "code_block",
416      "blank",
417      "list_item",
418      "blank",
419      "blockquote",
420      "blank",
421      "hr",
422    ]);
423  });
424});