Elffuss BraidHub

kikocis/continue public

main · 8.1 KiBRawPermalink
1import { Text } from "ink";
2import React from "react";
3
4import { MarkdownRenderer } from "./MarkdownRenderer.js";
5
6describe("MarkdownRenderer", () => {
7 // Helper function to simulate calling the component function directly
8 function renderMarkdown(content: string) {
9 // Handle React.memo wrapped component by casting to any to access .type
10 const ComponentType = (MarkdownRenderer as any).type || MarkdownRenderer;
11 const component = ComponentType({ content });
12 return (component as any).props.children;
13 }
14
15 it("renders plain text without formatting", () => {
16 const result = renderMarkdown("Hello world");
17 expect(result).toEqual(["Hello world"]);
18 });
19
20 it("renders bold text", () => {
21 const result = renderMarkdown("This is **bold** text");
22 expect(result.length).toBe(3);
23 expect(result[0]).toBe("This is ");
24 expect(React.isValidElement(result[1])).toBe(true);
25 expect((result[1] as any).props.bold).toBe(true);
26 expect((result[1] as any).props.children).toBe("bold");
27 expect(result[2]).toBe(" text");
28 });
29
30 it("renders italic text with asterisks", () => {
31 const result = renderMarkdown("This is *italic* text");
32 expect(result.length).toBe(3);
33 expect(result[0]).toBe("This is ");
34 expect(React.isValidElement(result[1])).toBe(true);
35 expect((result[1] as any).props.italic).toBe(true);
36 expect((result[1] as any).props.children).toBe("italic");
37 expect(result[2]).toBe(" text");
38 });
39
40 it("renders italic text with underscores", () => {
41 const result = renderMarkdown("This is _italic_ text");
42 expect(result.length).toBe(3);
43 expect(result[0]).toBe("This is ");
44 expect(React.isValidElement(result[1])).toBe(true);
45 expect((result[1] as any).props.italic).toBe(true);
46 expect((result[1] as any).props.children).toBe("italic");
47 expect(result[2]).toBe(" text");
48 });
49
50 it("renders strikethrough text", () => {
51 const result = renderMarkdown("This is ~~strikethrough~~ text");
52 expect(result.length).toBe(3);
53 expect(result[0]).toBe("This is ");
54 expect(React.isValidElement(result[1])).toBe(true);
55 expect((result[1] as any).props.strikethrough).toBe(true);
56 expect((result[1] as any).props.children).toBe("strikethrough");
57 expect(result[2]).toBe(" text");
58 });
59
60 it("renders code text", () => {
61 const result = renderMarkdown("This is `code` text");
62 expect(result.length).toBe(3);
63 expect(result[0]).toBe("This is ");
64 expect(React.isValidElement(result[1])).toBe(true);
65 expect((result[1] as any).props.color).toBe("magentaBright");
66 expect((result[1] as any).props.children).toBe("code");
67 expect(result[2]).toBe(" text");
68 });
69
70 it("handles empty string", () => {
71 const result = renderMarkdown("");
72 expect(result).toEqual([]);
73 });
74
75 it("handles text without formatting", () => {
76 const result = renderMarkdown("Just plain text");
77 expect(result).toEqual(["Just plain text"]);
78 });
79
80 it("handles multiple formatting types", () => {
81 const result = renderMarkdown("**Bold** and *italic*");
82 expect(result.length).toBe(3);
83 expect((result[0] as any).props.bold).toBe(true);
84 expect((result[0] as any).props.children).toBe("Bold");
85 expect(result[1]).toBe(" and ");
86 expect((result[2] as any).props.italic).toBe(true);
87 expect((result[2] as any).props.children).toBe("italic");
88 });
89
90 // Test the overlapping pattern handling
91 it("handles overlapping patterns by prioritizing first match", () => {
92 const result = renderMarkdown("**bold*italic**");
93 // Should now match bold pattern and render "bold*italic" as bold
94 expect(result.length).toBe(1);
95 expect(React.isValidElement(result[0])).toBe(true);
96 expect((result[0] as any).props.bold).toBe(true);
97 expect((result[0] as any).props.children).toBe("bold*italic");
98 });
99
100 describe("Heading formatting", () => {
101 it("renders h1 heading as bold", () => {
102 const result = renderMarkdown("# Main Title");
103 expect(result.length).toBe(1);
104 expect(React.isValidElement(result[0])).toBe(true);
105 expect((result[0] as any).props.bold).toBe(true);
106 expect((result[0] as any).props.children).toBe("Main Title");
107 });
108
109 it("renders h2 heading as bold", () => {
110 const result = renderMarkdown("## Section Title");
111 expect(result.length).toBe(1);
112 expect(React.isValidElement(result[0])).toBe(true);
113 expect((result[0] as any).props.bold).toBe(true);
114 expect((result[0] as any).props.children).toBe("Section Title");
115 });
116
117 it("renders h3 heading as bold", () => {
118 const result = renderMarkdown("### Subsection");
119 expect(result.length).toBe(1);
120 expect(React.isValidElement(result[0])).toBe(true);
121 expect((result[0] as any).props.bold).toBe(true);
122 expect((result[0] as any).props.children).toBe("Subsection");
123 });
124
125 it("renders h6 heading as bold", () => {
126 const result = renderMarkdown("###### Deep Heading");
127 expect(result.length).toBe(1);
128 expect(React.isValidElement(result[0])).toBe(true);
129 expect((result[0] as any).props.bold).toBe(true);
130 expect((result[0] as any).props.children).toBe("Deep Heading");
131 });
132
133 it("handles headings with multiple words", () => {
134 const result = renderMarkdown(
135 "# This is a long heading with multiple words",
136 );
137 expect(result.length).toBe(1);
138 expect(React.isValidElement(result[0])).toBe(true);
139 expect((result[0] as any).props.bold).toBe(true);
140 expect((result[0] as any).props.children).toBe(
141 "This is a long heading with multiple words",
142 );
143 });
144
145 it("handles mixed content with headings", () => {
146 const result = renderMarkdown("# Title\nNormal text **bold** text");
147 expect(result.length).toBe(4);
148 expect((result[0] as any).props.bold).toBe(true);
149 expect((result[0] as any).props.children).toBe("Title");
150 expect(result[1]).toBe("\nNormal text ");
151 expect((result[2] as any).props.bold).toBe(true);
152 expect((result[2] as any).props.children).toBe("bold");
153 expect(result[3]).toBe(" text");
154 });
155 });
156
157 describe("Code block formatting", () => {
158 it("renders code blocks with triple backticks", () => {
159 const result = renderMarkdown("```\nconst x = 1;\nconsole.log(x);\n```");
160 expect(result.length).toBe(1);
161 expect(React.isValidElement(result[0])).toBe(true);
162 // The result should be a Text element containing syntax highlighted code
163 expect((result[0] as any).type).toBe(Text);
164 expect((result[0] as any).props.children).toBeDefined();
165 expect(Array.isArray((result[0] as any).props.children)).toBe(true);
166 });
167
168 it("renders single line code blocks", () => {
169 const result = renderMarkdown("```console.log('hello')```");
170 expect(result.length).toBe(1);
171 expect(React.isValidElement(result[0])).toBe(true);
172 expect((result[0] as any).type).toBe(Text);
173 expect((result[0] as any).props.children).toBeDefined();
174 });
175
176 it("renders code blocks with language specification", () => {
177 const result = renderMarkdown("```javascript\nconst x = 1;\n```");
178 expect(result.length).toBe(1);
179 expect(React.isValidElement(result[0])).toBe(true);
180 expect((result[0] as any).type).toBe(Text);
181 expect((result[0] as any).props.children).toBeDefined();
182 });
183
184 it("handles code blocks mixed with other formatting", () => {
185 const result = renderMarkdown(
186 "Here's some code:\n```\nfunction test() {\n return 42;\n}\n```\nAnd **bold** text",
187 );
188 expect(result.length).toBe(5);
189 expect(result[0]).toBe("Here's some code:\n");
190 expect(React.isValidElement(result[1])).toBe(true);
191 expect((result[1] as any).type).toBe(Text);
192 expect(result[2]).toBe("\nAnd ");
193 expect((result[3] as any).props.bold).toBe(true);
194 expect((result[3] as any).props.children).toBe("bold");
195 expect(result[4]).toBe(" text");
196 });
197
198 it("handles empty code blocks", () => {
199 const result = renderMarkdown("``````");
200 expect(result.length).toBe(1);
201 expect(React.isValidElement(result[0])).toBe(true);
202 expect((result[0] as any).type).toBe(Text);
203 });
204 });
205});