@@ -0,0 +1,346 @@
1+import { describe, expect, test } from "bun:test";
2+import { renderMarkdown } from "./markdown.ts";
3+
4+// ---------------------------------------------------------------------------
5+// Helpers
6+// ---------------------------------------------------------------------------
7+
8+/** Strip all ANSI escape sequences, leaving only the visible text. */
9+function strip(s: string): string {
10+ // Use a dynamic RegExp so the ESC literal doesn't trigger the lint rule
11+ // for control characters in regex literals.
12+ const esc = String.fromCharCode(0x1b);
13+ return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
14+}
15+
16+/** Return true if the string contains the given ANSI SGR sequence (e.g. "[1m"). */
17+function hasAnsi(s: string, code: string): boolean {
18+ return s.includes(`${String.fromCharCode(0x1b)}${code}`);
19+}
20+
21+/** Return true if the string contains ANSI bold codes. */
22+function hasBold(s: string): boolean {
23+ return hasAnsi(s, "[1m");
24+}
25+
26+/** Return true if the string contains ANSI dim codes. */
27+function hasDim(s: string): boolean {
28+ return hasAnsi(s, "[2m");
29+}
30+
31+/** Return true if the string contains ANSI cyan codes. */
32+function hasCyan(s: string): boolean {
33+ return hasAnsi(s, "[36m");
34+}
35+
36+/** Return true if the string contains ANSI yellow codes. */
37+function hasYellow(s: string): boolean {
38+ return hasAnsi(s, "[33m");
39+}
40+
41+// ---------------------------------------------------------------------------
42+// renderMarkdown — block elements
43+// ---------------------------------------------------------------------------
44+
45+describe("renderMarkdown – headings", () => {
46+ test("h1 renders the heading text without the # prefix", () => {
47+ const out = renderMarkdown("# Hello World");
48+ expect(strip(out)).toBe("Hello World");
49+ });
50+
51+ test("h1 is bold and cyan", () => {
52+ const out = renderMarkdown("# Hello");
53+ expect(hasBold(out)).toBe(true);
54+ expect(hasCyan(out)).toBe(true);
55+ });
56+
57+ test("h2 renders the heading text without the ## prefix", () => {
58+ const out = renderMarkdown("## Section");
59+ expect(strip(out)).toBe("Section");
60+ });
61+
62+ test("h2 is bold and cyan", () => {
63+ const out = renderMarkdown("## Section");
64+ expect(hasBold(out)).toBe(true);
65+ expect(hasCyan(out)).toBe(true);
66+ });
67+
68+ test("h3 renders the heading text without the ### prefix", () => {
69+ const out = renderMarkdown("### Sub");
70+ expect(strip(out)).toBe("Sub");
71+ });
72+
73+ test("h3 is bold but NOT cyan (only bold)", () => {
74+ const out = renderMarkdown("### Sub");
75+ expect(hasBold(out)).toBe(true);
76+ expect(hasCyan(out)).toBe(false);
77+ });
78+
79+ test("h4+ is treated the same as h3 (#{3,} pattern)", () => {
80+ const h4 = strip(renderMarkdown("#### Deep"));
81+ const h3 = strip(renderMarkdown("### Deep"));
82+ expect(h4).toBe(h3);
83+ });
84+
85+ test("multiple headings each rendered on own line", () => {
86+ const out = strip(renderMarkdown("# H1\n## H2\n### H3"));
87+ expect(out).toBe("H1\nH2\nH3");
88+ });
89+});
90+
91+describe("renderMarkdown – horizontal rules", () => {
92+ test("--- renders as a dim rule", () => {
93+ const out = renderMarkdown("---");
94+ expect(hasDim(out)).toBe(true);
95+ expect(strip(out)).toMatch(/^─+$/);
96+ });
97+
98+ test("=== renders as a dim rule", () => {
99+ const out = renderMarkdown("===");
100+ expect(hasDim(out)).toBe(true);
101+ expect(strip(out)).toMatch(/^─+$/);
102+ });
103+
104+ test("*** renders as a dim rule", () => {
105+ const out = renderMarkdown("***");
106+ expect(hasDim(out)).toBe(true);
107+ expect(strip(out)).toMatch(/^─+$/);
108+ });
109+
110+ test("---- (4 dashes) is also a horizontal rule", () => {
111+ const out = strip(renderMarkdown("----"));
112+ expect(out).toMatch(/^─+$/);
113+ });
114+
115+ test("-- (2 dashes) is NOT a horizontal rule — passed through as plain text", () => {
116+ const out = strip(renderMarkdown("--"));
117+ expect(out).toBe("--");
118+ });
119+});
120+
121+describe("renderMarkdown – fenced code blocks", () => {
122+ test("lines inside ``` fence are yellow", () => {
123+ const out = renderMarkdown("```\ncode line\n```");
124+ const lines = out.split("\n");
125+ // line index 1 is "code line"
126+ expect(hasYellow(lines[1] ?? "")).toBe(true);
127+ });
128+
129+ test("fence delimiter lines are dim", () => {
130+ const out = renderMarkdown("```\ncode\n```");
131+ const lines = out.split("\n");
132+ expect(hasDim(lines[0] ?? "")).toBe(true);
133+ expect(hasDim(lines[2] ?? "")).toBe(true);
134+ });
135+
136+ test("language hint on opening fence is preserved in stripped output", () => {
137+ const out = strip(renderMarkdown("```ts\nconst x = 1;\n```"));
138+ expect(out).toBe("```ts\nconst x = 1;\n```");
139+ });
140+
141+ test("inline markdown inside fenced block is NOT processed", () => {
142+ const out = strip(renderMarkdown("```\n**not bold**\n```"));
143+ expect(out).toContain("**not bold**");
144+ });
145+
146+ test("~~~ also opens and closes a fenced block", () => {
147+ const out = renderMarkdown("~~~\ncode\n~~~");
148+ const lines = out.split("\n");
149+ expect(hasYellow(lines[1] ?? "")).toBe(true);
150+ });
151+
152+ test("two consecutive fenced blocks are both fenced correctly", () => {
153+ const input = "```\nfirst\n```\n```\nsecond\n```";
154+ const out = renderMarkdown(input);
155+ const lines = out.split("\n");
156+ // line 1 = "first" (inside first fence), line 4 = "second" (inside second fence)
157+ expect(hasYellow(lines[1] ?? "")).toBe(true);
158+ expect(hasYellow(lines[4] ?? "")).toBe(true);
159+ // line 3 = "```" (opening second fence), should be dim not yellow
160+ expect(hasDim(lines[3] ?? "")).toBe(true);
161+ expect(hasYellow(lines[3] ?? "")).toBe(false);
162+ });
163+
164+ test("unclosed fence: lines after opening ``` are treated as code", () => {
165+ const out = renderMarkdown("```\ncode\nnormal line");
166+ const lines = out.split("\n");
167+ expect(hasYellow(lines[1] ?? "")).toBe(true);
168+ expect(hasYellow(lines[2] ?? "")).toBe(true);
169+ });
170+});
171+
172+describe("renderMarkdown – blockquotes", () => {
173+ test("> line renders with │ prefix and dim styling", () => {
174+ const out = renderMarkdown("> quoted");
175+ expect(strip(out)).toBe("│ quoted");
176+ expect(hasDim(out)).toBe(true);
177+ });
178+
179+ test("> with no space also matches", () => {
180+ const out = strip(renderMarkdown(">no space"));
181+ expect(out).toBe("│ no space");
182+ });
183+});
184+
185+describe("renderMarkdown – unordered lists", () => {
186+ test("- item renders with · bullet", () => {
187+ const out = strip(renderMarkdown("- item"));
188+ expect(out).toBe("· item");
189+ });
190+
191+ test("* item renders with · bullet", () => {
192+ const out = strip(renderMarkdown("* item"));
193+ expect(out).toBe("· item");
194+ });
195+
196+ test("+ item renders with · bullet", () => {
197+ const out = strip(renderMarkdown("+ item"));
198+ expect(out).toBe("· item");
199+ });
200+
201+ test("indented - item preserves leading whitespace", () => {
202+ const out = strip(renderMarkdown(" - nested"));
203+ expect(out).toBe(" · nested");
204+ });
205+
206+ test("list item content goes through inline processing", () => {
207+ const out = strip(renderMarkdown("- **bold** item"));
208+ // bold markers stripped in plain text, content preserved
209+ expect(out).toBe("· bold item");
210+ });
211+});
212+
213+describe("renderMarkdown – ordered lists", () => {
214+ test("1. item renders number with dim styling", () => {
215+ const out = strip(renderMarkdown("1. first"));
216+ expect(out).toBe("1. first");
217+ });
218+
219+ test("multi-item ordered list", () => {
220+ const out = strip(renderMarkdown("1. alpha\n2. beta\n3. gamma"));
221+ expect(out).toBe("1. alpha\n2. beta\n3. gamma");
222+ });
223+
224+ test("ordered list item content goes through inline processing", () => {
225+ const out = strip(renderMarkdown("1. **bold** text"));
226+ expect(out).toBe("1. bold text");
227+ });
228+});
229+
230+describe("renderMarkdown – plain text", () => {
231+ test("plain text is passed through unchanged", () => {
232+ const out = strip(renderMarkdown("hello world"));
233+ expect(out).toBe("hello world");
234+ });
235+
236+ test("empty string produces empty string", () => {
237+ expect(renderMarkdown("")).toBe("");
238+ });
239+
240+ test("empty line in multi-line input is preserved", () => {
241+ const out = strip(renderMarkdown("line1\n\nline3"));
242+ expect(out).toBe("line1\n\nline3");
243+ });
244+});
245+
246+// ---------------------------------------------------------------------------
247+// renderMarkdown — inline spans (via plain text lines)
248+// ---------------------------------------------------------------------------
249+
250+describe("renderMarkdown – inline code", () => {
251+ test("`code` renders yellow and preserves backticks in stripped text", () => {
252+ const out = renderMarkdown("`code`");
253+ expect(hasYellow(out)).toBe(true);
254+ expect(strip(out)).toBe("`code`");
255+ });
256+
257+ test("unclosed backtick is passed through as plain text", () => {
258+ const out = strip(renderMarkdown("hello `world"));
259+ expect(out).toBe("hello `world");
260+ });
261+
262+ test("two separate inline code spans on one line both get highlighted", () => {
263+ const out = renderMarkdown("`foo` and `bar`");
264+ expect(strip(out)).toBe("`foo` and `bar`");
265+ // Two yellow regions should appear
266+ const yellowCount =
267+ out.split(`${String.fromCharCode(0x1b)}[33m`).length - 1;
268+ expect(yellowCount).toBe(2);
269+ });
270+});
271+
272+describe("renderMarkdown – bold", () => {
273+ test("**bold** renders bold styling and strips markers from plain text", () => {
274+ const out = renderMarkdown("**bold**");
275+ expect(hasBold(out)).toBe(true);
276+ expect(strip(out)).toBe("bold");
277+ });
278+
279+ test("bold with surrounding text", () => {
280+ const out = strip(renderMarkdown("before **bold** after"));
281+ expect(out).toBe("before bold after");
282+ });
283+
284+ test("unclosed ** is passed through as plain text", () => {
285+ const out = strip(renderMarkdown("**not closed"));
286+ expect(out).toBe("**not closed");
287+ });
288+});
289+
290+describe("renderMarkdown – italic", () => {
291+ test("*italic* renders dim styling", () => {
292+ const out = renderMarkdown("*italic*");
293+ expect(hasDim(out)).toBe(true);
294+ expect(strip(out)).toBe("italic");
295+ });
296+
297+ test("italic with surrounding text", () => {
298+ const out = strip(renderMarkdown("before *italic* after"));
299+ expect(out).toBe("before italic after");
300+ });
301+
302+ test("unclosed * is passed through as plain text", () => {
303+ const out = strip(renderMarkdown("*not closed"));
304+ expect(out).toBe("*not closed");
305+ });
306+});
307+
308+describe("renderMarkdown – inline combinations", () => {
309+ test("bold and italic on same line both render correctly", () => {
310+ const out = strip(renderMarkdown("**bold** and *italic*"));
311+ expect(out).toBe("bold and italic");
312+ });
313+
314+ test("inline code and bold on same line both render correctly", () => {
315+ const out = strip(renderMarkdown("`code` and **bold**"));
316+ expect(out).toBe("`code` and bold");
317+ });
318+
319+ test("heading with inline bold strips # and bold markers", () => {
320+ const out = strip(renderMarkdown("## Section **key**"));
321+ expect(out).toBe("Section key");
322+ });
323+
324+ test("italic immediately after bold (no space) renders both correctly", () => {
325+ const out = strip(renderMarkdown("**bold***italic*"));
326+ expect(out).toBe("bolditalic");
327+ });
328+
329+ test("bold immediately after italic (no space) renders both correctly", () => {
330+ const out = strip(renderMarkdown("*italic***bold**"));
331+ expect(out).toBe("italicbold");
332+ });
333+
334+ test("unclosed ** does not corrupt a following *italic* span", () => {
335+ const out = strip(renderMarkdown("**foo *bar*"));
336+ expect(out).toBe("**foo bar");
337+ });
338+
339+ test("***text*** does not corrupt surrounding text — stray * passed through as plain", () => {
340+ // Triple-star is ambiguous; the parser should not silently eat content.
341+ // Current behaviour: outer * passed through as plain, inner text bolded.
342+ const out = strip(renderMarkdown("***text***"));
343+ expect(out).toContain("text");
344+ expect(out).not.toContain("**");
345+ });
346+});